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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /**
  15. * Participant object.
  16. * @typedef {Object} Participant
  17. * @property {string} id - Participant ID.
  18. * @property {string} name - Participant name.
  19. * @property {string} avatar - Path to participant avatar if any.
  20. * @property {string} role - Participant role.
  21. * @property {boolean} local - If true, participant is local.
  22. * @property {boolean} pinned - If true, participant is currently a
  23. * "PINNED_ENDPOINT".
  24. * @property {boolean} dominantSpeaker - If this participant is the dominant
  25. * speaker in the (associated) conference, {@code true}; otherwise,
  26. * {@code false}.
  27. * @property {string} email - Participant email.
  28. */
  29. declare var APP: Object;
  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. haveParticipantWithScreenSharingFeature: false,
  49. dominantSpeaker: undefined,
  50. everyoneIsModerator: false,
  51. pinnedParticipant: undefined,
  52. local: undefined,
  53. remote: new Map(),
  54. fakeParticipants: new Map()
  55. };
  56. /**
  57. * Listen for actions which add, remove, or update the set of participants in
  58. * the conference.
  59. *
  60. * @param {Participant[]} state - List of participants to be modified.
  61. * @param {Object} action - Action object.
  62. * @param {string} action.type - Type of action.
  63. * @param {Participant} action.participant - Information about participant to be
  64. * added/removed/modified.
  65. * @returns {Participant[]}
  66. */
  67. ReducerRegistry.register('features/base/participants', (state = DEFAULT_STATE, action) => {
  68. switch (action.type) {
  69. case PARTICIPANT_ID_CHANGED: {
  70. const { local } = state;
  71. if (local) {
  72. state.local = {
  73. ...local,
  74. id: action.newValue
  75. };
  76. return {
  77. ...state
  78. };
  79. }
  80. return state;
  81. }
  82. case DOMINANT_SPEAKER_CHANGED: {
  83. const { participant } = action;
  84. const { id } = participant;
  85. const { dominantSpeaker } = state;
  86. // Only one dominant speaker is allowed.
  87. if (dominantSpeaker) {
  88. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  89. }
  90. if (_updateParticipantProperty(state, id, 'dominantSpeaker', true)) {
  91. return {
  92. ...state,
  93. dominantSpeaker: id
  94. };
  95. }
  96. delete state.dominantSpeaker;
  97. return {
  98. ...state
  99. };
  100. }
  101. case PIN_PARTICIPANT: {
  102. const { participant } = action;
  103. const { id } = participant;
  104. const { pinnedParticipant } = state;
  105. // Only one pinned participant is allowed.
  106. if (pinnedParticipant) {
  107. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  108. }
  109. if (_updateParticipantProperty(state, id, 'pinned', true)) {
  110. return {
  111. ...state,
  112. pinnedParticipant: id
  113. };
  114. }
  115. delete state.pinnedParticipant;
  116. return {
  117. ...state
  118. };
  119. }
  120. case SET_LOADABLE_AVATAR_URL:
  121. case PARTICIPANT_UPDATED: {
  122. const { participant } = action;
  123. let { id } = participant;
  124. const { local } = participant;
  125. if (!id && local) {
  126. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  127. }
  128. let newParticipant;
  129. if (state.remote.has(id)) {
  130. newParticipant = _participant(state.remote.get(id), action);
  131. state.remote.set(id, newParticipant);
  132. } else if (id === state.local?.id) {
  133. newParticipant = state.local = _participant(state.local, action);
  134. }
  135. if (newParticipant) {
  136. // everyoneIsModerator calculation:
  137. const isModerator = isParticipantModerator(newParticipant);
  138. if (state.everyoneIsModerator && !isModerator) {
  139. state.everyoneIsModerator = false;
  140. } else if (!state.everyoneIsModerator && isModerator) {
  141. state.everyoneIsModerator = _isEveryoneModerator(state);
  142. }
  143. // haveParticipantWithScreenSharingFeature calculation:
  144. const { features = {} } = participant;
  145. // Currently we use only PARTICIPANT_UPDATED to set a feature to enabled and we never disable it.
  146. if (String(features['screen-sharing']) === 'true') {
  147. state.haveParticipantWithScreenSharingFeature = true;
  148. }
  149. }
  150. return {
  151. ...state
  152. };
  153. }
  154. case PARTICIPANT_JOINED: {
  155. const participant = _participantJoined(action);
  156. const { pinnedParticipant, dominantSpeaker } = state;
  157. if (participant.pinned) {
  158. if (pinnedParticipant) {
  159. _updateParticipantProperty(state, pinnedParticipant, 'pinned', false);
  160. }
  161. state.pinnedParticipant = participant.id;
  162. }
  163. if (participant.dominantSpeaker) {
  164. if (dominantSpeaker) {
  165. _updateParticipantProperty(state, dominantSpeaker, 'dominantSpeaker', false);
  166. }
  167. state.dominantSpeaker = participant.id;
  168. }
  169. const isModerator = isParticipantModerator(participant);
  170. const { local, remote } = state;
  171. if (state.everyoneIsModerator && !isModerator) {
  172. state.everyoneIsModerator = false;
  173. } else if (!local && remote.size === 0 && isModerator) {
  174. state.everyoneIsModerator = true;
  175. }
  176. if (participant.local) {
  177. return {
  178. ...state,
  179. local: participant
  180. };
  181. }
  182. state.remote.set(participant.id, participant);
  183. if (participant.isFakeParticipant) {
  184. state.fakeParticipants.set(participant.id, participant);
  185. }
  186. return { ...state };
  187. }
  188. case PARTICIPANT_LEFT: {
  189. // XXX A remote participant is uniquely identified by their id in a
  190. // specific JitsiConference instance. The local participant is uniquely
  191. // identified by the very fact that there is only one local participant
  192. // (and the fact that the local participant "joins" at the beginning of
  193. // the app and "leaves" at the end of the app).
  194. const { conference, id } = action.participant;
  195. const { fakeParticipants, remote, local, dominantSpeaker, pinnedParticipant } = state;
  196. let oldParticipant = remote.get(id);
  197. if (oldParticipant && oldParticipant.conference === conference) {
  198. remote.delete(id);
  199. } else if (local?.id === id) {
  200. oldParticipant = state.local;
  201. delete state.local;
  202. } else {
  203. // no participant found
  204. return state;
  205. }
  206. if (!state.everyoneIsModerator && !isParticipantModerator(oldParticipant)) {
  207. state.everyoneIsModerator = _isEveryoneModerator(state);
  208. }
  209. const { features = {} } = oldParticipant || {};
  210. if (state.haveParticipantWithScreenSharingFeature && String(features['screen-sharing']) === 'true') {
  211. const { features: localFeatures = {} } = state.local || {};
  212. if (String(localFeatures['screen-sharing']) !== 'true') {
  213. state.haveParticipantWithScreenSharingFeature = false;
  214. // eslint-disable-next-line no-unused-vars
  215. for (const [ key, participant ] of state.remote) {
  216. const { features: f = {} } = participant;
  217. if (String(f['screen-sharing']) === 'true') {
  218. state.haveParticipantWithScreenSharingFeature = true;
  219. break;
  220. }
  221. }
  222. }
  223. }
  224. if (dominantSpeaker === id) {
  225. state.dominantSpeaker = undefined;
  226. }
  227. if (pinnedParticipant === id) {
  228. state.pinnedParticipant = undefined;
  229. }
  230. if (fakeParticipants.has(id)) {
  231. fakeParticipants.delete(id);
  232. }
  233. return { ...state };
  234. }
  235. }
  236. return state;
  237. });
  238. /**
  239. * Loops trough the participants in the state in order to check if all participants are moderators.
  240. *
  241. * @param {Object} state - The local participant redux state.
  242. * @returns {boolean}
  243. */
  244. function _isEveryoneModerator(state) {
  245. if (isParticipantModerator(state.local)) {
  246. // eslint-disable-next-line no-unused-vars
  247. for (const [ k, p ] of state.remote) {
  248. if (!isParticipantModerator(p)) {
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. return false;
  255. }
  256. /**
  257. * Updates a specific property for a participant.
  258. *
  259. * @param {State} state - The redux state.
  260. * @param {string} id - The ID of the participant.
  261. * @param {string} property - The property to update.
  262. * @param {*} value - The new value.
  263. * @returns {boolean} - True if a participant was updated and false otherwise.
  264. */
  265. function _updateParticipantProperty(state, id, property, value) {
  266. const { remote, local } = state;
  267. if (remote.has(id)) {
  268. remote.set(id, set(remote.get(id), property, value));
  269. return true;
  270. } else if (local?.id === id) {
  271. state.local = set(local, property, value);
  272. return true;
  273. }
  274. return false;
  275. }
  276. /**
  277. * Reducer function for a single participant.
  278. *
  279. * @param {Participant|undefined} state - Participant to be modified.
  280. * @param {Object} action - Action object.
  281. * @param {string} action.type - Type of action.
  282. * @param {Participant} action.participant - Information about participant to be
  283. * added/modified.
  284. * @param {JitsiConference} action.conference - Conference instance.
  285. * @private
  286. * @returns {Participant}
  287. */
  288. function _participant(state: Object = {}, action) {
  289. switch (action.type) {
  290. case SET_LOADABLE_AVATAR_URL:
  291. case PARTICIPANT_UPDATED: {
  292. const { participant } = action; // eslint-disable-line no-shadow
  293. const newState = { ...state };
  294. for (const key in participant) {
  295. if (participant.hasOwnProperty(key)
  296. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  297. === -1) {
  298. newState[key] = participant[key];
  299. }
  300. }
  301. return newState;
  302. }
  303. }
  304. return state;
  305. }
  306. /**
  307. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  308. * feature base/participants.
  309. *
  310. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  311. * to reduce.
  312. * @private
  313. * @returns {Object} The new participant derived from the payload of the
  314. * specified {@code action} to be added into the redux state of the feature
  315. * base/participants after the reduction of the specified
  316. * {@code action}.
  317. */
  318. function _participantJoined({ participant }) {
  319. const {
  320. avatarURL,
  321. botType,
  322. connectionStatus,
  323. dominantSpeaker,
  324. email,
  325. isFakeParticipant,
  326. isReplacing,
  327. isJigasi,
  328. loadableAvatarUrl,
  329. local,
  330. name,
  331. pinned,
  332. presence,
  333. role
  334. } = participant;
  335. let { conference, id } = participant;
  336. if (local) {
  337. // conference
  338. //
  339. // XXX The local participant is not identified in association with a
  340. // JitsiConference because it is identified by the very fact that it is
  341. // the local participant.
  342. conference = undefined;
  343. // id
  344. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  345. }
  346. return {
  347. avatarURL,
  348. botType,
  349. conference,
  350. connectionStatus,
  351. dominantSpeaker: dominantSpeaker || false,
  352. email,
  353. id,
  354. isFakeParticipant,
  355. isReplacing,
  356. isJigasi,
  357. loadableAvatarUrl,
  358. local: local || false,
  359. name,
  360. pinned: pinned || false,
  361. presence,
  362. role: role || PARTICIPANT_ROLE.NONE
  363. };
  364. }