hippofish/src/server/api/endpoints/i/update.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
2018-04-24 11:13:06 +02:00
import $ from 'cafy'; import ID from '../../../../cafy-id';
2018-03-29 13:32:18 +02:00
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
2018-04-02 06:33:46 +02:00
import event from '../../../../publishers/stream';
2016-12-28 23:49:51 +01:00
/**
* Update myself
*/
module.exports = async (params, user, app) => new Promise(async (res, rej) => {
const isSecure = user != null && app == null;
2016-12-28 23:49:51 +01:00
// Get 'name' parameter
2018-05-02 11:06:16 +02:00
const [name, nameErr] = $.str.optional().nullable().pipe(isValidName).get(params.name);
2017-03-03 00:56:07 +01:00
if (nameErr) return rej('invalid name param');
2017-03-03 01:15:38 +01:00
if (name) user.name = name;
2016-12-28 23:49:51 +01:00
2017-02-23 09:19:52 +01:00
// Get 'description' parameter
2018-05-02 11:06:16 +02:00
const [description, descriptionErr] = $.str.optional().nullable().pipe(isValidDescription).get(params.description);
2017-03-03 01:24:17 +01:00
if (descriptionErr) return rej('invalid description param');
if (description !== undefined) user.description = description;
2017-02-23 09:19:52 +01:00
2016-12-28 23:49:51 +01:00
// Get 'location' parameter
2018-05-02 11:06:16 +02:00
const [location, locationErr] = $.str.optional().nullable().pipe(isValidLocation).get(params.location);
2017-03-03 01:24:17 +01:00
if (locationErr) return rej('invalid location param');
2018-04-07 20:58:11 +02:00
if (location !== undefined) user.profile.location = location;
2016-12-28 23:49:51 +01:00
2017-01-06 07:35:07 +01:00
// Get 'birthday' parameter
2018-05-02 11:06:16 +02:00
const [birthday, birthdayErr] = $.str.optional().nullable().pipe(isValidBirthday).get(params.birthday);
2017-03-03 01:24:17 +01:00
if (birthdayErr) return rej('invalid birthday param');
2018-04-07 20:58:11 +02:00
if (birthday !== undefined) user.profile.birthday = birthday;
2017-01-06 07:35:07 +01:00
2018-03-29 07:48:47 +02:00
// Get 'avatarId' parameter
2018-05-02 11:06:16 +02:00
const [avatarId, avatarIdErr] = $.type(ID).optional().get(params.avatarId);
2018-03-29 07:48:47 +02:00
if (avatarIdErr) return rej('invalid avatarId param');
if (avatarId) user.avatarId = avatarId;
2016-12-28 23:49:51 +01:00
2018-03-29 07:48:47 +02:00
// Get 'bannerId' parameter
2018-05-02 11:06:16 +02:00
const [bannerId, bannerIdErr] = $.type(ID).optional().get(params.bannerId);
2018-03-29 07:48:47 +02:00
if (bannerIdErr) return rej('invalid bannerId param');
if (bannerId) user.bannerId = bannerId;
2016-12-28 23:49:51 +01:00
2018-03-29 07:48:47 +02:00
// Get 'isBot' parameter
2018-05-02 11:06:16 +02:00
const [isBot, isBotErr] = $.bool.optional().get(params.isBot);
2018-03-29 07:48:47 +02:00
if (isBotErr) return rej('invalid isBot param');
2018-04-07 20:58:11 +02:00
if (isBot != null) user.isBot = isBot;
2018-03-01 22:26:31 +01:00
2018-03-29 07:48:47 +02:00
// Get 'autoWatch' parameter
2018-05-02 11:06:16 +02:00
const [autoWatch, autoWatchErr] = $.bool.optional().get(params.autoWatch);
2018-03-29 07:48:47 +02:00
if (autoWatchErr) return rej('invalid autoWatch param');
2018-04-07 20:58:11 +02:00
if (autoWatch != null) user.settings.autoWatch = autoWatch;
2018-03-05 00:07:09 +01:00
2017-02-08 17:37:50 +01:00
await User.update(user._id, {
2017-02-08 18:20:47 +01:00
$set: {
name: user.name,
2017-02-23 09:19:52 +01:00
description: user.description,
2018-03-29 07:48:47 +02:00
avatarId: user.avatarId,
bannerId: user.bannerId,
2018-04-07 22:02:50 +02:00
profile: user.profile,
isBot: user.isBot,
settings: user.settings
2017-02-08 18:20:47 +01:00
}
2017-02-08 17:37:50 +01:00
});
2016-12-28 23:49:51 +01:00
// Serialize
2018-02-02 00:21:30 +01:00
const iObj = await pack(user, user, {
2016-12-28 23:49:51 +01:00
detail: true,
includeSecrets: isSecure
2017-01-21 07:30:40 +01:00
});
2016-12-28 23:49:51 +01:00
// Send response
res(iObj);
// Publish i updated event
event(user._id, 'i_updated', iObj);
});