page.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = serializePageSecurely(createdPage);
  57. result.tags = savedTags;
  58. return result;
  59. }
  60. async duplicateRecursively(page, newPagePath, user) {
  61. const pageCollection = mongoose.connection.collection('pages');
  62. const revisionCollection = mongoose.connection.collection('revisions');
  63. const Page = this.crowi.model('Page');
  64. const newPagePathPrefix = newPagePath;
  65. const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
  66. const pages = await Page.findManageableListWithDescendants(page, user);
  67. const duplicatePageBulkOp = pageCollection.initializeUnorderedBulkOp();
  68. const revisionPrepareBulkOp = revisionCollection.initializeUnorderedBulkOp();
  69. pages.forEach(async(page) => {
  70. const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
  71. // await page.populate({ path: 'revision', model: 'Revision', select: 'body' }).execPopulate();
  72. duplicatePageBulkOp.insert({
  73. path: newPagePath, body: 'new', creator: user._id, lastUpdateUser: user._id,
  74. });
  75. revisionPrepareBulkOp.insert({
  76. path: newPagePath, body: 'new', author: user._id, format: 'markdown',
  77. });
  78. });
  79. const { result } = await revisionPrepareBulkOp.execute();
  80. console.log(result);
  81. // const promise = pages.map(async(page) => {
  82. // const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
  83. // return this.duplicate(page, newPagePath, user);
  84. // });
  85. const newPath = page.path.replace(pathRegExp, newPagePathPrefix);
  86. // await Promise.allSettled(promise);
  87. const newParentpage = await Page.findByPath(newPath);
  88. // TODO GW-4634 use stream
  89. return newParentpage;
  90. }
  91. }
  92. module.exports = PageService;