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 15KB

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