2019-02-05 02:48:08 +00:00
|
|
|
import $ from 'cafy';
|
2021-04-02 01:36:11 +00:00
|
|
|
import * as mfm from 'mfm-js';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream';
|
|
|
|
import acceptAllFollowRequests from '@/services/following/requests/accept-all';
|
|
|
|
import { publishToFollowers } from '@/services/i/update';
|
|
|
|
import define from '../../define';
|
|
|
|
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm';
|
|
|
|
import { extractHashtags } from '@/misc/extract-hashtags';
|
2019-02-03 14:41:05 +00:00
|
|
|
import * as langmap from 'langmap';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { updateUsertags } from '@/services/update-hashtag';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { Users, DriveFiles, UserProfiles, Pages } from '@/models/index';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { UserProfile } from '@/models/entities/user-profile';
|
2021-10-09 03:47:40 +00:00
|
|
|
import { notificationTypes } from '@/types';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-07-16 19:36:44 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['account'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:account',
|
2018-09-09 17:21:16 +00:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 18:32:24 +00:00
|
|
|
name: {
|
2019-06-14 15:07:41 +00:00
|
|
|
validator: $.optional.nullable.use(Users.validateName),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
description: {
|
2019-06-14 15:07:41 +00:00
|
|
|
validator: $.optional.nullable.use(Users.validateDescription),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-12-03 11:08:18 +00:00
|
|
|
lang: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.nullable.str.or(Object.keys(langmap)),
|
2018-12-03 11:08:18 +00:00
|
|
|
},
|
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
location: {
|
2019-06-14 15:07:41 +00:00
|
|
|
validator: $.optional.nullable.use(Users.validateLocation),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
birthday: {
|
2019-06-14 15:07:41 +00:00
|
|
|
validator: $.optional.nullable.use(Users.validateBirthday),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
avatarId: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
bannerId: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2019-07-17 15:11:39 +00:00
|
|
|
fields: {
|
|
|
|
validator: $.optional.arr($.object()).range(1, 4),
|
|
|
|
},
|
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
isLocked: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2020-12-11 12:16:20 +00:00
|
|
|
isExplorable: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-04-17 06:30:26 +00:00
|
|
|
hideOnlineStatus: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-10-17 16:16:59 +00:00
|
|
|
publicReactions: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-11-07 09:04:32 +00:00
|
|
|
ffVisibility: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
},
|
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
carefulBot: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-10-13 11:11:00 +00:00
|
|
|
|
2018-12-28 12:36:58 +00:00
|
|
|
autoAcceptFollowed: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-12-28 12:36:58 +00:00
|
|
|
},
|
|
|
|
|
2020-11-25 12:31:34 +00:00
|
|
|
noCrawle: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
isBot: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
isCat: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-09-09 17:21:16 +00:00
|
|
|
|
2020-02-18 10:05:11 +00:00
|
|
|
injectFeaturedNote: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-02-06 13:47:15 +00:00
|
|
|
receiveAnnouncementEmail: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
alwaysMarkNsfw: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2019-07-06 21:56:13 +00:00
|
|
|
|
|
|
|
pinnedPageId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
2020-07-27 04:34:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mutedWords: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.arr($.arr($.str)),
|
2020-07-27 04:34:20 +00:00
|
|
|
},
|
2020-08-22 01:06:17 +00:00
|
|
|
|
2021-12-09 12:38:56 +00:00
|
|
|
mutedInstances: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.arr($.str),
|
2021-12-09 12:38:56 +00:00
|
|
|
},
|
|
|
|
|
2020-09-18 13:18:21 +00:00
|
|
|
mutingNotificationTypes: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.arr($.str.or(notificationTypes as unknown as string[])),
|
2020-08-22 01:06:17 +00:00
|
|
|
},
|
2021-02-13 03:28:26 +00:00
|
|
|
|
|
|
|
emailNotificationTypes: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.arr($.str),
|
2021-02-13 03:28:26 +00:00
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchAvatar: {
|
|
|
|
message: 'No such avatar file.',
|
|
|
|
code: 'NO_SUCH_AVATAR',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '539f3a45-f215-4f81-a9a8-31293640207f',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchBanner: {
|
|
|
|
message: 'No such banner file.',
|
|
|
|
code: 'NO_SUCH_BANNER',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '0d8f5629-f210-41c2-9433-735831a58595',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
avatarNotAnImage: {
|
|
|
|
message: 'The file specified as an avatar is not an image.',
|
|
|
|
code: 'AVATAR_NOT_AN_IMAGE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'f419f9f8-2f4d-46b1-9fb4-49d3a2fd7191',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
bannerNotAnImage: {
|
|
|
|
message: 'The file specified as a banner is not an image.',
|
|
|
|
code: 'BANNER_NOT_AN_IMAGE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '75aedb19-2afd-4e6d-87fc-67941256fa60',
|
2019-07-06 21:56:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '8e01b590-7eb9-431b-a239-860e086c408e',
|
2019-07-06 21:56:13 +00:00
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'MeDetailed',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2021-03-24 02:05:37 +00:00
|
|
|
export default define(meta, async (ps, _user, token) => {
|
|
|
|
const user = await Users.findOneOrFail(_user.id);
|
2020-03-28 09:07:41 +00:00
|
|
|
const isSecure = token == null;
|
|
|
|
|
2019-04-10 06:04:27 +00:00
|
|
|
const updates = {} as Partial<User>;
|
2019-04-17 05:30:31 +00:00
|
|
|
const profileUpdates = {} as Partial<UserProfile>;
|
|
|
|
|
2021-02-13 06:33:38 +00:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
2018-05-31 13:56:02 +00:00
|
|
|
|
2018-09-09 17:21:16 +00:00
|
|
|
if (ps.name !== undefined) updates.name = ps.name;
|
2019-04-17 05:30:31 +00:00
|
|
|
if (ps.description !== undefined) profileUpdates.description = ps.description;
|
2021-02-13 03:28:26 +00:00
|
|
|
if (ps.lang !== undefined) profileUpdates.lang = ps.lang;
|
2019-04-17 05:30:31 +00:00
|
|
|
if (ps.location !== undefined) profileUpdates.location = ps.location;
|
|
|
|
if (ps.birthday !== undefined) profileUpdates.birthday = ps.birthday;
|
2021-11-07 09:04:32 +00:00
|
|
|
if (ps.ffVisibility !== undefined) profileUpdates.ffVisibility = ps.ffVisibility;
|
2018-09-09 17:21:16 +00:00
|
|
|
if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId;
|
|
|
|
if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId;
|
2020-07-27 04:34:20 +00:00
|
|
|
if (ps.mutedWords !== undefined) {
|
|
|
|
profileUpdates.mutedWords = ps.mutedWords;
|
|
|
|
profileUpdates.enableWordMute = ps.mutedWords.length > 0;
|
|
|
|
}
|
2021-12-09 12:38:56 +00:00
|
|
|
if (ps.mutedInstances !== undefined) profileUpdates.mutedInstances = ps.mutedInstances;
|
2020-09-18 13:18:21 +00:00
|
|
|
if (ps.mutingNotificationTypes !== undefined) profileUpdates.mutingNotificationTypes = ps.mutingNotificationTypes as typeof notificationTypes[number][];
|
2020-04-03 23:46:54 +00:00
|
|
|
if (typeof ps.isLocked === 'boolean') updates.isLocked = ps.isLocked;
|
2020-12-11 12:16:20 +00:00
|
|
|
if (typeof ps.isExplorable === 'boolean') updates.isExplorable = ps.isExplorable;
|
2021-04-17 06:30:26 +00:00
|
|
|
if (typeof ps.hideOnlineStatus === 'boolean') updates.hideOnlineStatus = ps.hideOnlineStatus;
|
2021-10-17 16:16:59 +00:00
|
|
|
if (typeof ps.publicReactions === 'boolean') profileUpdates.publicReactions = ps.publicReactions;
|
2020-04-03 23:46:54 +00: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 12:31:34 +00:00
|
|
|
if (typeof ps.noCrawle === 'boolean') profileUpdates.noCrawle = ps.noCrawle;
|
2020-04-03 23:46:54 +00:00
|
|
|
if (typeof ps.isCat === 'boolean') updates.isCat = ps.isCat;
|
|
|
|
if (typeof ps.injectFeaturedNote === 'boolean') profileUpdates.injectFeaturedNote = ps.injectFeaturedNote;
|
2021-02-06 13:47:15 +00:00
|
|
|
if (typeof ps.receiveAnnouncementEmail === 'boolean') profileUpdates.receiveAnnouncementEmail = ps.receiveAnnouncementEmail;
|
2020-04-03 23:46:54 +00:00
|
|
|
if (typeof ps.alwaysMarkNsfw === 'boolean') profileUpdates.alwaysMarkNsfw = ps.alwaysMarkNsfw;
|
2021-02-13 03:28:26 +00:00
|
|
|
if (ps.emailNotificationTypes !== undefined) profileUpdates.emailNotificationTypes = ps.emailNotificationTypes;
|
2018-09-09 17:21:16 +00:00
|
|
|
|
|
|
|
if (ps.avatarId) {
|
2019-04-07 12:50:36 +00:00
|
|
|
const avatar = await DriveFiles.findOne(ps.avatarId);
|
2018-05-06 09:04:37 +00:00
|
|
|
|
2019-04-07 12:50:36 +00: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-27 22:52:48 +00:00
|
|
|
|
2019-04-09 14:40:55 +00:00
|
|
|
updates.avatarUrl = DriveFiles.getPublicUrl(avatar, true);
|
2018-07-27 22:52:48 +00:00
|
|
|
|
2020-07-18 15:24:07 +00:00
|
|
|
if (avatar.blurhash) {
|
|
|
|
updates.avatarBlurhash = avatar.blurhash;
|
2018-05-06 09:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:21:16 +00:00
|
|
|
if (ps.bannerId) {
|
2019-04-07 12:50:36 +00:00
|
|
|
const banner = await DriveFiles.findOne(ps.bannerId);
|
2018-05-06 09:04:37 +00:00
|
|
|
|
2019-04-07 12:50:36 +00: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-27 22:52:48 +00:00
|
|
|
|
2019-04-09 14:40:55 +00:00
|
|
|
updates.bannerUrl = DriveFiles.getPublicUrl(banner, false);
|
2018-07-27 22:52:48 +00:00
|
|
|
|
2020-07-18 15:24:07 +00:00
|
|
|
if (banner.blurhash) {
|
|
|
|
updates.bannerBlurhash = banner.blurhash;
|
2018-06-06 20:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 21:56:13 +00: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 15:11:39 +00: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 11:42:45 +00:00
|
|
|
//#region emojis/tags
|
2019-04-17 05:30:31 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
let emojis = [] as string[];
|
|
|
|
let tags = [] as string[];
|
2018-12-06 01:02:04 +00:00
|
|
|
|
2019-04-17 05:30:31 +00:00
|
|
|
const newName = updates.name === undefined ? user.name : updates.name;
|
|
|
|
const newDescription = profileUpdates.description === undefined ? profile.description : profileUpdates.description;
|
|
|
|
|
|
|
|
if (newName != null) {
|
2021-04-02 01:36:11 +00:00
|
|
|
const tokens = mfm.parsePlain(newName);
|
|
|
|
emojis = emojis.concat(extractCustomEmojisFromMfm(tokens!));
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2018-12-06 01:02:04 +00:00
|
|
|
|
2019-04-17 05:30:31 +00:00
|
|
|
if (newDescription != null) {
|
2021-04-02 01:36:11 +00:00
|
|
|
const tokens = mfm.parse(newDescription);
|
|
|
|
emojis = emojis.concat(extractCustomEmojisFromMfm(tokens!));
|
2021-02-07 01:43:34 +00:00
|
|
|
tags = extractHashtags(tokens!).map(tag => normalizeForSearch(tag)).splice(0, 32);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2018-12-06 01:02:04 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
updates.emojis = emojis;
|
|
|
|
updates.tags = tags;
|
2019-02-17 14:41:47 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
// ハッシュタグ更新
|
2019-08-18 03:47:45 +00:00
|
|
|
updateUsertags(user, tags);
|
2018-12-06 01:02:04 +00:00
|
|
|
//#endregion
|
|
|
|
|
2019-04-10 09:13:33 +00:00
|
|
|
if (Object.keys(updates).length > 0) await Users.update(user.id, updates);
|
2020-01-31 22:16:52 +00:00
|
|
|
if (Object.keys(profileUpdates).length > 0) await UserProfiles.update(user.id, profileUpdates);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
const iObj = await Users.pack<true, true>(user.id, user, {
|
2016-12-28 22:49:51 +00:00
|
|
|
detail: true,
|
2021-12-09 14:58:30 +00:00
|
|
|
includeSecrets: isSecure,
|
2017-01-21 06:30:40 +00:00
|
|
|
});
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-06-02 07:27:24 +00:00
|
|
|
// Publish meUpdated event
|
2019-04-07 12:50:36 +00:00
|
|
|
publishMainStream(user.id, 'meUpdated', iObj);
|
2021-03-21 06:14:03 +00:00
|
|
|
publishUserEvent(user.id, 'updateUserProfile', await UserProfiles.findOne(user.id));
|
2018-05-31 13:56:02 +00:00
|
|
|
|
|
|
|
// 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認
|
2018-09-09 17:21:16 +00:00
|
|
|
if (user.isLocked && ps.isLocked === false) {
|
2018-05-31 13:56:02 +00:00
|
|
|
acceptAllFollowRequests(user);
|
|
|
|
}
|
2018-09-01 11:17:30 +00:00
|
|
|
|
|
|
|
// フォロワーにUpdateを配信
|
2019-04-07 12:50:36 +00:00
|
|
|
publishToFollowers(user.id);
|
2019-02-22 02:46:58 +00:00
|
|
|
|
|
|
|
return iObj;
|
|
|
|
});
|