2019-02-04 20:48:08 -06:00
|
|
|
import $ from 'cafy';
|
2021-04-01 20:36:11 -05:00
|
|
|
import * as mfm from 'mfm-js';
|
2021-08-19 07:55:45 -05: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 08:41:05 -06:00
|
|
|
import * as langmap from 'langmap';
|
2021-08-19 07:55:45 -05: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-08 22:47:40 -05:00
|
|
|
import { notificationTypes } from '@/types';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-07-16 14:36:44 -05:00
|
|
|
export const meta = {
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['account'],
|
|
|
|
|
2020-02-15 06:33:32 -06:00
|
|
|
requireCredential: true as const,
|
2018-07-16 14:36:44 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
kind: 'write:account',
|
2018-09-09 12:21:16 -05:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 13:32:24 -05:00
|
|
|
name: {
|
2019-06-14 10:07:41 -05:00
|
|
|
validator: $.optional.nullable.use(Users.validateName),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
description: {
|
2019-06-14 10:07:41 -05:00
|
|
|
validator: $.optional.nullable.use(Users.validateDescription),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-12-03 05:08:18 -06:00
|
|
|
lang: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.nullable.str.or(Object.keys(langmap)),
|
2018-12-03 05:08:18 -06:00
|
|
|
},
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
location: {
|
2019-06-14 10:07:41 -05:00
|
|
|
validator: $.optional.nullable.use(Users.validateLocation),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
birthday: {
|
2019-06-14 10:07:41 -05:00
|
|
|
validator: $.optional.nullable.use(Users.validateBirthday),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
avatarId: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
bannerId: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2019-07-17 10:11:39 -05:00
|
|
|
fields: {
|
|
|
|
validator: $.optional.arr($.object()).range(1, 4),
|
|
|
|
},
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
isLocked: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2020-12-11 06:16:20 -06:00
|
|
|
isExplorable: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-04-17 01:30:26 -05:00
|
|
|
hideOnlineStatus: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
carefulBot: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-10-13 06:11:00 -05:00
|
|
|
|
2018-12-28 06:36:58 -06:00
|
|
|
autoAcceptFollowed: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-12-28 06:36:58 -06:00
|
|
|
},
|
|
|
|
|
2020-11-25 06:31:34 -06:00
|
|
|
noCrawle: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
isBot: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
isCat: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-09-09 12:21:16 -05:00
|
|
|
|
2020-02-18 04:05:11 -06:00
|
|
|
injectFeaturedNote: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2021-02-06 07:47:15 -06:00
|
|
|
receiveAnnouncementEmail: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
alwaysMarkNsfw: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2019-07-06 16:56:13 -05:00
|
|
|
|
|
|
|
pinnedPageId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
2020-07-26 23:34:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
mutedWords: {
|
|
|
|
validator: $.optional.arr($.arr($.str))
|
|
|
|
},
|
2020-08-21 20:06:17 -05:00
|
|
|
|
2020-09-18 08:18:21 -05:00
|
|
|
mutingNotificationTypes: {
|
2020-08-21 20:06:17 -05:00
|
|
|
validator: $.optional.arr($.str.or(notificationTypes as unknown as string[]))
|
|
|
|
},
|
2021-02-12 21:28:26 -06:00
|
|
|
|
|
|
|
emailNotificationTypes: {
|
|
|
|
validator: $.optional.arr($.str)
|
|
|
|
},
|
2019-02-21 20:46:58 -06: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-06 16:56:13 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
|
|
|
id: '8e01b590-7eb9-431b-a239-860e086c408e'
|
|
|
|
},
|
2021-03-06 07:34:11 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
ref: 'User'
|
2018-09-09 12:21:16 -05:00
|
|
|
}
|
2018-07-16 14:36:44 -05:00
|
|
|
};
|
|
|
|
|
2021-03-23 21:05:37 -05:00
|
|
|
export default define(meta, async (ps, _user, token) => {
|
|
|
|
const user = await Users.findOneOrFail(_user.id);
|
2020-03-28 04:07:41 -05:00
|
|
|
const isSecure = token == null;
|
|
|
|
|
2019-04-10 01:04:27 -05:00
|
|
|
const updates = {} as Partial<User>;
|
2019-04-17 00:30:31 -05:00
|
|
|
const profileUpdates = {} as Partial<UserProfile>;
|
|
|
|
|
2021-02-13 00:33:38 -06:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
2018-05-31 08:56:02 -05:00
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
if (ps.name !== undefined) updates.name = ps.name;
|
2019-04-17 00:30:31 -05:00
|
|
|
if (ps.description !== undefined) profileUpdates.description = ps.description;
|
2021-02-12 21:28:26 -06:00
|
|
|
if (ps.lang !== undefined) profileUpdates.lang = ps.lang;
|
2019-04-17 00:30:31 -05:00
|
|
|
if (ps.location !== undefined) profileUpdates.location = ps.location;
|
|
|
|
if (ps.birthday !== undefined) profileUpdates.birthday = ps.birthday;
|
2018-09-09 12:21:16 -05:00
|
|
|
if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId;
|
|
|
|
if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId;
|
2020-07-26 23:34:20 -05:00
|
|
|
if (ps.mutedWords !== undefined) {
|
|
|
|
profileUpdates.mutedWords = ps.mutedWords;
|
|
|
|
profileUpdates.enableWordMute = ps.mutedWords.length > 0;
|
|
|
|
}
|
2020-09-18 08:18:21 -05:00
|
|
|
if (ps.mutingNotificationTypes !== undefined) profileUpdates.mutingNotificationTypes = ps.mutingNotificationTypes as typeof notificationTypes[number][];
|
2020-04-03 18:46:54 -05:00
|
|
|
if (typeof ps.isLocked === 'boolean') updates.isLocked = ps.isLocked;
|
2020-12-11 06:16:20 -06:00
|
|
|
if (typeof ps.isExplorable === 'boolean') updates.isExplorable = ps.isExplorable;
|
2021-04-17 01:30:26 -05:00
|
|
|
if (typeof ps.hideOnlineStatus === 'boolean') updates.hideOnlineStatus = ps.hideOnlineStatus;
|
2020-04-03 18:46:54 -05: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 06:31:34 -06:00
|
|
|
if (typeof ps.noCrawle === 'boolean') profileUpdates.noCrawle = ps.noCrawle;
|
2020-04-03 18:46:54 -05:00
|
|
|
if (typeof ps.isCat === 'boolean') updates.isCat = ps.isCat;
|
|
|
|
if (typeof ps.injectFeaturedNote === 'boolean') profileUpdates.injectFeaturedNote = ps.injectFeaturedNote;
|
2021-02-06 07:47:15 -06:00
|
|
|
if (typeof ps.receiveAnnouncementEmail === 'boolean') profileUpdates.receiveAnnouncementEmail = ps.receiveAnnouncementEmail;
|
2020-04-03 18:46:54 -05:00
|
|
|
if (typeof ps.alwaysMarkNsfw === 'boolean') profileUpdates.alwaysMarkNsfw = ps.alwaysMarkNsfw;
|
2021-02-12 21:28:26 -06:00
|
|
|
if (ps.emailNotificationTypes !== undefined) profileUpdates.emailNotificationTypes = ps.emailNotificationTypes;
|
2018-09-09 12:21:16 -05:00
|
|
|
|
|
|
|
if (ps.avatarId) {
|
2019-04-07 07:50:36 -05:00
|
|
|
const avatar = await DriveFiles.findOne(ps.avatarId);
|
2018-05-06 04:04:37 -05:00
|
|
|
|
2019-04-07 07:50:36 -05: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 17:52:48 -05:00
|
|
|
|
2019-04-09 09:40:55 -05:00
|
|
|
updates.avatarUrl = DriveFiles.getPublicUrl(avatar, true);
|
2018-07-27 17:52:48 -05:00
|
|
|
|
2020-07-18 10:24:07 -05:00
|
|
|
if (avatar.blurhash) {
|
|
|
|
updates.avatarBlurhash = avatar.blurhash;
|
2018-05-06 04:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 12:21:16 -05:00
|
|
|
if (ps.bannerId) {
|
2019-04-07 07:50:36 -05:00
|
|
|
const banner = await DriveFiles.findOne(ps.bannerId);
|
2018-05-06 04:04:37 -05:00
|
|
|
|
2019-04-07 07:50:36 -05: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 17:52:48 -05:00
|
|
|
|
2019-04-09 09:40:55 -05:00
|
|
|
updates.bannerUrl = DriveFiles.getPublicUrl(banner, false);
|
2018-07-27 17:52:48 -05:00
|
|
|
|
2020-07-18 10:24:07 -05:00
|
|
|
if (banner.blurhash) {
|
|
|
|
updates.bannerBlurhash = banner.blurhash;
|
2018-06-06 15:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 16:56:13 -05: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 10:11:39 -05: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 05:42:45 -06:00
|
|
|
//#region emojis/tags
|
2019-04-17 00:30:31 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
let emojis = [] as string[];
|
|
|
|
let tags = [] as string[];
|
2018-12-05 19:02:04 -06:00
|
|
|
|
2019-04-17 00:30:31 -05:00
|
|
|
const newName = updates.name === undefined ? user.name : updates.name;
|
|
|
|
const newDescription = profileUpdates.description === undefined ? profile.description : profileUpdates.description;
|
|
|
|
|
|
|
|
if (newName != null) {
|
2021-04-01 20:36:11 -05:00
|
|
|
const tokens = mfm.parsePlain(newName);
|
|
|
|
emojis = emojis.concat(extractCustomEmojisFromMfm(tokens!));
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2018-12-05 19:02:04 -06:00
|
|
|
|
2019-04-17 00:30:31 -05:00
|
|
|
if (newDescription != null) {
|
2021-04-01 20:36:11 -05:00
|
|
|
const tokens = mfm.parse(newDescription);
|
|
|
|
emojis = emojis.concat(extractCustomEmojisFromMfm(tokens!));
|
2021-02-06 19:43:34 -06:00
|
|
|
tags = extractHashtags(tokens!).map(tag => normalizeForSearch(tag)).splice(0, 32);
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2018-12-05 19:02:04 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
updates.emojis = emojis;
|
|
|
|
updates.tags = tags;
|
2019-02-17 08:41:47 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
// ハッシュタグ更新
|
2019-08-17 22:47:45 -05:00
|
|
|
updateUsertags(user, tags);
|
2018-12-05 19:02:04 -06:00
|
|
|
//#endregion
|
|
|
|
|
2019-04-10 04:13:33 -05:00
|
|
|
if (Object.keys(updates).length > 0) await Users.update(user.id, updates);
|
2020-01-31 16:16:52 -06:00
|
|
|
if (Object.keys(profileUpdates).length > 0) await UserProfiles.update(user.id, profileUpdates);
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const iObj = await Users.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
|
|
|
|
2018-06-02 02:27:24 -05:00
|
|
|
// Publish meUpdated event
|
2019-04-07 07:50:36 -05:00
|
|
|
publishMainStream(user.id, 'meUpdated', iObj);
|
2021-03-21 01:14:03 -05:00
|
|
|
publishUserEvent(user.id, 'updateUserProfile', await UserProfiles.findOne(user.id));
|
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を配信
|
2019-04-07 07:50:36 -05:00
|
|
|
publishToFollowers(user.id);
|
2019-02-21 20:46:58 -06:00
|
|
|
|
|
|
|
return iObj;
|
|
|
|
});
|