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

middleware.js 8.8KB

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