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.6KB

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