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.web.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @flow
  2. import { Dropbox } from 'dropbox';
  3. import { parseURLParams } from '../base/config';
  4. import {
  5. getJitsiMeetGlobalNS,
  6. parseStandardURIString
  7. } from '../base/util';
  8. /**
  9. * Executes the oauth flow.
  10. *
  11. * @param {string} authUrl - The URL to oauth service.
  12. * @returns {Promise<string>} - The URL with the authorization details.
  13. */
  14. function authorize(authUrl: string): Promise<string> {
  15. const windowName = `oauth${Date.now()}`;
  16. const gloabalNS = getJitsiMeetGlobalNS();
  17. gloabalNS.oauthCallbacks = gloabalNS.oauthCallbacks || {};
  18. return new Promise(resolve => {
  19. const popup = window.open(authUrl, windowName);
  20. gloabalNS.oauthCallbacks[windowName] = () => {
  21. const returnURL = popup.location.href;
  22. popup.close();
  23. delete gloabalNS.oauthCallbacks.windowName;
  24. resolve(returnURL);
  25. };
  26. });
  27. }
  28. /**
  29. * Action to authorize the Jitsi Recording app in dropbox.
  30. *
  31. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  32. * @param {string} redirectURI - The return URL.
  33. * @returns {Promise<string>}
  34. */
  35. export function _authorizeDropbox(
  36. appKey: string,
  37. redirectURI: string
  38. ): Promise<string> {
  39. const dropboxAPI = new Dropbox({ clientId: appKey });
  40. const url = dropboxAPI.getAuthenticationUrl(redirectURI);
  41. return authorize(url).then(returnUrl => {
  42. const params
  43. = parseURLParams(parseStandardURIString(returnUrl), true) || {};
  44. return params.access_token;
  45. });
  46. }
  47. /**
  48. * Returns the display name for the current dropbox account.
  49. *
  50. * @param {string} token - The dropbox access token.
  51. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  52. * @returns {Promise<string>}
  53. */
  54. export function getDisplayName(token: string, appKey: string) {
  55. const dropboxAPI = new Dropbox({
  56. accessToken: token,
  57. clientId: appKey
  58. });
  59. return (
  60. dropboxAPI.usersGetCurrentAccount()
  61. .then(account => account.name.display_name));
  62. }
  63. /**
  64. * Returns information about the space usage for the current dropbox account.
  65. *
  66. * @param {string} token - The dropbox access token.
  67. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  68. * @returns {Promise<Object>}
  69. */
  70. export function getSpaceUsage(token: string, appKey: string) {
  71. const dropboxAPI = new Dropbox({
  72. accessToken: token,
  73. clientId: appKey
  74. });
  75. return dropboxAPI.usersGetSpaceUsage().then(space => {
  76. const { allocation, used } = space;
  77. const { allocated } = allocation;
  78. return {
  79. allocated,
  80. used
  81. };
  82. });
  83. }
  84. /**
  85. * Returns <tt>true</tt> if the dropbox features is enabled and <tt>false</tt>
  86. * otherwise.
  87. *
  88. * @param {Object} state - The redux state.
  89. * @returns {boolean}
  90. */
  91. export function isEnabled(state: Object) {
  92. const { dropbox = {} } = state['features/base/config'];
  93. return typeof dropbox.appKey === 'string';
  94. }