您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. import { isFollowMeActive } from './functions';
  18. import logger from './logger';
  19. declare var APP: Object;
  20. /**
  21. * The timeout after which a follow-me command that has been received will be
  22. * ignored if not consumed.
  23. *
  24. * @type {number} in seconds
  25. * @private
  26. */
  27. const _FOLLOW_ME_RECEIVED_TIMEOUT = 30;
  28. /**
  29. * An instance of a timeout used as a workaround when attempting to pin a
  30. * non-existent particapant, which may be caused by participant join information
  31. * not being received yet.
  32. *
  33. * @type {TimeoutID}
  34. */
  35. let nextOnStageTimeout;
  36. /**
  37. * A count of how many seconds the nextOnStageTimeout has ticked while waiting
  38. * for a participant to be discovered that should be pinned. This variable
  39. * works in conjunction with {@code _FOLLOW_ME_RECEIVED_TIMEOUT} and
  40. * {@code nextOnStageTimeout}.
  41. *
  42. * @type {number}
  43. */
  44. let nextOnStageTimer = 0;
  45. /**
  46. * Represents "Follow Me" feature which enables a moderator to (partially)
  47. * control the user experience/interface (e.g. filmstrip visibility) of (other)
  48. * non-moderator participant.
  49. */
  50. MiddlewareRegistry.register(store => next => action => {
  51. switch (action.type) {
  52. case CONFERENCE_WILL_JOIN: {
  53. const { conference } = action;
  54. conference.addCommandListener(
  55. FOLLOW_ME_COMMAND, ({ attributes }, id) => {
  56. _onFollowMeCommand(attributes, id, store);
  57. });
  58. break;
  59. }
  60. case PARTICIPANT_LEFT:
  61. if (store.getState()['features/follow-me'].moderator === action.participant.id) {
  62. store.dispatch(setFollowMeModerator());
  63. }
  64. break;
  65. }
  66. return next(action);
  67. });
  68. /**
  69. * Notifies this instance about a "Follow Me" command received by the Jitsi
  70. * conference.
  71. *
  72. * @param {Object} attributes - The attributes carried by the command.
  73. * @param {string} id - The identifier of the participant who issuing the
  74. * command. A notable idiosyncrasy to be mindful of here is that the command
  75. * may be issued by the local participant.
  76. * @param {Object} store - The redux store. Used to calculate and dispatch
  77. * updates.
  78. * @private
  79. * @returns {void}
  80. */
  81. function _onFollowMeCommand(attributes = {}, id, store) {
  82. const state = store.getState();
  83. // We require to know who issued the command because (1) only a
  84. // moderator is allowed to send commands and (2) a command MUST be
  85. // issued by a defined commander.
  86. if (typeof id === 'undefined') {
  87. return;
  88. }
  89. const participantSendingCommand = getParticipantById(state, id);
  90. // The Command(s) API will send us our own commands and we don't want
  91. // to act upon them.
  92. if (participantSendingCommand.local) {
  93. return;
  94. }
  95. if (participantSendingCommand.role !== 'moderator') {
  96. logger.warn('Received follow-me command not from moderator');
  97. return;
  98. }
  99. if (!isFollowMeActive(state)) {
  100. store.dispatch(setFollowMeModerator(id));
  101. }
  102. // just a command that follow me was turned off
  103. if (attributes.off) {
  104. store.dispatch(setFollowMeModerator());
  105. return;
  106. }
  107. const oldState = state['features/follow-me'].state || {};
  108. store.dispatch(setFollowMeState(attributes));
  109. // XMPP will translate all booleans to strings, so explicitly check against
  110. // the string form of the boolean {@code true}.
  111. if (oldState.filmstripVisible !== attributes.filmstripVisible) {
  112. store.dispatch(setFilmstripVisible(attributes.filmstripVisible === 'true'));
  113. }
  114. if (oldState.tileViewEnabled !== attributes.tileViewEnabled) {
  115. store.dispatch(setTileView(attributes.tileViewEnabled === 'true'));
  116. }
  117. // For now gate etherpad checks behind a web-app check to be extra safe
  118. // against calling a web-app global.
  119. if (typeof APP !== 'undefined'
  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. }