您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 18KB

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