Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.ts 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // @ts-expect-error
  2. import { getJitsiMeetTransport } from '../../../modules/transport';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. DATA_CHANNEL_CLOSED,
  7. DATA_CHANNEL_OPENED,
  8. KICKED_OUT
  9. } from '../base/conference/actionTypes';
  10. import { SET_CONFIG } from '../base/config/actionTypes';
  11. import { NOTIFY_CAMERA_ERROR, NOTIFY_MIC_ERROR } from '../base/devices/actionTypes';
  12. import { JitsiConferenceErrors } from '../base/lib-jitsi-meet';
  13. import {
  14. DOMINANT_SPEAKER_CHANGED,
  15. PARTICIPANT_JOINED,
  16. PARTICIPANT_KICKED,
  17. PARTICIPANT_LEFT,
  18. PARTICIPANT_ROLE_CHANGED,
  19. SET_LOADABLE_AVATAR_URL
  20. } from '../base/participants/actionTypes';
  21. import {
  22. getDominantSpeakerParticipant,
  23. getLocalParticipant,
  24. getParticipantById,
  25. getParticipantDisplayName
  26. } from '../base/participants/functions';
  27. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  28. import { getBaseUrl } from '../base/util/helpers';
  29. import { appendSuffix } from '../display-name/functions';
  30. import { SUBMIT_FEEDBACK_ERROR, SUBMIT_FEEDBACK_SUCCESS } from '../feedback/actionTypes';
  31. import { SET_FILMSTRIP_VISIBLE } from '../filmstrip/actionTypes';
  32. import { iAmVisitor } from '../visitors/functions';
  33. import './subscriber';
  34. /**
  35. * The middleware of the feature {@code external-api}.
  36. *
  37. * @returns {Function}
  38. */
  39. MiddlewareRegistry.register(store => next => action => {
  40. // We need to do these before executing the rest of the middelware chain
  41. switch (action.type) {
  42. case DOMINANT_SPEAKER_CHANGED: {
  43. const dominantSpeaker = getDominantSpeakerParticipant(store.getState());
  44. if (dominantSpeaker?.id !== action.participant.id) {
  45. const result = next(action);
  46. APP.API.notifyDominantSpeakerChanged(action.participant.id);
  47. return result;
  48. }
  49. break;
  50. }
  51. case SET_LOADABLE_AVATAR_URL: {
  52. const { id, loadableAvatarUrl } = action.participant;
  53. const participant = getParticipantById(
  54. store.getState(),
  55. id
  56. );
  57. const result = next(action);
  58. if (participant) {
  59. if (loadableAvatarUrl) {
  60. participant.loadableAvatarUrl !== loadableAvatarUrl && APP.API.notifyAvatarChanged(
  61. id,
  62. loadableAvatarUrl
  63. );
  64. } else {
  65. // There is no loadable explicit URL. In this case the Avatar component would
  66. // decide to render initials or the default avatar, but the external API needs
  67. // a URL when it needs to be rendered, so if there is no initials, we return the default
  68. // Avatar URL as if it was a usual avatar URL. If there are (or may be) initials
  69. // we send undefined to signal the api user that it's not an URL that needs to be rendered.
  70. //
  71. // NOTE: we may implement a special URL format later to signal that the avatar is based
  72. // on initials, that API consumers can handle as they want, e.g. initials://jm
  73. APP.API.notifyAvatarChanged(
  74. id,
  75. participant.name ? undefined : _getDefaultAvatarUrl()
  76. );
  77. }
  78. }
  79. return result;
  80. }
  81. }
  82. const result = next(action);
  83. // These should happen after the rest of the middleware chain ran
  84. switch (action.type) {
  85. case CONFERENCE_FAILED: {
  86. if (action.conference
  87. && action.error.name === JitsiConferenceErrors.PASSWORD_REQUIRED) {
  88. APP.API.notifyOnPasswordRequired();
  89. }
  90. break;
  91. }
  92. case CONFERENCE_JOINED: {
  93. const state = store.getState();
  94. const { defaultLocalDisplayName } = state['features/base/config'];
  95. const { room } = state['features/base/conference'];
  96. const { loadableAvatarUrl, name, id, email } = getLocalParticipant(state) ?? {};
  97. const breakoutRoom = APP.conference.roomName.toString() !== room?.toLowerCase();
  98. // we use APP.conference.roomName as we do not update state['features/base/conference'].room when
  99. // moving between rooms in case of breakout rooms and it stays always with the name of the main room
  100. APP.API.notifyConferenceJoined(
  101. APP.conference.roomName,
  102. id,
  103. {
  104. displayName: name,
  105. formattedDisplayName: appendSuffix(
  106. name ?? '',
  107. defaultLocalDisplayName
  108. ),
  109. avatarURL: loadableAvatarUrl,
  110. breakoutRoom,
  111. email,
  112. visitor: iAmVisitor(state)
  113. }
  114. );
  115. break;
  116. }
  117. case DATA_CHANNEL_CLOSED:
  118. APP.API.notifyDataChannelClosed(action.code, action.reason);
  119. break;
  120. case DATA_CHANNEL_OPENED:
  121. APP.API.notifyDataChannelOpened();
  122. break;
  123. case KICKED_OUT: {
  124. const state = store.getState();
  125. const localParticipant = getLocalParticipant(state);
  126. if (!localParticipant) {
  127. break;
  128. }
  129. const actor = action.participant;
  130. APP.API.notifyKickedOut(
  131. {
  132. id: localParticipant.id,
  133. name: getParticipantDisplayName(state, localParticipant.id),
  134. local: true
  135. },
  136. {
  137. id: actor?.getId(),
  138. name: actor?.getDisplayName()
  139. }
  140. );
  141. break;
  142. }
  143. case NOTIFY_CAMERA_ERROR:
  144. if (action.error) {
  145. APP.API.notifyOnCameraError(
  146. action.error.name, action.error.message);
  147. }
  148. break;
  149. case NOTIFY_MIC_ERROR:
  150. if (action.error) {
  151. APP.API.notifyOnMicError(action.error.name, action.error.message);
  152. }
  153. break;
  154. case PARTICIPANT_KICKED: {
  155. const state = store.getState();
  156. const kickedParticipant = getParticipantById(state, action.kicked);
  157. const kickerParticipant = getParticipantById(state, action.kicker);
  158. if (!kickerParticipant || !kickedParticipant) {
  159. break;
  160. }
  161. APP.API.notifyKickedOut(
  162. {
  163. id: kickedParticipant.id,
  164. local: kickedParticipant.local,
  165. name: getParticipantDisplayName(state, kickedParticipant.id)
  166. },
  167. {
  168. id: kickerParticipant.id,
  169. local: kickerParticipant.local,
  170. name: getParticipantDisplayName(state, kickerParticipant.id)
  171. });
  172. break;
  173. }
  174. case PARTICIPANT_LEFT: {
  175. const { participant } = action;
  176. const { fakeParticipant } = participant;
  177. // Skip sending participant left event for fake participants.
  178. if (fakeParticipant) {
  179. break;
  180. }
  181. APP.API.notifyUserLeft(action.participant.id);
  182. break;
  183. }
  184. case PARTICIPANT_JOINED: {
  185. const state = store.getState();
  186. const { defaultRemoteDisplayName } = state['features/base/config'];
  187. const { participant } = action;
  188. const { fakeParticipant, id, local, name } = participant;
  189. // The version of external api outside of middleware did not emit
  190. // the local participant being created.
  191. if (!local) {
  192. // Skip sending participant joined event for fake participants.
  193. if (fakeParticipant) {
  194. break;
  195. }
  196. APP.API.notifyUserJoined(id, {
  197. displayName: name,
  198. formattedDisplayName: appendSuffix(
  199. name || defaultRemoteDisplayName)
  200. });
  201. }
  202. break;
  203. }
  204. case PARTICIPANT_ROLE_CHANGED:
  205. APP.API.notifyUserRoleChanged(action.participant.id, action.participant.role);
  206. break;
  207. case SET_CONFIG: {
  208. const state = store.getState();
  209. const { disableBeforeUnloadHandlers = false } = state['features/base/config'];
  210. /**
  211. * Disposing the API when the user closes the page.
  212. */
  213. window.addEventListener(disableBeforeUnloadHandlers ? 'unload' : 'beforeunload', () => {
  214. APP.API.notifyConferenceLeft(APP.conference.roomName);
  215. APP.API.dispose();
  216. getJitsiMeetTransport().dispose();
  217. });
  218. break;
  219. }
  220. case SET_FILMSTRIP_VISIBLE:
  221. APP.API.notifyFilmstripDisplayChanged(action.visible);
  222. break;
  223. case SUBMIT_FEEDBACK_ERROR:
  224. APP.API.notifyFeedbackSubmitted(action.error || 'Unknown error');
  225. break;
  226. case SUBMIT_FEEDBACK_SUCCESS:
  227. APP.API.notifyFeedbackSubmitted();
  228. break;
  229. }
  230. return result;
  231. });
  232. /**
  233. * Returns the absolute URL of the default avatar.
  234. *
  235. * @returns {string}
  236. */
  237. function _getDefaultAvatarUrl() {
  238. return new URL('images/avatar.png', getBaseUrl()).href;
  239. }