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.8KB

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