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