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

middleware.js 9.9KB

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