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ů.

middleware.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // @flow
  2. import { CONFERENCE_LEFT, getCurrentConference } from '../base/conference';
  3. import {
  4. PARTICIPANT_LEFT,
  5. getLocalParticipant,
  6. participantJoined,
  7. participantLeft,
  8. pinParticipant
  9. } from '../base/participants';
  10. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  11. import { TOGGLE_SHARED_VIDEO, SET_SHARED_VIDEO_STATUS } from './actionTypes';
  12. import { setSharedVideoStatus, showEnterVideoLinkPrompt } from './actions';
  13. const SHARED_VIDEO = 'shared-video';
  14. /**
  15. * Middleware that captures actions related to YouTube video sharing and updates
  16. * components not hooked into redux.
  17. *
  18. * @param {Store} store - The redux store.
  19. * @returns {Function}
  20. */
  21. MiddlewareRegistry.register(store => next => action => {
  22. const { dispatch, getState } = store;
  23. const state = getState();
  24. const conference = getCurrentConference(state);
  25. const localParticipantId = getLocalParticipant(state)?.id;
  26. const { videoId, status, ownerId, time } = action;
  27. const { ownerId: stateOwnerId, videoId: stateVideoId } = state['features/youtube-player'];
  28. switch (action.type) {
  29. case TOGGLE_SHARED_VIDEO:
  30. _toggleSharedVideo(store, next, action);
  31. break;
  32. case CONFERENCE_LEFT:
  33. dispatch(setSharedVideoStatus('', 'stop', 0, ''));
  34. break;
  35. case PARTICIPANT_LEFT:
  36. if (action.participant.id === stateOwnerId) {
  37. dispatch(setSharedVideoStatus('', 'stop', 0, ''));
  38. dispatch(participantLeft(stateVideoId, conference));
  39. }
  40. break;
  41. case SET_SHARED_VIDEO_STATUS:
  42. if (localParticipantId === ownerId) {
  43. sendShareVideoCommand(videoId, status, conference, localParticipantId, time);
  44. }
  45. break;
  46. }
  47. return next(action);
  48. });
  49. /**
  50. * Set up state change listener to perform maintenance tasks when the conference
  51. * is left or failed, e.g. clear messages or close the chat modal if it's left
  52. * open.
  53. */
  54. StateListenerRegistry.register(
  55. state => getCurrentConference(state),
  56. (conference, store, previousConference) => {
  57. if (conference && conference !== previousConference) {
  58. conference.addCommandListener(SHARED_VIDEO,
  59. ({ value, attributes }) => {
  60. const { dispatch, getState } = store;
  61. const { from } = attributes;
  62. const localParticipantId = getLocalParticipant(getState()).id;
  63. const status = attributes.state;
  64. if ([ 'playing', 'pause', 'start' ].includes(status)) {
  65. handleSharingVideoStatus(store, value, attributes, conference);
  66. } else if (status === 'stop') {
  67. dispatch(participantLeft(value, conference));
  68. if (localParticipantId !== from) {
  69. dispatch(setSharedVideoStatus(value, 'stop', 0, from));
  70. }
  71. }
  72. }
  73. );
  74. }
  75. });
  76. /**
  77. * Handles the playing, pause and start statuses for the shared video.
  78. * Dispatches participantJoined event and, if necessary, pins it.
  79. * Sets the SharedVideoStatus if the event was triggered by the local user.
  80. *
  81. * @param {Store} store - The redux store.
  82. * @param {string} videoId - The YoutubeId of the video to the shared.
  83. * @param {Object} attributes - The attributes received from the share video command.
  84. * @param {JitsiConference} conference - The current conference.
  85. * @returns {void}
  86. */
  87. function handleSharingVideoStatus(store, videoId, { state, time, from }, conference) {
  88. const { dispatch, getState } = store;
  89. const localParticipantId = getLocalParticipant(getState()).id;
  90. const oldStatus = getState()['features/youtube-player']?.status;
  91. if (state === 'start' || ![ 'playing', 'pause', 'start' ].includes(oldStatus)) {
  92. dispatch(participantJoined({
  93. conference,
  94. id: videoId,
  95. isFakeParticipant: true,
  96. avatarURL: `https://img.youtube.com/vi/${videoId}/0.jpg`,
  97. name: 'YouTube'
  98. }));
  99. dispatch(pinParticipant(videoId));
  100. }
  101. if (localParticipantId !== from) {
  102. dispatch(setSharedVideoStatus(videoId, state, time, from));
  103. }
  104. }
  105. /**
  106. * Dispatches shared video status.
  107. *
  108. * @param {Store} store - The redux store.
  109. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  110. * specified {@code action} in the specified {@code store}.
  111. * @param {Action} action - The redux action which is
  112. * being dispatched in the specified {@code store}.
  113. * @returns {Function}
  114. */
  115. function _toggleSharedVideo(store, next, action) {
  116. const { dispatch, getState } = store;
  117. const state = getState();
  118. const { videoId, ownerId, status } = state['features/youtube-player'];
  119. const localParticipant = getLocalParticipant(state);
  120. if (status === 'playing' || status === 'start' || status === 'pause') {
  121. if (ownerId === localParticipant.id) {
  122. dispatch(setSharedVideoStatus(videoId, 'stop', 0, localParticipant.id));
  123. }
  124. } else {
  125. dispatch(showEnterVideoLinkPrompt(id => _onVideoLinkEntered(store, id)));
  126. }
  127. return next(action);
  128. }
  129. /**
  130. * Sends SHARED_VIDEO start command.
  131. *
  132. * @param {Store} store - The redux store.
  133. * @param {string} id - The youtube id of the video to be shared.
  134. * @returns {void}
  135. */
  136. function _onVideoLinkEntered(store, id) {
  137. const { dispatch, getState } = store;
  138. const conference = getCurrentConference(getState());
  139. if (conference) {
  140. const localParticipant = getLocalParticipant(getState());
  141. dispatch(setSharedVideoStatus(id, 'start', 0, localParticipant.id));
  142. }
  143. }
  144. /* eslint-disable max-params */
  145. /**
  146. * Sends SHARED_VIDEO command.
  147. *
  148. * @param {string} id - The youtube id of the video.
  149. * @param {string} status - The status of the shared video.
  150. * @param {JitsiConference} conference - The current conference.
  151. * @param {string} localParticipantId - The id of the local participant.
  152. * @param {string} time - The seek position of the video.
  153. * @returns {void}
  154. */
  155. function sendShareVideoCommand(id, status, conference, localParticipantId, time) {
  156. conference.sendCommandOnce(SHARED_VIDEO, {
  157. value: id,
  158. attributes: {
  159. from: localParticipantId,
  160. state: status,
  161. time
  162. }
  163. });
  164. }