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 8.6KB

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