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