Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import throttle from 'lodash/throttle';
  2. import { IStore } from '../app/types';
  3. import { NOTIFICATIONS_ENABLED } from '../base/flags/constants';
  4. import { getFeatureFlag } from '../base/flags/functions';
  5. import { getParticipantCount } from '../base/participants/functions';
  6. import {
  7. CLEAR_NOTIFICATIONS,
  8. HIDE_NOTIFICATION,
  9. SET_NOTIFICATIONS_ENABLED,
  10. SHOW_NOTIFICATION
  11. } from './actionTypes';
  12. import {
  13. NOTIFICATION_ICON,
  14. NOTIFICATION_TIMEOUT,
  15. NOTIFICATION_TIMEOUT_TYPE,
  16. NOTIFICATION_TYPE,
  17. SILENT_JOIN_THRESHOLD,
  18. SILENT_LEFT_THRESHOLD
  19. } from './constants';
  20. import { INotificationProps } from './types';
  21. /**
  22. * Function that returns notification timeout value based on notification timeout type.
  23. *
  24. * @param {string} type - Notification type.
  25. * @param {Object} notificationTimeouts - Config notification timeouts.
  26. * @returns {number}
  27. */
  28. function getNotificationTimeout(type?: string, notificationTimeouts?: {
  29. long?: number;
  30. medium?: number;
  31. short?: number;
  32. }) {
  33. if (type === NOTIFICATION_TIMEOUT_TYPE.SHORT) {
  34. return notificationTimeouts?.short ?? NOTIFICATION_TIMEOUT.SHORT;
  35. } else if (type === NOTIFICATION_TIMEOUT_TYPE.MEDIUM) {
  36. return notificationTimeouts?.medium ?? NOTIFICATION_TIMEOUT.MEDIUM;
  37. } else if (type === NOTIFICATION_TIMEOUT_TYPE.LONG) {
  38. return notificationTimeouts?.long ?? NOTIFICATION_TIMEOUT.LONG;
  39. }
  40. return NOTIFICATION_TIMEOUT.STICKY;
  41. }
  42. /**
  43. * Clears (removes) all the notifications.
  44. *
  45. * @returns {{
  46. * type: CLEAR_NOTIFICATIONS
  47. * }}
  48. */
  49. export function clearNotifications() {
  50. return {
  51. type: CLEAR_NOTIFICATIONS
  52. };
  53. }
  54. /**
  55. * Removes the notification with the passed in id.
  56. *
  57. * @param {string} uid - The unique identifier for the notification to be
  58. * removed.
  59. * @returns {{
  60. * type: HIDE_NOTIFICATION,
  61. * uid: string
  62. * }}
  63. */
  64. export function hideNotification(uid: string) {
  65. return {
  66. type: HIDE_NOTIFICATION,
  67. uid
  68. };
  69. }
  70. /**
  71. * Stops notifications from being displayed.
  72. *
  73. * @param {boolean} enabled - Whether or not notifications should display.
  74. * @returns {{
  75. * type: SET_NOTIFICATIONS_ENABLED,
  76. * enabled: boolean
  77. * }}
  78. */
  79. export function setNotificationsEnabled(enabled: boolean) {
  80. return {
  81. type: SET_NOTIFICATIONS_ENABLED,
  82. enabled
  83. };
  84. }
  85. /**
  86. * Queues an error notification for display.
  87. *
  88. * @param {Object} props - The props needed to show the notification component.
  89. * @param {string} type - Notification type.
  90. * @returns {Object}
  91. */
  92. export function showErrorNotification(props: INotificationProps, type?: string) {
  93. return showNotification({
  94. ...props,
  95. appearance: NOTIFICATION_TYPE.ERROR
  96. }, type);
  97. }
  98. /**
  99. * Queues a notification for display.
  100. *
  101. * @param {Object} props - The props needed to show the notification component.
  102. * @param {string} type - Timeout type.
  103. * @returns {Function}
  104. */
  105. export function showNotification(props: INotificationProps = {}, type?: string) {
  106. return function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
  107. const { disabledNotifications = [], notifications, notificationTimeouts } = getState()['features/base/config'];
  108. const enabledFlag = getFeatureFlag(getState(), NOTIFICATIONS_ENABLED, true);
  109. const { descriptionKey, titleKey } = props;
  110. const shouldDisplay = enabledFlag
  111. && !(disabledNotifications.includes(descriptionKey ?? '')
  112. || disabledNotifications.includes(titleKey ?? ''))
  113. && (!notifications
  114. || notifications.includes(descriptionKey ?? '')
  115. || notifications.includes(titleKey ?? ''));
  116. if (typeof APP !== 'undefined') {
  117. APP.API.notifyNotificationTriggered(titleKey, descriptionKey);
  118. }
  119. if (shouldDisplay) {
  120. return dispatch({
  121. type: SHOW_NOTIFICATION,
  122. props,
  123. timeout: getNotificationTimeout(type, notificationTimeouts),
  124. uid: props.uid || Date.now().toString()
  125. });
  126. }
  127. };
  128. }
  129. /**
  130. * Queues a warning notification for display.
  131. *
  132. * @param {Object} props - The props needed to show the notification component.
  133. * @param {string} type - Notification type.
  134. * @returns {Object}
  135. */
  136. export function showWarningNotification(props: INotificationProps, type?: string) {
  137. return showNotification({
  138. ...props,
  139. appearance: NOTIFICATION_TYPE.WARNING
  140. }, type);
  141. }
  142. /**
  143. * Queues a message notification for display.
  144. *
  145. * @param {Object} props - The props needed to show the notification component.
  146. * @param {string} type - Notification type.
  147. * @returns {Object}
  148. */
  149. export function showMessageNotification(props: INotificationProps, type?: string) {
  150. return showNotification({
  151. ...props,
  152. concatText: true,
  153. titleKey: 'notify.chatMessages',
  154. appearance: NOTIFICATION_TYPE.NORMAL,
  155. icon: NOTIFICATION_ICON.MESSAGE
  156. }, type);
  157. }
  158. /**
  159. * An array of names of participants that have joined the conference. The array
  160. * is replaced with an empty array as notifications are displayed.
  161. *
  162. * @private
  163. * @type {string[]}
  164. */
  165. let joinedParticipantsNames: string[] = [];
  166. /**
  167. * A throttled internal function that takes the internal list of participant
  168. * names, {@code joinedParticipantsNames}, and triggers the display of a
  169. * notification informing of their joining.
  170. *
  171. * @private
  172. * @type {Function}
  173. */
  174. const _throttledNotifyParticipantConnected = throttle((dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  175. const participantCount = getParticipantCount(getState());
  176. // Skip join notifications altogether for large meetings.
  177. if (participantCount > SILENT_JOIN_THRESHOLD) {
  178. joinedParticipantsNames = [];
  179. return;
  180. }
  181. const joinedParticipantsCount = joinedParticipantsNames.length;
  182. let notificationProps;
  183. if (joinedParticipantsCount >= 3) {
  184. notificationProps = {
  185. titleArguments: {
  186. name: joinedParticipantsNames[0]
  187. },
  188. titleKey: 'notify.connectedThreePlusMembers'
  189. };
  190. } else if (joinedParticipantsCount === 2) {
  191. notificationProps = {
  192. titleArguments: {
  193. first: joinedParticipantsNames[0],
  194. second: joinedParticipantsNames[1]
  195. },
  196. titleKey: 'notify.connectedTwoMembers'
  197. };
  198. } else if (joinedParticipantsCount) {
  199. notificationProps = {
  200. titleArguments: {
  201. name: joinedParticipantsNames[0]
  202. },
  203. titleKey: 'notify.connectedOneMember'
  204. };
  205. }
  206. if (notificationProps) {
  207. dispatch(
  208. showNotification(notificationProps, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  209. }
  210. joinedParticipantsNames = [];
  211. }, 2000, { leading: false });
  212. /**
  213. * An array of names of participants that have left the conference. The array
  214. * is replaced with an empty array as notifications are displayed.
  215. *
  216. * @private
  217. * @type {string[]}
  218. */
  219. let leftParticipantsNames: string[] = [];
  220. /**
  221. * A throttled internal function that takes the internal list of participant
  222. * names, {@code leftParticipantsNames}, and triggers the display of a
  223. * notification informing of their leaving.
  224. *
  225. * @private
  226. * @type {Function}
  227. */
  228. const _throttledNotifyParticipantLeft = throttle((dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  229. const participantCount = getParticipantCount(getState());
  230. // Skip left notifications altogether for large meetings.
  231. if (participantCount > SILENT_LEFT_THRESHOLD) {
  232. leftParticipantsNames = [];
  233. return;
  234. }
  235. const leftParticipantsCount = leftParticipantsNames.length;
  236. let notificationProps;
  237. if (leftParticipantsCount >= 3) {
  238. notificationProps = {
  239. titleArguments: {
  240. name: leftParticipantsNames[0]
  241. },
  242. titleKey: 'notify.leftThreePlusMembers'
  243. };
  244. } else if (leftParticipantsCount === 2) {
  245. notificationProps = {
  246. titleArguments: {
  247. first: leftParticipantsNames[0],
  248. second: leftParticipantsNames[1]
  249. },
  250. titleKey: 'notify.leftTwoMembers'
  251. };
  252. } else if (leftParticipantsCount) {
  253. notificationProps = {
  254. titleArguments: {
  255. name: leftParticipantsNames[0]
  256. },
  257. titleKey: 'notify.leftOneMember'
  258. };
  259. }
  260. if (notificationProps) {
  261. dispatch(
  262. showNotification(notificationProps, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  263. }
  264. leftParticipantsNames = [];
  265. }, 2000, { leading: false });
  266. /**
  267. * Queues the display of a notification of a participant having connected to
  268. * the meeting. The notifications are batched so that quick consecutive
  269. * connection events are shown in one notification.
  270. *
  271. * @param {string} displayName - The name of the participant that connected.
  272. * @returns {Function}
  273. */
  274. export function showParticipantJoinedNotification(displayName: string) {
  275. joinedParticipantsNames.push(displayName);
  276. return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>
  277. _throttledNotifyParticipantConnected(dispatch, getState);
  278. }
  279. /**
  280. * Queues the display of a notification of a participant having left to
  281. * the meeting. The notifications are batched so that quick consecutive
  282. * connection events are shown in one notification.
  283. *
  284. * @param {string} displayName - The name of the participant that left.
  285. * @returns {Function}
  286. */
  287. export function showParticipantLeftNotification(displayName: string) {
  288. leftParticipantsNames.push(displayName);
  289. return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>
  290. _throttledNotifyParticipantLeft(dispatch, getState);
  291. }