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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // @flow
  2. import { ReducerRegistry, set } from '../redux';
  3. import {
  4. DOMINANT_SPEAKER_CHANGED,
  5. PARTICIPANT_ID_CHANGED,
  6. PARTICIPANT_JOINED,
  7. PARTICIPANT_LEFT,
  8. PARTICIPANT_UPDATED,
  9. PIN_PARTICIPANT,
  10. SET_LOADABLE_AVATAR_URL
  11. } from './actionTypes';
  12. import { LOCAL_PARTICIPANT_DEFAULT_ID, PARTICIPANT_ROLE } from './constants';
  13. import { isParticipantModerator } from './functions';
  14. declare var interfaceConfig: Object;
  15. /**
  16. * Participant object.
  17. * @typedef {Object} Participant
  18. * @property {string} id - Participant ID.
  19. * @property {string} name - Participant name.
  20. * @property {string} avatar - Path to participant avatar if any.
  21. * @property {string} role - Participant role.
  22. * @property {boolean} local - If true, participant is local.
  23. * @property {boolean} pinned - If true, participant is currently a
  24. * "PINNED_ENDPOINT".
  25. * @property {boolean} dominantSpeaker - If this participant is the dominant
  26. * speaker in the (associated) conference, {@code true}; otherwise,
  27. * {@code false}.
  28. * @property {string} email - Participant email.
  29. */
  30. /**
  31. * The participant properties which cannot be updated through
  32. * {@link PARTICIPANT_UPDATED}. They either identify the participant or can only
  33. * be modified through property-dedicated actions.
  34. *
  35. * @type {string[]}
  36. */
  37. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE = [
  38. // The following properties identify the participant:
  39. 'conference',
  40. 'id',
  41. 'local',
  42. // The following properties can only be modified through property-dedicated
  43. // actions:
  44. 'dominantSpeaker',
  45. 'pinned'
  46. ];
  47. const DEFAULT_STATE = {
  48. dominantSpeaker: undefined,
  49. everyoneIsModerator: false,
  50. fakeParticipants: new Map(),
  51. haveParticipantWithScreenSharingFeature: false,
  52. local: undefined,
  53. pinnedParticipant: undefined,
  54. remote: new Map(),
  55. sortedRemoteParticipants: new Map(),
  56. speakersList: new Map()
  57. };
  58. /**
  59. * Listen for actions which add, remove, or update the set of participants in
  60. * the conference.
  61. *
  62. * @param {Participant[]} state - List of participants to be modified.
  63. * @param {Object} action - Action object.
  64. * @param {string} action.type - Type of action.
  65. * @param {Participant} action.participant - Information about participant to be
  66. * added/removed/modified.
  67. * @returns {Participant[]}
  68. */
  69. ReducerRegistry.register('features/base/participants', (state = DEFAULT_STATE, action) => {
  70. switch (action.type) {
  71. case PARTICIPANT_ID_CHANGED: {
  72. const { local } = state;
  73. if (local) {
  74. state.local = {
  75. ...local,
  76. id: action.newValue
  77. };
  78. return {
  79. ...state
  80. };
  81. }
  82. return state;
  83. }
  84. case DOMINANT_SPEAKER_CHANGED: {
  85. const { participant } = action;
  86. const { id, previousSpeakers = [] } = participant;
  87. const { dominantSpeaker, local, speakersList } = state;
  88. const newSpeakers = [ id, ...previousSpeakers ];
  89. const sortedSpeakersList = Array.from(speakersList);
  90. // Update the speakers list.
  91. for (const speaker of newSpeakers) {
  92. if (!state.speakersList.has(speaker) && speaker !== local?.id) {
  93. const remoteParticipant = state.remote.get(speaker);
  94. remoteParticipant && sortedSpeakersList.push([ speaker, _getDisplayName(remoteParticipant.name) ]);
  95. }
  96. }
  97. // Also check if any of the existing speakers have been kicked off the list.
  98. for (const existingSpeaker of sortedSpeakersList.keys()) {
  99. if (!newSpeakers.find(s => s === existingSpeaker)) {
  100. sortedSpeakersList.filter(sortedSpeaker => sortedSpeaker[0] !== existingSpeaker);
  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(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. if (!state.everyoneIsModerator && !isParticipantModerator(oldParticipant)) {
  236. state.everyoneIsModerator = _isEveryoneModerator(state);
  237. }
  238. const { features = {} } = oldParticipant || {};
  239. if (state.haveParticipantWithScreenSharingFeature && String(features['screen-sharing']) === 'true') {
  240. const { features: localFeatures = {} } = state.local || {};
  241. if (String(localFeatures['screen-sharing']) !== 'true') {
  242. state.haveParticipantWithScreenSharingFeature = false;
  243. // eslint-disable-next-line no-unused-vars
  244. for (const [ key, participant ] of state.remote) {
  245. const { features: f = {} } = participant;
  246. if (String(f['screen-sharing']) === 'true') {
  247. state.haveParticipantWithScreenSharingFeature = true;
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. if (dominantSpeaker === id) {
  254. state.dominantSpeaker = undefined;
  255. }
  256. // Remove the participant from the list of speakers.
  257. state.speakersList.has(id) && state.speakersList.delete(id);
  258. if (pinnedParticipant === id) {
  259. state.pinnedParticipant = undefined;
  260. }
  261. if (fakeParticipants.has(id)) {
  262. fakeParticipants.delete(id);
  263. }
  264. return { ...state };
  265. }
  266. }
  267. return state;
  268. });
  269. /**
  270. * Returns the participant's display name, default string if display name is not set on the participant.
  271. *
  272. * @param {string} name - The display name of the participant.
  273. * @returns {string}
  274. */
  275. function _getDisplayName(name) {
  276. return name
  277. ?? (typeof interfaceConfig === 'object' ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME : 'Fellow Jitser');
  278. }
  279. /**
  280. * Loops trough the participants in the state in order to check if all participants are moderators.
  281. *
  282. * @param {Object} state - The local participant redux state.
  283. * @returns {boolean}
  284. */
  285. function _isEveryoneModerator(state) {
  286. if (isParticipantModerator(state.local)) {
  287. // eslint-disable-next-line no-unused-vars
  288. for (const [ k, p ] of state.remote) {
  289. if (!isParticipantModerator(p)) {
  290. return false;
  291. }
  292. }
  293. return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * Reducer function for a single participant.
  299. *
  300. * @param {Participant|undefined} state - Participant to be modified.
  301. * @param {Object} action - Action object.
  302. * @param {string} action.type - Type of action.
  303. * @param {Participant} action.participant - Information about participant to be
  304. * added/modified.
  305. * @param {JitsiConference} action.conference - Conference instance.
  306. * @private
  307. * @returns {Participant}
  308. */
  309. function _participant(state: Object = {}, action) {
  310. switch (action.type) {
  311. case SET_LOADABLE_AVATAR_URL:
  312. case PARTICIPANT_UPDATED: {
  313. const { participant } = action; // eslint-disable-line no-shadow
  314. const newState = { ...state };
  315. for (const key in participant) {
  316. if (participant.hasOwnProperty(key)
  317. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  318. === -1) {
  319. newState[key] = participant[key];
  320. }
  321. }
  322. return newState;
  323. }
  324. }
  325. return state;
  326. }
  327. /**
  328. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  329. * feature base/participants.
  330. *
  331. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  332. * to reduce.
  333. * @private
  334. * @returns {Object} The new participant derived from the payload of the
  335. * specified {@code action} to be added into the redux state of the feature
  336. * base/participants after the reduction of the specified
  337. * {@code action}.
  338. */
  339. function _participantJoined({ participant }) {
  340. const {
  341. avatarURL,
  342. botType,
  343. connectionStatus,
  344. dominantSpeaker,
  345. email,
  346. isFakeParticipant,
  347. isReplacing,
  348. isJigasi,
  349. loadableAvatarUrl,
  350. local,
  351. name,
  352. pinned,
  353. presence,
  354. role
  355. } = participant;
  356. let { conference, id } = participant;
  357. if (local) {
  358. // conference
  359. //
  360. // XXX The local participant is not identified in association with a
  361. // JitsiConference because it is identified by the very fact that it is
  362. // the local participant.
  363. conference = undefined;
  364. // id
  365. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  366. }
  367. return {
  368. avatarURL,
  369. botType,
  370. conference,
  371. connectionStatus,
  372. dominantSpeaker: dominantSpeaker || false,
  373. email,
  374. id,
  375. isFakeParticipant,
  376. isReplacing,
  377. isJigasi,
  378. loadableAvatarUrl,
  379. local: local || false,
  380. name,
  381. pinned: pinned || false,
  382. presence,
  383. role: role || PARTICIPANT_ROLE.NONE
  384. };
  385. }
  386. /**
  387. * Updates a specific property for a participant.
  388. *
  389. * @param {State} state - The redux state.
  390. * @param {string} id - The ID of the participant.
  391. * @param {string} property - The property to update.
  392. * @param {*} value - The new value.
  393. * @returns {boolean} - True if a participant was updated and false otherwise.
  394. */
  395. function _updateParticipantProperty(state, id, property, value) {
  396. const { remote, local } = state;
  397. if (remote.has(id)) {
  398. remote.set(id, set(remote.get(id), property, value));
  399. return true;
  400. } else if (local?.id === id) {
  401. state.local = set(local, property, value);
  402. return true;
  403. }
  404. return false;
  405. }