mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
populate group.leader
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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});
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user