page.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const mongoose = require('mongoose');
  2. const escapeStringRegexp = require('escape-string-regexp');
  3. const logger = require('@alias/logger')('growi:models:page');
  4. const debug = require('debug')('growi:models:page');
  5. const { serializePageSecurely } = require('../models/serializers/page-serializer');
  6. const STATUS_PUBLISHED = 'published';
  7. class PageService {
  8. constructor(crowi) {
  9. this.crowi = crowi;
  10. }
  11. async deleteCompletely(pageIds, pagePaths) {
  12. // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
  13. const Bookmark = this.crowi.model('Bookmark');
  14. const Comment = this.crowi.model('Comment');
  15. const Page = this.crowi.model('Page');
  16. const PageTagRelation = this.crowi.model('PageTagRelation');
  17. const ShareLink = this.crowi.model('ShareLink');
  18. const Revision = this.crowi.model('Revision');
  19. await Promise.all([
  20. Bookmark.find({ page: { $in: pageIds } }).remove({}),
  21. Comment.find({ page: { $in: pageIds } }).remove({}),
  22. PageTagRelation.find({ relatedPage: { $in: pageIds } }).remove({}),
  23. ShareLink.find({ relatedPage: { $in: pageIds } }).remove({}),
  24. Revision.find({ path: { $in: pagePaths } }).remove({}),
  25. Page.find({ _id: { $in: pageIds } }).remove({}),
  26. Page.find({ path: { $in: pagePaths } }).remove({}),
  27. this.removeAllAttachments(pageIds),
  28. ]);
  29. }
  30. async removeAllAttachments(pageIds) {
  31. const Attachment = this.crowi.model('Attachment');
  32. const { attachmentService } = this.crowi;
  33. const attachments = await Attachment.find({ page: { $in: pageIds } });
  34. attachmentService.removeAttachment(attachments);
  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 Page = this.crowi.model('Page');
  62. const newPagePathPrefix = newPagePath;
  63. const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
  64. const pages = await Page.findManageableListWithDescendants(page, user);
  65. const promise = pages.map(async(page) => {
  66. const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
  67. return this.duplicate(page, newPagePath, user);
  68. });
  69. const newPath = page.path.replace(pathRegExp, newPagePathPrefix);
  70. await Promise.allSettled(promise);
  71. const newParentpage = await Page.findByPath(newPath);
  72. // TODO GW-4634 use stream
  73. return newParentpage;
  74. }
  75. async completelyDeletePage(pagesData, user, options = {}) {
  76. this.validateCrowi();
  77. let pageEvent;
  78. // init event
  79. if (this.crowi != null) {
  80. pageEvent = this.crowi.event('page');
  81. pageEvent.on('create', pageEvent.onCreate);
  82. pageEvent.on('update', pageEvent.onUpdate);
  83. }
  84. const ids = pagesData.map(page => (page._id));
  85. const paths = pagesData.map(page => (page.path));
  86. const socketClientId = options.socketClientId || null;
  87. logger.debug('Deleting completely', paths);
  88. await this.deleteCompletely(ids, paths);
  89. if (socketClientId != null) {
  90. pageEvent.emit('delete', pagesData, user, socketClientId); // update as renamed page
  91. }
  92. }
  93. /**
  94. * Delete Bookmarks, Attachments, Revisions, Pages and emit delete
  95. */
  96. async completelyDeletePageRecursively(targetPage, user, options = {}) {
  97. const findOpts = { includeTrashed: true };
  98. const Page = this.crowi.model('Page');
  99. // find manageable descendants (this array does not include GRANT_RESTRICTED)
  100. const pages = await Page.findManageableListWithDescendants(targetPage, user, findOpts);
  101. // TODO streaming bellow action
  102. await this.completelyDeletePage(pages, user, options);
  103. }
  104. async revertDeletedPage(page, user, options = {}) {
  105. const Page = this.crowi.model('Page');
  106. const newPath = Page.getRevertDeletedPageName(page.path);
  107. const originPage = await Page.findByPath(newPath);
  108. if (originPage != null) {
  109. // When the page is deleted, it will always be created with "redirectTo" in the path of the original page.
  110. // So, it's ok to delete the page
  111. // However, If a page exists that is not "redirectTo", something is wrong. (Data correction is needed).
  112. if (originPage.redirectTo !== page.path) {
  113. throw new Error('The new page of to revert is exists and the redirect path of the page is not the deleted page.');
  114. }
  115. await this.completelyDeletePage([originPage], options);
  116. }
  117. page.status = STATUS_PUBLISHED;
  118. page.lastUpdateUser = user;
  119. debug('Revert deleted the page', page, newPath);
  120. const updatedPage = await Page.rename(page, newPath, user, {});
  121. return updatedPage;
  122. }
  123. async handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId) {
  124. const Page = this.crowi.model('Page');
  125. const pages = await Page.find({ grantedGroup: deletedGroup });
  126. switch (action) {
  127. case 'public':
  128. await Promise.all(pages.map((page) => {
  129. return Page.publicizePage(page);
  130. }));
  131. break;
  132. case 'delete':
  133. await Promise.all(pages.map((page) => {
  134. return this.completelyDeletePage(page);
  135. }));
  136. break;
  137. case 'transfer':
  138. await Promise.all(pages.map((page) => {
  139. return Page.transferPageToGroup(page, transferToUserGroupId);
  140. }));
  141. break;
  142. default:
  143. throw new Error('Unknown action for private pages');
  144. }
  145. }
  146. validateCrowi() {
  147. if (this.crowi == null) {
  148. throw new Error('"crowi" is null. Init User model with "crowi" argument first.');
  149. }
  150. }
  151. }
  152. module.exports = PageService;