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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id';
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
import { publishMainStream } from '../../../../stream';
import DriveFile from '../../../../models/drive-file';
import acceptAllFollowRequests from '../../../../services/following/requests/accept-all';
import { publishToFollowers } from '../../../../services/i/update';
import define from '../../define';
import getDriveFileUrl from '../../../../misc/get-drive-file-url';
export const meta = {
desc: {
'ja-JP': 'アカウント情報を更新します。',
'en-US': 'Update myself'
},
requireCredential: true,
kind: 'account-write',
params: {
name: {
validator: $.str.optional.nullable.pipe(isValidName),
desc: {
'ja-JP': '名前(ハンドルネームやニックネーム)'
}
},
description: {
validator: $.str.optional.nullable.pipe(isValidDescription),
desc: {
'ja-JP': 'アカウントの説明や自己紹介'
}
},
location: {
validator: $.str.optional.nullable.pipe(isValidLocation),
desc: {
'ja-JP': '住んでいる地域、所在'
}
},
birthday: {
validator: $.str.optional.nullable.pipe(isValidBirthday),
desc: {
'ja-JP': '誕生日 (YYYY-MM-DD形式)'
}
},
avatarId: {
validator: $.type(ID).optional.nullable,
transform: transform,
desc: {
'ja-JP': 'アイコンに設定する画像のドライブファイルID'
}
},
bannerId: {
validator: $.type(ID).optional.nullable,
transform: transform,
desc: {
'ja-JP': 'バナーに設定する画像のドライブファイルID'
}
},
wallpaperId: {
validator: $.type(ID).optional.nullable,
transform: transform,
desc: {
'ja-JP': '壁紙に設定する画像のドライブファイルID'
}
},
isLocked: {
validator: $.bool.optional,
desc: {
'ja-JP': '鍵アカウントか否か'
}
},
carefulBot: {
validator: $.bool.optional,
desc: {
'ja-JP': 'Botからのフォローを承認制にするか'
}
},
isBot: {
validator: $.bool.optional,
desc: {
'ja-JP': 'Botか否か'
}
},
isCat: {
validator: $.bool.optional,
desc: {
'ja-JP': '猫か否か'
}
},
autoWatch: {
validator: $.bool.optional,
desc: {
'ja-JP': '投稿の自動ウォッチをするか否か'
}
},
alwaysMarkNsfw: {
validator: $.bool.optional,
desc: {
'ja-JP': 'アップロードするメディアをデフォルトで「閲覧注意」として設定するか'
}
},
}
};
export default define(meta, (ps, user, app) => new Promise(async (res, rej) => {
const isSecure = user != null && app == null;
const updates = {} as any;
if (ps.name !== undefined) updates.name = ps.name;
if (ps.description !== undefined) updates.description = ps.description;
if (ps.location !== undefined) updates['profile.location'] = ps.location;
if (ps.birthday !== undefined) updates['profile.birthday'] = ps.birthday;
if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId;
if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId;
if (ps.wallpaperId !== undefined) updates.wallpaperId = ps.wallpaperId;
if (typeof ps.isLocked == 'boolean') updates.isLocked = ps.isLocked;
if (typeof ps.isBot == 'boolean') updates.isBot = ps.isBot;
if (typeof ps.carefulBot == 'boolean') updates.carefulBot = ps.carefulBot;
if (typeof ps.isCat == 'boolean') updates.isCat = ps.isCat;
if (typeof ps.autoWatch == 'boolean') updates['settings.autoWatch'] = ps.autoWatch;
if (typeof ps.alwaysMarkNsfw == 'boolean') updates['settings.alwaysMarkNsfw'] = ps.alwaysMarkNsfw;
if (ps.avatarId) {
const avatar = await DriveFile.findOne({
_id: ps.avatarId
});
if (avatar == null) return rej('avatar not found');
if (!avatar.contentType.startsWith('image/')) return rej('avatar not an image');
updates.avatarUrl = getDriveFileUrl(avatar, true);
if (avatar.metadata.properties.avgColor) {
updates.avatarColor = avatar.metadata.properties.avgColor;
}
}
if (ps.bannerId) {
const banner = await DriveFile.findOne({
_id: ps.bannerId
});
if (banner == null) return rej('banner not found');
if (!banner.contentType.startsWith('image/')) return rej('banner not an image');
updates.bannerUrl = getDriveFileUrl(banner, false);
if (banner.metadata.properties.avgColor) {
updates.bannerColor = banner.metadata.properties.avgColor;
}
}
if (ps.wallpaperId !== undefined) {
if (ps.wallpaperId === null) {
updates.wallpaperUrl = null;
updates.wallpaperColor = null;
} else {
const wallpaper = await DriveFile.findOne({
_id: ps.wallpaperId
});
if (wallpaper == null) return rej('wallpaper not found');
updates.wallpaperUrl = getDriveFileUrl(wallpaper);
if (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
publishMainStream(user._id, 'meUpdated', iObj);
// 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認
if (user.isLocked && ps.isLocked === false) {
acceptAllFollowRequests(user);
}
// フォロワーにUpdateを配信
publishToFollowers(user._id);
}));
|