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.

113 lines
4.6 KiB

11 months ago
  1. const { getLinkText } = require("../../processLink");
  2. /**
  3. * Fetches the content of a raw link. Returns the content as a text string of the link in question.
  4. * @param {object} data - metadata from document (eg: link)
  5. * @param {import("../../middleware/setDataSigner").ResponseWithSigner} response
  6. */
  7. async function resyncLink({ link }, response) {
  8. if (!link) throw new Error('Invalid link provided');
  9. try {
  10. const { success, content = null } = await getLinkText(link);
  11. if (!success) throw new Error(`Failed to sync link content. ${reason}`);
  12. response.status(200).json({ success, content });
  13. } catch (e) {
  14. console.error(e);
  15. response.status(200).json({
  16. success: false,
  17. content: null,
  18. });
  19. }
  20. }
  21. /**
  22. * Fetches the content of a YouTube link. Returns the content as a text string of the video in question.
  23. * We offer this as there may be some videos where a transcription could be manually edited after initial scraping
  24. * but in general - transcriptions often never change.
  25. * @param {object} data - metadata from document (eg: link)
  26. * @param {import("../../middleware/setDataSigner").ResponseWithSigner} response
  27. */
  28. async function resyncYouTube({ link }, response) {
  29. if (!link) throw new Error('Invalid link provided');
  30. try {
  31. const { fetchVideoTranscriptContent } = require("../../utils/extensions/YoutubeTranscript");
  32. const { success, reason, content } = await fetchVideoTranscriptContent({ url: link });
  33. if (!success) throw new Error(`Failed to sync YouTube video transcript. ${reason}`);
  34. response.status(200).json({ success, content });
  35. } catch (e) {
  36. console.error(e);
  37. response.status(200).json({
  38. success: false,
  39. content: null,
  40. });
  41. }
  42. }
  43. /**
  44. * Fetches the content of a specific confluence page via its chunkSource.
  45. * Returns the content as a text string of the page in question and only that page.
  46. * @param {object} data - metadata from document (eg: chunkSource)
  47. * @param {import("../../middleware/setDataSigner").ResponseWithSigner} response
  48. */
  49. async function resyncConfluence({ chunkSource }, response) {
  50. if (!chunkSource) throw new Error('Invalid source property provided');
  51. try {
  52. // Confluence data is `payload` encrypted. So we need to expand its
  53. // encrypted payload back into query params so we can reFetch the page with same access token/params.
  54. const source = response.locals.encryptionWorker.expandPayload(chunkSource);
  55. const { fetchConfluencePage } = require("../../utils/extensions/Confluence");
  56. const { success, reason, content } = await fetchConfluencePage({
  57. pageUrl: `https:${source.pathname}`, // need to add back the real protocol
  58. baseUrl: source.searchParams.get('baseUrl'),
  59. spaceKey: source.searchParams.get('spaceKey'),
  60. accessToken: source.searchParams.get('token'),
  61. username: source.searchParams.get('username'),
  62. });
  63. if (!success) throw new Error(`Failed to sync Confluence page content. ${reason}`);
  64. response.status(200).json({ success, content });
  65. } catch (e) {
  66. console.error(e);
  67. response.status(200).json({
  68. success: false,
  69. content: null,
  70. });
  71. }
  72. }
  73. /**
  74. * Fetches the content of a specific confluence page via its chunkSource.
  75. * Returns the content as a text string of the page in question and only that page.
  76. * @param {object} data - metadata from document (eg: chunkSource)
  77. * @param {import("../../middleware/setDataSigner").ResponseWithSigner} response
  78. */
  79. async function resyncGithub({ chunkSource }, response) {
  80. if (!chunkSource) throw new Error('Invalid source property provided');
  81. try {
  82. // Github file data is `payload` encrypted (might contain PAT). So we need to expand its
  83. // encrypted payload back into query params so we can reFetch the page with same access token/params.
  84. const source = response.locals.encryptionWorker.expandPayload(chunkSource);
  85. const { fetchGithubFile } = require("../../utils/extensions/RepoLoader/GithubRepo");
  86. const { success, reason, content } = await fetchGithubFile({
  87. repoUrl: `https:${source.pathname}`, // need to add back the real protocol
  88. branch: source.searchParams.get('branch'),
  89. accessToken: source.searchParams.get('pat'),
  90. sourceFilePath: source.searchParams.get('path'),
  91. });
  92. if (!success) throw new Error(`Failed to sync GitHub file content. ${reason}`);
  93. response.status(200).json({ success, content });
  94. } catch (e) {
  95. console.error(e);
  96. response.status(200).json({
  97. success: false,
  98. content: null,
  99. });
  100. }
  101. }
  102. module.exports = {
  103. link: resyncLink,
  104. youtube: resyncYouTube,
  105. confluence: resyncConfluence,
  106. github: resyncGithub,
  107. }