From dcf30c1f4aada1a6fc608311bb47d3244cc24657 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 28 Jun 2022 10:41:22 +0900 Subject: [PATCH] feat(api): add federation/stats endpoint --- packages/backend/src/server/api/endpoints.ts | 2 + .../server/api/endpoints/federation/stats.ts | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 packages/backend/src/server/api/endpoints/federation/stats.ts diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 93f93cef0..1a3fc199d 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -135,6 +135,7 @@ import * as ep___federation_instances from './endpoints/federation/instances.js' import * as ep___federation_showInstance from './endpoints/federation/show-instance.js'; import * as ep___federation_updateRemoteUser from './endpoints/federation/update-remote-user.js'; import * as ep___federation_users from './endpoints/federation/users.js'; +import * as ep___federation_stats from './endpoints/federation/stats.js'; import * as ep___following_create from './endpoints/following/create.js'; import * as ep___following_delete from './endpoints/following/delete.js'; import * as ep___following_invalidate from './endpoints/following/invalidate.js'; @@ -447,6 +448,7 @@ const eps = [ ['federation/show-instance', ep___federation_showInstance], ['federation/update-remote-user', ep___federation_updateRemoteUser], ['federation/users', ep___federation_users], + ['federation/stats', ep___federation_stats], ['following/create', ep___following_create], ['following/delete', ep___following_delete], ['following/invalidate', ep___following_invalidate], diff --git a/packages/backend/src/server/api/endpoints/federation/stats.ts b/packages/backend/src/server/api/endpoints/federation/stats.ts new file mode 100644 index 000000000..576999670 --- /dev/null +++ b/packages/backend/src/server/api/endpoints/federation/stats.ts @@ -0,0 +1,49 @@ +import { MoreThan } from 'typeorm'; +import { Instances } from '@/models/index.js'; +import { awaitAll } from '@/prelude/await-all.js'; +import define from '../../define.js'; + +export const meta = { + tags: ['federation'], + + requireCredential: false, + + allowGet: true, + cacheSec: 60 * 60, +} as const; + +export const paramDef = { + type: 'object', + properties: { + }, + required: [], +} as const; + +// eslint-disable-next-line import/no-default-export +export default define(meta, paramDef, async (ps) => { + const [topSubInstances, topPubInstances] = await Promise.all([ + Instances.find({ + where: { + followersCount: MoreThan(0), + }, + order: { + followersCount: 'DESC', + }, + take: 10, + }), + Instances.find({ + where: { + followingCount: MoreThan(0), + }, + order: { + followingCount: 'DESC', + }, + take: 10, + }), + ]); + + return await awaitAll({ + topSubInstances: Instances.packMany(topSubInstances), + topPubInstances: Instances.packMany(topPubInstances), + }); +});