Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

reducer.ts 18KB

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