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 4.2KB

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