summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/federation/stats.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-09-18 03:27:08 +0900
committerGitHub <noreply@github.com>2022-09-18 03:27:08 +0900
commitb75184ec8e3436200bacdcd832e3324702553d20 (patch)
tree8b7e316f29e95df921db57289c8b8da476d18f07 /packages/backend/src/server/api/endpoints/federation/stats.ts
parentUpdate ROADMAP.md (diff)
downloadsharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.gz
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.bz2
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.zip
なんかもうめっちゃ変えた
Diffstat (limited to 'packages/backend/src/server/api/endpoints/federation/stats.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/federation/stats.ts102
1 files changed, 59 insertions, 43 deletions
diff --git a/packages/backend/src/server/api/endpoints/federation/stats.ts b/packages/backend/src/server/api/endpoints/federation/stats.ts
index e02c7b97e0..d07a08637f 100644
--- a/packages/backend/src/server/api/endpoints/federation/stats.ts
+++ b/packages/backend/src/server/api/endpoints/federation/stats.ts
@@ -1,7 +1,10 @@
import { IsNull, MoreThan, Not } from 'typeorm';
-import { Followings, Instances } from '@/models/index.js';
-import { awaitAll } from '@/prelude/await-all.js';
-import define from '../../define.js';
+import { Inject, Injectable } from '@nestjs/common';
+import { FollowingsRepository, InstancesRepository } from '@/models/index.js';
+import { awaitAll } from '@/misc/prelude/await-all.js';
+import { Endpoint } from '@/server/api/endpoint-base.js';
+import { InstanceEntityService } from '@/core/entities/InstanceEntityService.js';
+import { DI } from '@/di-symbols.js';
export const meta = {
tags: ['federation'],
@@ -21,45 +24,58 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
-export default define(meta, paramDef, async (ps) => {
- const [topSubInstances, topPubInstances, allSubCount, allPubCount] = await Promise.all([
- Instances.find({
- where: {
- followersCount: MoreThan(0),
- },
- order: {
- followersCount: 'DESC',
- },
- take: ps.limit,
- }),
- Instances.find({
- where: {
- followingCount: MoreThan(0),
- },
- order: {
- followingCount: 'DESC',
- },
- take: ps.limit,
- }),
- Followings.count({
- where: {
- followeeHost: Not(IsNull()),
- },
- }),
- Followings.count({
- where: {
- followerHost: Not(IsNull()),
- },
- }),
- ]);
+@Injectable()
+export default class extends Endpoint<typeof meta, typeof paramDef> {
+ constructor(
+ @Inject(DI.instancesRepository)
+ private instancesRepository: InstancesRepository,
- const gotSubCount = topSubInstances.map(x => x.followersCount).reduce((a, b) => a + b, 0);
- const gotPubCount = topPubInstances.map(x => x.followingCount).reduce((a, b) => a + b, 0);
+ @Inject(DI.followingsRepository)
+ private followingsRepository: FollowingsRepository,
- return await awaitAll({
- topSubInstances: Instances.packMany(topSubInstances),
- otherFollowersCount: Math.max(0, allSubCount - gotSubCount),
- topPubInstances: Instances.packMany(topPubInstances),
- otherFollowingCount: Math.max(0, allPubCount - gotPubCount),
- });
-});
+ private instanceEntityService: InstanceEntityService,
+ ) {
+ super(meta, paramDef, async (ps, me) => {
+ const [topSubInstances, topPubInstances, allSubCount, allPubCount] = await Promise.all([
+ this.instancesRepository.find({
+ where: {
+ followersCount: MoreThan(0),
+ },
+ order: {
+ followersCount: 'DESC',
+ },
+ take: ps.limit,
+ }),
+ this.instancesRepository.find({
+ where: {
+ followingCount: MoreThan(0),
+ },
+ order: {
+ followingCount: 'DESC',
+ },
+ take: ps.limit,
+ }),
+ this.followingsRepository.count({
+ where: {
+ followeeHost: Not(IsNull()),
+ },
+ }),
+ this.followingsRepository.count({
+ where: {
+ followerHost: Not(IsNull()),
+ },
+ }),
+ ]);
+
+ const gotSubCount = topSubInstances.map(x => x.followersCount).reduce((a, b) => a + b, 0);
+ const gotPubCount = topPubInstances.map(x => x.followingCount).reduce((a, b) => a + b, 0);
+
+ return await awaitAll({
+ topSubInstances: this.instanceEntityService.packMany(topSubInstances),
+ otherFollowersCount: Math.max(0, allSubCount - gotSubCount),
+ topPubInstances: this.instanceEntityService.packMany(topPubInstances),
+ otherFollowingCount: Math.max(0, allPubCount - gotPubCount),
+ });
+ });
+ }
+}