Просмотр исходного кода

Merge pull request #1113 from weseek/imprv/remove-PageTagRelation-when-complete-delete-page

Imprv/remove page tag relation when delete page
Yuki Takei 6 лет назад
Родитель
Сommit
73cd4324c8
2 измененных файлов с 7 добавлено и 1 удалено
  1. 5 1
      src/server/models/page-tag-relation.js
  2. 2 0
      src/server/models/page.js

+ 5 - 1
src/server/models/page-tag-relation.js

@@ -38,12 +38,15 @@ class PageTagRelation {
   }
 
   static async createTagListWithCount(option) {
+    const Tag = mongoose.model('Tag');
     const opt = option || {};
     const sortOpt = opt.sortOpt || {};
     const offset = opt.offset || 0;
     const limit = opt.limit || 50;
 
+    const existTagIds = await Tag.find().distinct('_id');
     const tags = await this.aggregate()
+      .match({ relatedTag: { $in: existTagIds } })
       .group({ _id: '$relatedTag', count: { $sum: 1 } })
       .sort(sortOpt);
 
@@ -54,7 +57,8 @@ class PageTagRelation {
   }
 
   static async listTagsByPage(pageId) {
-    return this.find({ relatedPage: pageId }).populate('relatedTag').select('-_id relatedTag');
+    const tags = await this.find({ relatedPage: pageId }).populate('relatedTag').select('-_id relatedTag');
+    return tags.filter((tag) => { return tag.relatedTag !== null });
   }
 
   static async listTagNamesByPage(pageId) {

+ 2 - 0
src/server/models/page.js

@@ -1155,6 +1155,7 @@ module.exports = function(crowi) {
     const Bookmark = crowi.model('Bookmark');
     const Attachment = crowi.model('Attachment');
     const Comment = crowi.model('Comment');
+    const PageTagRelation = crowi.model('PageTagRelation');
     const Revision = crowi.model('Revision');
     const pageId = pageData._id;
     const socketClientId = options.socketClientId || null;
@@ -1164,6 +1165,7 @@ module.exports = function(crowi) {
     await Bookmark.removeBookmarksByPageId(pageId);
     await Attachment.removeAttachmentsByPageId(pageId);
     await Comment.removeCommentsByPageId(pageId);
+    await PageTagRelation.remove({ relatedPage: pageId });
     await Revision.removeRevisionsByPath(pageData.path);
     await this.findByIdAndRemove(pageId);
     await this.removeRedirectOriginPageByPath(pageData.path);