您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.4KB

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