summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/i/update.ts
blob: 4002bcdc39e25eee3d86b177c4db9c0e234176d9 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack, ILocalUser } from '../../../../models/user';
import event from '../../../../stream';
import DriveFile from '../../../../models/drive-file';
import acceptAllFollowRequests from '../../../../services/following/requests/accept-all';
import { IApp } from '../../../../models/app';

export const meta = {
	desc: {
		ja: 'アカウント情報を更新します。',
		en: 'Update myself'
	},

	requireCredential: true,

	kind: 'account-write'
};

export default async (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
	const isSecure = user != null && app == null;

	const updates = {} as any;

	// Get 'name' parameter
	const [name, nameErr] = $.str.optional.nullable.pipe(isValidName).get(params.name);
	if (nameErr) return rej('invalid name param');
	if (name) updates.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) updates.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) updates['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) updates['profile.birthday'] = birthday;

	// Get 'avatarId' parameter
	const [avatarId, avatarIdErr] = $.type(ID).optional.nullable.get(params.avatarId);
	if (avatarIdErr) return rej('invalid avatarId param');
	if (avatarId !== undefined) updates.avatarId = avatarId;

	// Get 'bannerId' parameter
	const [bannerId, bannerIdErr] = $.type(ID).optional.nullable.get(params.bannerId);
	if (bannerIdErr) return rej('invalid bannerId param');
	if (bannerId !== undefined) updates.bannerId = bannerId;

	// Get 'wallpaperId' parameter
	const [wallpaperId, wallpaperIdErr] = $.type(ID).optional.nullable.get(params.wallpaperId);
	if (wallpaperIdErr) return rej('invalid wallpaperId param');
	if (wallpaperId !== undefined) updates.wallpaperId = wallpaperId;

	// Get 'isLocked' parameter
	const [isLocked, isLockedErr] = $.bool.optional.get(params.isLocked);
	if (isLockedErr) return rej('invalid isLocked param');
	if (isLocked != null) updates.isLocked = isLocked;

	// Get 'isBot' parameter
	const [isBot, isBotErr] = $.bool.optional.get(params.isBot);
	if (isBotErr) return rej('invalid isBot param');
	if (isBot != null) updates.isBot = isBot;

	// Get 'isCat' parameter
	const [isCat, isCatErr] = $.bool.optional.get(params.isCat);
	if (isCatErr) return rej('invalid isCat param');
	if (isCat != null) updates.isCat = isCat;

	// Get 'autoWatch' parameter
	const [autoWatch, autoWatchErr] = $.bool.optional.get(params.autoWatch);
	if (autoWatchErr) return rej('invalid autoWatch param');
	if (autoWatch != null) updates['settings.autoWatch'] = autoWatch;

	if (avatarId) {
		const avatar = await DriveFile.findOne({
			_id: avatarId
		});

		if (avatar != null && avatar.metadata.properties.avgColor) {
			updates.avatarColor = avatar.metadata.properties.avgColor;
		}
	}

	if (bannerId) {
		const banner = await DriveFile.findOne({
			_id: bannerId
		});

		if (banner != null && banner.metadata.properties.avgColor) {
			updates.bannerColor = banner.metadata.properties.avgColor;
		}
	}

	if (wallpaperId) {
		const wallpaper = await DriveFile.findOne({
			_id: wallpaperId
		});

		if (wallpaper != null && wallpaper.metadata.properties.avgColor) {
			updates.wallpaperColor = wallpaper.metadata.properties.avgColor;
		}
	}

	await User.update(user._id, {
		$set: updates
	});

	// Serialize
	const iObj = await pack(user._id, user, {
		detail: true,
		includeSecrets: isSecure
	});

	// Send response
	res(iObj);

	// Publish meUpdated event
	event(user._id, 'meUpdated', iObj);

	// 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認
	if (user.isLocked && isLocked === false) {
		acceptAllFollowRequests(user);
	}
});