Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

middleware.js 9.4KB

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