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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @flow
  2. declare var $: Function;
  3. declare var interfaceConfig: Object;
  4. /**
  5. * Get the position of the invite option in the interfaceConfig.INVITE_OPTIONS
  6. * list.
  7. *
  8. * @param {string} name - The invite option name.
  9. * @private
  10. * @returns {number} - The position of the option in the list.
  11. */
  12. export function getInviteOptionPosition(name: string): number {
  13. return interfaceConfig.INVITE_OPTIONS.indexOf(name);
  14. }
  15. /**
  16. * Sends a post request to an invite service.
  17. *
  18. * @param {string} inviteServiceUrl - The invite service that generates the
  19. * invitation.
  20. * @param {string} inviteUrl - The url to the conference.
  21. * @param {string} jwt - The jwt token to pass to the search service.
  22. * @param {Immutable.List} inviteItems - The list of the "user" or "room"
  23. * type items to invite.
  24. * @returns {Promise} - The promise created by the request.
  25. */
  26. export function invitePeopleAndChatRooms( // eslint-disable-line max-params
  27. inviteServiceUrl: string,
  28. inviteUrl: string,
  29. jwt: string,
  30. inviteItems: Object): Promise<void> {
  31. if (!inviteItems || inviteItems.length === 0) {
  32. return Promise.resolve();
  33. }
  34. return new Promise((resolve, reject) => {
  35. $.post(
  36. `${inviteServiceUrl}?token=${jwt}`,
  37. JSON.stringify({
  38. 'invited': inviteItems,
  39. 'url': inviteUrl
  40. }),
  41. resolve,
  42. 'json')
  43. .fail((jqxhr, textStatus, error) => reject(error));
  44. });
  45. }
  46. /**
  47. * Indicates if an invite option is enabled in the configuration.
  48. *
  49. * @param {string} name - The name of the option defined in
  50. * interfaceConfig.INVITE_OPTIONS.
  51. * @returns {boolean} - True to indicate that the given invite option is
  52. * enabled, false - otherwise.
  53. */
  54. export function isInviteOptionEnabled(name: string) {
  55. return getInviteOptionPosition(name) !== -1;
  56. }
  57. /**
  58. * Sends an ajax request to a directory service.
  59. *
  60. * @param {string} serviceUrl - The service to query.
  61. * @param {string} jwt - The jwt token to pass to the search service.
  62. * @param {string} text - Text to search.
  63. * @param {Array<string>} queryTypes - Array with the query types that will be
  64. * executed - "conferenceRooms" | "user" | "room".
  65. * @returns {Promise} - The promise created by the request.
  66. */
  67. export function searchDirectory( // eslint-disable-line max-params
  68. serviceUrl: string,
  69. jwt: string,
  70. text: string,
  71. queryTypes: Array<string> = [ 'conferenceRooms', 'user', 'room' ]
  72. ): Promise<Array<Object>> {
  73. const queryTypesString = JSON.stringify(queryTypes);
  74. return new Promise((resolve, reject) => {
  75. $.getJSON(
  76. `${serviceUrl}?query=${encodeURIComponent(text)}&queryTypes=${
  77. queryTypesString}&jwt=${jwt}`,
  78. resolve)
  79. .catch((jqxhr, textStatus, error) => reject(error));
  80. });
  81. }
  82. /**
  83. * Sends an ajax request to check if the phone number can be called.
  84. *
  85. * @param {string} dialNumber - The dial number to check for validity.
  86. * @param {string} dialOutAuthUrl - The endpoint to use for checking validity.
  87. * @returns {Promise} - The promise created by the request.
  88. */
  89. export function checkDialNumber(
  90. dialNumber: string, dialOutAuthUrl: string): Promise<Object> {
  91. if (!dialOutAuthUrl) {
  92. // no auth url, let's say it is valid
  93. const response = {
  94. allow: true,
  95. phone: `+${dialNumber}`
  96. };
  97. return Promise.resolve(response);
  98. }
  99. const fullUrl = `${dialOutAuthUrl}?phone=${dialNumber}`;
  100. return new Promise((resolve, reject) => {
  101. $.getJSON(fullUrl)
  102. .then(resolve)
  103. .catch(reject);
  104. });
  105. }