Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.web.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // @flow
  2. import { Dropbox, DropboxAuth } from 'dropbox';
  3. import { getJitsiMeetGlobalNS } from '../base/util';
  4. /**
  5. * Executes the oauth flow.
  6. *
  7. * @param {string} authUrl - The URL to oauth service.
  8. * @returns {Promise<string>} - The URL with the authorization details.
  9. */
  10. function authorize(authUrl: string): Promise<string> {
  11. const windowName = `oauth${Date.now()}`;
  12. const gloabalNS = getJitsiMeetGlobalNS();
  13. gloabalNS.oauthCallbacks = gloabalNS.oauthCallbacks || {};
  14. return new Promise(resolve => {
  15. const popup = window.open(authUrl, windowName);
  16. gloabalNS.oauthCallbacks[windowName] = url => {
  17. popup.close();
  18. delete gloabalNS.oauthCallbacks.windowName;
  19. resolve(url);
  20. };
  21. });
  22. }
  23. /**
  24. * Returns the token's expiry date as UNIX timestamp.
  25. *
  26. * @param {number} expiresIn - The seconds in which the token expires.
  27. * @returns {number} - The timestamp value for the expiry date.
  28. */
  29. function getTokenExpiresAtTimestamp(expiresIn: number) {
  30. return new Date(Date.now() + (expiresIn * 1000)).getTime();
  31. }
  32. /**
  33. * Action to authorize the Jitsi Recording app in dropbox.
  34. *
  35. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  36. * @param {string} redirectURI - The return URL.
  37. * @returns {Promise<Object>}
  38. */
  39. export function _authorizeDropbox(
  40. appKey: string,
  41. redirectURI: string
  42. ): Promise<Object> {
  43. const dropbox = new DropboxAuth({ clientId: appKey });
  44. return dropbox.getAuthenticationUrl(redirectURI, undefined, 'code', 'offline', undefined, undefined, true)
  45. .then(authorize)
  46. .then(returnUrl => {
  47. const params = new URLSearchParams(new URL(returnUrl).search);
  48. const code = params.get('code');
  49. return dropbox.getAccessTokenFromCode(redirectURI, code);
  50. })
  51. .then(resp => {
  52. return {
  53. token: resp.result.access_token,
  54. rToken: resp.result.refresh_token,
  55. expireDate: getTokenExpiresAtTimestamp(resp.result.expires_in)
  56. };
  57. });
  58. }
  59. /**
  60. * Gets a new acccess token based on the refresh token.
  61. *
  62. * @param {string} appKey - The dropbox appKey.
  63. * @param {string} rToken - The refresh token.
  64. * @returns {Promise}
  65. */
  66. export function getNewAccessToken(appKey: string, rToken: string) {
  67. const dropbox = new DropboxAuth({ clientId: appKey });
  68. dropbox.setRefreshToken(rToken);
  69. return dropbox.refreshAccessToken()
  70. .then(() => {
  71. return {
  72. token: dropbox.getAccessToken(),
  73. rToken: dropbox.getRefreshToken(),
  74. expireDate: dropbox.getAccessTokenExpiresAt().getTime()
  75. };
  76. });
  77. }
  78. /**
  79. * Returns the display name for the current dropbox account.
  80. *
  81. * @param {string} token - The dropbox access token.
  82. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  83. * @returns {Promise<string>}
  84. */
  85. export function getDisplayName(token: string, appKey: string) {
  86. const dropboxAPI = new Dropbox({
  87. accessToken: token,
  88. clientId: appKey
  89. });
  90. return (
  91. dropboxAPI.usersGetCurrentAccount()
  92. .then(account => account.result.name.display_name));
  93. }
  94. /**
  95. * Returns information about the space usage for the current dropbox account.
  96. *
  97. * @param {string} token - The dropbox access token.
  98. * @param {string} appKey - The Jitsi Recorder dropbox app key.
  99. * @returns {Promise<Object>}
  100. */
  101. export function getSpaceUsage(token: string, appKey: string) {
  102. const dropboxAPI = new Dropbox({
  103. accessToken: token,
  104. clientId: appKey
  105. });
  106. return dropboxAPI.usersGetSpaceUsage().then(space => {
  107. const { allocation, used } = space.result;
  108. const { allocated } = allocation;
  109. return {
  110. allocated,
  111. used
  112. };
  113. });
  114. }
  115. /**
  116. * Returns <tt>true</tt> if the dropbox features is enabled and <tt>false</tt>
  117. * otherwise.
  118. *
  119. * @param {Object} state - The redux state.
  120. * @returns {boolean}
  121. */
  122. export function isEnabled(state: Object) {
  123. const { dropbox = {} } = state['features/base/config'];
  124. return typeof dropbox.appKey === 'string';
  125. }