|
@@ -301,71 +301,26 @@ module.exports = function(crowi) {
|
|
|
return false;
|
|
return false;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- pageSchema.methods.updateTags = async function(newTagsName) {
|
|
|
|
|
- // const page = this;
|
|
|
|
|
- // const PageTagRelation = mongoose.model('PageTagRelation');
|
|
|
|
|
- // const Tag = mongoose.model('Tag');
|
|
|
|
|
-
|
|
|
|
|
- // const newTagNameList = [newTagsName]; // [TODO] listing requested Tags on client side
|
|
|
|
|
-
|
|
|
|
|
- // // get tags relate this page
|
|
|
|
|
- // const relations = await PageTagRelation.find({relatedPage: page._id}).populate('relatedTag').select('-_id relatedTag');
|
|
|
|
|
- // console.log(relations);
|
|
|
|
|
- // // relations.populate(...)
|
|
|
|
|
- // // const relatedTagNameList = await Promise.all(relations.map(async function(relation) {
|
|
|
|
|
- // // const relatedTag = await Tag.findOne({_id: relation.relatedTag});
|
|
|
|
|
- // // return relatedTag.name;
|
|
|
|
|
- // // }));
|
|
|
|
|
-
|
|
|
|
|
- // const relatedTags = ...;
|
|
|
|
|
-
|
|
|
|
|
- // // creat unlinked tag list
|
|
|
|
|
- // // TODO use filter and includes
|
|
|
|
|
- // const unlinkedTagNameList = [];
|
|
|
|
|
- // relatedTagNameList.map(function(relatedTagName) {
|
|
|
|
|
- // if (!newTagNameList.includes(relatedTagName)) {
|
|
|
|
|
- // return relatedTagName;
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
-
|
|
|
|
|
- // relatedTagTags.forEach(tag => {
|
|
|
|
|
- // // await つかえないかも
|
|
|
|
|
- // await tag.remove();
|
|
|
|
|
- // })
|
|
|
|
|
- // // unlinked page-tag-relations
|
|
|
|
|
- // // unlinkedTagNameList.map(function(tagName) {
|
|
|
|
|
- // // Tag.findOne({name: tagName}, function(err, tag) {
|
|
|
|
|
- // // PageTagRelation.remove({relatedPage: page._id, relatedTag: tag._id}, function(err, relation) {
|
|
|
|
|
- // // if (err) {
|
|
|
|
|
- // // throw new Error(err);
|
|
|
|
|
- // // }
|
|
|
|
|
- // // debug('remove tag relation: ', tag.name);
|
|
|
|
|
- // // });
|
|
|
|
|
- // // });
|
|
|
|
|
- // // });
|
|
|
|
|
-
|
|
|
|
|
- // // creat set tag list
|
|
|
|
|
- // const setTagNameList = [];
|
|
|
|
|
- // newTagNameList.map(function(newTagName) {
|
|
|
|
|
- // if (!relatedTagNameList.includes(newTagName)) {
|
|
|
|
|
- // setTagNameList.push(newTagName);
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
- // // set tags
|
|
|
|
|
- // setTagNameList.map((tagName) => {
|
|
|
|
|
- // Tag.findOne({name: tagName}, async function(err, tag) {
|
|
|
|
|
- // if (tag == null) {
|
|
|
|
|
- // tag = await Tag.create({name: tagName});
|
|
|
|
|
- // }
|
|
|
|
|
- // // make a relation
|
|
|
|
|
- // PageTagRelation.create({relatedPage: page._id, relatedTag: tag._id}, function(err, relation) {
|
|
|
|
|
- // if (err) {
|
|
|
|
|
- // throw new Error(err);
|
|
|
|
|
- // }
|
|
|
|
|
- // debug('tag linked this page: ', tag.name);
|
|
|
|
|
- // });
|
|
|
|
|
- // });
|
|
|
|
|
- // });
|
|
|
|
|
|
|
+ pageSchema.methods.updateTags = async function(newTags) {
|
|
|
|
|
+ const page = this;
|
|
|
|
|
+ const PageTagRelation = mongoose.model('PageTagRelation');
|
|
|
|
|
+ const Tag = mongoose.model('Tag');
|
|
|
|
|
+
|
|
|
|
|
+ // get tags relate this page
|
|
|
|
|
+ const relatedTags = await PageTagRelation.find({ relatedPage: page._id }).populate('relatedTag').select('-_id relatedTag');
|
|
|
|
|
+
|
|
|
|
|
+ // unlink relations
|
|
|
|
|
+ const unlinkTagRelations = relatedTags.filter((tag) => { return !newTags.includes(tag.relatedTag.name) });
|
|
|
|
|
+ await PageTagRelation.deleteMany({
|
|
|
|
|
+ relatedPage: page._id,
|
|
|
|
|
+ relatedTag: { $in: unlinkTagRelations.map((relation) => { return relation.relatedTag._id }) },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // create tag and relations
|
|
|
|
|
+ newTags.forEach(async(tag) => {
|
|
|
|
|
+ const setTag = await Tag.findOrCreate(tag);
|
|
|
|
|
+ PageTagRelation.createIfNotExist(page._id, setTag._id);
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|