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