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

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