From 46925c589fcd17b656e086361c98c745ac322262 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Wed, 14 Aug 2024 15:21:23 +0200 Subject: [PATCH] Aftral[#71]: moved reaction attributes to block --- .../web/server/plugins/aftral-lms/block.js | 12 ++++++- .../server/plugins/aftral-lms/functions.js | 21 ++++-------- .../server/plugins/aftral-lms/resources.js | 12 +------ .../plugins/aftral-lms/schemas/BlockSchema.js | 34 +++++++++++++++++++ .../aftral-lms/schemas/ResourceSchema.js | 34 ------------------- 5 files changed, 53 insertions(+), 60 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index d3a79644b6..3759323d77 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -305,11 +305,21 @@ getSession = async (userId, params, data) => { } } +const isBlockLiked = async (userId, params, data) => { + const likes = data.likes.map(l=> l._id) + return likes.includes(userId) +} + +const isBlockDisliked = async (userId, params, data) => { + const dislikes = data.dislikes.map(l=> l._id) + return dislikes.includes(userId) +} + module.exports={ getBlockStatus, getBlockName, getSessionBlocks, setParentSession, cloneTree, getAttribute, LINKED_ATTRIBUTES, onBlockFinished, onBlockCurrent, onBlockAction, getNextResource, getPreviousResource, getParentBlocks,LINKED_ATTRIBUTES_CONVERSION, - ChainCache, ATTRIBUTES_CACHE,getSession + ChainCache, ATTRIBUTES_CACHE,getSession, isBlockDisliked, isBlockLiked } diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 53ff74851b..dc878d0f86 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -18,7 +18,7 @@ require('../../models/Chapter') //Added chapter, it was removed somehow const { computeStatistics } = require('./statistics') const { searchUsers, searchBlocks } = require('./search') const { getUserHomeworks, getResourceType, getAchievementRules, getBlockSpentTime, getBlockSpentTimeStr, getResourcesCount, getFinishedResourcesCount, getRessourceSession } = require('./resources') -const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession} = require('./block') +const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession, isBlockLiked, isBlockDisliked} = require('./block') const { getResourcesProgress } = require('./resources') const { getResourceAnnotation } = require('./resources') const { setResourceAnnotation } = require('./resources') @@ -35,8 +35,6 @@ const { getTraineeResources } = require('./user') const { isMine } = require('./message') const { DURATION_UNIT } = require('./consts') const { isLiked } = require('./post') -const { isResourceLiked } = require('./resources') -const { isResourceDisliked } = require('./resources') const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF' @@ -75,6 +73,12 @@ BLOCK_MODELS.forEach(model => { declareVirtualField({model, field: 'plain_url', type: 'String'}) declareComputedField({model, field: 'resources_count', getterFn: getResourcesCount}) declareComputedField({model, field: 'session', getterFn: getSession, requires:'parent.parent.parent.parent.parent'}) + + declareVirtualField({model, field: 'likes_count', instance: 'Number', requires:'likes'}) + declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) + + declareComputedField({model, field: 'liked', getterFn: isBlockLiked, requires:'likes'}) + declareComputedField({model, field: 'disliked', getterFn: isBlockDisliked, requires:'dislikes'}) }) declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE}) @@ -129,14 +133,6 @@ declareVirtualField({model:'post', field: 'comments_count', instance: 'Number', declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'likes'}) declareComputedField({model: 'post', field: 'liked', getterFn: isLiked}) -// Resource start -declareVirtualField({model:'resource', field: 'likes_count', instance: 'Number', requires:'likes'}) -declareVirtualField({model:'resource', field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) - -declareComputedField({model: 'resource', field: 'liked', getterFn: isResourceLiked, requires:'likes'}) -declareComputedField({model: 'resource', field: 'disliked', getterFn: isResourceDisliked, requires:'dislikes'}) -// Resource end - const preCreate = async ({model, params, user}) => { params.creator=params.creator || user._id params.last_updater=user._id @@ -174,7 +170,6 @@ const preCreate = async ({model, params, user}) => { params.receiver=params.parent } if (model == 'comment'){ - console.log(params) params.user = user._id params.post = params.parent } @@ -184,7 +179,6 @@ const preCreate = async ({model, params, user}) => { setPreCreateData(preCreate) const prePut = async ({model, id, params, user, skip_validation}) => { - console.log('put',model,'****************') if (model=='resource') { const block=await Resource.findById(id, {resource_type:1, origin:1, url:1, plain_url:1}) if (!block.origin) { @@ -293,7 +287,6 @@ const getFeeds = async (user, id) => { } const preprocessGet = async ({model, fields, id, user, params}) => { - console.log('process',model,'*****************************************',params, fields) if (model=='loggedUser') { model='user' id = user?._id || 'INVALIDID' diff --git a/backend/web/server/plugins/aftral-lms/resources.js b/backend/web/server/plugins/aftral-lms/resources.js index 6b82f2c647..0e92444e58 100644 --- a/backend/web/server/plugins/aftral-lms/resources.js +++ b/backend/web/server/plugins/aftral-lms/resources.js @@ -96,18 +96,8 @@ const canReplay = async ({dataId, user }) => { return blockHasStatus({user, block: dataId, status: BLOCK_STATUS_FINISHED}) } -const isResourceLiked = async (userId, params, data) => { - const likes = data.likes.map(l=> l._id) - return likes.includes(userId) -} - -const isResourceDisliked = async (userId, params, data) => { - const dislikes = data.dislikes.map(l=> l._id) - return dislikes.includes(userId) -} - module.exports={ getFinishedResourcesCount, isResourceMine, setResourceAnnotation, getResourceAnnotation, getResourcesProgress, getUserHomeworks, onSpentTimeChanged, getResourceType, getBlockSpentTime, getBlockSpentTimeStr, getResourcesCount, canPlay, canReplay, canResume, - getBlockResources, isResourceLiked, isResourceDisliked + getBlockResources } \ No newline at end of file diff --git a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js index d977983abf..9da3d75d9a 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js @@ -181,6 +181,32 @@ const BlockSchema = new Schema({ type: Schema.Types.ObjectId, ref: 'path', }], + liked: { + type: Boolean, + required: true, + default: false, + }, + disliked: { + type: Boolean, + default: false, + required: true, + }, + likes:{ + type: [{ + type: Schema.Types.ObjectId, + ref: 'user', + }], + required: true, + default: [] + }, + dislikes:{ + type: [{ + type: Schema.Types.ObjectId, + ref: 'user', + }], + required: true, + default: [] + }, }, {...schemaOptions, ...BLOCK_DISCRIMINATOR}) BlockSchema.virtual('is_template', DUMMY_REF).get(function() { @@ -205,6 +231,14 @@ BlockSchema.virtual('search_text', {localField: 'tagada', foreignField: 'tagada' return `${this.name} ${this.code}` }) +BlockSchema.virtual('likes_count', DUMMY_REF).get(function(){ + return this.likes.length || 0 +}) + +BlockSchema.virtual('dislikes_count', DUMMY_REF).get(function(){ + return this.dislikes.length || 0 +}) + // Validate Succes achievemnt BlockSchema.pre('validate', async function(next) { // #36 Can't create two templates with same type and same name diff --git a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js index 9124e5b456..86d29a63c2 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js @@ -22,40 +22,6 @@ const ResourceSchema = new Schema({ mine: { type: Boolean, }, - liked: { - type: Boolean, - required: true, - default: false, - }, - disliked: { - type: Boolean, - default: false, - required: true, - }, - likes:{ - type: [{ - type: Schema.Types.ObjectId, - ref: 'user', - }], - required: true, - default: [] - }, - dislikes:{ - type: [{ - type: Schema.Types.ObjectId, - ref: 'user', - }], - required: true, - default: [] - }, }, {...schemaOptions, ...BLOCK_DISCRIMINATOR}) -ResourceSchema.virtual('likes_count', DUMMY_REF).get(function(){ - return this.likes.length || 0 -}) - -ResourceSchema.virtual('dislikes_count', DUMMY_REF).get(function(){ - return this.dislikes.length || 0 -}) - module.exports = ResourceSchema