You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.native.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { isRoomValid } from '../base/conference';
  2. import { RouteRegistry } from '../base/react';
  3. import { Conference } from '../conference';
  4. import { WelcomePage } from '../welcome';
  5. /**
  6. * The RegExp pattern of the authority of a URI.
  7. *
  8. * @private
  9. * @type {string}
  10. */
  11. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  12. /**
  13. * The RegExp pattern of the path of a URI.
  14. *
  15. * @private
  16. * @type {string}
  17. */
  18. const _URI_PATH_PATTERN = '([^?#]*)';
  19. /**
  20. * The RegExp patther of the protocol of a URI.
  21. *
  22. * @private
  23. * @type {string}
  24. */
  25. const _URI_PROTOCOL_PATTERN = '([a-z][a-z0-9\\.\\+-]*:)';
  26. /**
  27. * Fixes the hier-part of a specific URI (string) so that the URI is well-known.
  28. * For example, certain Jitsi Meet deployments are not conventional but it is
  29. * possible to translate their URLs into conventional.
  30. *
  31. * @param {string} uri - The URI (string) to fix the hier-part of.
  32. * @private
  33. * @returns {string}
  34. */
  35. function _fixURIStringHierPart(uri) {
  36. // Rewrite the specified URL in order to handle special cases such as
  37. // hipchat.com and enso.me which do not follow the common pattern of most
  38. // Jitsi Meet deployments.
  39. // hipchat.com
  40. let regex
  41. = new RegExp(
  42. `^${_URI_PROTOCOL_PATTERN}//hipchat\\.com/video/call/`,
  43. 'gi');
  44. let match = regex.exec(uri);
  45. if (!match) {
  46. // enso.me
  47. regex
  48. = new RegExp(
  49. `^${_URI_PROTOCOL_PATTERN}//enso\\.me/(?:call|meeting)/`,
  50. 'gi');
  51. match = regex.exec(uri);
  52. }
  53. if (match) {
  54. /* eslint-disable no-param-reassign, prefer-template */
  55. uri
  56. = match[1] /* protocol */
  57. + '//enso.hipchat.me/'
  58. + uri.substring(regex.lastIndex); /* room (name) */
  59. /* eslint-enable no-param-reassign, prefer-template */
  60. }
  61. return uri;
  62. }
  63. /**
  64. * Fixes the scheme part of a specific URI (string) so that it contains a
  65. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  66. * app-specific URI scheme in addition to Universal Links. The app-specific
  67. * scheme may precede or replace the well-known scheme. In such a case, dealing
  68. * with the app-specific scheme only complicates the logic and it is simpler to
  69. * get rid of it (by translating the app-specific scheme into a well-known
  70. * scheme).
  71. *
  72. * @param {string} uri - The URI (string) to fix the scheme of.
  73. * @private
  74. * @returns {string}
  75. */
  76. function _fixURIStringScheme(uri) {
  77. const regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}+`, 'gi');
  78. const match = regex.exec(uri);
  79. if (match) {
  80. // As an implementation convenience, pick up the last scheme and make
  81. // sure that it is a well-known one.
  82. let protocol = match[match.length - 1].toLowerCase();
  83. if (protocol !== 'http:' && protocol !== 'https:') {
  84. protocol = 'https:';
  85. }
  86. /* eslint-disable no-param-reassign */
  87. uri = uri.substring(regex.lastIndex);
  88. if (uri.startsWith('//')) {
  89. // The specified URL was not a room name only, it contained an
  90. // authority.
  91. uri = protocol + uri;
  92. }
  93. /* eslint-enable no-param-reassign */
  94. }
  95. return uri;
  96. }
  97. /**
  98. * Gets the (Web application) context root defined by a specific location (URI).
  99. *
  100. * @param {Object} location - The location (URI) which defines the (Web
  101. * application) context root.
  102. * @returns {string} - The (Web application) context root defined by the
  103. * specified {@code location} (URI).
  104. */
  105. export function getLocationContextRoot(location: Object) {
  106. const pathname = location.pathname;
  107. const contextRootEndIndex = pathname.lastIndexOf('/');
  108. return (
  109. contextRootEndIndex === -1
  110. ? '/'
  111. : pathname.substring(0, contextRootEndIndex + 1));
  112. }
  113. /**
  114. * Determines which route is to be rendered in order to depict a specific Redux
  115. * store.
  116. *
  117. * @param {(Object|Function)} stateOrGetState - Redux state or Regux getState()
  118. * method.
  119. * @returns {Route}
  120. */
  121. export function _getRouteToRender(stateOrGetState) {
  122. const state
  123. = typeof stateOrGetState === 'function'
  124. ? stateOrGetState()
  125. : stateOrGetState;
  126. const { room } = state['features/base/conference'];
  127. let component;
  128. if (isRoomValid(room)) {
  129. component = Conference;
  130. } else {
  131. // The value of the App prop welcomePageEnabled was stored in redux in
  132. // saghul's PR. But I removed the redux state, action, action type, etc.
  133. // because I didn't like the name. We are not using the prop is a
  134. // React-ive way anyway so it's all the same difference.
  135. const { app } = state['features/app'];
  136. component = app && app.props.welcomePageEnabled ? WelcomePage : null;
  137. }
  138. return RouteRegistry.getRouteByComponent(component);
  139. }
  140. /**
  141. * Parses a specific URI string into an object with the well-known properties of
  142. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  143. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  144. *
  145. * @param {string} str - The URI string to parse.
  146. * @returns {{
  147. * hash: string,
  148. * host: (string|undefined),
  149. * hostname: (string|undefined),
  150. * pathname: string,
  151. * port: (string|undefined),
  152. * protocol: (string|undefined),
  153. * search: string
  154. * }}
  155. */
  156. function _parseStandardURIString(str: string) {
  157. /* eslint-disable no-param-reassign */
  158. const obj = {};
  159. let regex;
  160. let match;
  161. // protocol
  162. regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}`, 'gi');
  163. match = regex.exec(str);
  164. if (match) {
  165. obj.protocol = match[1].toLowerCase();
  166. str = str.substring(regex.lastIndex);
  167. }
  168. // authority
  169. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  170. match = regex.exec(str);
  171. if (match) {
  172. let authority = match[1].substring(/* // */ 2);
  173. str = str.substring(regex.lastIndex);
  174. // userinfo
  175. const userinfoEndIndex = authority.indexOf('@');
  176. if (userinfoEndIndex !== -1) {
  177. authority = authority.substring(userinfoEndIndex + 1);
  178. }
  179. obj.host = authority;
  180. // port
  181. const portBeginIndex = authority.lastIndexOf(':');
  182. if (portBeginIndex !== -1) {
  183. obj.port = authority.substring(portBeginIndex + 1);
  184. authority = authority.substring(0, portBeginIndex);
  185. }
  186. // hostname
  187. obj.hostname = authority;
  188. }
  189. // pathname
  190. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  191. match = regex.exec(str);
  192. let pathname;
  193. if (match) {
  194. pathname = match[1];
  195. str = str.substring(regex.lastIndex);
  196. }
  197. if (pathname) {
  198. if (!pathname.startsWith('/')) {
  199. pathname = `/${pathname}`;
  200. }
  201. } else {
  202. pathname = '/';
  203. }
  204. obj.pathname = pathname;
  205. // query
  206. if (str.startsWith('?')) {
  207. let hashBeginIndex = str.indexOf('#', 1);
  208. if (hashBeginIndex === -1) {
  209. hashBeginIndex = str.length;
  210. }
  211. obj.search = str.substring(0, hashBeginIndex);
  212. str = str.substring(hashBeginIndex);
  213. } else {
  214. obj.search = ''; // Google Chrome
  215. }
  216. // fragment
  217. obj.hash = str.startsWith('#') ? str : '';
  218. /* eslint-enable no-param-reassign */
  219. return obj;
  220. }
  221. /**
  222. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  223. * (location).
  224. *
  225. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  226. * references a Jitsi Meet resource (location).
  227. * @returns {{
  228. * room: (string|undefined)
  229. * }}
  230. */
  231. export function _parseURIString(uri: ?string) {
  232. if (typeof uri !== 'string') {
  233. return undefined;
  234. }
  235. const obj
  236. = _parseStandardURIString(
  237. _fixURIStringHierPart(_fixURIStringScheme(uri)));
  238. // Add the properties that are specific to a Jitsi Meet resource (location)
  239. // such as contextRoot, room:
  240. // contextRoot
  241. obj.contextRoot = getLocationContextRoot(obj);
  242. // The room (name) is the last component of pathname.
  243. const { pathname } = obj;
  244. obj.room = pathname.substring(pathname.lastIndexOf('/') + 1) || undefined;
  245. return obj;
  246. }