From 60df819c60824611cff24db1428d9b46a56e6b92 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 17 Feb 2023 15:36:36 +0900 Subject: [PATCH] refactor: fix types --- .../backend/src/core/CustomEmojiService.ts | 2 +- .../src/core/entities/EmojiEntityService.ts | 43 ++++++---- packages/backend/src/misc/schema.ts | 5 +- packages/backend/src/models/schema/emoji.ts | 36 +++++++-- .../backend/src/server/api/ApiCallService.ts | 4 +- .../endpoints/admin/emoji/add-aliases-bulk.ts | 2 +- .../server/api/endpoints/admin/emoji/copy.ts | 2 +- .../api/endpoints/admin/emoji/delete-bulk.ts | 2 +- .../api/endpoints/admin/emoji/delete.ts | 4 +- .../api/endpoints/admin/emoji/list-remote.ts | 2 +- .../server/api/endpoints/admin/emoji/list.ts | 2 +- .../admin/emoji/remove-aliases-bulk.ts | 2 +- .../endpoints/admin/emoji/set-aliases-bulk.ts | 2 +- .../admin/emoji/set-category-bulk.ts | 2 +- .../api/endpoints/admin/emoji/update.ts | 6 +- .../src/server/api/endpoints/admin/meta.ts | 81 ++----------------- .../src/server/api/endpoints/emojis.ts | 6 +- .../backend/src/server/api/endpoints/meta.ts | 2 +- .../backend/src/server/api/stream/types.ts | 4 +- 19 files changed, 91 insertions(+), 118 deletions(-) diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts index 63f0319442..a1a257fbd1 100644 --- a/packages/backend/src/core/CustomEmojiService.ts +++ b/packages/backend/src/core/CustomEmojiService.ts @@ -61,7 +61,7 @@ export class CustomEmojiService { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiAdded', { - emoji: await this.emojiEntityService.pack(emoji.id), + emoji: await this.emojiEntityService.packDetailed(emoji.id), }); } diff --git a/packages/backend/src/core/entities/EmojiEntityService.ts b/packages/backend/src/core/entities/EmojiEntityService.ts index f4a01ab032..f5c8f2d4bb 100644 --- a/packages/backend/src/core/entities/EmojiEntityService.ts +++ b/packages/backend/src/core/entities/EmojiEntityService.ts @@ -5,44 +5,59 @@ import type { Packed } from '@/misc/schema.js'; import type { } from '@/models/entities/Blocking.js'; import type { Emoji } from '@/models/entities/Emoji.js'; import { bindThis } from '@/decorators.js'; -import { UserEntityService } from './UserEntityService.js'; @Injectable() export class EmojiEntityService { constructor( @Inject(DI.emojisRepository) private emojisRepository: EmojisRepository, - - private userEntityService: UserEntityService, ) { } @bindThis - public async pack( + public async packSimple( src: Emoji['id'] | Emoji, - opts: { omitHost?: boolean; omitId?: boolean; withUrl?: boolean; } = { omitHost: true, omitId: true, withUrl: true }, - ): Promise> { - opts = { omitHost: true, omitId: true, withUrl: true, ...opts }; - + ): Promise> { const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); return { - id: opts.omitId ? undefined : emoji.id, aliases: emoji.aliases, name: emoji.name, category: emoji.category, - host: opts.omitHost ? undefined : emoji.host, // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ) - url: opts.withUrl ? (emoji.publicUrl || emoji.originalUrl) : undefined, + url: emoji.publicUrl || emoji.originalUrl, }; } @bindThis - public packMany( + public packSimpleMany( emojis: any[], - opts: { omitHost?: boolean; omitId?: boolean; withUrl?: boolean; } = {}, ) { - return Promise.all(emojis.map(x => this.pack(x, opts))); + return Promise.all(emojis.map(x => this.packSimple(x))); + } + + @bindThis + public async packDetailed( + src: Emoji['id'] | Emoji, + ): Promise> { + const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); + + return { + id: emoji.id, + aliases: emoji.aliases, + name: emoji.name, + category: emoji.category, + host: emoji.host, + // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ) + url: emoji.publicUrl || emoji.originalUrl, + }; + } + + @bindThis + public packDetailedMany( + emojis: any[], + ) { + return Promise.all(emojis.map(x => this.packDetailed(x))); } } diff --git a/packages/backend/src/misc/schema.ts b/packages/backend/src/misc/schema.ts index 6bd714b0f9..7fc4a3e654 100644 --- a/packages/backend/src/misc/schema.ts +++ b/packages/backend/src/misc/schema.ts @@ -26,7 +26,7 @@ import { packedClipSchema } from '@/models/schema/clip.js'; import { packedFederationInstanceSchema } from '@/models/schema/federation-instance.js'; import { packedQueueCountSchema } from '@/models/schema/queue.js'; import { packedGalleryPostSchema } from '@/models/schema/gallery-post.js'; -import { packedEmojiSchema } from '@/models/schema/emoji.js'; +import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from '@/models/schema/emoji.js'; import { packedFlashSchema } from '@/models/schema/flash.js'; export const refs = { @@ -57,7 +57,8 @@ export const refs = { Clip: packedClipSchema, FederationInstance: packedFederationInstanceSchema, GalleryPost: packedGalleryPostSchema, - Emoji: packedEmojiSchema, + EmojiSimple: packedEmojiSimpleSchema, + EmojiDetailed: packedEmojiDetailedSchema, Flash: packedFlashSchema, }; diff --git a/packages/backend/src/models/schema/emoji.ts b/packages/backend/src/models/schema/emoji.ts index 143f25373c..c00c3dac1d 100644 --- a/packages/backend/src/models/schema/emoji.ts +++ b/packages/backend/src/models/schema/emoji.ts @@ -1,11 +1,37 @@ -export const packedEmojiSchema = { +export const packedEmojiSimpleSchema = { + type: 'object', + properties: { + aliases: { + type: 'array', + optional: false, nullable: false, + items: { + type: 'string', + optional: false, nullable: false, + format: 'id', + }, + }, + name: { + type: 'string', + optional: false, nullable: false, + }, + category: { + type: 'string', + optional: false, nullable: true, + }, + url: { + type: 'string', + optional: false, nullable: false, + }, + }, +} as const; + +export const packedEmojiDetailedSchema = { type: 'object', properties: { id: { type: 'string', - optional: true, nullable: false, + optional: false, nullable: false, format: 'id', - example: 'xxxxxxxxxx', }, aliases: { type: 'array', @@ -26,12 +52,12 @@ export const packedEmojiSchema = { }, host: { type: 'string', - optional: true, nullable: true, + optional: false, nullable: true, description: 'The local host is represented with `null`.', }, url: { type: 'string', - optional: true, nullable: false, + optional: false, nullable: false, }, }, } as const; diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts index 8aa570213d..cc27b36966 100644 --- a/packages/backend/src/server/api/ApiCallService.ts +++ b/packages/backend/src/server/api/ApiCallService.ts @@ -219,8 +219,8 @@ export class ApiCallService implements OnApplicationShutdown { const limit = Object.assign({}, ep.meta.limit); - if (!limit.key) { - limit.key = ep.name; + if (limit.key == null) { + (limit as any).key = ep.name; } // TODO: 毎リクエスト計算するのもあれだしキャッシュしたい diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add-aliases-bulk.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add-aliases-bulk.ts index c683cd24c1..0cc60e9191 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add-aliases-bulk.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add-aliases-bulk.ts @@ -56,7 +56,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiUpdated', { - emojis: await this.emojiEntityService.packMany(ps.ids), + emojis: await this.emojiEntityService.packDetailedMany(ps.ids), }); }); } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts index b4fc7fd6f5..8885a40fd9 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts @@ -92,7 +92,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiAdded', { - emoji: await this.emojiEntityService.pack(copied.id), + emoji: await this.emojiEntityService.packDetailed(copied.id), }); return { diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/delete-bulk.ts b/packages/backend/src/server/api/endpoints/admin/emoji/delete-bulk.ts index 0c337237d3..f298baaedf 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/delete-bulk.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/delete-bulk.ts @@ -54,7 +54,7 @@ export default class extends Endpoint { } this.globalEventService.publishBroadcastStream('emojiDeleted', { - emojis: await this.emojiEntityService.packMany(emojis), + emojis: await this.emojiEntityService.packDetailedMany(emojis), }); }); } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/delete.ts b/packages/backend/src/server/api/endpoints/admin/emoji/delete.ts index c51e7fd1a8..a5fbe3f4ea 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/delete.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/delete.ts @@ -4,9 +4,9 @@ import { Endpoint } from '@/server/api/endpoint-base.js'; import type { EmojisRepository } from '@/models/index.js'; import { DI } from '@/di-symbols.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; -import { ApiError } from '../../../error.js'; import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; +import { ApiError } from '../../../error.js'; export const meta = { tags: ['admin'], @@ -57,7 +57,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiDeleted', { - emojis: [await this.emojiEntityService.pack(emoji)], + emojis: [await this.emojiEntityService.packDetailed(emoji)], }); this.moderationLogService.insertModerationLog(me, 'deleteEmoji', { diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts b/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts index 8e0ea2e117..df3c28deff 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts @@ -101,7 +101,7 @@ export default class extends Endpoint { .take(ps.limit) .getMany(); - return this.emojiEntityService.packMany(emojis, { omitHost: false, omitId: false, withUrl: false }); + return this.emojiEntityService.packDetailedMany(emojis); }); } } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/list.ts b/packages/backend/src/server/api/endpoints/admin/emoji/list.ts index 1b1931f8e6..814668294f 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/list.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/list.ts @@ -98,7 +98,7 @@ export default class extends Endpoint { emojis = await q.take(ps.limit).getMany(); } - return this.emojiEntityService.packMany(emojis, { omitHost: false, omitId: false, withUrl: false }); + return this.emojiEntityService.packDetailedMany(emojis); }); } } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/remove-aliases-bulk.ts b/packages/backend/src/server/api/endpoints/admin/emoji/remove-aliases-bulk.ts index 065965f64a..66547024f7 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/remove-aliases-bulk.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/remove-aliases-bulk.ts @@ -56,7 +56,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiUpdated', { - emojis: await this.emojiEntityService.packMany(ps.ids), + emojis: await this.emojiEntityService.packDetailedMany(ps.ids), }); }); } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/set-aliases-bulk.ts b/packages/backend/src/server/api/endpoints/admin/emoji/set-aliases-bulk.ts index 51c0f329ac..c8992eeb04 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/set-aliases-bulk.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/set-aliases-bulk.ts @@ -52,7 +52,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiUpdated', { - emojis: await this.emojiEntityService.packMany(ps.ids), + emojis: await this.emojiEntityService.packDetailedMany(ps.ids), }); }); } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/set-category-bulk.ts b/packages/backend/src/server/api/endpoints/admin/emoji/set-category-bulk.ts index 3329cab7b9..8a538c1003 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/set-category-bulk.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/set-category-bulk.ts @@ -54,7 +54,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); this.globalEventService.publishBroadcastStream('emojiUpdated', { - emojis: await this.emojiEntityService.packMany(ps.ids), + emojis: await this.emojiEntityService.packDetailedMany(ps.ids), }); }); } diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/update.ts b/packages/backend/src/server/api/endpoints/admin/emoji/update.ts index af610ddf9c..809bf77d6b 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/update.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/update.ts @@ -3,9 +3,9 @@ import { DataSource } from 'typeorm'; import { Endpoint } from '@/server/api/endpoint-base.js'; import type { EmojisRepository } from '@/models/index.js'; import { DI } from '@/di-symbols.js'; -import { ApiError } from '../../../error.js'; import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; +import { ApiError } from '../../../error.js'; export const meta = { tags: ['admin'], @@ -68,7 +68,7 @@ export default class extends Endpoint { await this.db.queryResultCache!.remove(['meta_emojis']); - const updated = await this.emojiEntityService.pack(emoji.id); + const updated = await this.emojiEntityService.packDetailed(emoji.id); if (emoji.name === ps.name) { this.globalEventService.publishBroadcastStream('emojiUpdated', { @@ -76,7 +76,7 @@ export default class extends Endpoint { }); } else { this.globalEventService.publishBroadcastStream('emojiDeleted', { - emojis: [await this.emojiEntityService.pack(emoji)], + emojis: [await this.emojiEntityService.packDetailed(emoji)], }); this.globalEventService.publishBroadcastStream('emojiAdded', { diff --git a/packages/backend/src/server/api/endpoints/admin/meta.ts b/packages/backend/src/server/api/endpoints/admin/meta.ts index 2b19104ea7..9eef1b29c5 100644 --- a/packages/backend/src/server/api/endpoints/admin/meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/meta.ts @@ -54,86 +54,22 @@ export const meta = { }, mascotImageUrl: { type: 'string', - optional: false, nullable: false, + optional: false, nullable: true, default: '/assets/ai.png', }, bannerUrl: { type: 'string', - optional: false, nullable: false, + optional: false, nullable: true, }, errorImageUrl: { type: 'string', - optional: false, nullable: false, + optional: false, nullable: true, default: 'https://xn--931a.moe/aiart/yubitun.png', }, iconUrl: { type: 'string', optional: false, nullable: true, }, - maxNoteTextLength: { - type: 'number', - optional: false, nullable: false, - }, - emojis: { - type: 'array', - optional: false, nullable: false, - items: { - type: 'object', - optional: false, nullable: false, - properties: { - id: { - type: 'string', - optional: false, nullable: false, - format: 'id', - }, - aliases: { - type: 'array', - optional: false, nullable: false, - items: { - type: 'string', - optional: false, nullable: false, - }, - }, - category: { - type: 'string', - optional: false, nullable: true, - }, - host: { - type: 'string', - optional: false, nullable: true, - }, - url: { - type: 'string', - optional: false, nullable: false, - format: 'url', - }, - }, - }, - }, - ads: { - type: 'array', - optional: false, nullable: false, - items: { - type: 'object', - optional: false, nullable: false, - properties: { - place: { - type: 'string', - optional: false, nullable: false, - }, - url: { - type: 'string', - optional: false, nullable: false, - format: 'url', - }, - imageUrl: { - type: 'string', - optional: false, nullable: false, - format: 'url', - }, - }, - }, - }, enableEmail: { type: 'boolean', optional: false, nullable: false, @@ -146,10 +82,6 @@ export const meta = { type: 'boolean', optional: false, nullable: false, }, - proxyAccountName: { - type: 'string', - optional: false, nullable: true, - }, userStarForReactionFallback: { type: 'boolean', optional: true, nullable: false, @@ -228,7 +160,7 @@ export const meta = { optional: true, nullable: true, }, smtpPort: { - type: 'string', + type: 'number', optional: true, nullable: true, }, smtpUser: { @@ -299,6 +231,10 @@ export const meta = { type: 'boolean', optional: true, nullable: false, }, + policies: { + type: 'object', + optional: false, nullable: false, + }, }, }, } as const; @@ -349,7 +285,6 @@ export default class extends Endpoint { iconUrl: instance.iconUrl, backgroundImageUrl: instance.backgroundImageUrl, logoImageUrl: instance.logoImageUrl, - maxNoteTextLength: MAX_NOTE_TEXT_LENGTH, // 後方互換性のため defaultLightTheme: instance.defaultLightTheme, defaultDarkTheme: instance.defaultDarkTheme, enableEmail: instance.enableEmail, diff --git a/packages/backend/src/server/api/endpoints/emojis.ts b/packages/backend/src/server/api/endpoints/emojis.ts index a909dddfd6..325b758358 100644 --- a/packages/backend/src/server/api/endpoints/emojis.ts +++ b/packages/backend/src/server/api/endpoints/emojis.ts @@ -82,11 +82,7 @@ export default class extends Endpoint { }); return { - emojis: await this.emojiEntityService.packMany(emojis, { - omitId: true, - omitHost: true, - withUrl: true, - }), + emojis: await this.emojiEntityService.packSimpleMany(emojis), }; }); } diff --git a/packages/backend/src/server/api/endpoints/meta.ts b/packages/backend/src/server/api/endpoints/meta.ts index f7463b8ccf..cdb314a873 100644 --- a/packages/backend/src/server/api/endpoints/meta.ts +++ b/packages/backend/src/server/api/endpoints/meta.ts @@ -295,7 +295,7 @@ export default class extends Endpoint { iconUrl: instance.iconUrl, backgroundImageUrl: instance.backgroundImageUrl, logoImageUrl: instance.logoImageUrl, - maxNoteTextLength: MAX_NOTE_TEXT_LENGTH, // 後方互換性のため + maxNoteTextLength: MAX_NOTE_TEXT_LENGTH, defaultLightTheme: instance.defaultLightTheme, defaultDarkTheme: instance.defaultDarkTheme, ads: ads.map(ad => ({ diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index bf6ece2864..9287952cb6 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -42,10 +42,10 @@ export interface InternalStreamTypes { export interface BroadcastTypes { emojiAdded: { - emoji: Packed<'Emoji'>; + emoji: Packed<'EmojiDetailed'>; }; emojiUpdated: { - emojis: Packed<'Emoji'>[]; + emojis: Packed<'EmojiDetailed'>[]; }; emojiDeleted: { emojis: {