Skip to content

Commit

Permalink
Aftral[premieroctet#71]: moved reaction attributes to block
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 14, 2024
1 parent 36507c1 commit 46925c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
12 changes: 11 additions & 1 deletion backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


21 changes: 7 additions & 14 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'

Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down
12 changes: 1 addition & 11 deletions backend/web/server/plugins/aftral-lms/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
34 changes: 0 additions & 34 deletions backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 46925c5

Please sign in to comment.