123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- import { throttle } from 'lodash-es';
-
- 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 = NOTIFICATION_TIMEOUT_TYPE.STICKY) {
- return showNotification({
- ...props,
- appearance: NOTIFICATION_TYPE.ERROR
- }, type);
- }
-
- /**
- * Queues a success notification for display.
- *
- * @param {Object} props - The props needed to show the notification component.
- * @param {string} type - Notification type.
- * @returns {Object}
- */
- export function showSuccessNotification(props: INotificationProps, type?: string) {
- return showNotification({
- ...props,
- appearance: NOTIFICATION_TYPE.SUCCESS
- }, 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);
- }
|