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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 localParticipant = getLocalParticipant(state);
  52. const headers = {
  53. 'Authorization': `Bearer ${jwt}`,
  54. 'Content-Type': 'application/json'
  55. };
  56. const reqBody = {
  57. meetingFqn: extractFqnFromPath(),
  58. sessionId: conference.sessionId,
  59. submitted: Date.now(),
  60. reactions,
  61. participantId: localParticipant.id,
  62. participantName: localParticipant.name
  63. };
  64. if (url) {
  65. try {
  66. const res = await fetch(`${url}/reactions`, {
  67. method: 'POST',
  68. headers,
  69. body: JSON.stringify(reqBody)
  70. });
  71. if (!res.ok) {
  72. logger.error('Status error:', res.status);
  73. }
  74. } catch (err) {
  75. logger.error('Could not send request', err);
  76. }
  77. }
  78. }
  79. /**
  80. * Returns unique reactions from the reactions buffer.
  81. *
  82. * @param {Array} reactions - The reactions buffer.
  83. * @returns {Array}
  84. */
  85. function getUniqueReactions(reactions: Array<string>) {
  86. return [ ...new Set(reactions) ];
  87. }
  88. /**
  89. * Returns frequency of given reaction in array.
  90. *
  91. * @param {Array} reactions - Array of reactions.
  92. * @param {string} reaction - Reaction to get frequency for.
  93. * @returns {number}
  94. */
  95. function getReactionFrequency(reactions: Array<string>, reaction: string) {
  96. return reactions.filter(r => r === reaction).length;
  97. }
  98. /**
  99. * Returns the threshold number for a given frequency.
  100. *
  101. * @param {number} frequency - Frequency of reaction.
  102. * @returns {number}
  103. */
  104. function getSoundThresholdByFrequency(frequency) {
  105. for (const i of SOUNDS_THRESHOLDS) {
  106. if (frequency <= i) {
  107. return i;
  108. }
  109. }
  110. return SOUNDS_THRESHOLDS[SOUNDS_THRESHOLDS.length - 1];
  111. }
  112. /**
  113. * Returns unique reactions with threshold.
  114. *
  115. * @param {Array} reactions - The reactions buffer.
  116. * @returns {Array}
  117. */
  118. export function getReactionsSoundsThresholds(reactions: Array<string>) {
  119. const unique = getUniqueReactions(reactions);
  120. return unique.map<Object>(reaction => {
  121. return {
  122. reaction,
  123. threshold: getSoundThresholdByFrequency(getReactionFrequency(reactions, reaction))
  124. };
  125. });
  126. }
  127. /**
  128. * Whether or not the reactions are enabled.
  129. *
  130. * @param {Object} state - The Redux state object.
  131. * @returns {boolean}
  132. */
  133. export function isReactionsEnabled(state: Object) {
  134. const { disableReactions } = state['features/base/config'];
  135. if (navigator.product === 'ReactNative') {
  136. return !disableReactions && getFeatureFlag(state, REACTIONS_ENABLED, true);
  137. }
  138. return !disableReactions;
  139. }