2021-08-13 16:10:05 +00:00
|
|
|
// @flow
|
|
|
|
|
2021-09-09 22:46:41 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2021-11-10 17:49:53 +00:00
|
|
|
import {
|
2022-09-27 07:10:28 +00:00
|
|
|
PARTICIPANT_ROLE,
|
|
|
|
getParticipantById
|
2021-11-10 17:49:53 +00:00
|
|
|
} from '../base/participants';
|
2021-09-09 22:46:41 +00:00
|
|
|
|
2021-08-13 16:10:05 +00:00
|
|
|
/**
|
|
|
|
* Checks if the speaker stats search is disabled.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
|
|
|
* @returns {boolean} - True if the speaker stats search is disabled and false otherwise.
|
|
|
|
*/
|
|
|
|
export function isSpeakerStatsSearchDisabled(state: Object) {
|
2022-10-17 20:40:13 +00:00
|
|
|
return state['features/base/config']?.speakerStats.disableSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the speaker stats is disabled.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
|
|
|
* @returns {boolean} - True if the speaker stats search is disabled and false otherwise.
|
|
|
|
*/
|
|
|
|
export function isSpeakerStatsDisabled(state: Object) {
|
|
|
|
return state['features/base/config']?.speakerStats?.disabled;
|
2021-08-13 16:10:05 +00:00
|
|
|
}
|
2021-09-09 22:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets whether participants in speaker stats should be ordered or not, and with what priority.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
|
|
|
* @returns {Array<string>} - The speaker stats order array or an empty array.
|
|
|
|
*/
|
|
|
|
export function getSpeakerStatsOrder(state: Object) {
|
2022-10-17 20:40:13 +00:00
|
|
|
return state['features/base/config']?.speakerStats.order ?? [
|
2021-09-09 22:46:41 +00:00
|
|
|
'role',
|
|
|
|
'name',
|
|
|
|
'hasLeft'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets speaker stats.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
|
|
|
* @returns {Object} - The speaker stats.
|
|
|
|
*/
|
|
|
|
export function getSpeakerStats(state: Object) {
|
|
|
|
return state['features/speaker-stats']?.stats ?? {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets speaker stats search criteria.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
2021-09-24 16:39:24 +00:00
|
|
|
* @returns {string | null} - The search criteria.
|
2021-09-09 22:46:41 +00:00
|
|
|
*/
|
|
|
|
export function getSearchCriteria(state: Object) {
|
2021-09-24 16:39:24 +00:00
|
|
|
return state['features/speaker-stats']?.criteria;
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets if speaker stats reorder is pending.
|
|
|
|
*
|
|
|
|
* @param {*} state - The redux state.
|
|
|
|
* @returns {boolean} - The pending reorder flag.
|
|
|
|
*/
|
|
|
|
export function getPendingReorder(state: Object) {
|
|
|
|
return state['features/speaker-stats']?.pendingReorder ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-28 07:51:53 +00:00
|
|
|
* Get sorted speaker stats ids based on a configuration setting.
|
2021-09-09 22:46:41 +00:00
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @param {Object} stats - The current speaker stats.
|
2022-09-28 07:51:53 +00:00
|
|
|
* @returns {Object} - Ordered speaker stats ids.
|
2021-09-09 22:46:41 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2022-09-28 07:51:53 +00:00
|
|
|
export function getSortedSpeakerStatsIds(state: Object, stats: Object) {
|
2021-09-09 22:46:41 +00:00
|
|
|
const orderConfig = getSpeakerStatsOrder(state);
|
|
|
|
|
|
|
|
if (orderConfig) {
|
|
|
|
const enhancedStats = getEnhancedStatsForOrdering(state, stats, orderConfig);
|
2022-09-28 07:51:53 +00:00
|
|
|
|
|
|
|
return Object.entries(enhancedStats)
|
|
|
|
.sort(([ , a ], [ , b ]) => compareFn(a, b))
|
|
|
|
.map(el => el[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Compares the order of two participants in the speaker stats list.
|
|
|
|
*
|
|
|
|
* @param {Object} currentParticipant - The first participant for comparison.
|
|
|
|
* @param {Object} nextParticipant - The second participant for comparison.
|
|
|
|
* @returns {number} - The sort order of the two participants.
|
|
|
|
*/
|
|
|
|
function compareFn(currentParticipant, nextParticipant) {
|
|
|
|
if (orderConfig.includes('hasLeft')) {
|
|
|
|
if (nextParticipant.hasLeft() && !currentParticipant.hasLeft()) {
|
|
|
|
return -1;
|
|
|
|
} else if (currentParticipant.hasLeft() && !nextParticipant.hasLeft()) {
|
|
|
|
return 1;
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
2022-09-28 07:51:53 +00:00
|
|
|
}
|
2021-09-09 22:46:41 +00:00
|
|
|
|
2022-09-28 07:51:53 +00:00
|
|
|
let result;
|
2021-09-09 22:46:41 +00:00
|
|
|
|
2022-09-28 07:51:53 +00:00
|
|
|
for (const sortCriteria of orderConfig) {
|
|
|
|
switch (sortCriteria) {
|
|
|
|
case 'role':
|
|
|
|
if (!nextParticipant.isModerator && currentParticipant.isModerator) {
|
|
|
|
result = -1;
|
|
|
|
} else if (!currentParticipant.isModerator && nextParticipant.isModerator) {
|
|
|
|
result = 1;
|
|
|
|
} else {
|
|
|
|
result = 0;
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
2022-09-28 07:51:53 +00:00
|
|
|
break;
|
|
|
|
case 'name':
|
|
|
|
result = (currentParticipant.displayName || '').localeCompare(
|
|
|
|
nextParticipant.displayName || ''
|
|
|
|
);
|
|
|
|
break;
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:51:53 +00:00
|
|
|
if (result !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 22:46:41 +00:00
|
|
|
|
2022-09-28 07:51:53 +00:00
|
|
|
return result;
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enhance speaker stats to include data needed for ordering.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @param {Object} stats - Speaker stats.
|
|
|
|
* @param {Array<string>} orderConfig - Ordering configuration.
|
|
|
|
* @returns {Object} - Enhanced speaker stats.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
function getEnhancedStatsForOrdering(state, stats, orderConfig) {
|
|
|
|
if (!orderConfig) {
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const id in stats) {
|
|
|
|
if (stats[id].hasOwnProperty('_hasLeft') && !stats[id].hasLeft()) {
|
|
|
|
if (orderConfig.includes('role')) {
|
|
|
|
const participant = getParticipantById(state, stats[id].getUserId());
|
|
|
|
|
|
|
|
stats[id].isModerator = participant && participant.role === PARTICIPANT_ROLE.MODERATOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter stats by search criteria.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @param {Object | undefined} stats - The unfiltered stats.
|
|
|
|
*
|
|
|
|
* @returns {Object} - Filtered speaker stats.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function filterBySearchCriteria(state: Object, stats: ?Object) {
|
|
|
|
const filteredStats = _.cloneDeep(stats ?? getSpeakerStats(state));
|
|
|
|
const criteria = getSearchCriteria(state);
|
|
|
|
|
2021-09-24 16:39:24 +00:00
|
|
|
if (criteria !== null) {
|
2021-09-09 22:46:41 +00:00
|
|
|
const searchRegex = new RegExp(criteria, 'gi');
|
|
|
|
|
|
|
|
for (const id in filteredStats) {
|
|
|
|
if (filteredStats[id].hasOwnProperty('_isLocalStats')) {
|
|
|
|
const name = filteredStats[id].getDisplayName();
|
|
|
|
|
2021-09-24 16:39:24 +00:00
|
|
|
filteredStats[id].hidden = !name || !name.match(searchRegex);
|
2021-09-09 22:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredStats;
|
|
|
|
}
|
2021-12-28 14:35:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the hidden speaker stats.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @param {Object | undefined} stats - The unfiltered stats.
|
|
|
|
*
|
|
|
|
* @returns {Object} - Speaker stats.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function resetHiddenStats(state: Object, stats: ?Object) {
|
|
|
|
const resetStats = _.cloneDeep(stats ?? getSpeakerStats(state));
|
|
|
|
|
|
|
|
for (const id in resetStats) {
|
|
|
|
if (resetStats[id].hidden) {
|
|
|
|
resetStats[id].hidden = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resetStats;
|
|
|
|
}
|