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

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