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

reducer.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. sortedRemoteFakeScreenShareParticipants: 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.sortedRemoteFakeScreenShareParticipants.has(id)) {
  185. state.sortedRemoteFakeScreenShareParticipants.delete(id);
  186. const sortedRemoteFakeScreenShareParticipants = [ ...state.sortedRemoteFakeScreenShareParticipants ];
  187. sortedRemoteFakeScreenShareParticipants.push([ id, name ]);
  188. sortedRemoteFakeScreenShareParticipants.sort((a, b) => a[1].localeCompare(b[1]));
  189. state.sortedRemoteFakeScreenShareParticipants = new Map(sortedRemoteFakeScreenShareParticipants);
  190. }
  191. return { ...state };
  192. }
  193. case PARTICIPANT_JOINED: {
  194. const participant = _participantJoined(action);
  195. const { id, isFakeParticipant, isFakeScreenShareParticipant, isLocalScreenShare, name, pinned } = participant;
  196. const { pinnedParticipant, dominantSpeaker } = state;
  197. if (pinned) {
  198. if (pinnedParticipant) {
  199. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  200. }
  201. state.pinnedParticipant = id;
  202. }
  203. if (participant.dominantSpeaker) {
  204. if (dominantSpeaker) {
  205. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  206. }
  207. state.dominantSpeaker = id;
  208. }
  209. const isModerator = isParticipantModerator(participant);
  210. const { local, remote } = state;
  211. if (state.everyoneIsModerator && !isModerator) {
  212. state.everyoneIsModerator = false;
  213. } else if (!local && remote.size === 0 && isModerator) {
  214. state.everyoneIsModerator = true;
  215. }
  216. if (participant.local) {
  217. return {
  218. ...state,
  219. local: participant
  220. };
  221. }
  222. if (isLocalScreenShare) {
  223. return {
  224. ...state,
  225. localScreenShare: participant
  226. };
  227. }
  228. state.remote.set(id, participant);
  229. // Insert the new participant.
  230. const displayName = _getDisplayName(state, name);
  231. const sortedRemoteParticipants = Array.from(state.sortedRemoteParticipants);
  232. sortedRemoteParticipants.push([ id, displayName ]);
  233. sortedRemoteParticipants.sort((a, b) => a[1].localeCompare(b[1]));
  234. // The sort order of participants is preserved since Map remembers the original insertion order of the keys.
  235. state.sortedRemoteParticipants = new Map(sortedRemoteParticipants);
  236. if (isFakeScreenShareParticipant) {
  237. const sortedRemoteFakeScreenShareParticipants = [ ...state.sortedRemoteFakeScreenShareParticipants ];
  238. sortedRemoteFakeScreenShareParticipants.push([ id, name ]);
  239. sortedRemoteFakeScreenShareParticipants.sort((a, b) => a[1].localeCompare(b[1]));
  240. state.sortedRemoteFakeScreenShareParticipants = new Map(sortedRemoteFakeScreenShareParticipants);
  241. }
  242. if (isFakeParticipant) {
  243. state.fakeParticipants.set(id, participant);
  244. }
  245. return { ...state };
  246. }
  247. case PARTICIPANT_LEFT: {
  248. // XXX A remote participant is uniquely identified by their id in a
  249. // specific JitsiConference instance. The local participant is uniquely
  250. // identified by the very fact that there is only one local participant
  251. // (and the fact that the local participant "joins" at the beginning of
  252. // the app and "leaves" at the end of the app).
  253. const { conference, id } = action.participant;
  254. const {
  255. fakeParticipants,
  256. sortedRemoteFakeScreenShareParticipants,
  257. remote,
  258. local,
  259. localScreenShare,
  260. dominantSpeaker,
  261. pinnedParticipant
  262. } = state;
  263. let oldParticipant = remote.get(id);
  264. if (oldParticipant && oldParticipant.conference === conference) {
  265. remote.delete(id);
  266. } else if (local?.id === id) {
  267. oldParticipant = state.local;
  268. delete state.local;
  269. } else if (localScreenShare?.id === id) {
  270. oldParticipant = state.local;
  271. delete state.localScreenShare;
  272. } else {
  273. // no participant found
  274. return state;
  275. }
  276. state.sortedRemoteParticipants.delete(id);
  277. state.raisedHandsQueue = state.raisedHandsQueue.filter(pid => pid.id !== id);
  278. if (!state.everyoneIsModerator && !isParticipantModerator(oldParticipant)) {
  279. state.everyoneIsModerator = _isEveryoneModerator(state);
  280. }
  281. const { features = {} } = oldParticipant || {};
  282. if (state.haveParticipantWithScreenSharingFeature && String(features['screen-sharing']) === 'true') {
  283. const { features: localFeatures = {} } = state.local || {};
  284. if (String(localFeatures['screen-sharing']) !== 'true') {
  285. state.haveParticipantWithScreenSharingFeature = false;
  286. // eslint-disable-next-line no-unused-vars
  287. for (const [ key, participant ] of state.remote) {
  288. const { features: f = {} } = participant;
  289. if (String(f['screen-sharing']) === 'true') {
  290. state.haveParticipantWithScreenSharingFeature = true;
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. if (dominantSpeaker === id) {
  297. state.dominantSpeaker = undefined;
  298. }
  299. // Remove the participant from the list of speakers.
  300. state.speakersList.has(id) && state.speakersList.delete(id);
  301. if (pinnedParticipant === id) {
  302. state.pinnedParticipant = undefined;
  303. }
  304. if (fakeParticipants.has(id)) {
  305. fakeParticipants.delete(id);
  306. }
  307. if (sortedRemoteFakeScreenShareParticipants.has(id)) {
  308. sortedRemoteFakeScreenShareParticipants.delete(id);
  309. state.sortedRemoteFakeScreenShareParticipants = new Map(sortedRemoteFakeScreenShareParticipants);
  310. }
  311. return { ...state };
  312. }
  313. case RAISE_HAND_UPDATED: {
  314. return {
  315. ...state,
  316. raisedHandsQueue: action.queue
  317. };
  318. }
  319. case SCREEN_SHARE_REMOTE_PARTICIPANTS_UPDATED: {
  320. const { participantIds } = action;
  321. const sortedSharesList = [];
  322. for (const participant of participantIds) {
  323. const remoteParticipant = state.remote.get(participant);
  324. if (remoteParticipant) {
  325. const displayName
  326. = _getDisplayName(state, remoteParticipant.name);
  327. sortedSharesList.push([ participant, displayName ]);
  328. }
  329. }
  330. // Keep the remote screen share list sorted alphabetically.
  331. sortedSharesList.length && sortedSharesList.sort((a, b) => a[1].localeCompare(b[1]));
  332. state.sortedRemoteScreenshares = new Map(sortedSharesList);
  333. return { ...state };
  334. }
  335. }
  336. return state;
  337. });
  338. /**
  339. * Returns the participant's display name, default string if display name is not set on the participant.
  340. *
  341. * @param {Object} state - The local participant redux state.
  342. * @param {string} name - The display name of the participant.
  343. * @returns {string}
  344. */
  345. function _getDisplayName(state: Object, name: string): string {
  346. const config = state['features/base/config'];
  347. return name ?? (config?.defaultRemoteDisplayName || 'Fellow Jitster');
  348. }
  349. /**
  350. * Loops trough the participants in the state in order to check if all participants are moderators.
  351. *
  352. * @param {Object} state - The local participant redux state.
  353. * @returns {boolean}
  354. */
  355. function _isEveryoneModerator(state) {
  356. if (isParticipantModerator(state.local)) {
  357. // eslint-disable-next-line no-unused-vars
  358. for (const [ k, p ] of state.remote) {
  359. if (!isParticipantModerator(p)) {
  360. return false;
  361. }
  362. }
  363. return true;
  364. }
  365. return false;
  366. }
  367. /**
  368. * Reducer function for a single participant.
  369. *
  370. * @param {Participant|undefined} state - Participant to be modified.
  371. * @param {Object} action - Action object.
  372. * @param {string} action.type - Type of action.
  373. * @param {Participant} action.participant - Information about participant to be
  374. * added/modified.
  375. * @param {JitsiConference} action.conference - Conference instance.
  376. * @private
  377. * @returns {Participant}
  378. */
  379. function _participant(state: Object = {}, action) {
  380. switch (action.type) {
  381. case SET_LOADABLE_AVATAR_URL:
  382. case PARTICIPANT_UPDATED: {
  383. const { participant } = action; // eslint-disable-line no-shadow
  384. const newState = { ...state };
  385. for (const key in participant) {
  386. if (participant.hasOwnProperty(key)
  387. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  388. === -1) {
  389. newState[key] = participant[key];
  390. }
  391. }
  392. return newState;
  393. }
  394. }
  395. return state;
  396. }
  397. /**
  398. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  399. * feature base/participants.
  400. *
  401. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  402. * to reduce.
  403. * @private
  404. * @returns {Object} The new participant derived from the payload of the
  405. * specified {@code action} to be added into the redux state of the feature
  406. * base/participants after the reduction of the specified
  407. * {@code action}.
  408. */
  409. function _participantJoined({ participant }) {
  410. const {
  411. avatarURL,
  412. botType,
  413. connectionStatus,
  414. dominantSpeaker,
  415. email,
  416. isFakeParticipant,
  417. isFakeScreenShareParticipant,
  418. isLocalScreenShare,
  419. isReplacing,
  420. isJigasi,
  421. loadableAvatarUrl,
  422. local,
  423. name,
  424. pinned,
  425. presence,
  426. role
  427. } = participant;
  428. let { conference, id } = participant;
  429. if (local) {
  430. // conference
  431. //
  432. // XXX The local participant is not identified in association with a
  433. // JitsiConference because it is identified by the very fact that it is
  434. // the local participant.
  435. conference = undefined;
  436. // id
  437. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  438. }
  439. return {
  440. avatarURL,
  441. botType,
  442. conference,
  443. connectionStatus,
  444. dominantSpeaker: dominantSpeaker || false,
  445. email,
  446. id,
  447. isFakeParticipant,
  448. isFakeScreenShareParticipant,
  449. isLocalScreenShare,
  450. isReplacing,
  451. isJigasi,
  452. loadableAvatarUrl,
  453. local: local || false,
  454. name,
  455. pinned: pinned || false,
  456. presence,
  457. role: role || PARTICIPANT_ROLE.NONE
  458. };
  459. }
  460. /**
  461. * Updates a specific property for a participant.
  462. *
  463. * @param {State} state - The redux state.
  464. * @param {string} id - The ID of the participant.
  465. * @param {string} property - The property to update.
  466. * @param {*} value - The new value.
  467. * @returns {boolean} - True if a participant was updated and false otherwise.
  468. */
  469. function _updateParticipantProperty(state, id, property, value) {
  470. const { remote, local, localScreenShare } = state;
  471. if (remote.has(id)) {
  472. remote.set(id, set(remote.get(id), property, value));
  473. return true;
  474. } else if (local?.id === id || local?.id === 'local') {
  475. // The local participant's ID can chance from something to "local" when
  476. // not in a conference.
  477. state.local = set(local, property, value);
  478. return true;
  479. } else if (localScreenShare?.id === id) {
  480. state.localScreenShare = set(localScreenShare, property, value);
  481. return true;
  482. }
  483. return false;
  484. }