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