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

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