fix(challenges): better screening

This commit is contained in:
SabreCat 2023-08-07 22:26:56 -05:00
parent c44b1670cf
commit ebd0cb72de
4 changed files with 18 additions and 4 deletions

View file

@ -403,8 +403,12 @@ api.getUserChallenges = {
orOptions.push({ leader: user._id });
if (!req.query.member) {
const userGroups = await Group.getGroups({
user,
types: ['party', 'guilds', 'tavern'],
});
orOptions.push({
group: { $in: user.getGroups() },
group: { $in: userGroups },
}); // Challenges in groups where I'm a member
}

View file

@ -564,6 +564,7 @@ api.joinGroup = {
if (!group) throw new NotFound(res.t('groupNotFound'));
let isUserInvited = false;
const seekingParty = Boolean(user.party.seeking);
if (group.type === 'party') {
// Check if was invited to party
@ -729,12 +730,11 @@ api.joinGroup = {
invited: isUserInvited,
};
if (group.type === 'party') {
analyticsObject.seekingParty = Boolean(user.party.seeking);
analyticsObject.seekingParty = seekingParty;
}
if (group.privacy === 'public') {
analyticsObject.groupName = group.name;
}
user.party.seeking = undefined;
if (inviter) promises.push(inviter.save());
promises = await Promise.all(promises);

View file

@ -8,6 +8,7 @@ import {
TAVERN_ID,
} from '../../models/group';
import {
BadRequest,
NotFound,
NotAuthorized,
} from '../errors';
@ -41,6 +42,9 @@ export async function createChallenge (user, req, res) {
});
if (!group) throw new NotFound(res.t('groupNotFound'));
if (!group.isMember(user)) throw new NotAuthorized(res.t('mustBeGroupMember'));
if (group.type === 'guild' && group._id !== TAVERN_ID && !group.hasActiveGroupPlan()) {
throw new BadRequest(res.t('featureRetired'));
}
if (group.leaderOnly && group.leaderOnly.challenges && group.leader !== user._id) {
throw new NotAuthorized(res.t('onlyGroupLeaderChal'));

View file

@ -7,6 +7,7 @@ import * as Tasks from './task';
import { model as User } from './user'; // eslint-disable-line import/no-cycle
import { // eslint-disable-line import/no-cycle
model as Group,
TAVERN_ID,
} from './group';
import { removeFromArray } from '../libs/collectionManipulators';
import shared from '../../common';
@ -90,9 +91,14 @@ schema.methods.canModify = function canModifyChallenge (user) {
// Returns true if user can join the challenge
schema.methods.canJoin = function canJoinChallenge (user, group) {
if (group.type === 'guild' && group.privacy === 'public') return true;
// for when leader has left private group that contains the challenge
if (this.isLeader(user)) return true;
if (group.type === 'guild' && group.privacy === 'public') {
return group._id === TAVERN_ID;
}
if (group.type === 'guild' && group.privacy === 'private') {
if (!group.hasActiveGroupPlan()) return false;
}
return user.getGroups().indexOf(this.group) !== -1;
};