2018-07-07 05:19:00 -05:00
|
|
|
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
|
2018-06-17 19:54:53 -05:00
|
|
|
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack, ILocalUser } from '../../../../models/user';
|
2018-10-06 21:06:17 -05:00
|
|
|
import { publishMainStream } from '../../../../stream';
|
2018-05-06 04:04:37 -05:00
|
|
|
import DriveFile from '../../../../models/drive-file';
|
2018-06-01 10:15:17 -05:00
|
|
|
import acceptAllFollowRequests from '../../../../services/following/requests/accept-all';
|
2018-06-17 19:54:53 -05:00
|
|
|
import { IApp } from '../../../../models/app';
|
2018-07-27 17:52:48 -05:00
|
|
|
import config from '../../../../config';
|
2018-09-01 06:17:30 -05:00
|
|
|
import { publishToFollowers } from '../../../../services/i/update';
|
2018-09-09 12:21:16 -05:00
|
|
|
import getParams from '../../get-params';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-07-16 14:36:44 -05:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-28 16:59:43 -05:00
|
|
|
'ja-JP': 'アカウント情報を更新します。',
|
|
|
|
'en-US': 'Update myself'
|
2018-07-16 14:36:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
kind: 'account-write',
|
|
|
|
|
|
|
|
params: {
|
|
|
|
name: $.str.optional.nullable.pipe(isValidName).note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '名前(ハンドルネームやニックネーム)'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
description: $.str.optional.nullable.pipe(isValidDescription).note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アカウントの説明や自己紹介'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
location: $.str.optional.nullable.pipe(isValidLocation).note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '住んでいる地域、所在'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
birthday: $.str.optional.nullable.pipe(isValidBirthday).note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '誕生日 (YYYY-MM-DD形式)'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
avatarId: $.type(ID).optional.nullable.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アイコンに設定する画像のドライブファイルID'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
bannerId: $.type(ID).optional.nullable.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'バナーに設定する画像のドライブファイルID'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
wallpaperId: $.type(ID).optional.nullable.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '壁紙に設定する画像のドライブファイルID'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
isLocked: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '鍵アカウントか否か'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-10-13 06:11:00 -05:00
|
|
|
carefulBot: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'Botからのフォローを承認制にするか'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
isBot: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'Botか否か'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
isCat: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '猫か否か'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
autoWatch: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿の自動ウォッチをするか否か'
|
|
|
|
}
|
|
|
|
}),
|
2018-09-14 06:11:01 -05:00
|
|
|
|
|
|
|
alwaysMarkNsfw: $.bool.optional.note({
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アップロードするメディアをデフォルトで「閲覧注意」として設定するか'
|
|
|
|
}
|
|
|
|
}),
|
2018-09-09 12:21:16 -05:00
|
|
|
}
|
2018-07-16 14:36:44 -05:00
|
|
|
};
|
|
|
|
|
2018-07-05 12:58:29 -05:00
|
|
|
export default async (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
|
2018-09-09 12:21:16 -05:00
|
|
|
const [ps, psErr] = getParams(meta, params);
|
2018-10-15 18:54:36 -05:00
|
|
|
if (psErr) return rej(psErr);
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-04-11 03:40:01 -05:00
|
|
|
const isSecure = user != null && app == null;
|
|
|
|
|
2018-05-31 08:56:02 -05:00
|
|
|
const updates = {} as any;
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
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;
|
2018-10-13 06:11:00 -05:00
|
|
|
if (typeof ps.carefulBot == 'boolean') updates.carefulBot = ps.carefulBot;
|
2018-09-09 12:21:16 -05:00
|
|
|
if (typeof ps.isCat == 'boolean') updates.isCat = ps.isCat;
|
|
|
|
if (typeof ps.autoWatch == 'boolean') updates['settings.autoWatch'] = ps.autoWatch;
|
2018-09-14 06:11:01 -05:00
|
|
|
if (typeof ps.alwaysMarkNsfw == 'boolean') updates['settings.alwaysMarkNsfw'] = ps.alwaysMarkNsfw;
|
2018-09-09 12:21:16 -05:00
|
|
|
|
|
|
|
if (ps.avatarId) {
|
2018-05-06 04:04:37 -05:00
|
|
|
const avatar = await DriveFile.findOne({
|
2018-09-09 12:21:16 -05:00
|
|
|
_id: ps.avatarId
|
2018-05-06 04:04:37 -05:00
|
|
|
});
|
|
|
|
|
2018-07-27 17:52:48 -05:00
|
|
|
if (avatar == null) return rej('avatar not found');
|
2018-09-09 12:09:33 -05:00
|
|
|
if (!avatar.contentType.startsWith('image/')) return rej('avatar not an image');
|
2018-07-27 17:52:48 -05:00
|
|
|
|
2018-08-25 05:44:47 -05:00
|
|
|
updates.avatarUrl = avatar.metadata.thumbnailUrl || avatar.metadata.url || `${config.drive_url}/${avatar._id}`;
|
2018-07-27 17:52:48 -05:00
|
|
|
|
|
|
|
if (avatar.metadata.properties.avgColor) {
|
2018-05-31 08:56:02 -05:00
|
|
|
updates.avatarColor = avatar.metadata.properties.avgColor;
|
2018-05-06 04:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
if (ps.bannerId) {
|
2018-05-06 04:04:37 -05:00
|
|
|
const banner = await DriveFile.findOne({
|
2018-09-09 12:21:16 -05:00
|
|
|
_id: ps.bannerId
|
2018-05-06 04:04:37 -05:00
|
|
|
});
|
|
|
|
|
2018-07-27 17:52:48 -05:00
|
|
|
if (banner == null) return rej('banner not found');
|
2018-09-09 12:09:33 -05:00
|
|
|
if (!banner.contentType.startsWith('image/')) return rej('banner not an image');
|
2018-07-27 17:52:48 -05:00
|
|
|
|
|
|
|
updates.bannerUrl = banner.metadata.url || `${config.drive_url}/${banner._id}`;
|
|
|
|
|
|
|
|
if (banner.metadata.properties.avgColor) {
|
2018-05-31 08:56:02 -05:00
|
|
|
updates.bannerColor = banner.metadata.properties.avgColor;
|
2018-05-06 04:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
if (ps.wallpaperId !== undefined) {
|
|
|
|
if (ps.wallpaperId === null) {
|
2018-08-18 14:01:10 -05:00
|
|
|
updates.wallpaperUrl = null;
|
|
|
|
updates.wallpaperColor = null;
|
|
|
|
} else {
|
|
|
|
const wallpaper = await DriveFile.findOne({
|
2018-09-09 12:21:16 -05:00
|
|
|
_id: ps.wallpaperId
|
2018-08-18 14:01:10 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (wallpaper == null) return rej('wallpaper not found');
|
|
|
|
|
|
|
|
updates.wallpaperUrl = wallpaper.metadata.url || `${config.drive_url}/${wallpaper._id}`;
|
|
|
|
|
|
|
|
if (wallpaper.metadata.properties.avgColor) {
|
|
|
|
updates.wallpaperColor = wallpaper.metadata.properties.avgColor;
|
|
|
|
}
|
2018-06-06 15:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:37:50 -06:00
|
|
|
await User.update(user._id, {
|
2018-05-31 08:56:02 -05:00
|
|
|
$set: updates
|
2017-02-08 10:37:50 -06:00
|
|
|
});
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Serialize
|
2018-06-02 02:27:24 -05:00
|
|
|
const iObj = await pack(user._id, user, {
|
2016-12-28 16:49:51 -06:00
|
|
|
detail: true,
|
|
|
|
includeSecrets: isSecure
|
2017-01-21 00:30:40 -06:00
|
|
|
});
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Send response
|
|
|
|
res(iObj);
|
|
|
|
|
2018-06-02 02:27:24 -05:00
|
|
|
// Publish meUpdated event
|
2018-10-06 21:06:17 -05:00
|
|
|
publishMainStream(user._id, 'meUpdated', iObj);
|
2018-05-31 08:56:02 -05:00
|
|
|
|
|
|
|
// 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認
|
2018-09-09 12:21:16 -05:00
|
|
|
if (user.isLocked && ps.isLocked === false) {
|
2018-05-31 08:56:02 -05:00
|
|
|
acceptAllFollowRequests(user);
|
|
|
|
}
|
2018-09-01 06:17:30 -05:00
|
|
|
|
|
|
|
// フォロワーにUpdateを配信
|
|
|
|
publishToFollowers(user._id);
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|