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.

middleware.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // @flow
  2. import _ from 'lodash';
  3. import { CONFERENCE_JOIN_IN_PROGRESS } from '../base/conference/actionTypes';
  4. import {
  5. getParticipantById,
  6. getPinnedParticipant,
  7. PARTICIPANT_LEFT,
  8. pinParticipant
  9. } from '../base/participants';
  10. import { MiddlewareRegistry } from '../base/redux';
  11. import { setFilmstripVisible } from '../filmstrip';
  12. import { addStageParticipant, setMaxStageParticipants } from '../filmstrip/actions.web';
  13. import { setTileView } from '../video-layout';
  14. import {
  15. setFollowMeModerator,
  16. setFollowMeState
  17. } from './actions';
  18. import { FOLLOW_ME_COMMAND } from './constants';
  19. import { isFollowMeActive } from './functions';
  20. import logger from './logger';
  21. import './subscriber';
  22. declare var APP: Object;
  23. /**
  24. * The timeout after which a follow-me command that has been received will be
  25. * ignored if not consumed.
  26. *
  27. * @type {number} in seconds
  28. * @private
  29. */
  30. const _FOLLOW_ME_RECEIVED_TIMEOUT = 30;
  31. /**
  32. * An instance of a timeout used as a workaround when attempting to pin a
  33. * non-existent particapant, which may be caused by participant join information
  34. * not being received yet.
  35. *
  36. * @type {TimeoutID}
  37. */
  38. let nextOnStageTimeout;
  39. /**
  40. * A count of how many seconds the nextOnStageTimeout has ticked while waiting
  41. * for a participant to be discovered that should be pinned. This variable
  42. * works in conjunction with {@code _FOLLOW_ME_RECEIVED_TIMEOUT} and
  43. * {@code nextOnStageTimeout}.
  44. *
  45. * @type {number}
  46. */
  47. let nextOnStageTimer = 0;
  48. /**
  49. * Represents "Follow Me" feature which enables a moderator to (partially)
  50. * control the user experience/interface (e.g. Filmstrip visibility) of (other)
  51. * non-moderator participant.
  52. */
  53. MiddlewareRegistry.register(store => next => action => {
  54. switch (action.type) {
  55. case CONFERENCE_JOIN_IN_PROGRESS: {
  56. const { conference } = action;
  57. conference.addCommandListener(
  58. FOLLOW_ME_COMMAND, ({ attributes }, id) => {
  59. _onFollowMeCommand(attributes, id, store);
  60. });
  61. break;
  62. }
  63. case PARTICIPANT_LEFT:
  64. if (store.getState()['features/follow-me'].moderator === action.participant.id) {
  65. store.dispatch(setFollowMeModerator());
  66. }
  67. break;
  68. }
  69. return next(action);
  70. });
  71. /**
  72. * Notifies this instance about a "Follow Me" command received by the Jitsi
  73. * conference.
  74. *
  75. * @param {Object} attributes - The attributes carried by the command.
  76. * @param {string} id - The identifier of the participant who issuing the
  77. * command. A notable idiosyncrasy to be mindful of here is that the command
  78. * may be issued by the local participant.
  79. * @param {Object} store - The redux store. Used to calculate and dispatch
  80. * updates.
  81. * @private
  82. * @returns {void}
  83. */
  84. function _onFollowMeCommand(attributes = {}, id, store) {
  85. const state = store.getState();
  86. // We require to know who issued the command because (1) only a
  87. // moderator is allowed to send commands and (2) a command MUST be
  88. // issued by a defined commander.
  89. if (typeof id === 'undefined') {
  90. return;
  91. }
  92. const participantSendingCommand = getParticipantById(state, id);
  93. if (participantSendingCommand) {
  94. // The Command(s) API will send us our own commands and we don't want
  95. // to act upon them.
  96. if (participantSendingCommand.local) {
  97. return;
  98. }
  99. if (participantSendingCommand.role !== 'moderator') {
  100. logger.warn('Received follow-me command not from moderator');
  101. return;
  102. }
  103. } else {
  104. // This is the case of jibri receiving commands from a hidden participant.
  105. const { iAmRecorder } = state['features/base/config'];
  106. const { conference } = state['features/base/conference'];
  107. // As this participant is not stored in redux store we do the checks on the JitsiParticipant from lib-jitsi-meet
  108. const participant = conference.getParticipantById(id);
  109. if (!iAmRecorder || !participant || participant.getRole() !== 'moderator'
  110. || !participant.isHiddenFromRecorder()) {
  111. logger.warn('Something went wrong with follow-me command');
  112. return;
  113. }
  114. }
  115. if (!isFollowMeActive(state)) {
  116. store.dispatch(setFollowMeModerator(id));
  117. }
  118. // just a command that follow me was turned off
  119. if (attributes.off) {
  120. store.dispatch(setFollowMeModerator());
  121. return;
  122. }
  123. const oldState = state['features/follow-me'].state || {};
  124. store.dispatch(setFollowMeState(attributes));
  125. // XMPP will translate all booleans to strings, so explicitly check against
  126. // the string form of the boolean {@code true}.
  127. if (oldState.filmstripVisible !== attributes.filmstripVisible) {
  128. store.dispatch(setFilmstripVisible(attributes.filmstripVisible === 'true'));
  129. }
  130. if (oldState.tileViewEnabled !== attributes.tileViewEnabled) {
  131. store.dispatch(setTileView(attributes.tileViewEnabled === 'true'));
  132. }
  133. // For now gate etherpad checks behind a web-app check to be extra safe
  134. // against calling a web-app global.
  135. if (typeof APP !== 'undefined'
  136. && oldState.sharedDocumentVisible !== attributes.sharedDocumentVisible) {
  137. const isEtherpadVisible = attributes.sharedDocumentVisible === 'true';
  138. const documentManager = APP.UI.getSharedDocumentManager();
  139. if (documentManager
  140. && isEtherpadVisible !== state['features/etherpad'].editing) {
  141. documentManager.toggleEtherpad();
  142. }
  143. }
  144. const pinnedParticipant = getPinnedParticipant(state);
  145. const idOfParticipantToPin = attributes.nextOnStage;
  146. if (typeof idOfParticipantToPin !== 'undefined'
  147. && (!pinnedParticipant || idOfParticipantToPin !== pinnedParticipant.id)
  148. && oldState.nextOnStage !== attributes.nextOnStage) {
  149. _pinVideoThumbnailById(store, idOfParticipantToPin);
  150. } else if (typeof idOfParticipantToPin === 'undefined' && pinnedParticipant) {
  151. store.dispatch(pinParticipant(null));
  152. }
  153. if (attributes.pinnedStageParticipants !== undefined) {
  154. const stageParticipants = JSON.parse(attributes.pinnedStageParticipants);
  155. if (!_.isEqual(stageParticipants, oldState.pinnedStageParticipants)) {
  156. stageParticipants.forEach(p => store.dispatch(addStageParticipant(p.participantId, true)));
  157. }
  158. }
  159. if (attributes.maxStageParticipants !== undefined
  160. && oldState.maxStageParticipants !== attributes.maxStageParticipants) {
  161. store.dispatch(setMaxStageParticipants(Number(attributes.maxStageParticipants)));
  162. }
  163. }
  164. /**
  165. * Pins the video thumbnail given by clickId.
  166. *
  167. * @param {Object} store - The redux store.
  168. * @param {string} clickId - The identifier of the participant to pin.
  169. * @private
  170. * @returns {void}
  171. */
  172. function _pinVideoThumbnailById(store, clickId) {
  173. if (getParticipantById(store.getState(), clickId)) {
  174. clearTimeout(nextOnStageTimeout);
  175. nextOnStageTimer = 0;
  176. store.dispatch(pinParticipant(clickId));
  177. } else {
  178. nextOnStageTimeout = setTimeout(() => {
  179. if (nextOnStageTimer > _FOLLOW_ME_RECEIVED_TIMEOUT) {
  180. nextOnStageTimer = 0;
  181. return;
  182. }
  183. nextOnStageTimer++;
  184. _pinVideoThumbnailById(store, clickId);
  185. }, 1000);
  186. }
  187. }