populate group.leader

This commit is contained in:
Matteo Pagliazzi
2016-01-16 15:10:18 +01:00
parent 96c523493a
commit d7d63ad229
6 changed files with 30 additions and 24 deletions

View File

@@ -201,11 +201,10 @@ export function resetHabiticaDB () {
groups.insertOne({ groups.insertOne({
_id: 'habitrpg', _id: 'habitrpg',
chat: [], chat: [],
leader: '9', leader: '9', // TODO change this
name: 'HabitRPG', name: 'HabitRPG',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'public',
members: [],
}, (insertErr) => { }, (insertErr) => {
if (insertErr) return reject(insertErr); if (insertErr) return reject(insertErr);

View File

@@ -38,7 +38,7 @@ api.createChallenge = {
let groupId = req.body.groupId; let groupId = req.body.groupId;
let prize = req.body.prize; 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) throw new NotFound(res.t('groupNotFound'));
if (group.leaderOnly && group.leaderOnly.challenges && group.leader !== user._id) { if (group.leaderOnly && group.leaderOnly.challenges && group.leader !== user._id) {

View File

@@ -33,7 +33,7 @@ api.getChat = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
res.respond(200, group.chat); res.respond(200, group.chat);
@@ -67,7 +67,7 @@ api.postChat = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party' && user.flags.chatRevoked) { if (group.type !== 'party' && user.flags.chatRevoked) {
@@ -118,7 +118,7 @@ api.likeChat = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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) throw new NotFound(res.t('groupNotFound'));
let message = _.find(group.chat, {id: req.params.chatId}); let message = _.find(group.chat, {id: req.params.chatId});
@@ -165,7 +165,7 @@ api.flagChat = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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) throw new NotFound(res.t('groupNotFound'));
let message = _.find(group.chat, {id: req.params.chatId}); let message = _.find(group.chat, {id: req.params.chatId});

View File

@@ -93,7 +93,7 @@ api.getGroups = {
types.forEach(type => { types.forEach(type => {
switch (type) { switch (type) {
case 'party': case 'party':
queries.push(Group.getGroup(user, 'party', groupFields)); queries.push(Group.getGroup({user, groupId: 'party', fields: groupFields, populateLeader: true}));
break; break;
case 'privateGuilds': case 'privateGuilds':
queries.push(Group.find({ queries.push(Group.find({
@@ -109,7 +109,7 @@ api.getGroups = {
}).select(groupFields).sort(sort).exec()); // TODO use lean? }).select(groupFields).sort(sort).exec()); // TODO use lean?
break; break;
case 'tavern': case 'tavern':
queries.push(Group.getGroup(user, 'habitrpg', groupFields)); queries.push(Group.getGroup({user, groupId: 'habitrpg', fields: groupFields, populateLeader: true}));
break; break;
} }
}); });
@@ -149,7 +149,7 @@ api.getGroup = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
res.respond(200, group); res.respond(200, group);
@@ -178,7 +178,7 @@ api.updateGroup = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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) throw new NotFound(res.t('groupNotFound'));
if (group.leader !== user._id) throw new NotAuthorized(res.t('messageGroupOnlyLeaderCanUpdate')); if (group.leader !== user._id) throw new NotAuthorized(res.t('messageGroupOnlyLeaderCanUpdate'));
@@ -214,7 +214,8 @@ api.joinGroup = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
let isUserInvited = false; let isUserInvited = false;
@@ -288,7 +289,7 @@ api.leaveGroup = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
// During quests, checke wheter user can leave // During quests, checke wheter user can leave
@@ -344,7 +345,7 @@ api.removeGroupMember = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
let uuid = req.query.memberId; let uuid = req.query.memberId;
@@ -523,7 +524,7 @@ api.inviteToGroup = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
let uuids = req.body.uuids; let uuids = req.body.uuids;

View File

@@ -76,7 +76,7 @@ function _getMembersForItem (type) {
challenge = await Challenge.findById(challengeId).select('_id type leader').exec(); challenge = await Challenge.findById(challengeId).select('_id type leader').exec();
if (!challenge || !challenge.hasAccess(user)) throw new NotFound(res.t('groupNotFound')); if (!challenge || !challenge.hasAccess(user)) throw new NotFound(res.t('groupNotFound'));
} else { } 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')); if (!group) throw new NotFound(res.t('groupNotFound'));
} }

View File

@@ -1,5 +1,8 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { model as User} from './user'; import {
model as User,
nameFields,
} from './user';
import shared from '../../../common'; import shared from '../../../common';
import _ from 'lodash'; import _ from 'lodash';
import { model as Challenge} from './challenge'; import { model as Challenge} from './challenge';
@@ -40,7 +43,6 @@ export let schema = new Schema({
balance: {type: Number, default: 0}, balance: {type: Number, default: 0},
logo: String, logo: String,
leaderMessage: 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: { quest: {
key: String, key: String,
active: {type: Boolean, default: false}, 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. // '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 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 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: () => { members: {type: Schema.Types.Mixed, default: () => {
return {}; return {};
}}, }},
@@ -125,7 +128,8 @@ schema.post('remove', function postRemoveGroup (group) {
firebase.deleteGroup(group._id); 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; let query;
// When optionalMembership is true it's not required for the user to be a member of the group // 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); let mQuery = this.findOne(query);
if (fields) mQuery.select(fields); 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? // 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); export let model = mongoose.model('Group', schema);
// initialize tavern if !exists (fresh installs) // initialize tavern if !exists (fresh installs)
@@ -511,12 +517,12 @@ model.count({_id: 'habitrpg'}, (err, ct) => {
if (ct > 0) return; if (ct > 0) return;
new model({ // eslint-disable-line babel/new-cap 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 leader: '9', // TODO change this user id
name: 'HabitRPG', name: 'HabitRPG',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'public',
}).save(); }).save({
validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise
}); // TODO catch/log?
}); });
export const INVITES_LIMIT = 100;