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

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