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

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