Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

reducer.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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: []
  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 } = state;
  88. const speakersList = [];
  89. // Update the speakers list.
  90. id !== local?.id && speakersList.push(id);
  91. speakersList.push(...previousSpeakers.filter(p => p !== local?.id));
  92. // Only one dominant speaker is allowed.
  93. if (dominantSpeaker) {
  94. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  95. }
  96. if (_updateParticipantProperty(state, id, 'dominantSpeaker', true)) {
  97. return {
  98. ...state,
  99. dominantSpeaker: id,
  100. speakersList
  101. };
  102. }
  103. delete state.dominantSpeaker;
  104. return {
  105. ...state
  106. };
  107. }
  108. case PIN_PARTICIPANT: {
  109. const { participant } = action;
  110. const { id } = participant;
  111. const { pinnedParticipant } = state;
  112. // Only one pinned participant is allowed.
  113. if (pinnedParticipant) {
  114. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  115. }
  116. if (_updateParticipantProperty(state, id, 'pinned', true)) {
  117. return {
  118. ...state,
  119. pinnedParticipant: id
  120. };
  121. }
  122. delete state.pinnedParticipant;
  123. return {
  124. ...state
  125. };
  126. }
  127. case SET_LOADABLE_AVATAR_URL:
  128. case PARTICIPANT_UPDATED: {
  129. const { participant } = action;
  130. let { id } = participant;
  131. const { local } = participant;
  132. if (!id && local) {
  133. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  134. }
  135. let newParticipant;
  136. if (state.remote.has(id)) {
  137. newParticipant = _participant(state.remote.get(id), action);
  138. state.remote.set(id, newParticipant);
  139. } else if (id === state.local?.id) {
  140. newParticipant = state.local = _participant(state.local, action);
  141. }
  142. if (newParticipant) {
  143. // everyoneIsModerator calculation:
  144. const isModerator = isParticipantModerator(newParticipant);
  145. if (state.everyoneIsModerator && !isModerator) {
  146. state.everyoneIsModerator = false;
  147. } else if (!state.everyoneIsModerator && isModerator) {
  148. state.everyoneIsModerator = _isEveryoneModerator(state);
  149. }
  150. // haveParticipantWithScreenSharingFeature calculation:
  151. const { features = {} } = participant;
  152. // Currently we use only PARTICIPANT_UPDATED to set a feature to enabled and we never disable it.
  153. if (String(features['screen-sharing']) === 'true') {
  154. state.haveParticipantWithScreenSharingFeature = true;
  155. }
  156. }
  157. return {
  158. ...state
  159. };
  160. }
  161. case PARTICIPANT_JOINED: {
  162. const participant = _participantJoined(action);
  163. const { id, isFakeParticipant, name, pinned } = participant;
  164. const { pinnedParticipant, dominantSpeaker } = state;
  165. if (pinned) {
  166. if (pinnedParticipant) {
  167. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  168. }
  169. state.pinnedParticipant = id;
  170. }
  171. if (participant.dominantSpeaker) {
  172. if (dominantSpeaker) {
  173. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  174. }
  175. state.dominantSpeaker = id;
  176. }
  177. const isModerator = isParticipantModerator(participant);
  178. const { local, remote } = state;
  179. if (state.everyoneIsModerator && !isModerator) {
  180. state.everyoneIsModerator = false;
  181. } else if (!local && remote.size === 0 && isModerator) {
  182. state.everyoneIsModerator = true;
  183. }
  184. if (participant.local) {
  185. return {
  186. ...state,
  187. local: participant
  188. };
  189. }
  190. state.remote.set(id, participant);
  191. // Insert the new participant.
  192. const displayName = name
  193. ?? (typeof interfaceConfig === 'object' ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME : 'Fellow Jitser');
  194. const sortedRemoteParticipants = Array.from(state.sortedRemoteParticipants);
  195. sortedRemoteParticipants.push([ id, displayName ]);
  196. sortedRemoteParticipants.sort((a, b) => a[1].localeCompare(b[1]));
  197. // The sort order of participants is preserved since Map remembers the original insertion order of the keys.
  198. state.sortedRemoteParticipants = new Map(sortedRemoteParticipants);
  199. if (isFakeParticipant) {
  200. state.fakeParticipants.set(id, participant);
  201. }
  202. return { ...state };
  203. }
  204. case PARTICIPANT_LEFT: {
  205. // XXX A remote participant is uniquely identified by their id in a
  206. // specific JitsiConference instance. The local participant is uniquely
  207. // identified by the very fact that there is only one local participant
  208. // (and the fact that the local participant "joins" at the beginning of
  209. // the app and "leaves" at the end of the app).
  210. const { conference, id } = action.participant;
  211. const { fakeParticipants, remote, local, dominantSpeaker, pinnedParticipant } = state;
  212. let oldParticipant = remote.get(id);
  213. if (oldParticipant && oldParticipant.conference === conference) {
  214. remote.delete(id);
  215. } else if (local?.id === id) {
  216. oldParticipant = state.local;
  217. delete state.local;
  218. } else {
  219. // no participant found
  220. return state;
  221. }
  222. state.sortedRemoteParticipants.delete(id);
  223. if (!state.everyoneIsModerator && !isParticipantModerator(oldParticipant)) {
  224. state.everyoneIsModerator = _isEveryoneModerator(state);
  225. }
  226. const { features = {} } = oldParticipant || {};
  227. if (state.haveParticipantWithScreenSharingFeature && String(features['screen-sharing']) === 'true') {
  228. const { features: localFeatures = {} } = state.local || {};
  229. if (String(localFeatures['screen-sharing']) !== 'true') {
  230. state.haveParticipantWithScreenSharingFeature = false;
  231. // eslint-disable-next-line no-unused-vars
  232. for (const [ key, participant ] of state.remote) {
  233. const { features: f = {} } = participant;
  234. if (String(f['screen-sharing']) === 'true') {
  235. state.haveParticipantWithScreenSharingFeature = true;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. if (dominantSpeaker === id) {
  242. state.dominantSpeaker = undefined;
  243. }
  244. // Remove the participant from the list of speakers.
  245. state.speakersList = state.speakersList.filter(speaker => speaker !== id);
  246. if (pinnedParticipant === id) {
  247. state.pinnedParticipant = undefined;
  248. }
  249. if (fakeParticipants.has(id)) {
  250. fakeParticipants.delete(id);
  251. }
  252. return { ...state };
  253. }
  254. }
  255. return state;
  256. });
  257. /**
  258. * Loops trough the participants in the state in order to check if all participants are moderators.
  259. *
  260. * @param {Object} state - The local participant redux state.
  261. * @returns {boolean}
  262. */
  263. function _isEveryoneModerator(state) {
  264. if (isParticipantModerator(state.local)) {
  265. // eslint-disable-next-line no-unused-vars
  266. for (const [ k, p ] of state.remote) {
  267. if (!isParticipantModerator(p)) {
  268. return false;
  269. }
  270. }
  271. return true;
  272. }
  273. return false;
  274. }
  275. /**
  276. * Updates a specific property for a participant.
  277. *
  278. * @param {State} state - The redux state.
  279. * @param {string} id - The ID of the participant.
  280. * @param {string} property - The property to update.
  281. * @param {*} value - The new value.
  282. * @returns {boolean} - True if a participant was updated and false otherwise.
  283. */
  284. function _updateParticipantProperty(state, id, property, value) {
  285. const { remote, local } = state;
  286. if (remote.has(id)) {
  287. remote.set(id, set(remote.get(id), property, value));
  288. return true;
  289. } else if (local?.id === id) {
  290. state.local = set(local, property, value);
  291. return true;
  292. }
  293. return false;
  294. }
  295. /**
  296. * Reducer function for a single participant.
  297. *
  298. * @param {Participant|undefined} state - Participant to be modified.
  299. * @param {Object} action - Action object.
  300. * @param {string} action.type - Type of action.
  301. * @param {Participant} action.participant - Information about participant to be
  302. * added/modified.
  303. * @param {JitsiConference} action.conference - Conference instance.
  304. * @private
  305. * @returns {Participant}
  306. */
  307. function _participant(state: Object = {}, action) {
  308. switch (action.type) {
  309. case SET_LOADABLE_AVATAR_URL:
  310. case PARTICIPANT_UPDATED: {
  311. const { participant } = action; // eslint-disable-line no-shadow
  312. const newState = { ...state };
  313. for (const key in participant) {
  314. if (participant.hasOwnProperty(key)
  315. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  316. === -1) {
  317. newState[key] = participant[key];
  318. }
  319. }
  320. return newState;
  321. }
  322. }
  323. return state;
  324. }
  325. /**
  326. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  327. * feature base/participants.
  328. *
  329. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  330. * to reduce.
  331. * @private
  332. * @returns {Object} The new participant derived from the payload of the
  333. * specified {@code action} to be added into the redux state of the feature
  334. * base/participants after the reduction of the specified
  335. * {@code action}.
  336. */
  337. function _participantJoined({ participant }) {
  338. const {
  339. avatarURL,
  340. botType,
  341. connectionStatus,
  342. dominantSpeaker,
  343. email,
  344. isFakeParticipant,
  345. isReplacing,
  346. isJigasi,
  347. loadableAvatarUrl,
  348. local,
  349. name,
  350. pinned,
  351. presence,
  352. role
  353. } = participant;
  354. let { conference, id } = participant;
  355. if (local) {
  356. // conference
  357. //
  358. // XXX The local participant is not identified in association with a
  359. // JitsiConference because it is identified by the very fact that it is
  360. // the local participant.
  361. conference = undefined;
  362. // id
  363. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  364. }
  365. return {
  366. avatarURL,
  367. botType,
  368. conference,
  369. connectionStatus,
  370. dominantSpeaker: dominantSpeaker || false,
  371. email,
  372. id,
  373. isFakeParticipant,
  374. isReplacing,
  375. isJigasi,
  376. loadableAvatarUrl,
  377. local: local || false,
  378. name,
  379. pinned: pinned || false,
  380. presence,
  381. role: role || PARTICIPANT_ROLE.NONE
  382. };
  383. }