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.ts 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import _ from 'lodash';
  2. import { IReduxState } from '../app/types';
  3. import { PARTICIPANT_ROLE } from '../base/participants/constants';
  4. import { getParticipantById } from '../base/participants/functions';
  5. /**
  6. * Checks if the speaker stats search is disabled.
  7. *
  8. * @param {IReduxState} state - The redux state.
  9. * @returns {boolean} - True if the speaker stats search is disabled and false otherwise.
  10. */
  11. export function isSpeakerStatsSearchDisabled(state: IReduxState) {
  12. return state['features/base/config']?.speakerStats?.disableSearch;
  13. }
  14. /**
  15. * Checks if the speaker stats is disabled.
  16. *
  17. * @param {IReduxState} state - The redux state.
  18. * @returns {boolean} - True if the speaker stats search is disabled and false otherwise.
  19. */
  20. export function isSpeakerStatsDisabled(state: IReduxState) {
  21. return state['features/base/config']?.speakerStats?.disabled;
  22. }
  23. /**
  24. * Gets whether participants in speaker stats should be ordered or not, and with what priority.
  25. *
  26. * @param {IReduxState} state - The redux state.
  27. * @returns {Array<string>} - The speaker stats order array or an empty array.
  28. */
  29. export function getSpeakerStatsOrder(state: IReduxState) {
  30. return state['features/base/config']?.speakerStats?.order ?? [
  31. 'role',
  32. 'name',
  33. 'hasLeft'
  34. ];
  35. }
  36. /**
  37. * Gets speaker stats.
  38. *
  39. * @param {IReduxState} state - The redux state.
  40. * @returns {Object} - The speaker stats.
  41. */
  42. export function getSpeakerStats(state: IReduxState) {
  43. return state['features/speaker-stats']?.stats ?? {};
  44. }
  45. /**
  46. * Gets speaker stats search criteria.
  47. *
  48. * @param {IReduxState} state - The redux state.
  49. * @returns {string | null} - The search criteria.
  50. */
  51. export function getSearchCriteria(state: IReduxState) {
  52. return state['features/speaker-stats']?.criteria;
  53. }
  54. /**
  55. * Gets if speaker stats reorder is pending.
  56. *
  57. * @param {IReduxState} state - The redux state.
  58. * @returns {boolean} - The pending reorder flag.
  59. */
  60. export function getPendingReorder(state: IReduxState) {
  61. return state['features/speaker-stats']?.pendingReorder ?? false;
  62. }
  63. /**
  64. * Get sorted speaker stats ids based on a configuration setting.
  65. *
  66. * @param {IReduxState} state - The redux state.
  67. * @param {Object} stats - The current speaker stats.
  68. * @returns {Object} - Ordered speaker stats ids.
  69. * @public
  70. */
  71. export function getSortedSpeakerStatsIds(state: IReduxState, stats: Object) {
  72. const orderConfig = getSpeakerStatsOrder(state);
  73. if (orderConfig) {
  74. const enhancedStats = getEnhancedStatsForOrdering(state, stats, orderConfig);
  75. return Object.entries(enhancedStats)
  76. .sort(([ , a ], [ , b ]) => compareFn(a, b))
  77. .map(el => el[0]);
  78. }
  79. /**
  80. *
  81. * Compares the order of two participants in the speaker stats list.
  82. *
  83. * @param {Object} currentParticipant - The first participant for comparison.
  84. * @param {Object} nextParticipant - The second participant for comparison.
  85. * @returns {number} - The sort order of the two participants.
  86. */
  87. function compareFn(currentParticipant: any, nextParticipant: any) {
  88. if (orderConfig.includes('hasLeft')) {
  89. if (nextParticipant.hasLeft() && !currentParticipant.hasLeft()) {
  90. return -1;
  91. } else if (currentParticipant.hasLeft() && !nextParticipant.hasLeft()) {
  92. return 1;
  93. }
  94. }
  95. let result;
  96. for (const sortCriteria of orderConfig) {
  97. switch (sortCriteria) {
  98. case 'role':
  99. if (!nextParticipant.isModerator && currentParticipant.isModerator) {
  100. result = -1;
  101. } else if (!currentParticipant.isModerator && nextParticipant.isModerator) {
  102. result = 1;
  103. } else {
  104. result = 0;
  105. }
  106. break;
  107. case 'name':
  108. result = (currentParticipant.displayName || '').localeCompare(
  109. nextParticipant.displayName || ''
  110. );
  111. break;
  112. }
  113. if (result !== 0) {
  114. break;
  115. }
  116. }
  117. return result;
  118. }
  119. }
  120. /**
  121. * Enhance speaker stats to include data needed for ordering.
  122. *
  123. * @param {IReduxState} state - The redux state.
  124. * @param {Object} stats - Speaker stats.
  125. * @param {Array<string>} orderConfig - Ordering configuration.
  126. * @returns {Object} - Enhanced speaker stats.
  127. * @public
  128. */
  129. function getEnhancedStatsForOrdering(state: IReduxState, stats: any, orderConfig?: string[]) {
  130. if (!orderConfig) {
  131. return stats;
  132. }
  133. for (const id in stats) {
  134. if (stats[id].hasOwnProperty('_hasLeft') && !stats[id].hasLeft()) {
  135. if (orderConfig.includes('role')) {
  136. const participant = getParticipantById(state, stats[id].getUserId());
  137. stats[id].isModerator = participant && participant.role === PARTICIPANT_ROLE.MODERATOR;
  138. }
  139. }
  140. }
  141. return stats;
  142. }
  143. /**
  144. * Filter stats by search criteria.
  145. *
  146. * @param {IReduxState} state - The redux state.
  147. * @param {Object | undefined} stats - The unfiltered stats.
  148. *
  149. * @returns {Object} - Filtered speaker stats.
  150. * @public
  151. */
  152. export function filterBySearchCriteria(state: IReduxState, stats?: Object) {
  153. const filteredStats: any = _.cloneDeep(stats ?? getSpeakerStats(state));
  154. const criteria = getSearchCriteria(state);
  155. if (criteria !== null) {
  156. const searchRegex = new RegExp(criteria, 'gi');
  157. for (const id in filteredStats) {
  158. if (filteredStats[id].hasOwnProperty('_isLocalStats')) {
  159. const name = filteredStats[id].getDisplayName();
  160. filteredStats[id].hidden = !name || !name.match(searchRegex);
  161. }
  162. }
  163. }
  164. return filteredStats;
  165. }
  166. /**
  167. * Reset the hidden speaker stats.
  168. *
  169. * @param {IReduxState} state - The redux state.
  170. * @param {Object | undefined} stats - The unfiltered stats.
  171. *
  172. * @returns {Object} - Speaker stats.
  173. * @public
  174. */
  175. export function resetHiddenStats(state: IReduxState, stats?: Object) {
  176. const resetStats: any = _.cloneDeep(stats ?? getSpeakerStats(state));
  177. for (const id in resetStats) {
  178. if (resetStats[id].hidden) {
  179. resetStats[id].hidden = false;
  180. }
  181. }
  182. return resetStats;
  183. }