2019-02-05 04:48:08 +02:00
|
|
|
import $ from 'cafy';
|
2021-03-23 10:43:07 +02:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
2021-03-21 08:14:03 +02:00
|
|
|
import { publishMainStream, publishUserEvent } from '../../../../services/stream';
|
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';
|
2019-01-30 09:56:27 +02:00
|
|
|
import { parse, parsePlain } from '../../../../mfm/parse';
|
2021-03-23 10:43:07 +02:00
|
|
|
import extractEmojis from '@/misc/extract-emojis';
|
|
|
|
import extractHashtags from '@/misc/extract-hashtags';
|
2019-02-03 16:41:05 +02:00
|
|
|
import * as langmap from 'langmap';
|
2019-08-18 06:47:45 +03:00
|
|
|
import { updateUsertags } from '../../../../services/update-hashtag';
|
2019-02-22 04:46:58 +02:00
|
|
|
import { ApiError } from '../../error';
|
2019-07-07 00:56:13 +03:00
|
|
|
import { Users, DriveFiles, UserProfiles, Pages } from '../../../../models';
|
2019-04-10 09:04:27 +03:00
|
|
|
import { User } from '../../../../models/entities/user';
|
|
|
|
import { UserProfile } from '../../../../models/entities/user-profile';
|
2020-08-22 04:06:17 +03:00
|
|
|
import { notificationTypes } from '../../../../types';
|
2021-03-23 10:43:07 +02:00
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search';
|
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
|
|
|
},
|
|
|
|
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['account'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: true as const,
|
2018-07-16 22:36:44 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
kind: 'write:account',
|
2018-09-09 20:21:16 +03:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 20:32:24 +02:00
|
|
|
name: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.nullable.use(Users.validateName),
|
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: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.nullable.use(Users.validateDescription),
|
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: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.nullable.str.or(Object.keys(langmap)),
|
2018-12-03 13:08:18 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '言語'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
location: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.nullable.use(Users.validateLocation),
|
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: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.nullable.use(Users.validateBirthday),
|
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: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-09-09 20:21:16 +03:00
|
|
|
desc: {
|
2019-08-25 10:11:20 +03:00
|
|
|
'ja-JP': 'アバターに設定する画像のドライブファイルID'
|
2018-09-09 20:21:16 +03:00
|
|
|
}
|
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: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.nullable.type(ID),
|
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
|
|
|
|
2019-07-17 18:11:39 +03:00
|
|
|
fields: {
|
|
|
|
validator: $.optional.arr($.object()).range(1, 4),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'プロフィール補足情報'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
isLocked: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
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
|
|
|
|
2020-12-11 14:16:20 +02:00
|
|
|
isExplorable: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
carefulBot: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
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: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-12-28 14:36:58 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'フォローしているユーザーからのフォローリクエストを自動承認するか'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-25 14:31:34 +02:00
|
|
|
noCrawle: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '検索エンジンによるインデックスを拒否するか否か'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
isBot: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
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: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
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
|
|
|
|
2020-02-18 12:05:11 +02:00
|
|
|
injectFeaturedNote: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-02-06 15:47:15 +02:00
|
|
|
receiveAnnouncementEmail: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
alwaysMarkNsfw: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-09-14 14:11:01 +03:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アップロードするメディアをデフォルトで「閲覧注意」として設定するか'
|
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2019-07-07 00:56:13 +03:00
|
|
|
|
|
|
|
pinnedPageId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ピン留めするページID'
|
|
|
|
}
|
2020-07-27 07:34:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
mutedWords: {
|
|
|
|
validator: $.optional.arr($.arr($.str))
|
|
|
|
},
|
2020-08-22 04:06:17 +03:00
|
|
|
|
2020-09-18 16:18:21 +03:00
|
|
|
mutingNotificationTypes: {
|
2020-08-22 04:06:17 +03:00
|
|
|
validator: $.optional.arr($.str.or(notificationTypes as unknown as string[]))
|
|
|
|
},
|
2021-02-13 05:28:26 +02:00
|
|
|
|
|
|
|
emailNotificationTypes: {
|
|
|
|
validator: $.optional.arr($.str)
|
|
|
|
},
|
2019-02-22 04:46:58 +02: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-07-07 00:56:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
|
|
|
id: '8e01b590-7eb9-431b-a239-860e086c408e'
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
ref: 'User'
|
2018-09-09 20:21:16 +03:00
|
|
|
}
|
2018-07-16 22:36:44 +03:00
|
|
|
};
|
|
|
|
|
2020-03-28 11:07:41 +02:00
|
|
|
export default define(meta, async (ps, user, token) => {
|
|
|
|
const isSecure = token == null;
|
|
|
|
|
2019-04-10 09:04:27 +03:00
|
|
|
const updates = {} as Partial<User>;
|
2019-04-17 08:30:31 +03:00
|
|
|
const profileUpdates = {} as Partial<UserProfile>;
|
|
|
|
|
2021-02-13 08:33:38 +02:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
2018-05-31 16:56:02 +03:00
|
|
|
|
2018-09-09 20:21:16 +03:00
|
|
|
if (ps.name !== undefined) updates.name = ps.name;
|
2019-04-17 08:30:31 +03:00
|
|
|
if (ps.description !== undefined) profileUpdates.description = ps.description;
|
2021-02-13 05:28:26 +02:00
|
|
|
if (ps.lang !== undefined) profileUpdates.lang = ps.lang;
|
2019-04-17 08:30:31 +03:00
|
|
|
if (ps.location !== undefined) profileUpdates.location = ps.location;
|
|
|
|
if (ps.birthday !== undefined) profileUpdates.birthday = ps.birthday;
|
2018-09-09 20:21:16 +03:00
|
|
|
if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId;
|
|
|
|
if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId;
|
2020-07-27 07:34:20 +03:00
|
|
|
if (ps.mutedWords !== undefined) {
|
|
|
|
profileUpdates.mutedWords = ps.mutedWords;
|
|
|
|
profileUpdates.enableWordMute = ps.mutedWords.length > 0;
|
|
|
|
}
|
2020-09-18 16:18:21 +03:00
|
|
|
if (ps.mutingNotificationTypes !== undefined) profileUpdates.mutingNotificationTypes = ps.mutingNotificationTypes as typeof notificationTypes[number][];
|
2020-04-04 02:46:54 +03:00
|
|
|
if (typeof ps.isLocked === 'boolean') updates.isLocked = ps.isLocked;
|
2020-12-11 14:16:20 +02:00
|
|
|
if (typeof ps.isExplorable === 'boolean') updates.isExplorable = ps.isExplorable;
|
2020-04-04 02:46:54 +03:00
|
|
|
if (typeof ps.isBot === 'boolean') updates.isBot = ps.isBot;
|
|
|
|
if (typeof ps.carefulBot === 'boolean') profileUpdates.carefulBot = ps.carefulBot;
|
|
|
|
if (typeof ps.autoAcceptFollowed === 'boolean') profileUpdates.autoAcceptFollowed = ps.autoAcceptFollowed;
|
2020-11-25 14:31:34 +02:00
|
|
|
if (typeof ps.noCrawle === 'boolean') profileUpdates.noCrawle = ps.noCrawle;
|
2020-04-04 02:46:54 +03:00
|
|
|
if (typeof ps.isCat === 'boolean') updates.isCat = ps.isCat;
|
|
|
|
if (typeof ps.injectFeaturedNote === 'boolean') profileUpdates.injectFeaturedNote = ps.injectFeaturedNote;
|
2021-02-06 15:47:15 +02:00
|
|
|
if (typeof ps.receiveAnnouncementEmail === 'boolean') profileUpdates.receiveAnnouncementEmail = ps.receiveAnnouncementEmail;
|
2020-04-04 02:46:54 +03:00
|
|
|
if (typeof ps.alwaysMarkNsfw === 'boolean') profileUpdates.alwaysMarkNsfw = ps.alwaysMarkNsfw;
|
2021-02-13 05:28:26 +02:00
|
|
|
if (ps.emailNotificationTypes !== undefined) profileUpdates.emailNotificationTypes = ps.emailNotificationTypes;
|
2018-09-09 20:21:16 +03:00
|
|
|
|
|
|
|
if (ps.avatarId) {
|
2019-04-07 15:50:36 +03:00
|
|
|
const avatar = await DriveFiles.findOne(ps.avatarId);
|
2018-05-06 12:04:37 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (avatar == null || avatar.userId !== user.id) throw new ApiError(meta.errors.noSuchAvatar);
|
|
|
|
if (!avatar.type.startsWith('image/')) throw new ApiError(meta.errors.avatarNotAnImage);
|
2018-07-28 01:52:48 +03:00
|
|
|
|
2019-04-09 17:40:55 +03:00
|
|
|
updates.avatarUrl = DriveFiles.getPublicUrl(avatar, true);
|
2018-07-28 01:52:48 +03:00
|
|
|
|
2020-07-18 18:24:07 +03:00
|
|
|
if (avatar.blurhash) {
|
|
|
|
updates.avatarBlurhash = avatar.blurhash;
|
2018-05-06 12:04:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 20:21:16 +03:00
|
|
|
if (ps.bannerId) {
|
2019-04-07 15:50:36 +03:00
|
|
|
const banner = await DriveFiles.findOne(ps.bannerId);
|
2018-05-06 12:04:37 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (banner == null || banner.userId !== user.id) throw new ApiError(meta.errors.noSuchBanner);
|
|
|
|
if (!banner.type.startsWith('image/')) throw new ApiError(meta.errors.bannerNotAnImage);
|
2018-07-28 01:52:48 +03:00
|
|
|
|
2019-04-09 17:40:55 +03:00
|
|
|
updates.bannerUrl = DriveFiles.getPublicUrl(banner, false);
|
2018-07-28 01:52:48 +03:00
|
|
|
|
2020-07-18 18:24:07 +03:00
|
|
|
if (banner.blurhash) {
|
|
|
|
updates.bannerBlurhash = banner.blurhash;
|
2018-06-06 23:14:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:56:13 +03:00
|
|
|
if (ps.pinnedPageId) {
|
|
|
|
const page = await Pages.findOne(ps.pinnedPageId);
|
|
|
|
|
|
|
|
if (page == null || page.userId !== user.id) throw new ApiError(meta.errors.noSuchPage);
|
|
|
|
|
|
|
|
profileUpdates.pinnedPageId = page.id;
|
|
|
|
} else if (ps.pinnedPageId === null) {
|
|
|
|
profileUpdates.pinnedPageId = null;
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:11:39 +03:00
|
|
|
if (ps.fields) {
|
|
|
|
profileUpdates.fields = ps.fields
|
|
|
|
.filter(x => typeof x.name === 'string' && x.name !== '' && typeof x.value === 'string' && x.value !== '')
|
|
|
|
.map(x => {
|
|
|
|
return { name: x.name, value: x.value };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:42:45 +02:00
|
|
|
//#region emojis/tags
|
2019-04-17 08:30:31 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
let emojis = [] as string[];
|
|
|
|
let tags = [] as string[];
|
2018-12-06 03:02:04 +02:00
|
|
|
|
2019-04-17 08:30:31 +03:00
|
|
|
const newName = updates.name === undefined ? user.name : updates.name;
|
|
|
|
const newDescription = profileUpdates.description === undefined ? profile.description : profileUpdates.description;
|
|
|
|
|
|
|
|
if (newName != null) {
|
|
|
|
const tokens = parsePlain(newName);
|
2019-04-12 19:43:22 +03:00
|
|
|
emojis = emojis.concat(extractEmojis(tokens!));
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
2018-12-06 03:02:04 +02:00
|
|
|
|
2019-04-17 08:30:31 +03:00
|
|
|
if (newDescription != null) {
|
|
|
|
const tokens = parse(newDescription);
|
2019-04-12 19:43:22 +03:00
|
|
|
emojis = emojis.concat(extractEmojis(tokens!));
|
2021-02-07 03:43:34 +02:00
|
|
|
tags = extractHashtags(tokens!).map(tag => normalizeForSearch(tag)).splice(0, 32);
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
2018-12-06 03:02:04 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
updates.emojis = emojis;
|
|
|
|
updates.tags = tags;
|
2019-02-17 16:41:47 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
// ハッシュタグ更新
|
2019-08-18 06:47:45 +03:00
|
|
|
updateUsertags(user, tags);
|
2018-12-06 03:02:04 +02:00
|
|
|
//#endregion
|
|
|
|
|
2019-04-10 12:13:33 +03:00
|
|
|
if (Object.keys(updates).length > 0) await Users.update(user.id, updates);
|
2020-02-01 00:16:52 +02:00
|
|
|
if (Object.keys(profileUpdates).length > 0) await UserProfiles.update(user.id, profileUpdates);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const iObj = await Users.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
|
|
|
|
2018-06-02 10:27:24 +03:00
|
|
|
// Publish meUpdated event
|
2019-04-07 15:50:36 +03:00
|
|
|
publishMainStream(user.id, 'meUpdated', iObj);
|
2021-03-21 08:14:03 +02:00
|
|
|
publishUserEvent(user.id, 'updateUserProfile', await UserProfiles.findOne(user.id));
|
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を配信
|
2019-04-07 15:50:36 +03:00
|
|
|
publishToFollowers(user.id);
|
2019-02-22 04:46:58 +02:00
|
|
|
|
|
|
|
return iObj;
|
|
|
|
});
|