Files
habitica/website/server/controllers/api-v3/quests.js
negue a8b58815b4 Update Party / Group Sidebar / Quest states (#12793)
* move groups/sidebar to groupSidebar.vue

* lint files

* extract group/party sidebar to rightSidebar.vue

* wip stories with example data

* update stories - wip sidebar re-styling

* message party / group leader + move items to the menu

* update paddings /place for quest section

* invite to party / guild

* update labels (* Party / Guild )

* guild-background to group-background

* correct menu order + missing a label based on the group type

* no quest - styles / layout applied

* quest owner / not started - styles applied   + extracted questActions from questDetailsModal.vue to a mixin

* no challenge style

* hover with underlines

* quest-pending area layout / margins

* "Collection Quest/Quest Owner Participating" Styling Done

* group sidebar menu with icons / background

* remove most participate button styles

* fix quest-invite panel

* move "Start Quest" + add "Leave Quest"

* Not Participating + Boss + Rage Quests restyling

* party quest changes - invitedToQuest + button styles + no-items style + view details

* fix icons + rage value + colors

* fix duplicate key

* hide items label if 0 items found + hide pending damage if there is none + sidebar section margin + fix percent calculation 0 => 0%

* combine quest abandon / cancel to one call + hide begin if quest has already started + close modal if quest was canceled

* remove unused translate string

* allow leaving an accepted but inactive quest + disable leave when user is quest leader

* update "are you sure" questions - remove "doubleSureAbort" - add "sureLeaveInactive"

* sidebar margins + menu icon color

* refactored css rules

* improve some styles

* fix button spacing

* fix dropmenu with icon hover

* hide leave quest for leaders + fix quest buttons spacing

* add pending items label

* remove "X items found" label

* first round of fixes

* last v-once

* Update Quest Dialogs (#13112)

* new quest rewards panel + extract questPopover and itemWithLabel

* WIP: questInfo still not applying the row-height..

* split up start-quest-modal into select and detail modal - also rename the current quest-details to be the group-quest-details modal

* remove start-quest-modal from modal-scss

* update package-lock

* WIP before using the quest sidebar branch as a base

* move quest detail actions to the "new" details dialog

* quest details layout for owner / participant

* fix quest rewards - open details modal from sidebar

* apply quest-details dialog styles to the buyQuestModal one

* fix quest reward icons / popover / texts

* WIP back to quest selection

* fix lint

* merge selectQuestModal.vue with questDetailModal.vue + UI for the select quest

* fix margins / layout / labels

* fix quest detail + wip invitationListModal.vue / participantListModal.vue

* fix questmodal user label centered

* fix centered reward items + grouping items and adding a count-badge

* sort quests by AZ or quantity

* invitations modal

* remove console.info

* complete participantListModal.vue + extracted getClassName

* missed a file for getClassName extraction

* fix invitations

* select the actual quest on details

* fix margins on invite to party / start quest buttons

* replace buyQuestModal close button and title

* fix recursion due to the same name

* missing import

* sort quantity by highest first

* fix "Can't find a Quest to start" styles

* fix "your balance" padding

* fix quest collections / drop items

* fix member details in participants list

* fix quest info

* remove nullable because the build doesn't like it (on this file..)

* add questCompleted to the stories + fix getDropName

* replace quest-rewards in questCompleted.vue

* fix questCompleted.vue style

* delete obsolete components

* add missing spritesheets to storebook

* requested pr changes

* refactored fetchMember

* revert optional chaining

* fix merge conflicts

* fix rightSidebar hover colors - $scss var to css var

* overflow auto instead of scroll

* prevent wrapping of quest collections

* rollback to multi line quest items

* use min-width for the quest popover
2021-05-28 16:11:43 -05:00

540 lines
16 KiB
JavaScript

import _ from 'lodash';
import { authWithHeaders } from '../../middlewares/auth';
import { getAnalyticsServiceByEnvironment } from '../../libs/analyticsService';
import {
model as Group,
basicFields as basicGroupFields,
} from '../../models/group';
import { model as User } from '../../models/user';
import {
NotFound,
NotAuthorized,
BadRequest,
} from '../../libs/errors';
import {
getUserInfo,
sendTxn as sendTxnEmail,
} from '../../libs/email';
import common from '../../../common';
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
import apiError from '../../libs/apiError';
import { questActivityWebhook } from '../../libs/webhook';
const analytics = getAnalyticsServiceByEnvironment();
const questScrolls = common.content.quests;
function canStartQuestAutomatically (group) {
// If all members are either true (accepted) or false (rejected) return true
// If any member is null/undefined (undecided) return false
return _.every(group.quest.members, _.isBoolean);
}
/**
* @apiDefine QuestNotFound
* @apiError (404) {NotFound} QuestNotFound The specified quest could not be found.
*/
/**
* @apiDefine QuestLeader Quest Leader
* The quest leader can use this route.
*/
const api = {};
/**
* @api {post} /api/v3/groups/:groupId/quests/invite/:questKey Invite users to a quest
* @apiName InviteToQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
* @apiParam (Path) {String} questKey
*
* @apiSuccess {Object} data Quest object
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.inviteToQuest = {
method: 'POST',
url: '/groups/:groupId/quests/invite/:questKey',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
const { questKey } = req.params;
const quest = questScrolls[questKey];
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId: req.params.groupId, fields: basicGroupFields.concat(' quest chat') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!quest) throw new NotFound(apiError('questNotFound', { key: questKey }));
if (!user.items.quests[questKey]) throw new NotAuthorized(res.t('questNotOwned'));
if (group.quest.key) throw new NotAuthorized(res.t('questAlreadyUnderway'));
const members = await User.find({
'party._id': group._id,
_id: { $ne: user._id },
})
.select('auth preferences.emailNotifications preferences.pushNotifications preferences.language profile.name pushDevices webhooks')
.exec();
group.markModified('quest');
group.quest.key = questKey;
group.quest.leader = user._id;
group.quest.members = {};
group.quest.members[user._id] = true;
user.party.quest.RSVPNeeded = false;
user.party.quest.key = questKey;
await User.update({
'party._id': group._id,
_id: { $ne: user._id },
}, {
$set: {
'party.quest.RSVPNeeded': true,
'party.quest.key': questKey,
},
}, { multi: true }).exec();
_.each(members, member => {
group.quest.members[member._id] = null;
});
if (canStartQuestAutomatically(group)) {
await group.startQuest(user);
}
const [savedGroup] = await Promise.all([
group.save(),
user.save(),
]);
res.respond(200, savedGroup.quest);
// send out invites
const inviterVars = getUserInfo(user, ['name', 'email']);
const membersToEmail = members.filter(member => {
// send push notifications while filtering members before sending emails
if (member.preferences.pushNotifications.invitedQuest !== false) {
sendPushNotification(
member,
{
title: quest.text(member.preferences.language),
message: res.t('questInvitationNotificationInfo', member.preferences.language),
identifier: 'questInvitation',
category: 'questInvitation',
},
);
}
// Send webhooks
questActivityWebhook.send(member, {
type: 'questInvited',
group,
quest,
});
return member.preferences.emailNotifications.invitedQuest !== false;
});
sendTxnEmail(membersToEmail, `invite-${quest.boss ? 'boss' : 'collection'}-quest`, [
{ name: 'QUEST_NAME', content: quest.text() },
{ name: 'INVITER', content: inviterVars.name },
{ name: 'PARTY_URL', content: '/party' },
]);
// Send webhook to the inviter too
questActivityWebhook.send(user, {
type: 'questInvited',
group,
quest,
});
// track that the inviting user has accepted the quest
analytics.track('quest', {
category: 'behavior',
owner: true,
response: 'accept',
gaLabel: 'accept',
questName: questKey,
uuid: user._id,
headers: req.headers,
}, true);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/accept Accept a pending quest
* @apiName AcceptQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.acceptQuest = {
method: 'POST',
url: '/groups/:groupId/quests/accept',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId: req.params.groupId, fields: basicGroupFields.concat(' quest chat') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.key) throw new NotFound(res.t('questInviteNotFound'));
if (group.quest.active) throw new NotAuthorized(res.t('questAlreadyStartedFriendly'));
if (group.quest.members[user._id]) throw new BadRequest(res.t('questAlreadyAccepted'));
const acceptedSuccessfully = await group.handleQuestInvitation(user, true);
if (!acceptedSuccessfully) {
throw new NotAuthorized(res.t('questAlreadyAccepted'));
}
user.party.quest.RSVPNeeded = false;
await user.save();
if (canStartQuestAutomatically(group)) {
await group.startQuest(user);
}
const savedGroup = await group.save();
res.respond(200, savedGroup.quest);
// track that a user has accepted the quest
analytics.track('quest', {
category: 'behavior',
owner: false,
response: 'accept',
gaLabel: 'accept',
questName: group.quest.key,
uuid: user._id,
headers: req.headers,
}, true);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/reject Reject a quest
* @apiName RejectQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.rejectQuest = {
method: 'POST',
url: '/groups/:groupId/quests/reject',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId: req.params.groupId, fields: basicGroupFields.concat(' quest chat') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.key) throw new NotFound(res.t('questInvitationDoesNotExist'));
if (group.quest.active) throw new NotAuthorized(res.t('questAlreadyStartedFriendly'));
if (group.quest.members[user._id]) throw new BadRequest(res.t('questAlreadyAccepted'));
if (group.quest.members[user._id] === false) throw new BadRequest(res.t('questAlreadyRejected'));
const rejectedSuccessfully = await group.handleQuestInvitation(user, false);
if (!rejectedSuccessfully) {
throw new NotAuthorized(res.t('questAlreadyRejected'));
}
user.party.quest = Group.cleanQuestUser(user.party.quest.progress);
user.markModified('party.quest');
await user.save();
if (canStartQuestAutomatically(group)) {
await group.startQuest(user);
}
const savedGroup = await group.save();
res.respond(200, savedGroup.quest);
analytics.track('quest', {
category: 'behavior',
owner: false,
response: 'reject',
gaLabel: 'reject',
questName: group.quest.key,
uuid: user._id,
headers: req.headers,
}, true);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/force-start Force-start a pending quest
* @apiName ForceQuestStart
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiPermission QuestLeader
* @apiPermission GroupLeader
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.forceStart = {
method: 'POST',
url: '/groups/:groupId/quests/force-start',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId: req.params.groupId, fields: basicGroupFields.concat(' quest chat') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.key) throw new NotFound(res.t('questNotPending'));
if (group.quest.active) throw new NotAuthorized(res.t('questAlreadyStarted'));
if (!(user._id === group.quest.leader || user._id === group.leader)) {
throw new NotAuthorized(res.t('questOrGroupLeaderOnlyStartQuest'));
}
group.markModified('quest');
await group.startQuest(user);
const [savedGroup] = await Promise.all([
group.save(),
user.save(),
]);
res.respond(200, savedGroup.quest);
analytics.track('quest', {
category: 'behavior',
owner: user._id === group.quest.leader,
response: 'force-start',
gaLabel: 'force-start',
questName: group.quest.key,
uuid: user._id,
headers: req.headers,
}, true);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/cancel Cancel a quest that is not active
* @apiName CancelQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiPermission QuestLeader
* @apiPermission GroupLeader
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.cancelQuest = {
method: 'POST',
url: '/groups/:groupId/quests/cancel',
middlewares: [authWithHeaders()],
async handler (req, res) {
// Cancel a quest BEFORE it has begun (i.e., in the invitation stage)
// Quest scroll has not yet left quest owner's inventory so no need to return it.
// Do not wipe quest progress for members because they'll
// want it to be applied to the next quest that's started.
const { user } = res.locals;
const { groupId } = req.params;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId, fields: basicGroupFields.concat(' quest') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.key) throw new NotFound(res.t('questInvitationDoesNotExist'));
if (user._id !== group.leader && group.quest.leader !== user._id) {
throw new NotAuthorized(res.t('onlyLeaderCancelQuest'));
}
if (group.quest.active) throw new NotAuthorized(res.t('cantCancelActiveQuest'));
const questName = questScrolls[group.quest.key].text('en');
const newChatMessage = group.sendChat({
message: `\`${user.profile.name} cancelled the party quest ${questName}.\``,
info: {
type: 'quest_cancel',
user: user.profile.name,
quest: group.quest.key,
},
});
group.quest = Group.cleanGroupQuest();
group.markModified('quest');
const [savedGroup] = await Promise.all([
group.save(),
newChatMessage.save(),
User.update(
{ 'party._id': groupId },
Group.cleanQuestParty(),
{ multi: true },
).exec(),
]);
res.respond(200, savedGroup.quest);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/abort Abort the current quest
* @apiName AbortQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiPermission QuestLeader
* @apiPermission GroupLeader
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.abortQuest = {
method: 'POST',
url: '/groups/:groupId/quests/abort',
middlewares: [authWithHeaders()],
async handler (req, res) {
// Abort a quest AFTER it has begun
const { user } = res.locals;
const { groupId } = req.params;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId, fields: basicGroupFields.concat(' quest chat') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.active) throw new NotFound(res.t('noActiveQuestToAbort'));
if (user._id !== group.leader && user._id !== group.quest.leader) throw new NotAuthorized(res.t('onlyLeaderAbortQuest'));
const questName = questScrolls[group.quest.key].text('en');
const newChatMessage = group.sendChat({
message: `\`${common.i18n.t('chatQuestAborted', { username: user.profile.name, questName }, 'en')}\``,
info: {
type: 'quest_abort',
user: user.profile.name,
quest: group.quest.key,
},
});
await newChatMessage.save();
const memberUpdates = User.update({
'party._id': groupId,
}, Group.cleanQuestParty(),
{ multi: true }).exec();
const questLeaderUpdate = User.update({
_id: group.quest.leader,
}, {
$inc: {
[`items.quests.${group.quest.key}`]: 1, // give back the quest to the quest leader
},
}).exec();
group.quest = Group.cleanGroupQuest();
group.markModified('quest');
const [groupSaved] = await Promise.all([group.save(), memberUpdates, questLeaderUpdate]);
res.respond(200, groupSaved.quest);
},
};
/**
* @api {post} /api/v3/groups/:groupId/quests/leave Leave the active quest
* @apiName LeaveQuest
* @apiGroup Quest
*
* @apiParam (Path) {String} groupId The group _id (or 'party')
*
* @apiSuccess {Object} data Quest Object
*
* @apiUse GroupNotFound
* @apiUse QuestNotFound
*/
api.leaveQuest = {
method: 'POST',
url: '/groups/:groupId/quests/leave',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
const { groupId } = req.params;
req.checkParams('groupId', apiError('groupIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const group = await Group.getGroup({ user, groupId, fields: basicGroupFields.concat(' quest') });
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (group.quest.leader === user._id) throw new NotAuthorized(res.t('questLeaderCannotLeaveQuest'));
if (!group.quest.members[user._id]) throw new NotAuthorized(res.t('notPartOfQuest'));
group.quest.members[user._id] = false;
group.markModified('quest.members');
user.party.quest = Group.cleanQuestUser(user.party.quest.progress);
user.markModified('party.quest');
const [savedGroup] = await Promise.all([
group.save(),
user.save(),
]);
res.respond(200, savedGroup.quest);
},
};
export default api;