You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.any.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // @flow
  2. import { getLocalParticipant } from '../base/participants';
  3. import { extractFqnFromPath } from '../dynamic-branding/functions';
  4. import { REACTIONS } from './constants';
  5. import logger from './logger';
  6. /**
  7. * Returns the queue of reactions.
  8. *
  9. * @param {Object} state - The state of the application.
  10. * @returns {boolean}
  11. */
  12. export function getReactionsQueue(state: Object) {
  13. return state['features/reactions'].queue;
  14. }
  15. /**
  16. * Returns reaction key from the reaction message.
  17. *
  18. * @param {string} message - The reaction message.
  19. * @returns {string}
  20. */
  21. export function getReactionKeyByMessage(message: string): ?string {
  22. return Object.keys(REACTIONS).find(key => REACTIONS[key].message === `:${message}:`);
  23. }
  24. /**
  25. * Gets reactions key array from concatenated message.
  26. *
  27. * @param {string} message - The reaction message.
  28. * @returns {Array}
  29. */
  30. export function messageToKeyArray(message: string) {
  31. let formattedMessage = message.replace(/::/g, '-');
  32. formattedMessage = formattedMessage.replace(/:/g, '');
  33. const messageArray = formattedMessage.split('-');
  34. return messageArray.map<?string>(getReactionKeyByMessage);
  35. }
  36. /**
  37. * Sends reactions to the backend.
  38. *
  39. * @param {Object} state - The redux state object.
  40. * @param {Array} reactions - Reactions array to be sent.
  41. * @returns {void}
  42. */
  43. export async function sendReactionsWebhook(state: Object, reactions: Array<?string>) {
  44. const { webhookProxyUrl: url } = state['features/base/config'];
  45. const { conference } = state['features/base/conference'];
  46. const { jwt } = state['features/base/jwt'];
  47. const { locationURL } = state['features/base/connection'];
  48. const localParticipant = getLocalParticipant(state);
  49. const headers = {
  50. 'Authorization': `Bearer ${jwt}`,
  51. 'Content-Type': 'application/json'
  52. };
  53. const reqBody = {
  54. meetingFqn: extractFqnFromPath(locationURL.pathname),
  55. sessionId: conference.sessionId,
  56. submitted: Date.now(),
  57. reactions,
  58. participantId: localParticipant.id,
  59. participantName: localParticipant.name
  60. };
  61. if (url) {
  62. try {
  63. const res = await fetch(`${url}/reactions`, {
  64. method: 'POST',
  65. headers,
  66. body: JSON.stringify(reqBody)
  67. });
  68. if (!res.ok) {
  69. logger.error('Status error:', res.status);
  70. }
  71. } catch (err) {
  72. logger.error('Could not send request', err);
  73. }
  74. }
  75. }