page.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const mongoose = require('mongoose');
  2. const escapeStringRegexp = require('escape-string-regexp');
  3. const { serializePageSecurely } = require('../models/serializers/page-serializer');
  4. class PageService {
  5. constructor(crowi) {
  6. this.crowi = crowi;
  7. }
  8. async deleteCompletely(pageId, pagePath) {
  9. // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
  10. const Bookmark = this.crowi.model('Bookmark');
  11. const Comment = this.crowi.model('Comment');
  12. const Page = this.crowi.model('Page');
  13. const PageTagRelation = this.crowi.model('PageTagRelation');
  14. const ShareLink = this.crowi.model('ShareLink');
  15. const Revision = this.crowi.model('Revision');
  16. return Promise.all([
  17. Bookmark.removeBookmarksByPageId(pageId),
  18. Comment.removeCommentsByPageId(pageId),
  19. PageTagRelation.remove({ relatedPage: pageId }),
  20. ShareLink.remove({ relatedPage: pageId }),
  21. Revision.removeRevisionsByPath(pagePath),
  22. Page.findByIdAndRemove(pageId),
  23. Page.removeRedirectOriginPageByPath(pagePath),
  24. this.removeAllAttachments(pageId),
  25. ]);
  26. }
  27. async removeAllAttachments(pageId) {
  28. const Attachment = this.crowi.model('Attachment');
  29. const { attachmentService } = this.crowi;
  30. const attachments = await Attachment.find({ page: pageId });
  31. const promises = attachments.map(async(attachment) => {
  32. return attachmentService.removeAttachment(attachment._id);
  33. });
  34. return Promise.all(promises);
  35. }
  36. async duplicate(page, newPagePath, user) {
  37. const Page = this.crowi.model('Page');
  38. const PageTagRelation = mongoose.model('PageTagRelation');
  39. // populate
  40. await page.populate({ path: 'revision', model: 'Revision', select: 'body' }).execPopulate();
  41. // create option
  42. const options = { page };
  43. options.grant = page.grant;
  44. options.grantUserGroupId = page.grantedGroup;
  45. options.grantedUsers = page.grantedUsers;
  46. const createdPage = await Page.create(
  47. newPagePath, page.revision.body, user, options,
  48. );
  49. // take over tags
  50. const originTags = await page.findRelatedTagsById();
  51. let savedTags = [];
  52. if (originTags != null) {
  53. await PageTagRelation.updatePageTags(createdPage.id, originTags);
  54. savedTags = await PageTagRelation.listTagNamesByPage(createdPage.id);
  55. }
  56. const result = [{ page: serializePageSecurely(createdPage), tags: savedTags }];
  57. return result;
  58. }
  59. async duplicateRecursively(page, newPagePath, user) {
  60. const Page = this.crowi.model('Page');
  61. const newPagePathPrefix = newPagePath;
  62. const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
  63. const pages = await Page.findManageableListWithDescendants(page, user);
  64. const promise = pages.map(async(page) => {
  65. const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
  66. return this.duplicate(page, newPagePath, user);
  67. });
  68. const promiseResults = await Promise.allSettled(promise);
  69. const result = promiseResults.map((promiseResult) => {
  70. return promiseResult.value[0];
  71. });
  72. // TODO GW-4634 use stream
  73. return result;
  74. }
  75. }
  76. module.exports = PageService;