feat(reactions-webhook) Added reactions backend call for webhook (#9534)

* Added backend call for reactions webhook

* Updated webhook url

* Fixed linting error

* Code review fixes

* Fixed linting errors
This commit is contained in:
robertpin 2021-07-15 15:23:48 +03:00 committed by GitHub
parent b3e03fe50c
commit ea0d953d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 1 deletions

View File

@ -1,5 +1,11 @@
// @flow // @flow
import { getLocalParticipant } from '../base/participants';
import { extractFqnFromPath } from '../dynamic-branding/functions';
import { REACTIONS } from './constants';
import logger from './logger';
/** /**
* Returns the queue of reactions. * Returns the queue of reactions.
* *
@ -9,3 +15,74 @@
export function getReactionsQueue(state: Object) { export function getReactionsQueue(state: Object) {
return state['features/reactions'].queue; return state['features/reactions'].queue;
} }
/**
* Returns reaction key from the reaction message.
*
* @param {string} message - The reaction message.
* @returns {string}
*/
export function getReactionKeyByMessage(message: string): ?string {
return Object.keys(REACTIONS).find(key => REACTIONS[key].message === `:${message}:`);
}
/**
* Gets reactions key array from concatenated message.
*
* @param {string} message - The reaction message.
* @returns {Array}
*/
export function messageToKeyArray(message: string) {
let formattedMessage = message.replace(/::/g, '-');
formattedMessage = formattedMessage.replace(/:/g, '');
const messageArray = formattedMessage.split('-');
return messageArray.map<?string>(getReactionKeyByMessage);
}
/**
* 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 { locationURL } = state['features/base/connection'];
const localParticipant = getLocalParticipant(state);
const headers = {
'Authorization': `Bearer ${jwt}`,
'Content-Type': 'application/json'
};
const reqBody = {
meetingFqn: extractFqnFromPath(locationURL.pathname),
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);
}
}
}

View File

@ -0,0 +1,5 @@
// @flow
import { getLogger } from '../base/logging/functions';
export default getLogger('features/base/reactions');

View File

@ -2,6 +2,7 @@
import { ENDPOINT_REACTION_NAME } from '../../../modules/API/constants'; import { ENDPOINT_REACTION_NAME } from '../../../modules/API/constants';
import { MiddlewareRegistry } from '../base/redux'; import { MiddlewareRegistry } from '../base/redux';
import { isVpaasMeeting } from '../billing-counter/functions';
import { import {
SET_REACTIONS_MESSAGE, SET_REACTIONS_MESSAGE,
@ -17,6 +18,7 @@ import {
setReactionQueue setReactionQueue
} from './actions.any'; } from './actions.any';
import { REACTIONS } from './constants'; import { REACTIONS } from './constants';
import { messageToKeyArray, sendReactionsWebhook } from './functions.any';
declare var APP: Object; declare var APP: Object;
@ -46,7 +48,12 @@ MiddlewareRegistry.register(store => next => action => {
} }
case CLEAR_REACTIONS_MESSAGE: { case CLEAR_REACTIONS_MESSAGE: {
const { message } = getState()['features/reactions']; const state = getState();
const { message } = state['features/reactions'];
if (isVpaasMeeting(state)) {
sendReactionsWebhook(state, messageToKeyArray(message));
}
dispatch(addReactionsMessageToChat(message)); dispatch(addReactionsMessageToChat(message));