page.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. class PageService {
  2. constructor(crowi) {
  3. this.crowi = crowi;
  4. }
  5. serializeToObj(page) {
  6. const { User } = this.crowi.models;
  7. const returnObj = page.toObject();
  8. // set the ObjectID to revisionHackmdSynced
  9. if (page.revisionHackmdSynced != null && page.revisionHackmdSynced._id != null) {
  10. returnObj.revisionHackmdSynced = page.revisionHackmdSynced._id;
  11. }
  12. if (page.lastUpdateUser != null && page.lastUpdateUser instanceof User) {
  13. returnObj.lastUpdateUser = page.lastUpdateUser.toObject();
  14. }
  15. if (page.creator != null && page.creator instanceof User) {
  16. returnObj.creator = page.creator.toObject();
  17. }
  18. return returnObj;
  19. }
  20. async deleteCompletely(pageId, pagePath) {
  21. // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
  22. const Bookmark = this.crowi.model('Bookmark');
  23. const Comment = this.crowi.model('Comment');
  24. const Page = this.crowi.model('Page');
  25. const PageTagRelation = this.crowi.model('PageTagRelation');
  26. const ShareLink = this.crowi.model('ShareLink');
  27. const Revision = this.crowi.model('Revision');
  28. return Promise.all([
  29. Bookmark.removeBookmarksByPageId(pageId),
  30. Comment.removeCommentsByPageId(pageId),
  31. PageTagRelation.remove({ relatedPage: pageId }),
  32. ShareLink.remove({ relatedPage: pageId }),
  33. Revision.removeRevisionsByPath(pagePath),
  34. Page.findByIdAndRemove(pageId),
  35. Page.removeRedirectOriginPageByPath(pagePath),
  36. this.removeAllAttachments(pageId),
  37. ]);
  38. }
  39. async removeAllAttachments(pageId) {
  40. const Attachment = this.crowi.model('Attachment');
  41. const { attachmentService } = this.crowi;
  42. const attachments = await Attachment.find({ page: pageId });
  43. const promises = attachments.map(async(attachment) => {
  44. return attachmentService.removeAttachment(attachment._id);
  45. });
  46. return Promise.all(promises);
  47. }
  48. }
  49. module.exports = PageService;