From 0d3757f2d32f85c456138851f438927300594180 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 18 Feb 2022 20:43:50 +0900 Subject: [PATCH] refactor: add InstanceRepository --- packages/backend/src/models/index.ts | 3 +- .../repositories/federation-instance.ts | 2 - .../src/models/repositories/instance.ts | 39 +++++++++++++++++++ .../src/models/schema/federation-instance.ts | 8 ---- 4 files changed, 41 insertions(+), 11 deletions(-) delete mode 100644 packages/backend/src/models/repositories/federation-instance.ts create mode 100644 packages/backend/src/models/repositories/instance.ts diff --git a/packages/backend/src/models/index.ts b/packages/backend/src/models/index.ts index 67da347395..da64063cef 100644 --- a/packages/backend/src/models/index.ts +++ b/packages/backend/src/models/index.ts @@ -61,6 +61,7 @@ import { RegistryItem } from './entities/registry-item'; import { Ad } from './entities/ad'; import { PasswordResetRequest } from './entities/password-reset-request'; import { UserPending } from './entities/user-pending'; +import { InstanceRepository } from './repositories/instance'; export const Announcements = getRepository(Announcement); export const AnnouncementReads = getRepository(AnnouncementRead); @@ -89,7 +90,7 @@ export const UserNotePinings = getRepository(UserNotePining); export const UsedUsernames = getRepository(UsedUsername); export const Followings = getCustomRepository(FollowingRepository); export const FollowRequests = getCustomRepository(FollowRequestRepository); -export const Instances = getRepository(Instance); +export const Instances = getCustomRepository(InstanceRepository); export const Emojis = getCustomRepository(EmojiRepository); export const DriveFiles = getCustomRepository(DriveFileRepository); export const DriveFolders = getCustomRepository(DriveFolderRepository); diff --git a/packages/backend/src/models/repositories/federation-instance.ts b/packages/backend/src/models/repositories/federation-instance.ts deleted file mode 100644 index 426fd5bfc3..0000000000 --- a/packages/backend/src/models/repositories/federation-instance.ts +++ /dev/null @@ -1,2 +0,0 @@ -import config from '@/config/index'; - diff --git a/packages/backend/src/models/repositories/instance.ts b/packages/backend/src/models/repositories/instance.ts new file mode 100644 index 0000000000..6097f58ba6 --- /dev/null +++ b/packages/backend/src/models/repositories/instance.ts @@ -0,0 +1,39 @@ +import { EntityRepository, Repository } from 'typeorm'; +import { Instance } from '@/models/entities/instance'; +import { Packed } from '@/misc/schema'; + +@EntityRepository(Instance) +export class InstanceRepository extends Repository { + public async pack( + instance: Instance, + ): Promise> { + return { + id: instance.id, + caughtAt: instance.caughtAt.toISOString(), + host: instance.host, + usersCount: instance.usersCount, + notesCount: instance.notesCount, + followingCount: instance.followingCount, + followersCount: instance.followersCount, + latestRequestSentAt: instance.latestRequestSentAt ? instance.latestRequestSentAt.toISOString() : null, + lastCommunicatedAt: instance.lastCommunicatedAt.toISOString(), + isNotResponding: instance.isNotResponding, + isSuspended: instance.isSuspended, + softwareName: instance.softwareName, + softwareVersion: instance.softwareVersion, + openRegistrations: instance.openRegistrations, + name: instance.name, + description: instance.description, + maintainerName: instance.maintainerName, + maintainerEmail: instance.maintainerEmail, + iconUrl: instance.iconUrl, + infoUpdatedAt: instance.infoUpdatedAt ? instance.infoUpdatedAt.toISOString() : null, + }; + } + + public packMany( + instances: Instance[], + ) { + return Promise.all(instances.map(x => this.pack(x))); + } +} diff --git a/packages/backend/src/models/schema/federation-instance.ts b/packages/backend/src/models/schema/federation-instance.ts index eef2f9e24f..41fc9d3ef7 100644 --- a/packages/backend/src/models/schema/federation-instance.ts +++ b/packages/backend/src/models/schema/federation-instance.ts @@ -34,14 +34,6 @@ export const packedFederationInstanceSchema = { type: 'number', optional: false, nullable: false, }, - driveUsage: { - type: 'number', - optional: false, nullable: false, - }, - driveFiles: { - type: 'number', - optional: false, nullable: false, - }, latestRequestSentAt: { type: 'string', optional: false, nullable: true,