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.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { batch } from 'react-redux';
  2. import { IStore } from '../app/types';
  3. import { CONFERENCE_JOIN_IN_PROGRESS, CONFERENCE_LEFT } from '../base/conference/actionTypes';
  4. import { getCurrentConference } from '../base/conference/functions';
  5. import { IJitsiConference } from '../base/conference/reducer';
  6. import { SET_CONFIG } from '../base/config/actionTypes';
  7. import { MEDIA_TYPE } from '../base/media/constants';
  8. import { PARTICIPANT_LEFT } from '../base/participants/actionTypes';
  9. import { participantJoined, participantLeft, pinParticipant } from '../base/participants/actions';
  10. import { getLocalParticipant, getParticipantById, getParticipantDisplayName } from '../base/participants/functions';
  11. import { FakeParticipant } from '../base/participants/types';
  12. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  13. import { SET_DYNAMIC_BRANDING_DATA } from '../dynamic-branding/actionTypes';
  14. import { showWarningNotification } from '../notifications/actions';
  15. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  16. import { RESET_SHARED_VIDEO_STATUS, SET_SHARED_VIDEO_STATUS } from './actionTypes';
  17. import {
  18. hideConfirmPlayingDialog,
  19. resetSharedVideoStatus,
  20. setAllowedUrlDomians,
  21. setSharedVideoStatus,
  22. showConfirmPlayingDialog
  23. } from './actions';
  24. import {
  25. DEFAULT_ALLOWED_URL_DOMAINS,
  26. PLAYBACK_START,
  27. PLAYBACK_STATUSES,
  28. SHARED_VIDEO,
  29. VIDEO_PLAYER_PARTICIPANT_NAME
  30. } from './constants';
  31. import { isSharedVideoEnabled, isSharingStatus, isURLAllowedForSharedVideo, sendShareVideoCommand } from './functions';
  32. import logger from './logger';
  33. /**
  34. * Middleware that captures actions related to video sharing and updates
  35. * components not hooked into redux.
  36. *
  37. * @param {Store} store - The redux store.
  38. * @returns {Function}
  39. */
  40. MiddlewareRegistry.register(store => next => action => {
  41. const { dispatch, getState } = store;
  42. if (!isSharedVideoEnabled(getState())) {
  43. return next(action);
  44. }
  45. switch (action.type) {
  46. case CONFERENCE_JOIN_IN_PROGRESS: {
  47. const { conference } = action;
  48. const localParticipantId = getLocalParticipant(getState())?.id;
  49. conference.addCommandListener(SHARED_VIDEO,
  50. ({ value, attributes }: { attributes: {
  51. muted: string; state: string; time: string; }; value: string; },
  52. from: string) => {
  53. const state = getState();
  54. const sharedVideoStatus = attributes.state;
  55. const { ownerId } = state['features/shared-video'];
  56. if (ownerId && ownerId !== from) {
  57. logger.warn(
  58. `User with id: ${from} sent shared video command: ${sharedVideoStatus} while we are playing.`);
  59. return;
  60. }
  61. if (isSharingStatus(sharedVideoStatus)) {
  62. // confirmShowVideo is undefined the first time we receive
  63. // when confirmShowVideo is false we ignore everything except stop that resets it
  64. if (state['features/shared-video'].confirmShowVideo === false) {
  65. return;
  66. }
  67. if (isURLAllowedForSharedVideo(value, state['features/shared-video'].allowedUrlDomains, true)
  68. || localParticipantId === from
  69. || state['features/shared-video'].confirmShowVideo) { // if confirmed skip asking again
  70. handleSharingVideoStatus(store, value, {
  71. ...attributes,
  72. from
  73. }, conference);
  74. } else {
  75. dispatch(showConfirmPlayingDialog(getParticipantDisplayName(state, from), () => {
  76. handleSharingVideoStatus(store, value, {
  77. ...attributes,
  78. from
  79. }, conference);
  80. return true; // on mobile this is used to close the dialog
  81. }));
  82. }
  83. return;
  84. }
  85. if (sharedVideoStatus === 'stop') {
  86. const videoParticipant = getParticipantById(state, value);
  87. if (state['features/shared-video'].confirmShowVideo === false) {
  88. dispatch(showWarningNotification({
  89. titleKey: 'dialog.shareVideoLinkStopped',
  90. titleArguments: {
  91. name: getParticipantDisplayName(state, from)
  92. }
  93. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  94. }
  95. dispatch(hideConfirmPlayingDialog());
  96. dispatch(participantLeft(value, conference, {
  97. fakeParticipant: videoParticipant?.fakeParticipant
  98. }));
  99. if (localParticipantId !== from) {
  100. dispatch(resetSharedVideoStatus());
  101. }
  102. }
  103. }
  104. );
  105. break;
  106. }
  107. case CONFERENCE_LEFT:
  108. dispatch(setAllowedUrlDomians(DEFAULT_ALLOWED_URL_DOMAINS));
  109. dispatch(resetSharedVideoStatus());
  110. break;
  111. case PARTICIPANT_LEFT: {
  112. const state = getState();
  113. const conference = getCurrentConference(state);
  114. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  115. if (action.participant.id === stateOwnerId) {
  116. batch(() => {
  117. dispatch(resetSharedVideoStatus());
  118. dispatch(participantLeft(statevideoUrl ?? '', conference));
  119. });
  120. }
  121. break;
  122. }
  123. case SET_CONFIG:
  124. case SET_DYNAMIC_BRANDING_DATA: {
  125. const result = next(action);
  126. const state = getState();
  127. const { sharedVideoAllowedURLDomains: allowedURLDomainsFromConfig = [] } = state['features/base/config'];
  128. const { sharedVideoAllowedURLDomains: allowedURLDomainsFromBranding = [] } = state['features/dynamic-branding'];
  129. dispatch(setAllowedUrlDomians([
  130. ...DEFAULT_ALLOWED_URL_DOMAINS,
  131. ...allowedURLDomainsFromBranding,
  132. ...allowedURLDomainsFromConfig
  133. ]));
  134. return result;
  135. }
  136. case SET_SHARED_VIDEO_STATUS: {
  137. const state = getState();
  138. const conference = getCurrentConference(state);
  139. const localParticipantId = getLocalParticipant(state)?.id;
  140. const { videoUrl, status, ownerId, time, muted, volume } = action;
  141. const operator = status === PLAYBACK_STATUSES.PLAYING ? 'is' : '';
  142. logger.debug(`User with id: ${ownerId} ${operator} ${status} video sharing.`);
  143. if (typeof APP !== 'undefined') {
  144. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, status, ownerId);
  145. }
  146. // when setting status we need to send the command for that, but not do it for the start command
  147. // as we are sending the command in playSharedVideo and setting the start status once
  148. // we receive the response, this way we will start the video at the same time when remote participants
  149. // start it, on receiving the command
  150. if (status === 'start') {
  151. break;
  152. }
  153. if (localParticipantId === ownerId) {
  154. sendShareVideoCommand({
  155. conference,
  156. localParticipantId,
  157. muted,
  158. status,
  159. time,
  160. id: videoUrl,
  161. volume
  162. });
  163. }
  164. break;
  165. }
  166. case RESET_SHARED_VIDEO_STATUS: {
  167. const state = getState();
  168. const localParticipantId = getLocalParticipant(state)?.id;
  169. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  170. if (!stateOwnerId) {
  171. break;
  172. }
  173. logger.debug(`User with id: ${stateOwnerId} stop video sharing.`);
  174. if (typeof APP !== 'undefined') {
  175. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, 'stop', stateOwnerId);
  176. }
  177. if (localParticipantId === stateOwnerId) {
  178. const conference = getCurrentConference(state);
  179. sendShareVideoCommand({
  180. conference,
  181. id: statevideoUrl ?? '',
  182. localParticipantId,
  183. muted: true,
  184. status: 'stop',
  185. time: 0,
  186. volume: 0
  187. });
  188. }
  189. break;
  190. }
  191. }
  192. return next(action);
  193. });
  194. /**
  195. * Handles the playing, pause and start statuses for the shared video.
  196. * Dispatches participantJoined event and, if necessary, pins it.
  197. * Sets the SharedVideoStatus if the event was triggered by the local user.
  198. *
  199. * @param {Store} store - The redux store.
  200. * @param {string} videoUrl - The id of the video to the shared.
  201. * @param {Object} attributes - The attributes received from the share video command.
  202. * @param {JitsiConference} conference - The current conference.
  203. * @returns {void}
  204. */
  205. function handleSharingVideoStatus(store: IStore, videoUrl: string,
  206. { state, time, from, muted }: { from: string; muted: string; state: string; time: string; },
  207. conference: IJitsiConference) {
  208. const { dispatch, getState } = store;
  209. const localParticipantId = getLocalParticipant(getState())?.id;
  210. const oldStatus = getState()['features/shared-video']?.status ?? '';
  211. const oldVideoUrl = getState()['features/shared-video'].videoUrl;
  212. if (oldVideoUrl && oldVideoUrl !== videoUrl) {
  213. logger.warn(
  214. `User with id: ${from} sent videoUrl: ${videoUrl} while we are playing: ${oldVideoUrl}`);
  215. return;
  216. }
  217. // If the video was not started (no participant) we want to create the participant
  218. // this can be triggered by start, but also by paused or playing
  219. // commands (joining late) and getting the current state
  220. if (state === PLAYBACK_START || !isSharingStatus(oldStatus)) {
  221. const youtubeId = videoUrl.match(/http/) ? false : videoUrl;
  222. const avatarURL = youtubeId ? `https://img.youtube.com/vi/${youtubeId}/0.jpg` : '';
  223. dispatch(participantJoined({
  224. conference,
  225. fakeParticipant: FakeParticipant.SharedVideo,
  226. id: videoUrl,
  227. avatarURL,
  228. name: VIDEO_PLAYER_PARTICIPANT_NAME
  229. }));
  230. dispatch(pinParticipant(videoUrl));
  231. if (localParticipantId === from) {
  232. dispatch(setSharedVideoStatus({
  233. videoUrl,
  234. status: state,
  235. time: Number(time),
  236. ownerId: localParticipantId
  237. }));
  238. }
  239. }
  240. if (localParticipantId !== from) {
  241. dispatch(setSharedVideoStatus({
  242. muted: muted === 'true',
  243. ownerId: from,
  244. status: state,
  245. time: Number(time),
  246. videoUrl
  247. }));
  248. }
  249. }