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.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // @flow
  2. import {
  3. CONFERENCE_FAILED,
  4. CONFERENCE_JOINED,
  5. KICKED_OUT
  6. } from '../base/conference';
  7. import { NOTIFY_CAMERA_ERROR, NOTIFY_MIC_ERROR } from '../base/devices';
  8. import { JitsiConferenceErrors } from '../base/lib-jitsi-meet';
  9. import {
  10. DOMINANT_SPEAKER_CHANGED,
  11. PARTICIPANT_KICKED,
  12. PARTICIPANT_LEFT,
  13. PARTICIPANT_JOINED,
  14. PARTICIPANT_ROLE_CHANGED,
  15. SET_LOADABLE_AVATAR_URL,
  16. getLocalParticipant,
  17. getParticipantById
  18. } from '../base/participants';
  19. import { MiddlewareRegistry } from '../base/redux';
  20. import { getBaseUrl } from '../base/util';
  21. import { appendSuffix } from '../display-name';
  22. import { SUBMIT_FEEDBACK_ERROR, SUBMIT_FEEDBACK_SUCCESS } from '../feedback';
  23. import { SET_FILMSTRIP_VISIBLE } from '../filmstrip';
  24. import './subscriber';
  25. declare var APP: Object;
  26. declare var interfaceConfig: Object;
  27. /**
  28. * The middleware of the feature {@code external-api}.
  29. *
  30. * @returns {Function}
  31. */
  32. MiddlewareRegistry.register(store => next => action => {
  33. // We need to do these before executing the rest of the middelware chain
  34. switch (action.type) {
  35. case SET_LOADABLE_AVATAR_URL: {
  36. const { id, loadableAvatarUrl } = action.participant;
  37. const participant = getParticipantById(
  38. store.getState(),
  39. id
  40. );
  41. const result = next(action);
  42. if (participant) {
  43. if (loadableAvatarUrl) {
  44. participant.loadableAvatarUrl !== loadableAvatarUrl && APP.API.notifyAvatarChanged(
  45. id,
  46. loadableAvatarUrl
  47. );
  48. } else {
  49. // There is no loadable explicit URL. In this case the Avatar component would
  50. // decide to render initials or the default avatar, but the external API needs
  51. // a URL when it needs to be rendered, so if there is no initials, we return the default
  52. // Avatar URL as if it was a usual avatar URL. If there are (or may be) initials
  53. // we send undefined to signal the api user that it's not an URL that needs to be rendered.
  54. //
  55. // NOTE: we may implement a special URL format later to signal that the avatar is based
  56. // on initials, that API consumers can handle as they want, e.g. initials://jm
  57. APP.API.notifyAvatarChanged(
  58. id,
  59. participant.name ? undefined : _getDefaultAvatarUrl()
  60. );
  61. }
  62. }
  63. return result;
  64. }
  65. }
  66. const result = next(action);
  67. // These should happen after the rest of the middleware chain ran
  68. switch (action.type) {
  69. case CONFERENCE_FAILED: {
  70. if (action.conference
  71. && action.error.name === JitsiConferenceErrors.PASSWORD_REQUIRED) {
  72. APP.API.notifyOnPasswordRequired();
  73. }
  74. break;
  75. }
  76. case CONFERENCE_JOINED: {
  77. const state = store.getState();
  78. const { room } = state['features/base/conference'];
  79. const { loadableAvatarUrl, name, id } = getLocalParticipant(state);
  80. APP.API.notifyConferenceJoined(
  81. room,
  82. id,
  83. {
  84. displayName: name,
  85. formattedDisplayName: appendSuffix(
  86. name,
  87. interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  88. ),
  89. avatarURL: loadableAvatarUrl
  90. }
  91. );
  92. break;
  93. }
  94. case DOMINANT_SPEAKER_CHANGED:
  95. APP.API.notifyDominantSpeakerChanged(action.participant.id);
  96. break;
  97. case KICKED_OUT:
  98. APP.API.notifyKickedOut(
  99. {
  100. id: getLocalParticipant(store.getState()).id,
  101. local: true
  102. },
  103. { id: action.participant ? action.participant.getId() : undefined }
  104. );
  105. break;
  106. case NOTIFY_CAMERA_ERROR:
  107. if (action.error) {
  108. APP.API.notifyOnCameraError(
  109. action.error.name, action.error.message);
  110. }
  111. break;
  112. case NOTIFY_MIC_ERROR:
  113. if (action.error) {
  114. APP.API.notifyOnMicError(action.error.name, action.error.message);
  115. }
  116. break;
  117. case PARTICIPANT_KICKED:
  118. APP.API.notifyKickedOut(
  119. {
  120. id: action.kicked,
  121. local: false
  122. },
  123. { id: action.kicker });
  124. break;
  125. case PARTICIPANT_LEFT:
  126. APP.API.notifyUserLeft(action.participant.id);
  127. break;
  128. case PARTICIPANT_JOINED: {
  129. const { participant } = action;
  130. const { id, local, name } = participant;
  131. // The version of external api outside of middleware did not emit
  132. // the local participant being created.
  133. if (!local) {
  134. APP.API.notifyUserJoined(id, {
  135. displayName: name,
  136. formattedDisplayName: appendSuffix(
  137. name || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME)
  138. });
  139. }
  140. break;
  141. }
  142. case PARTICIPANT_ROLE_CHANGED:
  143. APP.API.notifyUserRoleChanged(action.participant.id, action.participant.role);
  144. break;
  145. case SET_FILMSTRIP_VISIBLE:
  146. APP.API.notifyFilmstripDisplayChanged(action.visible);
  147. break;
  148. case SUBMIT_FEEDBACK_ERROR:
  149. APP.API.notifyFeedbackSubmitted(action.error || 'Unknown error');
  150. break;
  151. case SUBMIT_FEEDBACK_SUCCESS:
  152. APP.API.notifyFeedbackSubmitted();
  153. break;
  154. }
  155. return result;
  156. });
  157. /**
  158. * Returns the absolute URL of the default avatar.
  159. *
  160. * @returns {string}
  161. */
  162. function _getDefaultAvatarUrl() {
  163. return new URL('images/avatar.png', getBaseUrl()).href;
  164. }