您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.web.ts 4.4KB

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