You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.ts 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 shouldDisplay = enabledFlag
  110. && !(disabledNotifications.includes(props.descriptionKey ?? '')
  111. || disabledNotifications.includes(props.titleKey ?? ''))
  112. && (!notifications
  113. || notifications.includes(props.descriptionKey ?? '')
  114. || notifications.includes(props.titleKey ?? ''));
  115. if (shouldDisplay) {
  116. return dispatch({
  117. type: SHOW_NOTIFICATION,
  118. props,
  119. timeout: getNotificationTimeout(type, notificationTimeouts),
  120. uid: props.uid || Date.now().toString()
  121. });
  122. }
  123. };
  124. }
  125. /**
  126. * Queues a warning notification for display.
  127. *
  128. * @param {Object} props - The props needed to show the notification component.
  129. * @param {string} type - Notification type.
  130. * @returns {Object}
  131. */
  132. export function showWarningNotification(props: INotificationProps, type?: string) {
  133. return showNotification({
  134. ...props,
  135. appearance: NOTIFICATION_TYPE.WARNING
  136. }, type);
  137. }
  138. /**
  139. * Queues a message notification for display.
  140. *
  141. * @param {Object} props - The props needed to show the notification component.
  142. * @param {string} type - Notification type.
  143. * @returns {Object}
  144. */
  145. export function showMessageNotification(props: INotificationProps, type?: string) {
  146. return showNotification({
  147. ...props,
  148. concatText: true,
  149. titleKey: 'notify.chatMessages',
  150. appearance: NOTIFICATION_TYPE.NORMAL,
  151. icon: NOTIFICATION_ICON.MESSAGE
  152. }, type);
  153. }
  154. /**
  155. * An array of names of participants that have joined the conference. The array
  156. * is replaced with an empty array as notifications are displayed.
  157. *
  158. * @private
  159. * @type {string[]}
  160. */
  161. let joinedParticipantsNames: string[] = [];
  162. /**
  163. * A throttled internal function that takes the internal list of participant
  164. * names, {@code joinedParticipantsNames}, and triggers the display of a
  165. * notification informing of their joining.
  166. *
  167. * @private
  168. * @type {Function}
  169. */
  170. const _throttledNotifyParticipantConnected = throttle((dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  171. const participantCount = getParticipantCount(getState());
  172. // Skip join notifications altogether for large meetings.
  173. if (participantCount > SILENT_JOIN_THRESHOLD) {
  174. joinedParticipantsNames = [];
  175. return;
  176. }
  177. const joinedParticipantsCount = joinedParticipantsNames.length;
  178. let notificationProps;
  179. if (joinedParticipantsCount >= 3) {
  180. notificationProps = {
  181. titleArguments: {
  182. name: joinedParticipantsNames[0]
  183. },
  184. titleKey: 'notify.connectedThreePlusMembers'
  185. };
  186. } else if (joinedParticipantsCount === 2) {
  187. notificationProps = {
  188. titleArguments: {
  189. first: joinedParticipantsNames[0],
  190. second: joinedParticipantsNames[1]
  191. },
  192. titleKey: 'notify.connectedTwoMembers'
  193. };
  194. } else if (joinedParticipantsCount) {
  195. notificationProps = {
  196. titleArguments: {
  197. name: joinedParticipantsNames[0]
  198. },
  199. titleKey: 'notify.connectedOneMember'
  200. };
  201. }
  202. if (notificationProps) {
  203. dispatch(
  204. showNotification(notificationProps, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  205. }
  206. joinedParticipantsNames = [];
  207. }, 2000, { leading: false });
  208. /**
  209. * An array of names of participants that have left the conference. The array
  210. * is replaced with an empty array as notifications are displayed.
  211. *
  212. * @private
  213. * @type {string[]}
  214. */
  215. let leftParticipantsNames: string[] = [];
  216. /**
  217. * A throttled internal function that takes the internal list of participant
  218. * names, {@code leftParticipantsNames}, and triggers the display of a
  219. * notification informing of their leaving.
  220. *
  221. * @private
  222. * @type {Function}
  223. */
  224. const _throttledNotifyParticipantLeft = throttle((dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  225. const participantCount = getParticipantCount(getState());
  226. // Skip left notifications altogether for large meetings.
  227. if (participantCount > SILENT_LEFT_THRESHOLD) {
  228. leftParticipantsNames = [];
  229. return;
  230. }
  231. const leftParticipantsCount = leftParticipantsNames.length;
  232. let notificationProps;
  233. if (leftParticipantsCount >= 3) {
  234. notificationProps = {
  235. titleArguments: {
  236. name: leftParticipantsNames[0]
  237. },
  238. titleKey: 'notify.leftThreePlusMembers'
  239. };
  240. } else if (leftParticipantsCount === 2) {
  241. notificationProps = {
  242. titleArguments: {
  243. first: leftParticipantsNames[0],
  244. second: leftParticipantsNames[1]
  245. },
  246. titleKey: 'notify.leftTwoMembers'
  247. };
  248. } else if (leftParticipantsCount) {
  249. notificationProps = {
  250. titleArguments: {
  251. name: leftParticipantsNames[0]
  252. },
  253. titleKey: 'notify.leftOneMember'
  254. };
  255. }
  256. if (notificationProps) {
  257. dispatch(
  258. showNotification(notificationProps, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  259. }
  260. leftParticipantsNames = [];
  261. }, 2000, { leading: false });
  262. /**
  263. * Queues the display of a notification of a participant having connected to
  264. * the meeting. The notifications are batched so that quick consecutive
  265. * connection events are shown in one notification.
  266. *
  267. * @param {string} displayName - The name of the participant that connected.
  268. * @returns {Function}
  269. */
  270. export function showParticipantJoinedNotification(displayName: string) {
  271. joinedParticipantsNames.push(displayName);
  272. return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>
  273. _throttledNotifyParticipantConnected(dispatch, getState);
  274. }
  275. /**
  276. * Queues the display of a notification of a participant having left to
  277. * the meeting. The notifications are batched so that quick consecutive
  278. * connection events are shown in one notification.
  279. *
  280. * @param {string} displayName - The name of the participant that left.
  281. * @returns {Function}
  282. */
  283. export function showParticipantLeftNotification(displayName: string) {
  284. leftParticipantsNames.push(displayName);
  285. return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>
  286. _throttledNotifyParticipantLeft(dispatch, getState);
  287. }