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.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @flow
  2. import { doGetJSON } from '../base/util';
  3. declare var $: Function;
  4. declare var interfaceConfig: Object;
  5. const logger = require('jitsi-meet-logger').getLogger(__filename);
  6. /**
  7. * Sends a GET request to obtain the conference ID necessary for identifying
  8. * which conference to join after diaing the dial-in service.
  9. *
  10. * @param {string} baseUrl - The url for obtaining the conference ID (pin) for
  11. * dialing into a conference.
  12. * @param {string} roomName - The conference name to find the associated
  13. * conference ID.
  14. * @param {string} mucURL - In which MUC the conference exists.
  15. * @returns {Promise} - The promise created by the request.
  16. */
  17. export function getDialInConferenceID(
  18. baseUrl: string,
  19. roomName: string,
  20. mucURL: string): Promise<Object> {
  21. const conferenceIDURL = `${baseUrl}?conference=${roomName}@${mucURL}`;
  22. return doGetJSON(conferenceIDURL);
  23. }
  24. /**
  25. * Sends a GET request for phone numbers used to dial into a conference.
  26. *
  27. * @param {string} url - The service that returns confernce dial-in numbers.
  28. * @returns {Promise} - The promise created by the request. The returned numbers
  29. * may be an array of numbers or an object with countries as keys and arrays of
  30. * phone number strings.
  31. */
  32. export function getDialInNumbers(url: string): Promise<*> {
  33. return doGetJSON(url);
  34. }
  35. /**
  36. * Get the position of the invite option in the interfaceConfig.INVITE_OPTIONS
  37. * list.
  38. *
  39. * @param {string} name - The invite option name.
  40. * @private
  41. * @returns {number} - The position of the option in the list.
  42. */
  43. export function getInviteOptionPosition(name: string): number {
  44. return interfaceConfig.INVITE_OPTIONS.indexOf(name);
  45. }
  46. /**
  47. * Sends a post request to an invite service.
  48. *
  49. * @param {string} inviteServiceUrl - The invite service that generates the
  50. * invitation.
  51. * @param {string} inviteUrl - The url to the conference.
  52. * @param {string} jwt - The jwt token to pass to the search service.
  53. * @param {Immutable.List} inviteItems - The list of the "user" or "room"
  54. * type items to invite.
  55. * @returns {Promise} - The promise created by the request.
  56. */
  57. export function invitePeopleAndChatRooms( // eslint-disable-line max-params
  58. inviteServiceUrl: string,
  59. inviteUrl: string,
  60. jwt: string,
  61. inviteItems: Array<Object>): Promise<void> {
  62. if (!inviteItems || inviteItems.length === 0) {
  63. return Promise.resolve();
  64. }
  65. return new Promise((resolve, reject) => {
  66. $.post(
  67. `${inviteServiceUrl}?token=${jwt}`,
  68. JSON.stringify({
  69. 'invited': inviteItems,
  70. 'url': inviteUrl
  71. }),
  72. resolve,
  73. 'json')
  74. .fail((jqxhr, textStatus, error) => reject(error));
  75. });
  76. }
  77. /**
  78. * Indicates if an invite option is enabled in the configuration.
  79. *
  80. * @param {string} name - The name of the option defined in
  81. * interfaceConfig.INVITE_OPTIONS.
  82. * @returns {boolean} - True to indicate that the given invite option is
  83. * enabled, false - otherwise.
  84. */
  85. export function isInviteOptionEnabled(name: string) {
  86. return getInviteOptionPosition(name) !== -1;
  87. }
  88. /**
  89. * Sends an ajax request to a directory service.
  90. *
  91. * @param {string} serviceUrl - The service to query.
  92. * @param {string} jwt - The jwt token to pass to the search service.
  93. * @param {string} text - Text to search.
  94. * @param {Array<string>} queryTypes - Array with the query types that will be
  95. * executed - "conferenceRooms" | "user" | "room".
  96. * @returns {Promise} - The promise created by the request.
  97. */
  98. export function searchDirectory( // eslint-disable-line max-params
  99. serviceUrl: string,
  100. jwt: string,
  101. text: string,
  102. queryTypes: Array<string> = [ 'conferenceRooms', 'user', 'room' ]
  103. ): Promise<Array<Object>> {
  104. const queryTypesString = JSON.stringify(queryTypes);
  105. return fetch(`${serviceUrl}?query=${encodeURIComponent(text)}&queryTypes=${
  106. queryTypesString}&jwt=${jwt}`)
  107. .then(response => {
  108. const jsonify = response.json();
  109. if (response.ok) {
  110. return jsonify;
  111. }
  112. return jsonify
  113. .then(result => Promise.reject(result));
  114. })
  115. .catch(error => {
  116. logger.error(
  117. 'Error searching directory:', error);
  118. return Promise.reject(error);
  119. });
  120. }
  121. /**
  122. * Sends an ajax request to check if the phone number can be called.
  123. *
  124. * @param {string} dialNumber - The dial number to check for validity.
  125. * @param {string} dialOutAuthUrl - The endpoint to use for checking validity.
  126. * @returns {Promise} - The promise created by the request.
  127. */
  128. export function checkDialNumber(
  129. dialNumber: string, dialOutAuthUrl: string): Promise<Object> {
  130. if (!dialOutAuthUrl) {
  131. // no auth url, let's say it is valid
  132. const response = {
  133. allow: true,
  134. phone: `+${dialNumber}`
  135. };
  136. return Promise.resolve(response);
  137. }
  138. const fullUrl = `${dialOutAuthUrl}?phone=${dialNumber}`;
  139. return new Promise((resolve, reject) => {
  140. $.getJSON(fullUrl)
  141. .then(resolve)
  142. .catch(reject);
  143. });
  144. }