2021-07-13 06:50:08 +00:00
|
|
|
// @flow
|
|
|
|
|
2021-07-20 11:31:49 +00:00
|
|
|
import uuid from 'uuid';
|
|
|
|
|
2021-08-31 11:00:27 +00:00
|
|
|
import { getFeatureFlag, REACTIONS_ENABLED } from '../base/flags';
|
2021-07-15 12:23:48 +00:00
|
|
|
import { getLocalParticipant } from '../base/participants';
|
|
|
|
import { extractFqnFromPath } from '../dynamic-branding/functions';
|
|
|
|
|
2021-08-23 09:57:56 +00:00
|
|
|
import { REACTIONS, SOUNDS_THRESHOLDS } from './constants';
|
2021-07-15 12:23:48 +00:00
|
|
|
import logger from './logger';
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the queue of reactions.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The state of the application.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function getReactionsQueue(state: Object) {
|
|
|
|
return state['features/reactions'].queue;
|
|
|
|
}
|
2021-07-15 12:23:48 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-20 11:31:49 +00:00
|
|
|
* Returns chat message from reactions buffer.
|
2021-07-15 12:23:48 +00:00
|
|
|
*
|
2021-07-20 11:31:49 +00:00
|
|
|
* @param {Array} buffer - The reactions buffer.
|
2021-07-15 12:23:48 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-07-20 11:31:49 +00:00
|
|
|
export function getReactionMessageFromBuffer(buffer: Array<string>) {
|
|
|
|
return buffer.map(reaction => REACTIONS[reaction].message).reduce((acc, val) => `${acc}${val}`);
|
2021-07-15 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-20 11:31:49 +00:00
|
|
|
* Returns reactions array with uid.
|
2021-07-15 12:23:48 +00:00
|
|
|
*
|
2021-07-20 11:31:49 +00:00
|
|
|
* @param {Array} buffer - The reactions buffer.
|
2021-07-15 12:23:48 +00:00
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2021-07-20 11:31:49 +00:00
|
|
|
export function getReactionsWithId(buffer: Array<string>) {
|
|
|
|
return buffer.map<Object>(reaction => {
|
|
|
|
return {
|
|
|
|
reaction,
|
|
|
|
uid: uuid.v4()
|
|
|
|
};
|
|
|
|
});
|
2021-07-15 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends reactions to the backend.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state object.
|
|
|
|
* @param {Array} reactions - Reactions array to be sent.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export async function sendReactionsWebhook(state: Object, reactions: Array<?string>) {
|
|
|
|
const { webhookProxyUrl: url } = state['features/base/config'];
|
|
|
|
const { conference } = state['features/base/conference'];
|
|
|
|
const { jwt } = state['features/base/jwt'];
|
|
|
|
const localParticipant = getLocalParticipant(state);
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
'Authorization': `Bearer ${jwt}`,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const reqBody = {
|
2021-09-10 07:00:54 +00:00
|
|
|
meetingFqn: extractFqnFromPath(),
|
2021-07-15 12:23:48 +00:00
|
|
|
sessionId: conference.sessionId,
|
|
|
|
submitted: Date.now(),
|
|
|
|
reactions,
|
|
|
|
participantId: localParticipant.id,
|
|
|
|
participantName: localParticipant.name
|
|
|
|
};
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
try {
|
|
|
|
const res = await fetch(`${url}/reactions`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: JSON.stringify(reqBody)
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
logger.error('Status error:', res.status);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Could not send request', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 09:57:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns unique reactions from the reactions buffer.
|
|
|
|
*
|
|
|
|
* @param {Array} reactions - The reactions buffer.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
function getUniqueReactions(reactions: Array<string>) {
|
|
|
|
return [ ...new Set(reactions) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns frequency of given reaction in array.
|
|
|
|
*
|
|
|
|
* @param {Array} reactions - Array of reactions.
|
|
|
|
* @param {string} reaction - Reaction to get frequency for.
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
function getReactionFrequency(reactions: Array<string>, reaction: string) {
|
|
|
|
return reactions.filter(r => r === reaction).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the threshold number for a given frequency.
|
|
|
|
*
|
|
|
|
* @param {number} frequency - Frequency of reaction.
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
function getSoundThresholdByFrequency(frequency) {
|
|
|
|
for (const i of SOUNDS_THRESHOLDS) {
|
|
|
|
if (frequency <= i) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SOUNDS_THRESHOLDS[SOUNDS_THRESHOLDS.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns unique reactions with threshold.
|
|
|
|
*
|
|
|
|
* @param {Array} reactions - The reactions buffer.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
export function getReactionsSoundsThresholds(reactions: Array<string>) {
|
|
|
|
const unique = getUniqueReactions(reactions);
|
|
|
|
|
|
|
|
return unique.map<Object>(reaction => {
|
|
|
|
return {
|
|
|
|
reaction,
|
|
|
|
threshold: getSoundThresholdByFrequency(getReactionFrequency(reactions, reaction))
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2021-08-31 11:00:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the reactions are enabled.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state object.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isReactionsEnabled(state: Object) {
|
2021-09-21 17:30:24 +00:00
|
|
|
const { disableReactions } = state['features/base/config'];
|
2021-08-31 11:00:27 +00:00
|
|
|
|
|
|
|
if (navigator.product === 'ReactNative') {
|
2021-09-21 17:30:24 +00:00
|
|
|
return !disableReactions && getFeatureFlag(state, REACTIONS_ENABLED, true);
|
2021-08-31 11:00:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 17:30:24 +00:00
|
|
|
return !disableReactions;
|
2021-08-31 11:00:27 +00:00
|
|
|
}
|