mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
commit 00affb306655a543f5d29b3af6361e686b577a97 Author: SabreCat <sabe@habitica.com> Date: Tue May 2 09:47:25 2023 -0500 fix(tests): account for invite limit changes commit 47661117f9fd661b8bc8f63b7cc7c8d5f8fa0fd7 Author: SabreCat <sabe@habitica.com> Date: Mon May 1 17:39:29 2023 -0500 fix(lfp): final polish commit 6a1e5af1db0dd90be3ced7e223f53c9183a206f5 Merge: 728ed2ddad9e0777bb42Author: SabreCat <sabe@habitica.com> Date: Mon May 1 16:54:12 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit 728ed2ddad7f0962d28f1ab0a271e3555b19296c Author: SabreCat <sabe@habitica.com> Date: Thu Apr 27 16:51:06 2023 -0500 fix(lfp): loading layout and page visit event commit 8a56ab329bff922e05963e3ef78fbc26ff273924 Author: SabreCat <sabe@habitica.com> Date: Wed Apr 26 16:54:46 2023 -0500 fix(faq): copy and style updates commit 6fd00d7f30150a1802e5a37edbb914ef120caf9a Author: SabreCat <sabe@habitica.com> Date: Fri Apr 21 17:12:52 2023 -0500 feat(lfp): fixes, analytics, FAQ commit 4b5d7304ad7cfc5f72320b23456ed2898e53caac Author: SabreCat <sabe@habitica.com> Date: Mon Apr 17 15:13:03 2023 -0500 fix(lfp): smol tweaks commit 9a5476a2558eb17a603f4aae1b5b2d35773be8b4 Author: SabreCat <sabe@habitica.com> Date: Thu Apr 13 16:03:33 2023 -0500 feat(lfp): refresh button commit aa58f5018469f38a9a9d31c3bffa26bb88a8c672 Merge: bbb03d006ec8adf20804Author: SabreCat <sabe@habitica.com> Date: Tue Apr 11 17:44:56 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit bbb03d006e8b122bb7206bdc778a31de422167bb Author: SabreCat <sabe@habitica.com> Date: Tue Apr 4 18:30:50 2023 -0500 fix(lint): whitespace and const commit 23683ad29a4cce0b0da061ad6c030982034c0a9c Author: SabreCat <sabe@habitica.com> Date: Tue Apr 4 17:02:57 2023 -0500 chore(LFP): add analytics also re-fix loading state commit 4477d84f5266c87f5583368029b72153f00f0568 Author: SabreCat <sabe@habitica.com> Date: Mon Apr 3 16:24:26 2023 -0500 fix(LFP): address issues with loading commit bdc5154f24bb5e50963376c3c0c9cc73c0b05ccc Merge: 81923eef6f229ed46425Author: SabreCat <sabe@habitica.com> Date: Mon Apr 3 15:58:12 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit 81923eef6f0c627d079475a28f9d93d8e4628934 Author: SabreCat <sabe@habitica.com> Date: Thu Mar 30 16:44:49 2023 -0500 feat(LFP): release candidate commit fe1f8939fc6b09d36cfaf0b6e5838df04e41009d Author: SabreCat <sabe@habitica.com> Date: Wed Mar 29 17:35:54 2023 -0500 WIP(LFP): fixes commit afc361f5a9f806cbd814ad910d1274e3a6609efd Merge: d6b5cbdebc7ede3acd01Author: SabreCat <sabe@habitica.com> Date: Wed Mar 29 16:24:39 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit d6b5cbdebc2829e9325ea57fb5deeccc128c1635 Author: SabreCat <sabe@habitica.com> Date: Tue Mar 28 16:13:18 2023 -0500 WIP(LFP): change copy, add close X, fix API response commit 4274a4625862351ef0ecf33c8a3249ca5ebec7cb Author: SabreCat <sabe@habitica.com> Date: Mon Mar 27 17:07:36 2023 -0500 fix(LFP): layout, unset when stopping commit 95abfcfa5f13c9cce6385206947a47f85b76d11d Merge: 4a360eedd853c536b525Author: SabreCat <sabe@habitica.com> Date: Mon Mar 27 16:32:46 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit 4a360eedd8b9cf41d3a0fe7a4cfaa72c5bd7bd26 Author: SabreCat <sabe@habitica.com> Date: Thu Mar 23 16:54:49 2023 -0500 feat(LFP): completed style and infinite scroll commit bbc439d9d03c9631a450236eb33af66f0428fa50 Author: SabreCat <sabe@habitica.com> Date: Tue Mar 21 10:40:26 2023 -0500 WIP(LFP): nicer layout, buffs fix commit 1658688597456663477ab19da61ae1b9bc85cf2a Merge: 664507434f027e61a93eAuthor: SabreCat <sabe@habitica.com> Date: Tue Mar 21 09:29:02 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit 664507434f2f76e6bf3b61cdc9e3daddb81204af Author: SabreCat <sabe@habitica.com> Date: Fri Mar 17 17:13:08 2023 -0500 WIP(LFP): API and client adjustments commit 2f0b6f2517f9e2d634cb23ee303cfb4542e998ce Merge: 0db1704325a16098ccdaAuthor: SabreCat <sabe@habitica.com> Date: Fri Mar 17 16:45:13 2023 -0500 Merge branch 'release' into sabrecat/party-seeking commit 0db1704325c3555f0b5d9c8d1dfc44177e90c093 Author: SabreCat <sabe@habitica.com> Date: Mon Mar 13 14:48:10 2023 -0500 fix(modal): scrollbar for squashed viewports commit 733c35192e0a4e31e1bebfdd7488cfc1f7587f36 Author: SabreCat <sabe@habitica.com> Date: Thu Mar 9 12:51:02 2023 -0600 WIP(party): seekers functional rough commit d4b854410b557db26eec6e6a26b6d174c02cee3a Merge: 7fe919825a0b6b967753Author: SabreCat <sabe@habitica.com> Date: Thu Mar 9 10:07:28 2023 -0600 Merge branch 'release' into sabrecat/party-seeking commit 7fe919825abfb6d518cb93b91f5997d3831bd0b5 Author: SabreCat <sabe@habitica.com> Date: Thu Mar 2 14:40:09 2023 -0600 feat(party): several adjustments to seeking feature commit c93900efcf925f7aaa4c4cb56b4451f19adfb1b3 Author: SabreCat <sabe@habitica.com> Date: Wed Mar 1 20:37:11 2023 -0600 feat(party): initial Seeking API commit 8bb784daeceb14c23992a6f3af1054a900fc26c1 Merge: e19a661a21f327795761Author: SabreCat <sabe@habitica.com> Date: Wed Mar 1 18:58:20 2023 -0600 Merge branch 'release' into sabrecat/party-seeking commit e19a661a2163a50307a286379bffb44201ed392e Author: SabreCat <sabe@habitica.com> Date: Fri Feb 24 15:51:42 2023 -0600 WIP(parties): add seeking flag and modal toggle
324 lines
9.3 KiB
JavaScript
324 lines
9.3 KiB
JavaScript
import _ from 'lodash';
|
|
import common from '../../../common';
|
|
import * as Tasks from '../../models/task';
|
|
import { model as Groups } from '../../models/group';
|
|
import {
|
|
BadRequest,
|
|
NotAuthorized,
|
|
} from '../errors';
|
|
import { model as User, schema as UserSchema } from '../../models/user';
|
|
import { model as NewsPost } from '../../models/newsPost';
|
|
import { stringContainsProfanity, nameContainsNewline } from './validation';
|
|
|
|
export async function get (req, res, { isV3 = false }) {
|
|
const { user } = res.locals;
|
|
let userToJSON;
|
|
|
|
if (isV3) {
|
|
userToJSON = await user.toJSONWithInbox();
|
|
} else {
|
|
userToJSON = user.toJSON();
|
|
}
|
|
|
|
// Remove apiToken from response TODO make it private at the user level? returned in signup/login
|
|
delete userToJSON.apiToken;
|
|
|
|
if (!req.query.userFields) {
|
|
const { daysMissed } = user.daysUserHasMissed(new Date(), req);
|
|
userToJSON.needsCron = false;
|
|
if (daysMissed > 0) userToJSON.needsCron = true;
|
|
User.addComputedStatsToJSONObj(userToJSON.stats, userToJSON);
|
|
}
|
|
|
|
return res.respond(200, userToJSON);
|
|
}
|
|
|
|
const updatablePaths = [
|
|
'_ABtests.counter',
|
|
|
|
'flags.customizationsNotification',
|
|
'flags.showTour',
|
|
'flags.tour',
|
|
'flags.tutorial',
|
|
'flags.communityGuidelinesAccepted',
|
|
'flags.welcomed',
|
|
'flags.cardReceived',
|
|
'flags.warnedLowHealth',
|
|
|
|
'achievements',
|
|
|
|
'party.order',
|
|
'party.orderAscending',
|
|
'party.quest.completed',
|
|
'party.quest.RSVPNeeded',
|
|
'party.seeking',
|
|
|
|
'preferences',
|
|
'profile',
|
|
'stats',
|
|
'inbox.optOut',
|
|
];
|
|
|
|
// This tells us for which paths users can call `PUT /user`.
|
|
// The trick here is to only accept leaf paths, not root/intermediate paths (see https://goo.gl/OEzkAs)
|
|
const acceptablePUTPaths = _.reduce(UserSchema.paths, (accumulator, val, leaf) => {
|
|
const found = _.find(updatablePaths, rootPath => leaf.indexOf(rootPath) === 0);
|
|
|
|
if (found) accumulator[leaf] = true;
|
|
|
|
return accumulator;
|
|
}, {});
|
|
|
|
const restrictedPUTSubPaths = [
|
|
'stats.class',
|
|
|
|
'preferences.disableClasses',
|
|
'preferences.sleep',
|
|
'preferences.webhooks',
|
|
];
|
|
|
|
_.each(restrictedPUTSubPaths, removePath => {
|
|
delete acceptablePUTPaths[removePath];
|
|
});
|
|
|
|
const requiresPurchase = {
|
|
'preferences.background': 'background',
|
|
'preferences.shirt': 'shirt',
|
|
'preferences.size': 'size',
|
|
'preferences.skin': 'skin',
|
|
'preferences.hair.bangs': 'hair.bangs',
|
|
'preferences.hair.base': 'hair.base',
|
|
'preferences.hair.beard': 'hair.beard',
|
|
'preferences.hair.color': 'hair.color',
|
|
'preferences.hair.flower': 'hair.flower',
|
|
'preferences.hair.mustache': 'hair.mustache',
|
|
};
|
|
|
|
function checkPreferencePurchase (user, path, item) {
|
|
const itemPath = `${path}.${item}`;
|
|
const appearance = _.get(common.content.appearances, itemPath);
|
|
if (!appearance) return false;
|
|
if (appearance.price === 0 && path !== 'background') {
|
|
return true;
|
|
}
|
|
|
|
return _.get(user.purchased, itemPath);
|
|
}
|
|
|
|
async function checkNewInputForProfanity (user, res, newValue) {
|
|
const containsSlur = stringContainsProfanity(newValue, 'slur');
|
|
const containsBannedWord = stringContainsProfanity(newValue);
|
|
if (containsSlur || containsBannedWord) {
|
|
if (containsSlur) {
|
|
user.flags.chatRevoked = true;
|
|
await user.save();
|
|
throw new BadRequest(res.t('bannedSlurUsedInProfile'));
|
|
}
|
|
throw new BadRequest(res.t('bannedWordUsedInProfile'));
|
|
}
|
|
}
|
|
|
|
export async function update (req, res, { isV3 = false }) {
|
|
const { user } = res.locals;
|
|
|
|
const promisesForTagsRemoval = [];
|
|
|
|
if (req.body['party.seeking'] !== undefined && req.body['party.seeking'] !== null) {
|
|
user.invitations.party = {};
|
|
user.invitations.parties = [];
|
|
res.analytics.track('Starts Looking for Party', {
|
|
uuid: user._id,
|
|
hitType: 'event',
|
|
category: 'behavior',
|
|
});
|
|
}
|
|
|
|
if (req.body['profile.name'] !== undefined) {
|
|
const newName = req.body['profile.name'];
|
|
if (newName === null) throw new BadRequest(res.t('invalidReqParams'));
|
|
if (newName.length > 30) throw new BadRequest(res.t('displaynameIssueLength'));
|
|
if (nameContainsNewline(newName)) throw new BadRequest(res.t('displaynameIssueNewline'));
|
|
await checkNewInputForProfanity(user, res, newName);
|
|
}
|
|
|
|
if (req.body['profile.blurb'] !== undefined) {
|
|
const newBlurb = req.body['profile.blurb'];
|
|
await checkNewInputForProfanity(user, res, newBlurb);
|
|
}
|
|
|
|
if (req.body['preferences.tasks.mirrorGroupTasks'] !== undefined) {
|
|
const groupsToMirror = req.body['preferences.tasks.mirrorGroupTasks'];
|
|
if (!Array.isArray(groupsToMirror)) {
|
|
throw new BadRequest('Groups to copy tasks from must be an array.');
|
|
}
|
|
const memberGroups = _.clone(user.guilds);
|
|
if (user.party._id) memberGroups.push(user.party._id);
|
|
for (const targetGroup of groupsToMirror) {
|
|
if (memberGroups.indexOf(targetGroup) === -1) {
|
|
throw new BadRequest(`User not a member of group ${targetGroup}.`);
|
|
}
|
|
}
|
|
|
|
const matchingGroupsCount = await Groups.countDocuments({
|
|
_id: { $in: groupsToMirror },
|
|
'purchased.plan.customerId': { $exists: true },
|
|
$or: [
|
|
{ 'purchased.plan.dateTerminated': { $exists: false } },
|
|
{ 'purchased.plan.dateTerminated': null },
|
|
{ 'purchased.plan.dateTerminated': { $gt: new Date() } },
|
|
],
|
|
}).exec();
|
|
|
|
if (matchingGroupsCount !== groupsToMirror.length) {
|
|
throw new BadRequest('Groups to copy tasks from must have subscriptions.');
|
|
}
|
|
}
|
|
|
|
_.each(req.body, (val, key) => {
|
|
const purchasable = requiresPurchase[key];
|
|
|
|
if (purchasable && !checkPreferencePurchase(user, purchasable, val)) {
|
|
throw new NotAuthorized(res.t('mustPurchaseToSet', { val, key }));
|
|
}
|
|
|
|
if (key === 'party.seeking' && val === null) {
|
|
user.party.seeking = undefined;
|
|
res.analytics.track('Leaves Looking for Party', {
|
|
uuid: user._id,
|
|
hitType: 'event',
|
|
category: 'behavior',
|
|
});
|
|
} else if (key === 'tags') {
|
|
if (!Array.isArray(val)) throw new BadRequest('Tag list must be an array.');
|
|
|
|
const removedTagsIds = [];
|
|
|
|
const oldTags = [];
|
|
|
|
// Keep challenge and group tags
|
|
user.tags.forEach(t => {
|
|
if (t.group) {
|
|
oldTags.push(t);
|
|
} else {
|
|
removedTagsIds.push(t.id);
|
|
}
|
|
});
|
|
|
|
user.tags = oldTags;
|
|
|
|
val.forEach(t => {
|
|
const oldI = removedTagsIds.findIndex(id => id === t.id);
|
|
if (oldI > -1) {
|
|
removedTagsIds.splice(oldI, 1);
|
|
}
|
|
|
|
user.tags.push(t);
|
|
});
|
|
|
|
// Remove from all the tasks
|
|
// NOTE each tag to remove requires a query
|
|
|
|
promisesForTagsRemoval.push(removedTagsIds.map(tagId => Tasks.Task.update({
|
|
userId: user._id,
|
|
}, {
|
|
$pull: {
|
|
tags: tagId,
|
|
},
|
|
}, { multi: true }).exec()));
|
|
} else if (key === 'flags.newStuff' && val === false) {
|
|
// flags.newStuff was removed from the user schema and is only returned for compatibility
|
|
// reasons but we're keeping the ability to set it in API v3
|
|
const lastNewsPost = NewsPost.lastNewsPost();
|
|
if (lastNewsPost) {
|
|
user.flags.lastNewStuffRead = lastNewsPost._id;
|
|
}
|
|
} else if (acceptablePUTPaths[key]) {
|
|
let adjustedVal = val;
|
|
if (key === 'stats.lvl' && val > common.constants.MAX_LEVEL_HARD_CAP) {
|
|
adjustedVal = common.constants.MAX_LEVEL_HARD_CAP;
|
|
}
|
|
_.set(user, key, adjustedVal);
|
|
} else {
|
|
throw new NotAuthorized(res.t('messageUserOperationProtected', { operation: key }));
|
|
}
|
|
});
|
|
|
|
await Promise.all([user.save()].concat(promisesForTagsRemoval));
|
|
|
|
let userToJSON = user;
|
|
|
|
if (isV3) userToJSON = await user.toJSONWithInbox();
|
|
|
|
return res.respond(200, userToJSON);
|
|
}
|
|
|
|
export async function reset (req, res, { isV3 = false }) {
|
|
const { user } = res.locals;
|
|
|
|
const tasks = await Tasks.Task.find({
|
|
userId: user._id,
|
|
...Tasks.taskIsGroupOrChallengeQuery,
|
|
}).select('_id type challenge group').exec();
|
|
|
|
const resetRes = common.ops.reset(user, tasks);
|
|
if (isV3) {
|
|
resetRes[0].user = await resetRes[0].user.toJSONWithInbox();
|
|
}
|
|
|
|
await Promise.all([
|
|
Tasks.Task.remove({ _id: { $in: resetRes[0].tasksToRemove }, userId: user._id }),
|
|
user.save(),
|
|
]);
|
|
|
|
res.analytics.track('account reset', {
|
|
uuid: user._id,
|
|
hitType: 'event',
|
|
category: 'behavior',
|
|
});
|
|
|
|
res.respond(200, ...resetRes);
|
|
}
|
|
|
|
export async function reroll (req, res, { isV3 = false }) {
|
|
const { user } = res.locals;
|
|
const query = {
|
|
userId: user._id,
|
|
type: { $in: ['daily', 'habit', 'todo'] },
|
|
...Tasks.taskIsGroupOrChallengeQuery,
|
|
};
|
|
const tasks = await Tasks.Task.find(query).exec();
|
|
const rerollRes = await common.ops.reroll(user, tasks, req, res.analytics);
|
|
if (isV3) {
|
|
rerollRes[0].user = await rerollRes[0].user.toJSONWithInbox();
|
|
}
|
|
|
|
const promises = tasks.map(task => task.save());
|
|
promises.push(user.save());
|
|
|
|
await Promise.all(promises);
|
|
|
|
res.respond(200, ...rerollRes);
|
|
}
|
|
|
|
export async function rebirth (req, res, { isV3 = false }) {
|
|
const { user } = res.locals;
|
|
const tasks = await Tasks.Task.find({
|
|
userId: user._id,
|
|
type: { $in: ['daily', 'habit', 'todo'] },
|
|
...Tasks.taskIsGroupOrChallengeQuery,
|
|
}).exec();
|
|
|
|
const rebirthRes = await common.ops.rebirth(user, tasks, req, res.analytics);
|
|
if (isV3) {
|
|
rebirthRes[0].user = await rebirthRes[0].user.toJSONWithInbox();
|
|
}
|
|
|
|
const toSave = tasks.map(task => task.save());
|
|
|
|
toSave.push(user.save());
|
|
|
|
await Promise.all(toSave);
|
|
|
|
res.respond(200, ...rebirthRes);
|
|
}
|