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

middleware.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // @flow
  2. import {
  3. CONFERENCE_FAILED,
  4. CONFERENCE_JOINED,
  5. CONFERENCE_LEFT,
  6. CONFERENCE_WILL_JOIN,
  7. CONFERENCE_WILL_LEAVE,
  8. JITSI_CONFERENCE_URL_KEY,
  9. SET_ROOM,
  10. forEachConference,
  11. isRoomValid
  12. } from '../../base/conference';
  13. import { LOAD_CONFIG_ERROR } from '../../base/config';
  14. import { CONNECTION_FAILED } from '../../base/connection';
  15. import { MiddlewareRegistry } from '../../base/redux';
  16. import { getSymbolDescription, toURLString } from '../../base/util';
  17. import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture';
  18. import { sendEvent } from './functions';
  19. /**
  20. * Middleware that captures Redux actions and uses the ExternalAPI module to
  21. * turn them into native events so the application knows about them.
  22. *
  23. * @param {Store} store - Redux store.
  24. * @returns {Function}
  25. */
  26. MiddlewareRegistry.register(store => next => action => {
  27. const result = next(action);
  28. const { type } = action;
  29. switch (type) {
  30. case CONFERENCE_FAILED: {
  31. const { error, ...data } = action;
  32. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  33. // prevented the user from joining a specific conference but the app may
  34. // be able to eventually join the conference. For example, the app will
  35. // ask the user for a password upon
  36. // JitsiConferenceErrors.PASSWORD_REQUIRED and will retry joining the
  37. // conference afterwards. Such errors are to not reach the native
  38. // counterpart of the External API (or at least not in the
  39. // fatality/finality semantics attributed to
  40. // conferenceFailed:/onConferenceFailed).
  41. if (!error.recoverable) {
  42. _sendConferenceEvent(store, /* action */ {
  43. error: _toErrorString(error),
  44. ...data
  45. });
  46. }
  47. break;
  48. }
  49. case CONFERENCE_JOINED:
  50. case CONFERENCE_LEFT:
  51. case CONFERENCE_WILL_JOIN:
  52. case CONFERENCE_WILL_LEAVE:
  53. _sendConferenceEvent(store, action);
  54. break;
  55. case CONNECTION_FAILED:
  56. !action.error.recoverable
  57. && _sendConferenceFailedOnConnectionError(store, action);
  58. break;
  59. case ENTER_PICTURE_IN_PICTURE:
  60. sendEvent(store, getSymbolDescription(type), /* data */ {});
  61. break;
  62. case LOAD_CONFIG_ERROR: {
  63. const { error, locationURL } = action;
  64. sendEvent(
  65. store,
  66. getSymbolDescription(type),
  67. /* data */ {
  68. error: _toErrorString(error),
  69. url: toURLString(locationURL)
  70. });
  71. break;
  72. }
  73. case SET_ROOM:
  74. _maybeTriggerEarlyConferenceWillJoin(store, action);
  75. break;
  76. }
  77. return result;
  78. });
  79. /**
  80. * Returns a {@code String} representation of a specific error {@code Object}.
  81. *
  82. * @param {Error|Object|string} error - The error {@code Object} to return a
  83. * {@code String} representation of.
  84. * @returns {string} A {@code String} representation of the specified
  85. * {@code error}.
  86. */
  87. function _toErrorString(
  88. error: Error | { message: ?string, name: ?string } | string) {
  89. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  90. // strings, Error instances, and plain objects which resemble Error.
  91. return (
  92. error
  93. ? typeof error === 'string'
  94. ? error
  95. : Error.prototype.toString.apply(error)
  96. : '');
  97. }
  98. /**
  99. * If {@link SET_ROOM} action happens for a valid conference room this method
  100. * will emit an early {@link CONFERENCE_WILL_JOIN} event to let the external API
  101. * know that a conference is being joined. Before that happens a connection must
  102. * be created and only then base/conference feature would emit
  103. * {@link CONFERENCE_WILL_JOIN}. That is fine for the Jitsi Meet app, because
  104. * that's the a conference instance gets created, but it's too late for
  105. * the external API to learn that. The latter {@link CONFERENCE_WILL_JOIN} is
  106. * swallowed in {@link _swallowEvent}.
  107. *
  108. * @param {Store} store - The redux store.
  109. * @param {Action} action - The redux action.
  110. * @returns {void}
  111. */
  112. function _maybeTriggerEarlyConferenceWillJoin(store, action) {
  113. const { locationURL } = store.getState()['features/base/connection'];
  114. const { room } = action;
  115. isRoomValid(room) && locationURL && sendEvent(
  116. store,
  117. getSymbolDescription(CONFERENCE_WILL_JOIN),
  118. /* data */ {
  119. url: toURLString(locationURL)
  120. });
  121. }
  122. /**
  123. * Sends an event to the native counterpart of the External API for a specific
  124. * conference-related redux action.
  125. *
  126. * @param {Store} store - The redux store.
  127. * @param {Action} action - The redux action.
  128. * @returns {void}
  129. */
  130. function _sendConferenceEvent(
  131. store: Object,
  132. action: {
  133. conference: Object,
  134. type: Symbol,
  135. url: ?string
  136. }) {
  137. const { conference, type, ...data } = action;
  138. // For these (redux) actions, conference identifies a JitsiConference
  139. // instance. The external API cannot transport such an object so we have to
  140. // transport an "equivalent".
  141. if (conference) {
  142. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  143. }
  144. _swallowEvent(store, action, data)
  145. || sendEvent(store, getSymbolDescription(type), data);
  146. }
  147. /**
  148. * Sends {@link CONFERENCE_FAILED} event when the {@link CONNECTION_FAILED}
  149. * occurs. It should be done only if the connection fails before the conference
  150. * instance is created. Otherwise the eventual failure event is supposed to be
  151. * emitted by the base/conference feature.
  152. *
  153. * @param {Store} store - The redux store.
  154. * @param {Action} action - The redux action.
  155. * @returns {void}
  156. */
  157. function _sendConferenceFailedOnConnectionError(store, action) {
  158. const { locationURL } = store.getState()['features/base/connection'];
  159. const { connection } = action;
  160. locationURL
  161. && forEachConference(
  162. store,
  163. // If there's any conference in the base/conference state then the
  164. // base/conference feature is supposed to emit a failure.
  165. conference => conference.getConnection() !== connection)
  166. && sendEvent(
  167. store,
  168. getSymbolDescription(CONFERENCE_FAILED),
  169. /* data */ {
  170. url: toURLString(locationURL),
  171. error: action.error.name
  172. });
  173. }
  174. /**
  175. * Determines whether to not send a {@code CONFERENCE_LEFT} event to the native
  176. * counterpart of the External API.
  177. *
  178. * @param {Object} store - The redux store.
  179. * @param {Action} action - The redux action which is causing the sending of the
  180. * event.
  181. * @param {Object} data - The details/specifics of the event to send determined
  182. * by/associated with the specified {@code action}.
  183. * @returns {boolean} If the specified event is to not be sent, {@code true};
  184. * otherwise, {@code false}.
  185. */
  186. function _swallowConferenceLeft({ getState }, action, { url }) {
  187. // XXX Internally, we work with JitsiConference instances. Externally
  188. // though, we deal with URL strings. The relation between the two is many to
  189. // one so it's technically and practically possible (by externally loading
  190. // the same URL string multiple times) to try to send CONFERENCE_LEFT
  191. // externally for a URL string which identifies a JitsiConference that the
  192. // app is internally legitimately working with.
  193. let swallowConferenceLeft = false;
  194. url
  195. && forEachConference(getState, (conference, conferenceURL) => {
  196. if (conferenceURL && conferenceURL.toString() === url) {
  197. swallowConferenceLeft = true;
  198. }
  199. return !swallowConferenceLeft;
  200. });
  201. return swallowConferenceLeft;
  202. }
  203. /**
  204. * Determines whether to not send a specific event to the native counterpart of
  205. * the External API.
  206. *
  207. * @param {Object} store - The redux store.
  208. * @param {Action} action - The redux action which is causing the sending of the
  209. * event.
  210. * @param {Object} data - The details/specifics of the event to send determined
  211. * by/associated with the specified {@code action}.
  212. * @returns {boolean} If the specified event is to not be sent, {@code true};
  213. * otherwise, {@code false}.
  214. */
  215. function _swallowEvent(store, action, data) {
  216. switch (action.type) {
  217. case CONFERENCE_LEFT:
  218. return _swallowConferenceLeft(store, action, data);
  219. case CONFERENCE_WILL_JOIN:
  220. // CONFERENCE_WILL_JOIN is dispatched to the external API on SET_ROOM,
  221. // before the connection is created, so we need to swallow the original
  222. // one emitted by base/conference.
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }