From a49ebfd8bdfc16449177555420d53dbeef3e1818 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Tue, 20 Aug 2024 13:41:02 +0200 Subject: [PATCH] Afvtral[#51]: refactored likes for post --- backend/web/server/plugins/aftral-lms/block.js | 1 - backend/web/server/plugins/aftral-lms/functions.js | 8 ++++---- backend/web/server/plugins/aftral-lms/post.js | 5 +++-- .../web/server/plugins/aftral-lms/schemas/PostSchema.js | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index 91adc6df30..84c3cdba00 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -306,7 +306,6 @@ const getBlockLiked = async (userId, params, data) => { const getBlockDisliked = async (userId, params, data) => { const user = await User.findById(userId, {role:1}) const template = await getTemplate(data._id) - console.log(template) if(user.role == ROLE_CONCEPTEUR) { return template._disliked_by.length > 0 } diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 9829c2e1cd..e6e819d637 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -162,8 +162,8 @@ declareVirtualField({model:'post', field: 'comments_count', instance: 'Number', instance: 'ObjectID', options: {ref: 'block'}}, }) -declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'likes'}) -declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires:'likes'}) +declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'_liked_by'}) +declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires:'_liked_by'}) // Post end // Ticket start @@ -257,8 +257,8 @@ const prePut = async ({model, id, params, user, skip_validation}) => { await Post.updateOne( {_id:id}, { - ...params.liked ? {$addToSet: {likes: user._id}} - : {$pull: {likes: user._id}} + ...params.liked ? {$addToSet: {_liked_by: user._id}} + : {$pull: {_liked_by: user._id}} } )} } diff --git a/backend/web/server/plugins/aftral-lms/post.js b/backend/web/server/plugins/aftral-lms/post.js index 99d08dcaae..8302b2900a 100644 --- a/backend/web/server/plugins/aftral-lms/post.js +++ b/backend/web/server/plugins/aftral-lms/post.js @@ -1,6 +1,7 @@ +const { idEqual } = require("../../utils/database") + const isLiked = async(userId, params, data) => { - const likes = data._likes.map(l=> l._id) - return likes.includes(userId) + return data._liked_by.some(like=> idEqual(l._id,userId)) } module.exports={ diff --git a/backend/web/server/plugins/aftral-lms/schemas/PostSchema.js b/backend/web/server/plugins/aftral-lms/schemas/PostSchema.js index 369938b350..c6ba0544b0 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/PostSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/PostSchema.js @@ -31,7 +31,7 @@ const PostSchema = new Schema({ required: true, enum: Object.keys(FEED_TYPE), }, - _likes: [{ + _liked_by: [{ type: Schema.Types.ObjectId, ref: 'user', }], @@ -43,7 +43,7 @@ const PostSchema = new Schema({ }, schemaOptions) PostSchema.virtual('likes_count', DUMMY_REF).get(function(){ - return this._likes.length || 0 + return this._liked_by.length || 0 }) PostSchema.virtual('comments', {