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.

reducer.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // @flow
  2. import { SCREEN_SHARE_REMOTE_PARTICIPANTS_UPDATED } from '../../video-layout/actionTypes';
  3. import { ReducerRegistry, set } from '../redux';
  4. import {
  5. DOMINANT_SPEAKER_CHANGED,
  6. PARTICIPANT_ID_CHANGED,
  7. PARTICIPANT_JOINED,
  8. PARTICIPANT_LEFT,
  9. PARTICIPANT_UPDATED,
  10. PIN_PARTICIPANT,
  11. RAISE_HAND_UPDATED,
  12. SET_LOADABLE_AVATAR_URL
  13. } from './actionTypes';
  14. import { LOCAL_PARTICIPANT_DEFAULT_ID, PARTICIPANT_ROLE } from './constants';
  15. import { isParticipantModerator } from './functions';
  16. /**
  17. * Participant object.
  18. *
  19. * @typedef {Object} Participant
  20. * @property {string} id - Participant ID.
  21. * @property {string} name - Participant name.
  22. * @property {string} avatar - Path to participant avatar if any.
  23. * @property {string} role - Participant role.
  24. * @property {boolean} local - If true, participant is local.
  25. * @property {boolean} pinned - If true, participant is currently a
  26. * "PINNED_ENDPOINT".
  27. * @property {boolean} dominantSpeaker - If this participant is the dominant
  28. * speaker in the (associated) conference, {@code true}; otherwise,
  29. * {@code false}.
  30. * @property {string} email - Participant email.
  31. */
  32. /**
  33. * The participant properties which cannot be updated through
  34. * {@link PARTICIPANT_UPDATED}. They either identify the participant or can only
  35. * be modified through property-dedicated actions.
  36. *
  37. * @type {string[]}
  38. */
  39. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE = [
  40. // The following properties identify the participant:
  41. 'conference',
  42. 'id',
  43. 'local',
  44. // The following properties can only be modified through property-dedicated
  45. // actions:
  46. 'dominantSpeaker',
  47. 'pinned'
  48. ];
  49. const DEFAULT_STATE = {
  50. dominantSpeaker: undefined,
  51. everyoneIsModerator: false,
  52. fakeParticipants: new Map(),
  53. haveParticipantWithScreenSharingFeature: false,
  54. local: undefined,
  55. pinnedParticipant: undefined,
  56. raisedHandsQueue: [],
  57. remote: new Map(),
  58. sortedRemoteParticipants: new Map(),
  59. sortedRemoteScreenshares: new Map(),
  60. speakersList: new Map()
  61. };
  62. /**
  63. * Listen for actions which add, remove, or update the set of participants in
  64. * the conference.
  65. *
  66. * @param {Participant[]} state - List of participants to be modified.
  67. * @param {Object} action - Action object.
  68. * @param {string} action.type - Type of action.
  69. * @param {Participant} action.participant - Information about participant to be
  70. * added/removed/modified.
  71. * @returns {Participant[]}
  72. */
  73. ReducerRegistry.register('features/base/participants', (state = DEFAULT_STATE, action) => {
  74. switch (action.type) {
  75. case PARTICIPANT_ID_CHANGED: {
  76. const { local } = state;
  77. if (local) {
  78. if (action.newValue === 'local' && state.raisedHandsQueue.find(pid => pid.id === local.id)) {
  79. state.raisedHandsQueue = state.raisedHandsQueue.filter(pid => pid.id !== local.id);
  80. }
  81. state.local = {
  82. ...local,
  83. id: action.newValue
  84. };
  85. return {
  86. ...state
  87. };
  88. }
  89. return state;
  90. }
  91. case DOMINANT_SPEAKER_CHANGED: {
  92. const { participant } = action;
  93. const { id, previousSpeakers = [] } = participant;
  94. const { dominantSpeaker, local } = state;
  95. const newSpeakers = [ id, ...previousSpeakers ];
  96. const sortedSpeakersList = [];
  97. for (const speaker of newSpeakers) {
  98. if (speaker !== local?.id) {
  99. const remoteParticipant = state.remote.get(speaker);
  100. remoteParticipant
  101. && sortedSpeakersList.push(
  102. [ speaker, _getDisplayName(state, remoteParticipant.name) ]
  103. );
  104. }
  105. }
  106. // Keep the remote speaker list sorted alphabetically.
  107. sortedSpeakersList.sort((a, b) => a[1].localeCompare(b[1]));
  108. // Only one dominant speaker is allowed.
  109. if (dominantSpeaker) {
  110. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  111. }
  112. if (_updateParticipantProperty(state, id, 'dominantSpeaker', true)) {
  113. return {
  114. ...state,
  115. dominantSpeaker: id,
  116. speakersList: new Map(sortedSpeakersList)
  117. };
  118. }
  119. delete state.dominantSpeaker;
  120. return {
  121. ...state
  122. };
  123. }
  124. case PIN_PARTICIPANT: {
  125. const { participant } = action;
  126. const { id } = participant;
  127. const { pinnedParticipant } = state;
  128. // Only one pinned participant is allowed.
  129. if (pinnedParticipant) {
  130. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  131. }
  132. if (id && _updateParticipantProperty(state, id, 'pinned', true)) {
  133. return {
  134. ...state,
  135. pinnedParticipant: id
  136. };
  137. }
  138. delete state.pinnedParticipant;
  139. return {
  140. ...state
  141. };
  142. }
  143. case SET_LOADABLE_AVATAR_URL:
  144. case PARTICIPANT_UPDATED: {
  145. const { participant } = action;
  146. let { id } = participant;
  147. const { local } = participant;
  148. if (!id && local) {
  149. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  150. }
  151. let newParticipant;
  152. if (state.remote.has(id)) {
  153. newParticipant = _participant(state.remote.get(id), action);
  154. state.remote.set(id, newParticipant);
  155. } else if (id === state.local?.id) {
  156. newParticipant = state.local = _participant(state.local, action);
  157. }
  158. if (newParticipant) {
  159. // everyoneIsModerator calculation:
  160. const isModerator = isParticipantModerator(newParticipant);
  161. if (state.everyoneIsModerator && !isModerator) {
  162. state.everyoneIsModerator = false;
  163. } else if (!state.everyoneIsModerator && isModerator) {
  164. state.everyoneIsModerator = _isEveryoneModerator(state);
  165. }
  166. // haveParticipantWithScreenSharingFeature calculation:
  167. const { features = {} } = participant;
  168. // Currently we use only PARTICIPANT_UPDATED to set a feature to enabled and we never disable it.
  169. if (String(features['screen-sharing']) === 'true') {
  170. state.haveParticipantWithScreenSharingFeature = true;
  171. }
  172. }
  173. return {
  174. ...state
  175. };
  176. }
  177. case PARTICIPANT_JOINED: {
  178. const participant = _participantJoined(action);
  179. const { id, isFakeParticipant, name, pinned } = participant;
  180. const { pinnedParticipant, dominantSpeaker } = state;
  181. if (pinned) {
  182. if (pinnedParticipant) {
  183. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  184. }
  185. state.pinnedParticipant = id;
  186. }
  187. if (participant.dominantSpeaker) {
  188. if (dominantSpeaker) {
  189. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  190. }
  191. state.dominantSpeaker = id;
  192. }
  193. const isModerator = isParticipantModerator(participant);
  194. const { local, remote } = state;
  195. if (state.everyoneIsModerator && !isModerator) {
  196. state.everyoneIsModerator = false;
  197. } else if (!local && remote.size === 0 && isModerator) {
  198. state.everyoneIsModerator = true;
  199. }
  200. if (participant.local) {
  201. return {
  202. ...state,
  203. local: participant
  204. };
  205. }
  206. state.remote.set(id, participant);
  207. // Insert the new participant.
  208. const displayName = _getDisplayName(state, name);
  209. const sortedRemoteParticipants = Array.from(state.sortedRemoteParticipants);
  210. sortedRemoteParticipants.push([ id, displayName ]);
  211. sortedRemoteParticipants.sort((a, b) => a[1].localeCompare(b[1]));
  212. // The sort order of participants is preserved since Map remembers the original insertion order of the keys.
  213. state.sortedRemoteParticipants = new Map(sortedRemoteParticipants);
  214. if (isFakeParticipant) {
  215. state.fakeParticipants.set(id, participant);
  216. }
  217. return { ...state };
  218. }
  219. case PARTICIPANT_LEFT: {
  220. // XXX A remote participant is uniquely identified by their id in a
  221. // specific JitsiConference instance. The local participant is uniquely
  222. // identified by the very fact that there is only one local participant
  223. // (and the fact that the local participant "joins" at the beginning of
  224. // the app and "leaves" at the end of the app).
  225. const { conference, id } = action.participant;
  226. const { fakeParticipants, remote, local, dominantSpeaker, pinnedParticipant } = state;
  227. let oldParticipant = remote.get(id);
  228. if (oldParticipant && oldParticipant.conference === conference) {
  229. remote.delete(id);
  230. } else if (local?.id === id) {
  231. oldParticipant = state.local;
  232. delete state.local;
  233. } else {
  234. // no participant found
  235. return state;
  236. }
  237. state.sortedRemoteParticipants.delete(id);
  238. state.raisedHandsQueue = state.raisedHandsQueue.filter(pid => pid.id !== id);
  239. if (!state.everyoneIsModerator && !isParticipantModerator(oldParticipant)) {
  240. state.everyoneIsModerator = _isEveryoneModerator(state);
  241. }
  242. const { features = {} } = oldParticipant || {};
  243. if (state.haveParticipantWithScreenSharingFeature && String(features['screen-sharing']) === 'true') {
  244. const { features: localFeatures = {} } = state.local || {};
  245. if (String(localFeatures['screen-sharing']) !== 'true') {
  246. state.haveParticipantWithScreenSharingFeature = false;
  247. // eslint-disable-next-line no-unused-vars
  248. for (const [ key, participant ] of state.remote) {
  249. const { features: f = {} } = participant;
  250. if (String(f['screen-sharing']) === 'true') {
  251. state.haveParticipantWithScreenSharingFeature = true;
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. if (dominantSpeaker === id) {
  258. state.dominantSpeaker = undefined;
  259. }
  260. // Remove the participant from the list of speakers.
  261. state.speakersList.has(id) && state.speakersList.delete(id);
  262. if (pinnedParticipant === id) {
  263. state.pinnedParticipant = undefined;
  264. }
  265. if (fakeParticipants.has(id)) {
  266. fakeParticipants.delete(id);
  267. }
  268. return { ...state };
  269. }
  270. case RAISE_HAND_UPDATED: {
  271. return {
  272. ...state,
  273. raisedHandsQueue: action.queue
  274. };
  275. }
  276. case SCREEN_SHARE_REMOTE_PARTICIPANTS_UPDATED: {
  277. const { participantIds } = action;
  278. const sortedSharesList = [];
  279. for (const participant of participantIds) {
  280. const remoteParticipant = state.remote.get(participant);
  281. if (remoteParticipant) {
  282. const displayName
  283. = _getDisplayName(state, remoteParticipant.name);
  284. sortedSharesList.push([ participant, displayName ]);
  285. }
  286. }
  287. // Keep the remote screen share list sorted alphabetically.
  288. sortedSharesList.length && sortedSharesList.sort((a, b) => a[1].localeCompare(b[1]));
  289. state.sortedRemoteScreenshares = new Map(sortedSharesList);
  290. return { ...state };
  291. }
  292. }
  293. return state;
  294. });
  295. /**
  296. * Returns the participant's display name, default string if display name is not set on the participant.
  297. *
  298. * @param {Object} state - The local participant redux state.
  299. * @param {string} name - The display name of the participant.
  300. * @returns {string}
  301. */
  302. function _getDisplayName(state: Object, name: string): string {
  303. const config = state['features/base/config'];
  304. return name ?? (config?.defaultRemoteDisplayName || 'Fellow Jitster');
  305. }
  306. /**
  307. * Loops trough the participants in the state in order to check if all participants are moderators.
  308. *
  309. * @param {Object} state - The local participant redux state.
  310. * @returns {boolean}
  311. */
  312. function _isEveryoneModerator(state) {
  313. if (isParticipantModerator(state.local)) {
  314. // eslint-disable-next-line no-unused-vars
  315. for (const [ k, p ] of state.remote) {
  316. if (!isParticipantModerator(p)) {
  317. return false;
  318. }
  319. }
  320. return true;
  321. }
  322. return false;
  323. }
  324. /**
  325. * Reducer function for a single participant.
  326. *
  327. * @param {Participant|undefined} state - Participant to be modified.
  328. * @param {Object} action - Action object.
  329. * @param {string} action.type - Type of action.
  330. * @param {Participant} action.participant - Information about participant to be
  331. * added/modified.
  332. * @param {JitsiConference} action.conference - Conference instance.
  333. * @private
  334. * @returns {Participant}
  335. */
  336. function _participant(state: Object = {}, action) {
  337. switch (action.type) {
  338. case SET_LOADABLE_AVATAR_URL:
  339. case PARTICIPANT_UPDATED: {
  340. const { participant } = action; // eslint-disable-line no-shadow
  341. const newState = { ...state };
  342. for (const key in participant) {
  343. if (participant.hasOwnProperty(key)
  344. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  345. === -1) {
  346. newState[key] = participant[key];
  347. }
  348. }
  349. return newState;
  350. }
  351. }
  352. return state;
  353. }
  354. /**
  355. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  356. * feature base/participants.
  357. *
  358. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  359. * to reduce.
  360. * @private
  361. * @returns {Object} The new participant derived from the payload of the
  362. * specified {@code action} to be added into the redux state of the feature
  363. * base/participants after the reduction of the specified
  364. * {@code action}.
  365. */
  366. function _participantJoined({ participant }) {
  367. const {
  368. avatarURL,
  369. botType,
  370. connectionStatus,
  371. dominantSpeaker,
  372. email,
  373. isFakeParticipant,
  374. isReplacing,
  375. isJigasi,
  376. loadableAvatarUrl,
  377. local,
  378. name,
  379. pinned,
  380. presence,
  381. role
  382. } = participant;
  383. let { conference, id } = participant;
  384. if (local) {
  385. // conference
  386. //
  387. // XXX The local participant is not identified in association with a
  388. // JitsiConference because it is identified by the very fact that it is
  389. // the local participant.
  390. conference = undefined;
  391. // id
  392. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  393. }
  394. return {
  395. avatarURL,
  396. botType,
  397. conference,
  398. connectionStatus,
  399. dominantSpeaker: dominantSpeaker || false,
  400. email,
  401. id,
  402. isFakeParticipant,
  403. isReplacing,
  404. isJigasi,
  405. loadableAvatarUrl,
  406. local: local || false,
  407. name,
  408. pinned: pinned || false,
  409. presence,
  410. role: role || PARTICIPANT_ROLE.NONE
  411. };
  412. }
  413. /**
  414. * Updates a specific property for a participant.
  415. *
  416. * @param {State} state - The redux state.
  417. * @param {string} id - The ID of the participant.
  418. * @param {string} property - The property to update.
  419. * @param {*} value - The new value.
  420. * @returns {boolean} - True if a participant was updated and false otherwise.
  421. */
  422. function _updateParticipantProperty(state, id, property, value) {
  423. const { remote, local } = state;
  424. if (remote.has(id)) {
  425. remote.set(id, set(remote.get(id), property, value));
  426. return true;
  427. } else if (local?.id === id || local?.id === 'local') {
  428. // The local participant's ID can chance from something to "local" when
  429. // not in a conference.
  430. state.local = set(local, property, value);
  431. return true;
  432. }
  433. return false;
  434. }