Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.any.js 4.3KB

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