From d7d63ad229c01f04c38617125901ca102485770d Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 16 Jan 2016 15:10:18 +0100 Subject: [PATCH] populate group.leader --- test/helpers/api-v3-integration.helper.js | 3 +-- website/src/controllers/api-v3/challenges.js | 2 +- website/src/controllers/api-v3/chat.js | 8 +++---- website/src/controllers/api-v3/groups.js | 17 ++++++++------- website/src/controllers/api-v3/members.js | 2 +- website/src/models/group.js | 22 +++++++++++++------- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/test/helpers/api-v3-integration.helper.js b/test/helpers/api-v3-integration.helper.js index 530b275fb6..a5d48478cd 100644 --- a/test/helpers/api-v3-integration.helper.js +++ b/test/helpers/api-v3-integration.helper.js @@ -201,11 +201,10 @@ export function resetHabiticaDB () { groups.insertOne({ _id: 'habitrpg', chat: [], - leader: '9', + leader: '9', // TODO change this name: 'HabitRPG', type: 'guild', privacy: 'public', - members: [], }, (insertErr) => { if (insertErr) return reject(insertErr); diff --git a/website/src/controllers/api-v3/challenges.js b/website/src/controllers/api-v3/challenges.js index f98cac0302..ed8440ff1f 100644 --- a/website/src/controllers/api-v3/challenges.js +++ b/website/src/controllers/api-v3/challenges.js @@ -38,7 +38,7 @@ api.createChallenge = { let groupId = req.body.groupId; let prize = req.body.prize; - let group = await Group.getGroup(user, groupId, '-chat'); + let group = await Group.getGroup({user, groupId, fields: '-chat'}); if (!group) throw new NotFound(res.t('groupNotFound')); if (group.leaderOnly && group.leaderOnly.challenges && group.leader !== user._id) { diff --git a/website/src/controllers/api-v3/chat.js b/website/src/controllers/api-v3/chat.js index 7c2d42bf62..212e064441 100644 --- a/website/src/controllers/api-v3/chat.js +++ b/website/src/controllers/api-v3/chat.js @@ -33,7 +33,7 @@ api.getChat = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId, 'chat'); + let group = await Group.getGroup({user, groupId: req.params.groupId, fields: 'chat'}); if (!group) throw new NotFound(res.t('groupNotFound')); res.respond(200, group.chat); @@ -67,7 +67,7 @@ api.postChat = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, groupId); + let group = await Group.getGroup({user, groupId}); if (!group) throw new NotFound(res.t('groupNotFound')); if (group.type !== 'party' && user.flags.chatRevoked) { @@ -118,7 +118,7 @@ api.likeChat = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, groupId); + let group = await Group.getGroup({user, groupId}); if (!group) throw new NotFound(res.t('groupNotFound')); let message = _.find(group.chat, {id: req.params.chatId}); @@ -165,7 +165,7 @@ api.flagChat = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, groupId); + let group = await Group.getGroup({user, groupId}); if (!group) throw new NotFound(res.t('groupNotFound')); let message = _.find(group.chat, {id: req.params.chatId}); diff --git a/website/src/controllers/api-v3/groups.js b/website/src/controllers/api-v3/groups.js index 8203e00b1b..d7d3eedbd3 100644 --- a/website/src/controllers/api-v3/groups.js +++ b/website/src/controllers/api-v3/groups.js @@ -93,7 +93,7 @@ api.getGroups = { types.forEach(type => { switch (type) { case 'party': - queries.push(Group.getGroup(user, 'party', groupFields)); + queries.push(Group.getGroup({user, groupId: 'party', fields: groupFields, populateLeader: true})); break; case 'privateGuilds': queries.push(Group.find({ @@ -109,7 +109,7 @@ api.getGroups = { }).select(groupFields).sort(sort).exec()); // TODO use lean? break; case 'tavern': - queries.push(Group.getGroup(user, 'habitrpg', groupFields)); + queries.push(Group.getGroup({user, groupId: 'habitrpg', fields: groupFields, populateLeader: true})); break; } }); @@ -149,7 +149,7 @@ api.getGroup = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId); + let group = await Group.getGroup({user, groupId: req.params.groupId, populateLeader: true}); if (!group) throw new NotFound(res.t('groupNotFound')); res.respond(200, group); @@ -178,7 +178,7 @@ api.updateGroup = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId); + let group = await Group.getGroup({user, groupId: req.params.groupId}); if (!group) throw new NotFound(res.t('groupNotFound')); if (group.leader !== user._id) throw new NotAuthorized(res.t('messageGroupOnlyLeaderCanUpdate')); @@ -214,7 +214,8 @@ api.joinGroup = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId, '-chat', true); // Do not fetch chat and work even if the user is not yet a member of the group + // Do not fetch chat and work even if the user is not yet a member of the group + let group = await Group.getGroup({user, groupId: req.params.groupId, fields: '-chat', optionalMembership: true}); // Do not fetch chat and work even if the user is not yet a member of the group if (!group) throw new NotFound(res.t('groupNotFound')); let isUserInvited = false; @@ -288,7 +289,7 @@ api.leaveGroup = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId, '-chat'); // Do not fetch chat + let group = await Group.getGroup({user, groupId: req.params.groupId, fields: '-chat'}); // Do not fetch chat if (!group) throw new NotFound(res.t('groupNotFound')); // During quests, checke wheter user can leave @@ -344,7 +345,7 @@ api.removeGroupMember = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId, '-chat'); // Do not fetch chat + let group = await Group.getGroup({user, groupId: req.params.groupId, fields: '-chat'}); // Do not fetch chat if (!group) throw new NotFound(res.t('groupNotFound')); let uuid = req.query.memberId; @@ -523,7 +524,7 @@ api.inviteToGroup = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup(user, req.params.groupId, '-chat'); // Do not fetch chat TODO other fields too? + let group = await Group.getGroup({user, groupId: req.params.groupId, fields: '-chat'}); // Do not fetch chat TODO other fields too? if (!group) throw new NotFound(res.t('groupNotFound')); let uuids = req.body.uuids; diff --git a/website/src/controllers/api-v3/members.js b/website/src/controllers/api-v3/members.js index 3f5ca5ac9d..0b5638a95a 100644 --- a/website/src/controllers/api-v3/members.js +++ b/website/src/controllers/api-v3/members.js @@ -76,7 +76,7 @@ function _getMembersForItem (type) { challenge = await Challenge.findById(challengeId).select('_id type leader').exec(); if (!challenge || !challenge.hasAccess(user)) throw new NotFound(res.t('groupNotFound')); } else { - group = await Group.getGroup(user, groupId, '_id type'); + group = await Group.getGroup({user, groupId, fields: '_id type'}); if (!group) throw new NotFound(res.t('groupNotFound')); } diff --git a/website/src/models/group.js b/website/src/models/group.js index 94072bfb79..169d977d08 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -1,5 +1,8 @@ import mongoose from 'mongoose'; -import { model as User} from './user'; +import { + model as User, + nameFields, +} from './user'; import shared from '../../../common'; import _ from 'lodash'; import { model as Challenge} from './challenge'; @@ -40,7 +43,6 @@ export let schema = new Schema({ balance: {type: Number, default: 0}, logo: String, leaderMessage: String, - // challenges: [{type: String, validate: [validator.isUUID, 'Invalid uuid.'], ref: 'Challenge'}], // TODO do we need this? could depend on back-ref instead (Challenge.find({group:GID})) quest: { key: String, active: {type: Boolean, default: false}, @@ -57,6 +59,7 @@ export let schema = new Schema({ // 'Accept', the quest begins. If a false user waits too long, probably a good sign to prod them or boot them. // TODO when booting user, remove from .joined and check again if we can now start the quest // TODO as long as quests are party only we can keep it here + // TODO are we sure we need this type of default for this to work? members: {type: Schema.Types.Mixed, default: () => { return {}; }}, @@ -125,7 +128,8 @@ schema.post('remove', function postRemoveGroup (group) { firebase.deleteGroup(group._id); }); -schema.statics.getGroup = function getGroup (user, groupId, fields, optionalMembership) { +schema.statics.getGroup = function getGroup (options = {}) { + let {user, groupId, fields, optionalMembership = false, populateLeader = false} = options; let query; // When optionalMembership is true it's not required for the user to be a member of the group @@ -141,7 +145,8 @@ schema.statics.getGroup = function getGroup (user, groupId, fields, optionalMemb let mQuery = this.findOne(query); if (fields) mQuery.select(fields); - return mQuery.exec(); // TODO catch errors here? + if (populateLeader === true) mQuery.populate('leader', nameFields); + return mQuery.exec(); // TODO purge chat flags info? in tojson? }; @@ -503,6 +508,7 @@ schema.methods.leave = function leaveGroup (user, keep) { }); }; +export const INVITES_LIMIT = 100; export let model = mongoose.model('Group', schema); // initialize tavern if !exists (fresh installs) @@ -511,12 +517,12 @@ model.count({_id: 'habitrpg'}, (err, ct) => { if (ct > 0) return; new model({ // eslint-disable-line babel/new-cap - _id: 'habitrpg', // TODO hmm this will probably break everything + _id: 'habitrpg', leader: '9', // TODO change this user id name: 'HabitRPG', type: 'guild', privacy: 'public', - }).save(); + }).save({ + validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise + }); // TODO catch/log? }); - -export const INVITES_LIMIT = 100;