Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { NOTIFICATION_TIMEOUT, showNotification } from '../../notifications';
  4. import { CALLING, INVITED } from '../../presence-status';
  5. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  6. import {
  7. CONFERENCE_WILL_JOIN,
  8. forEachConference,
  9. getCurrentConference
  10. } from '../conference';
  11. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  12. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  13. import { playSound, registerSound, unregisterSound } from '../sounds';
  14. import {
  15. localParticipantIdChanged,
  16. localParticipantJoined,
  17. localParticipantLeft,
  18. participantLeft,
  19. participantUpdated,
  20. showParticipantJoinedNotification
  21. } from './actions';
  22. import {
  23. DOMINANT_SPEAKER_CHANGED,
  24. KICK_PARTICIPANT,
  25. MUTE_REMOTE_PARTICIPANT,
  26. PARTICIPANT_DISPLAY_NAME_CHANGED,
  27. PARTICIPANT_JOINED,
  28. PARTICIPANT_LEFT,
  29. PARTICIPANT_UPDATED
  30. } from './actionTypes';
  31. import {
  32. LOCAL_PARTICIPANT_DEFAULT_ID,
  33. PARTICIPANT_JOINED_SOUND_ID,
  34. PARTICIPANT_LEFT_SOUND_ID
  35. } from './constants';
  36. import {
  37. getAvatarURLByParticipantId,
  38. getLocalParticipant,
  39. getParticipantCount,
  40. getParticipantDisplayName
  41. } from './functions';
  42. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  43. declare var APP: Object;
  44. /**
  45. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  46. * updates respectively ID of local participant.
  47. *
  48. * @param {Store} store - The redux store.
  49. * @returns {Function}
  50. */
  51. MiddlewareRegistry.register(store => next => action => {
  52. switch (action.type) {
  53. case APP_WILL_MOUNT:
  54. _registerSounds(store);
  55. return _localParticipantJoined(store, next, action);
  56. case APP_WILL_UNMOUNT:
  57. _unregisterSounds(store);
  58. return _localParticipantLeft(store, next, action);
  59. case CONFERENCE_WILL_JOIN:
  60. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  61. break;
  62. case DOMINANT_SPEAKER_CHANGED: {
  63. // Ensure the raised hand state is cleared for the dominant speaker.
  64. const { conference, id } = action.participant;
  65. const participant = getLocalParticipant(store.getState());
  66. participant
  67. && store.dispatch(participantUpdated({
  68. conference,
  69. id,
  70. local: participant.id === id,
  71. raisedHand: false
  72. }));
  73. break;
  74. }
  75. case KICK_PARTICIPANT: {
  76. const { conference } = store.getState()['features/base/conference'];
  77. conference.kickParticipant(action.id);
  78. break;
  79. }
  80. case MUTE_REMOTE_PARTICIPANT: {
  81. const { conference } = store.getState()['features/base/conference'];
  82. conference.muteParticipant(action.id);
  83. break;
  84. }
  85. // TODO Remove this middleware when the local display name update flow is
  86. // fully brought into redux.
  87. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  88. if (typeof APP !== 'undefined') {
  89. const participant = getLocalParticipant(store.getState());
  90. if (participant && participant.id === action.id) {
  91. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  92. }
  93. }
  94. break;
  95. }
  96. case PARTICIPANT_JOINED: {
  97. _maybePlaySounds(store, action);
  98. const result = _participantJoinedOrUpdated(store, next, action);
  99. const { participant: p } = action;
  100. if (!p.local) {
  101. store.dispatch(showParticipantJoinedNotification(getParticipantDisplayName(store.getState, p.id)));
  102. }
  103. return result;
  104. }
  105. case PARTICIPANT_LEFT:
  106. _maybePlaySounds(store, action);
  107. break;
  108. case PARTICIPANT_UPDATED:
  109. return _participantJoinedOrUpdated(store, next, action);
  110. }
  111. return next(action);
  112. });
  113. /**
  114. * Syncs the redux state features/base/participants up with the redux state
  115. * features/base/conference by ensuring that the former does not contain remote
  116. * participants no longer relevant to the latter. Introduced to address an issue
  117. * with multiplying thumbnails in the filmstrip.
  118. */
  119. StateListenerRegistry.register(
  120. /* selector */ state => getCurrentConference(state),
  121. /* listener */ (conference, { dispatch, getState }) => {
  122. for (const p of getState()['features/base/participants']) {
  123. !p.local
  124. && (!conference || p.conference !== conference)
  125. && dispatch(participantLeft(p.id, p.conference));
  126. }
  127. });
  128. /**
  129. * Reset the ID of the local participant to
  130. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  131. * the local participant and, respectively, her ID is not involved in a
  132. * conference which is still of interest to the user and, consequently, the app.
  133. * For example, a conference which is in the process of leaving is no longer of
  134. * interest the user, is unrecoverable from the perspective of the user and,
  135. * consequently, the app.
  136. */
  137. StateListenerRegistry.register(
  138. /* selector */ state => state['features/base/conference'],
  139. /* listener */ ({ leaving }, { dispatch, getState }) => {
  140. const state = getState();
  141. const localParticipant = getLocalParticipant(state);
  142. let id;
  143. if (!localParticipant
  144. || (id = localParticipant.id)
  145. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  146. // The ID of the local participant has been reset already.
  147. return;
  148. }
  149. // The ID of the local may be reset only if it is not in use.
  150. const dispatchLocalParticipantIdChanged
  151. = forEachConference(
  152. state,
  153. conference =>
  154. conference === leaving || conference.myUserId() !== id);
  155. dispatchLocalParticipantIdChanged
  156. && dispatch(
  157. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  158. });
  159. /**
  160. * Registers listeners for participant change events.
  161. */
  162. StateListenerRegistry.register(
  163. state => state['features/base/conference'].conference,
  164. (conference, store) => {
  165. if (conference) {
  166. // We joined a conference
  167. conference.on(
  168. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  169. (participant, propertyName, oldValue, newValue) => {
  170. switch (propertyName) {
  171. case 'features_screen-sharing':
  172. store.dispatch(participantUpdated({
  173. conference,
  174. id: participant.getId(),
  175. features: { 'screen-sharing': true }
  176. }));
  177. break;
  178. case 'raisedHand': {
  179. _raiseHandUpdated(
  180. store, conference, participant.getId(), newValue);
  181. break;
  182. }
  183. default:
  184. // Ignore for now.
  185. }
  186. });
  187. } else {
  188. // We left the conference, raise hand of the local participant must be updated.
  189. _raiseHandUpdated(
  190. store, conference, undefined, false);
  191. }
  192. }
  193. );
  194. /**
  195. * Initializes the local participant and signals that it joined.
  196. *
  197. * @private
  198. * @param {Store} store - The redux store.
  199. * @param {Dispatch} next - The redux dispatch function to dispatch the
  200. * specified action to the specified store.
  201. * @param {Action} action - The redux action which is being dispatched
  202. * in the specified store.
  203. * @private
  204. * @returns {Object} The value returned by {@code next(action)}.
  205. */
  206. function _localParticipantJoined({ getState, dispatch }, next, action) {
  207. const result = next(action);
  208. const settings = getState()['features/base/settings'];
  209. dispatch(localParticipantJoined({
  210. avatarID: settings.avatarID,
  211. avatarURL: settings.avatarURL,
  212. email: settings.email,
  213. name: settings.displayName
  214. }));
  215. return result;
  216. }
  217. /**
  218. * Signals that the local participant has left.
  219. *
  220. * @param {Store} store - The redux store.
  221. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  222. * specified {@code action} into the specified {@code store}.
  223. * @param {Action} action - The redux action which is being dispatched in the
  224. * specified {@code store}.
  225. * @private
  226. * @returns {Object} The value returned by {@code next(action)}.
  227. */
  228. function _localParticipantLeft({ dispatch }, next, action) {
  229. const result = next(action);
  230. dispatch(localParticipantLeft());
  231. return result;
  232. }
  233. /**
  234. * Plays sounds when participants join/leave conference.
  235. *
  236. * @param {Store} store - The redux store.
  237. * @param {Action} action - The redux action. Should be either
  238. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  239. * @private
  240. * @returns {void}
  241. */
  242. function _maybePlaySounds({ getState, dispatch }, action) {
  243. const state = getState();
  244. const { startAudioMuted } = state['features/base/config'];
  245. // We're not playing sounds for local participant
  246. // nor when the user is joining past the "startAudioMuted" limit.
  247. // The intention there was to not play user joined notification in big
  248. // conferences where 100th person is joining.
  249. if (!action.participant.local
  250. && (!startAudioMuted
  251. || getParticipantCount(state) < startAudioMuted)) {
  252. if (action.type === PARTICIPANT_JOINED) {
  253. const { presence } = action.participant;
  254. // The sounds for the poltergeist are handled by features/invite.
  255. if (presence !== INVITED && presence !== CALLING) {
  256. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  257. }
  258. } else if (action.type === PARTICIPANT_LEFT) {
  259. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  260. }
  261. }
  262. }
  263. /**
  264. * Notifies the feature base/participants that the action
  265. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  266. * within a specific redux store.
  267. *
  268. * @param {Store} store - The redux store in which the specified {@code action}
  269. * is being dispatched.
  270. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  271. * specified {@code action} in the specified {@code store}.
  272. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  273. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  274. * {@code store}.
  275. * @private
  276. * @returns {Object} The value returned by {@code next(action)}.
  277. */
  278. function _participantJoinedOrUpdated({ getState }, next, action) {
  279. const { participant: { id, local, raisedHand } } = action;
  280. // Send an external update of the local participant's raised hand state
  281. // if a new raised hand state is defined in the action.
  282. if (typeof raisedHand !== 'undefined') {
  283. if (local) {
  284. const { conference } = getState()['features/base/conference'];
  285. conference
  286. && conference.setLocalParticipantProperty(
  287. 'raisedHand',
  288. raisedHand);
  289. }
  290. }
  291. // Notify external listeners of potential avatarURL changes.
  292. if (typeof APP === 'object') {
  293. const oldAvatarURL = getAvatarURLByParticipantId(getState(), id);
  294. // Allow the redux update to go through and compare the old avatar
  295. // to the new avatar and emit out change events if necessary.
  296. const result = next(action);
  297. const newAvatarURL = getAvatarURLByParticipantId(getState(), id);
  298. if (oldAvatarURL !== newAvatarURL) {
  299. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  300. APP.UI.refreshAvatarDisplay(currentKnownId, newAvatarURL);
  301. APP.API.notifyAvatarChanged(currentKnownId, newAvatarURL);
  302. }
  303. return result;
  304. }
  305. return next(action);
  306. }
  307. /**
  308. * Handles a raise hand status update.
  309. *
  310. * @param {Function} dispatch - The Redux dispatch function.
  311. * @param {Object} conference - The conference for which we got an update.
  312. * @param {string?} participantId - The ID of the participant from which we got an update. If undefined,
  313. * we update the local participant.
  314. * @param {boolean} newValue - The new value of the raise hand status.
  315. * @returns {void}
  316. */
  317. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  318. const raisedHand = newValue === 'true';
  319. const pid = participantId || getLocalParticipant(getState()).id;
  320. dispatch(participantUpdated({
  321. conference,
  322. id: pid,
  323. raisedHand
  324. }));
  325. if (raisedHand) {
  326. dispatch(showNotification({
  327. titleArguments: {
  328. name: getParticipantDisplayName(getState, pid)
  329. },
  330. titleKey: 'notify.raisedHand'
  331. }, NOTIFICATION_TIMEOUT));
  332. }
  333. }
  334. /**
  335. * Registers sounds related with the participants feature.
  336. *
  337. * @param {Store} store - The redux store.
  338. * @private
  339. * @returns {void}
  340. */
  341. function _registerSounds({ dispatch }) {
  342. dispatch(
  343. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  344. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  345. }
  346. /**
  347. * Unregisters sounds related with the participants feature.
  348. *
  349. * @param {Store} store - The redux store.
  350. * @private
  351. * @returns {void}
  352. */
  353. function _unregisterSounds({ dispatch }) {
  354. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  355. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  356. }