1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
import event from '../../../../publishers/stream';
/**
* Update myself
*/
module.exports = async (params, user, app) => new Promise(async (res, rej) => {
const isSecure = user != null && app == null;
// Get 'name' parameter
const [name, nameErr] = $.str.optional().nullable().pipe(isValidName).get(params.name);
if (nameErr) return rej('invalid name param');
if (name) user.name = name;
// Get 'description' parameter
const [description, descriptionErr] = $.str.optional().nullable().pipe(isValidDescription).get(params.description);
if (descriptionErr) return rej('invalid description param');
if (description !== undefined) user.description = description;
// Get 'location' parameter
const [location, locationErr] = $.str.optional().nullable().pipe(isValidLocation).get(params.location);
if (locationErr) return rej('invalid location param');
if (location !== undefined) user.profile.location = location;
// Get 'birthday' parameter
const [birthday, birthdayErr] = $.str.optional().nullable().pipe(isValidBirthday).get(params.birthday);
if (birthdayErr) return rej('invalid birthday param');
if (birthday !== undefined) user.profile.birthday = birthday;
// Get 'avatarId' parameter
const [avatarId, avatarIdErr] = $.type(ID).optional().get(params.avatarId);
if (avatarIdErr) return rej('invalid avatarId param');
if (avatarId) user.avatarId = avatarId;
// Get 'bannerId' parameter
const [bannerId, bannerIdErr] = $.type(ID).optional().get(params.bannerId);
if (bannerIdErr) return rej('invalid bannerId param');
if (bannerId) user.bannerId = bannerId;
// Get 'isBot' parameter
const [isBot, isBotErr] = $.bool.optional().get(params.isBot);
if (isBotErr) return rej('invalid isBot param');
if (isBot != null) user.isBot = isBot;
// Get 'autoWatch' parameter
const [autoWatch, autoWatchErr] = $.bool.optional().get(params.autoWatch);
if (autoWatchErr) return rej('invalid autoWatch param');
if (autoWatch != null) user.settings.autoWatch = autoWatch;
await User.update(user._id, {
$set: {
name: user.name,
description: user.description,
avatarId: user.avatarId,
bannerId: user.bannerId,
profile: user.profile,
isBot: user.isBot,
settings: user.settings
}
});
// Serialize
const iObj = await pack(user, user, {
detail: true,
includeSecrets: isSecure
});
// Send response
res(iObj);
// Publish i updated event
event(user._id, 'i_updated', iObj);
});
|