Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import { getAppProp } from '../../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. isRoomValid
  13. } from '../../base/conference';
  14. import { LOAD_CONFIG_ERROR } from '../../base/config';
  15. import { CONNECTION_FAILED } from '../../base/connection';
  16. import { MiddlewareRegistry } from '../../base/redux';
  17. import { toURLString } from '../../base/util';
  18. import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture';
  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. * Gets the description of a specific {@code Symbol}.
  100. *
  101. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  102. * @private
  103. * @returns {string} The description of {@code symbol}.
  104. */
  105. function _getSymbolDescription(symbol: Symbol) {
  106. let description = symbol.toString();
  107. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  108. description = description.slice(7, -1);
  109. }
  110. // The polyfill es6-symbol that we use does not appear to comply with the
  111. // Symbol standard and, merely, adds @@ at the beginning of the description.
  112. if (description.startsWith('@@')) {
  113. description = description.slice(2);
  114. }
  115. return description;
  116. }
  117. /**
  118. * If {@link SET_ROOM} action happens for a valid conference room this method
  119. * will emit an early {@link CONFERENCE_WILL_JOIN} event to let the external API
  120. * know that a conference is being joined. Before that happens a connection must
  121. * be created and only then base/conference feature would emit
  122. * {@link CONFERENCE_WILL_JOIN}. That is fine for the Jitsi Meet app, because
  123. * that's the a conference instance gets created, but it's too late for
  124. * the external API to learn that. The latter {@link CONFERENCE_WILL_JOIN} is
  125. * swallowed in {@link _swallowEvent}.
  126. *
  127. * @param {Store} store - The redux store.
  128. * @param {Action} action - The redux action.
  129. * @returns {void}
  130. */
  131. function _maybeTriggerEarlyConferenceWillJoin(store, action) {
  132. const { locationURL } = store.getState()['features/base/connection'];
  133. const { room } = action;
  134. isRoomValid(room) && locationURL && _sendEvent(
  135. store,
  136. _getSymbolDescription(CONFERENCE_WILL_JOIN),
  137. /* data */ {
  138. url: toURLString(locationURL)
  139. });
  140. }
  141. /**
  142. * Sends an event to the native counterpart of the External API for a specific
  143. * conference-related redux action.
  144. *
  145. * @param {Store} store - The redux store.
  146. * @param {Action} action - The redux action.
  147. * @returns {void}
  148. */
  149. function _sendConferenceEvent(
  150. store: Object,
  151. action: {
  152. conference: Object,
  153. type: Symbol,
  154. url: ?string
  155. }) {
  156. const { conference, type, ...data } = action;
  157. // For these (redux) actions, conference identifies a JitsiConference
  158. // instance. The external API cannot transport such an object so we have to
  159. // transport an "equivalent".
  160. if (conference) {
  161. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  162. }
  163. _swallowEvent(store, action, data)
  164. || _sendEvent(store, _getSymbolDescription(type), data);
  165. }
  166. /**
  167. * Sends {@link CONFERENCE_FAILED} event when the {@link CONNECTION_FAILED}
  168. * occurs. Otherwise the external API will not emit such event, because at this
  169. * point conference has not been created yet and the base/conference feature
  170. * will not emit it.
  171. *
  172. * @param {Store} store - The redux store.
  173. * @param {Action} action - The redux action.
  174. * @returns {void}
  175. */
  176. function _sendConferenceFailedOnConnectionError(store, action) {
  177. const { locationURL } = store.getState()['features/base/connection'];
  178. locationURL && _sendEvent(
  179. store,
  180. _getSymbolDescription(CONFERENCE_FAILED),
  181. /* data */ {
  182. url: toURLString(locationURL),
  183. error: action.error.name
  184. });
  185. }
  186. /**
  187. * Sends a specific event to the native counterpart of the External API. Native
  188. * apps may listen to such events via the mechanisms provided by the (native)
  189. * mobile Jitsi Meet SDK.
  190. *
  191. * @param {Object} store - The redux store.
  192. * @param {string} name - The name of the event to send.
  193. * @param {Object} data - The details/specifics of the event to send determined
  194. * by/associated with the specified {@code name}.
  195. * @private
  196. * @returns {void}
  197. */
  198. function _sendEvent(store: Object, name: string, data: Object) {
  199. // The JavaScript App needs to provide uniquely identifying information to
  200. // the native ExternalAPI module so that the latter may match the former to
  201. // the native JitsiMeetView which hosts it.
  202. const externalAPIScope = getAppProp(store, 'externalAPIScope');
  203. externalAPIScope
  204. && NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  205. }
  206. /**
  207. * Determines whether to not send a {@code CONFERENCE_LEFT} event to the native
  208. * counterpart of the External API.
  209. *
  210. * @param {Object} store - The redux store.
  211. * @param {Action} action - The redux action which is causing the sending of the
  212. * event.
  213. * @param {Object} data - The details/specifics of the event to send determined
  214. * by/associated with the specified {@code action}.
  215. * @returns {boolean} If the specified event is to not be sent, {@code true};
  216. * otherwise, {@code false}.
  217. */
  218. function _swallowConferenceLeft({ getState }, action, { url }) {
  219. // XXX Internally, we work with JitsiConference instances. Externally
  220. // though, we deal with URL strings. The relation between the two is many to
  221. // one so it's technically and practically possible (by externally loading
  222. // the same URL string multiple times) to try to send CONFERENCE_LEFT
  223. // externally for a URL string which identifies a JitsiConference that the
  224. // app is internally legitimately working with.
  225. if (url) {
  226. const stateFeaturesBaseConference
  227. = getState()['features/base/conference'];
  228. // eslint-disable-next-line guard-for-in
  229. for (const p in stateFeaturesBaseConference) {
  230. const v = stateFeaturesBaseConference[p];
  231. // Does the value of the base/conference's property look like a
  232. // JitsiConference?
  233. if (v && typeof v === 'object') {
  234. const vURL = v[JITSI_CONFERENCE_URL_KEY];
  235. if (vURL && vURL.toString() === url) {
  236. return true;
  237. }
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. /**
  244. * Determines whether to not send a specific event to the native counterpart of
  245. * the External API.
  246. *
  247. * @param {Object} store - The redux store.
  248. * @param {Action} action - The redux action which is causing the sending of the
  249. * event.
  250. * @param {Object} data - The details/specifics of the event to send determined
  251. * by/associated with the specified {@code action}.
  252. * @returns {boolean} If the specified event is to not be sent, {@code true};
  253. * otherwise, {@code false}.
  254. */
  255. function _swallowEvent(store, action, data) {
  256. switch (action.type) {
  257. case CONFERENCE_LEFT:
  258. return _swallowConferenceLeft(store, action, data);
  259. case CONFERENCE_WILL_JOIN:
  260. // CONFERENCE_WILL_JOIN is dispatched to the external API on SET_ROOM,
  261. // before the connection is created, so we need to swallow the original
  262. // one emitted by base/conference.
  263. return true;
  264. default:
  265. return false;
  266. }
  267. }