2022-06-27 23:05:20 -05:00
|
|
|
import { IsNull, MoreThan, Not } from 'typeorm';
|
|
|
|
import { Followings, Instances } from '@/models/index.js';
|
2022-06-27 20:41:22 -05:00
|
|
|
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: {
|
2022-06-30 06:15:14 -05:00
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
2022-06-27 20:41:22 -05:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
|
|
export default define(meta, paramDef, async (ps) => {
|
2022-06-27 23:05:20 -05:00
|
|
|
const [topSubInstances, topPubInstances, allSubCount, allPubCount] = await Promise.all([
|
2022-06-27 20:41:22 -05:00
|
|
|
Instances.find({
|
|
|
|
where: {
|
|
|
|
followersCount: MoreThan(0),
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
followersCount: 'DESC',
|
|
|
|
},
|
2022-06-30 06:15:14 -05:00
|
|
|
take: ps.limit,
|
2022-06-27 20:41:22 -05:00
|
|
|
}),
|
|
|
|
Instances.find({
|
|
|
|
where: {
|
|
|
|
followingCount: MoreThan(0),
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
followingCount: 'DESC',
|
|
|
|
},
|
2022-06-30 06:15:14 -05:00
|
|
|
take: ps.limit,
|
2022-06-27 20:41:22 -05:00
|
|
|
}),
|
2022-06-27 23:05:20 -05:00
|
|
|
Followings.count({
|
|
|
|
where: {
|
|
|
|
followeeHost: Not(IsNull()),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
Followings.count({
|
|
|
|
where: {
|
|
|
|
followerHost: Not(IsNull()),
|
|
|
|
},
|
|
|
|
}),
|
2022-06-27 20:41:22 -05:00
|
|
|
]);
|
|
|
|
|
2022-06-27 23:05:20 -05:00
|
|
|
const gotSubCount = topSubInstances.map(x => x.followersCount).reduce((a, b) => a + b, 0);
|
|
|
|
const gotPubCount = topSubInstances.map(x => x.followingCount).reduce((a, b) => a + b, 0);
|
|
|
|
|
2022-06-27 20:41:22 -05:00
|
|
|
return await awaitAll({
|
|
|
|
topSubInstances: Instances.packMany(topSubInstances),
|
2022-06-27 23:05:20 -05:00
|
|
|
otherFollowersCount: Math.max(0, allSubCount - gotSubCount),
|
2022-06-27 20:41:22 -05:00
|
|
|
topPubInstances: Instances.packMany(topPubInstances),
|
2022-06-27 23:05:20 -05:00
|
|
|
otherFollowingCount: Math.max(0, allPubCount - gotPubCount),
|
2022-06-27 20:41:22 -05:00
|
|
|
});
|
|
|
|
});
|