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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 { participant: { name } } = action;
  99. if (name) {
  100. store.dispatch(showParticipantJoinedNotification(name));
  101. }
  102. return _participantJoinedOrUpdated(store, next, action);
  103. }
  104. case PARTICIPANT_LEFT:
  105. _maybePlaySounds(store, action);
  106. break;
  107. case PARTICIPANT_UPDATED:
  108. return _participantJoinedOrUpdated(store, next, action);
  109. }
  110. return next(action);
  111. });
  112. /**
  113. * Syncs the redux state features/base/participants up with the redux state
  114. * features/base/conference by ensuring that the former does not contain remote
  115. * participants no longer relevant to the latter. Introduced to address an issue
  116. * with multiplying thumbnails in the filmstrip.
  117. */
  118. StateListenerRegistry.register(
  119. /* selector */ state => getCurrentConference(state),
  120. /* listener */ (conference, { dispatch, getState }) => {
  121. for (const p of getState()['features/base/participants']) {
  122. !p.local
  123. && (!conference || p.conference !== conference)
  124. && dispatch(participantLeft(p.id, p.conference));
  125. }
  126. });
  127. /**
  128. * Reset the ID of the local participant to
  129. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  130. * the local participant and, respectively, her ID is not involved in a
  131. * conference which is still of interest to the user and, consequently, the app.
  132. * For example, a conference which is in the process of leaving is no longer of
  133. * interest the user, is unrecoverable from the perspective of the user and,
  134. * consequently, the app.
  135. */
  136. StateListenerRegistry.register(
  137. /* selector */ state => state['features/base/conference'],
  138. /* listener */ ({ leaving }, { dispatch, getState }) => {
  139. const state = getState();
  140. const localParticipant = getLocalParticipant(state);
  141. let id;
  142. if (!localParticipant
  143. || (id = localParticipant.id)
  144. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  145. // The ID of the local participant has been reset already.
  146. return;
  147. }
  148. // The ID of the local may be reset only if it is not in use.
  149. const dispatchLocalParticipantIdChanged
  150. = forEachConference(
  151. state,
  152. conference =>
  153. conference === leaving || conference.myUserId() !== id);
  154. dispatchLocalParticipantIdChanged
  155. && dispatch(
  156. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  157. });
  158. /**
  159. * Registers listeners for participant change events.
  160. */
  161. StateListenerRegistry.register(
  162. state => state['features/base/conference'].conference,
  163. (conference, store) => {
  164. if (conference) {
  165. // We joined a conference
  166. conference.on(
  167. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  168. (participant, propertyName, oldValue, newValue) => {
  169. switch (propertyName) {
  170. case 'features_screen-sharing':
  171. store.dispatch(participantUpdated({
  172. conference,
  173. id: participant.getId(),
  174. features: { 'screen-sharing': true }
  175. }));
  176. break;
  177. case 'raisedHand': {
  178. _raiseHandUpdated(
  179. store, conference, participant.getId(), newValue);
  180. break;
  181. }
  182. default:
  183. // Ignore for now.
  184. }
  185. });
  186. } else {
  187. // We left the conference, raise hand of the local participant must be updated.
  188. _raiseHandUpdated(
  189. store, conference, undefined, false);
  190. }
  191. }
  192. );
  193. /**
  194. * Initializes the local participant and signals that it joined.
  195. *
  196. * @private
  197. * @param {Store} store - The redux store.
  198. * @param {Dispatch} next - The redux dispatch function to dispatch the
  199. * specified action to the specified store.
  200. * @param {Action} action - The redux action which is being dispatched
  201. * in the specified store.
  202. * @private
  203. * @returns {Object} The value returned by {@code next(action)}.
  204. */
  205. function _localParticipantJoined({ getState, dispatch }, next, action) {
  206. const result = next(action);
  207. const settings = getState()['features/base/settings'];
  208. dispatch(localParticipantJoined({
  209. avatarID: settings.avatarID,
  210. avatarURL: settings.avatarURL,
  211. email: settings.email,
  212. name: settings.displayName
  213. }));
  214. return result;
  215. }
  216. /**
  217. * Signals that the local participant has left.
  218. *
  219. * @param {Store} store - The redux store.
  220. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  221. * specified {@code action} into the specified {@code store}.
  222. * @param {Action} action - The redux action which is being dispatched in the
  223. * specified {@code store}.
  224. * @private
  225. * @returns {Object} The value returned by {@code next(action)}.
  226. */
  227. function _localParticipantLeft({ dispatch }, next, action) {
  228. const result = next(action);
  229. dispatch(localParticipantLeft());
  230. return result;
  231. }
  232. /**
  233. * Plays sounds when participants join/leave conference.
  234. *
  235. * @param {Store} store - The redux store.
  236. * @param {Action} action - The redux action. Should be either
  237. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  238. * @private
  239. * @returns {void}
  240. */
  241. function _maybePlaySounds({ getState, dispatch }, action) {
  242. const state = getState();
  243. const { startAudioMuted } = state['features/base/config'];
  244. // We're not playing sounds for local participant
  245. // nor when the user is joining past the "startAudioMuted" limit.
  246. // The intention there was to not play user joined notification in big
  247. // conferences where 100th person is joining.
  248. if (!action.participant.local
  249. && (!startAudioMuted
  250. || getParticipantCount(state) < startAudioMuted)) {
  251. if (action.type === PARTICIPANT_JOINED) {
  252. const { presence } = action.participant;
  253. // The sounds for the poltergeist are handled by features/invite.
  254. if (presence !== INVITED && presence !== CALLING) {
  255. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  256. }
  257. } else if (action.type === PARTICIPANT_LEFT) {
  258. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  259. }
  260. }
  261. }
  262. /**
  263. * Notifies the feature base/participants that the action
  264. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  265. * within a specific redux store.
  266. *
  267. * @param {Store} store - The redux store in which the specified {@code action}
  268. * is being dispatched.
  269. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  270. * specified {@code action} in the specified {@code store}.
  271. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  272. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  273. * {@code store}.
  274. * @private
  275. * @returns {Object} The value returned by {@code next(action)}.
  276. */
  277. function _participantJoinedOrUpdated({ getState }, next, action) {
  278. const { participant: { id, local, raisedHand } } = action;
  279. // Send an external update of the local participant's raised hand state
  280. // if a new raised hand state is defined in the action.
  281. if (typeof raisedHand !== 'undefined') {
  282. if (local) {
  283. const { conference } = getState()['features/base/conference'];
  284. conference
  285. && conference.setLocalParticipantProperty(
  286. 'raisedHand',
  287. raisedHand);
  288. }
  289. }
  290. // Notify external listeners of potential avatarURL changes.
  291. if (typeof APP === 'object') {
  292. const oldAvatarURL = getAvatarURLByParticipantId(getState(), id);
  293. // Allow the redux update to go through and compare the old avatar
  294. // to the new avatar and emit out change events if necessary.
  295. const result = next(action);
  296. const newAvatarURL = getAvatarURLByParticipantId(getState(), id);
  297. if (oldAvatarURL !== newAvatarURL) {
  298. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  299. APP.UI.refreshAvatarDisplay(currentKnownId, newAvatarURL);
  300. APP.API.notifyAvatarChanged(currentKnownId, newAvatarURL);
  301. }
  302. return result;
  303. }
  304. return next(action);
  305. }
  306. /**
  307. * Handles a raise hand status update.
  308. *
  309. * @param {Function} dispatch - The Redux dispatch function.
  310. * @param {Object} conference - The conference for which we got an update.
  311. * @param {string?} participantId - The ID of the participant from which we got an update. If undefined,
  312. * we update the local participant.
  313. * @param {boolean} newValue - The new value of the raise hand status.
  314. * @returns {void}
  315. */
  316. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  317. const raisedHand = newValue === 'true';
  318. const pid = participantId || getLocalParticipant(getState()).id;
  319. dispatch(participantUpdated({
  320. conference,
  321. id: pid,
  322. raisedHand
  323. }));
  324. if (raisedHand) {
  325. dispatch(showNotification({
  326. titleArguments: {
  327. name: getParticipantDisplayName(getState, pid)
  328. },
  329. titleKey: 'notify.raisedHand'
  330. }, NOTIFICATION_TIMEOUT));
  331. }
  332. }
  333. /**
  334. * Registers sounds related with the participants feature.
  335. *
  336. * @param {Store} store - The redux store.
  337. * @private
  338. * @returns {void}
  339. */
  340. function _registerSounds({ dispatch }) {
  341. dispatch(
  342. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  343. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  344. }
  345. /**
  346. * Unregisters sounds related with the participants feature.
  347. *
  348. * @param {Store} store - The redux store.
  349. * @private
  350. * @returns {void}
  351. */
  352. function _unregisterSounds({ dispatch }) {
  353. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  354. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  355. }