"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-7313/react/features/reactions/functions.any.ts" (2 Jun 2023, 4715 Bytes) of package /linux/misc/jitsi-meet-7313.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import { v4 as uuidv4 } from 'uuid';
    2 
    3 import { IReduxState } from '../app/types';
    4 import { REACTIONS_ENABLED } from '../base/flags/constants';
    5 import { getFeatureFlag } from '../base/flags/functions';
    6 import { getLocalParticipant } from '../base/participants/functions';
    7 import { extractFqnFromPath } from '../dynamic-branding/functions.any';
    8 
    9 import { IReactionEmojiProps, REACTIONS, ReactionThreshold, SOUNDS_THRESHOLDS } from './constants';
   10 import logger from './logger';
   11 
   12 /**
   13  * Returns the queue of reactions.
   14  *
   15  * @param {Object} state - The state of the application.
   16  * @returns {Array}
   17  */
   18 export function getReactionsQueue(state: IReduxState): Array<IReactionEmojiProps> {
   19     return state['features/reactions'].queue;
   20 }
   21 
   22 /**
   23  * Returns chat message from reactions buffer.
   24  *
   25  * @param {Array} buffer - The reactions buffer.
   26  * @returns {string}
   27  */
   28 export function getReactionMessageFromBuffer(buffer: Array<string>): string {
   29     return buffer.map<string>(reaction => REACTIONS[reaction].message).reduce((acc, val) => `${acc}${val}`);
   30 }
   31 
   32 /**
   33  * Returns reactions array with uid.
   34  *
   35  * @param {Array} buffer - The reactions buffer.
   36  * @returns {Array}
   37  */
   38 export function getReactionsWithId(buffer: Array<string>): Array<IReactionEmojiProps> {
   39     return buffer.map<IReactionEmojiProps>(reaction => {
   40         return {
   41             reaction,
   42             uid: uuidv4()
   43         };
   44     });
   45 }
   46 
   47 /**
   48  * Sends reactions to the backend.
   49  *
   50  * @param {Object} state - The redux state object.
   51  * @param {Array} reactions - Reactions array to be sent.
   52  * @returns {void}
   53  */
   54 export async function sendReactionsWebhook(state: IReduxState, reactions: Array<string>) {
   55     const { webhookProxyUrl: url } = state['features/base/config'];
   56     const { conference } = state['features/base/conference'];
   57     const { jwt } = state['features/base/jwt'];
   58     const { connection } = state['features/base/connection'];
   59     const jid = connection?.getJid();
   60     const localParticipant = getLocalParticipant(state);
   61 
   62     const headers = {
   63         ...jwt ? { 'Authorization': `Bearer ${jwt}` } : {},
   64         'Content-Type': 'application/json'
   65     };
   66 
   67 
   68     const reqBody = {
   69         meetingFqn: extractFqnFromPath(),
   70         sessionId: conference?.sessionId,
   71         submitted: Date.now(),
   72         reactions,
   73         participantId: localParticipant?.jwtId,
   74         participantName: localParticipant?.name,
   75         participantJid: jid
   76     };
   77 
   78     if (url) {
   79         try {
   80             const res = await fetch(`${url}/reactions`, {
   81                 method: 'POST',
   82                 headers,
   83                 body: JSON.stringify(reqBody)
   84             });
   85 
   86             if (!res.ok) {
   87                 logger.error('Status error:', res.status);
   88             }
   89         } catch (err) {
   90             logger.error('Could not send request', err);
   91         }
   92     }
   93 }
   94 
   95 /**
   96  * Returns unique reactions from the reactions buffer.
   97  *
   98  * @param {Array} reactions - The reactions buffer.
   99  * @returns {Array}
  100  */
  101 function getUniqueReactions(reactions: Array<string>): Array<string> {
  102     return [ ...new Set(reactions) ];
  103 }
  104 
  105 /**
  106  * Returns frequency of given reaction in array.
  107  *
  108  * @param {Array} reactions - Array of reactions.
  109  * @param {string} reaction - Reaction to get frequency for.
  110  * @returns {number}
  111  */
  112 function getReactionFrequency(reactions: Array<string>, reaction: string): number {
  113     return reactions.filter(r => r === reaction).length;
  114 }
  115 
  116 /**
  117  * Returns the threshold number for a given frequency.
  118  *
  119  * @param {number} frequency - Frequency of reaction.
  120  * @returns {number}
  121  */
  122 function getSoundThresholdByFrequency(frequency: number): number {
  123     for (const i of SOUNDS_THRESHOLDS) {
  124         if (frequency <= i) {
  125             return i;
  126         }
  127     }
  128 
  129     return SOUNDS_THRESHOLDS[SOUNDS_THRESHOLDS.length - 1];
  130 }
  131 
  132 /**
  133  * Returns unique reactions with threshold.
  134  *
  135  * @param {Array} reactions - The reactions buffer.
  136  * @returns {Array}
  137  */
  138 export function getReactionsSoundsThresholds(reactions: Array<string>): Array<ReactionThreshold> {
  139     const unique = getUniqueReactions(reactions);
  140 
  141     return unique.map<ReactionThreshold>(reaction => {
  142         return {
  143             reaction,
  144             threshold: getSoundThresholdByFrequency(getReactionFrequency(reactions, reaction))
  145         };
  146     });
  147 }
  148 
  149 /**
  150  * Whether or not the reactions are enabled.
  151  *
  152  * @param {Object} state - The Redux state object.
  153  * @returns {boolean}
  154  */
  155 export function isReactionsEnabled(state: IReduxState): boolean {
  156     const { disableReactions } = state['features/base/config'];
  157 
  158     if (navigator.product === 'ReactNative') {
  159         return !disableReactions && getFeatureFlag(state, REACTIONS_ENABLED, true);
  160     }
  161 
  162     return !disableReactions;
  163 }