fix nodeinfo cache
This commit is contained in:
parent
79ea21fdc4
commit
08fcb7f88c
1 changed files with 30 additions and 48 deletions
|
|
@ -4,9 +4,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { IsNull, MoreThan } from 'typeorm';
|
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import type { UsersRepository } from '@/models/_.js';
|
import type { MiMeta } from '@/models/Meta.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import { MetaService } from '@/core/MetaService.js';
|
import { MetaService } from '@/core/MetaService.js';
|
||||||
import { MemorySingleCache } from '@/misc/cache.js';
|
import { MemorySingleCache } from '@/misc/cache.js';
|
||||||
|
|
@ -15,6 +14,7 @@ import NotesChart from '@/core/chart/charts/notes.js';
|
||||||
import UsersChart from '@/core/chart/charts/users.js';
|
import UsersChart from '@/core/chart/charts/users.js';
|
||||||
import { DEFAULT_POLICIES } from '@/core/RoleService.js';
|
import { DEFAULT_POLICIES } from '@/core/RoleService.js';
|
||||||
import { SystemAccountService } from '@/core/SystemAccountService.js';
|
import { SystemAccountService } from '@/core/SystemAccountService.js';
|
||||||
|
import { InstanceStatsService } from '@/core/InstanceStatsService.js';
|
||||||
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
||||||
|
|
||||||
const nodeinfo2_1path = '/nodeinfo/2.1';
|
const nodeinfo2_1path = '/nodeinfo/2.1';
|
||||||
|
|
@ -27,13 +27,14 @@ export class NodeinfoServerService {
|
||||||
@Inject(DI.config)
|
@Inject(DI.config)
|
||||||
private config: Config,
|
private config: Config,
|
||||||
|
|
||||||
@Inject(DI.usersRepository)
|
@Inject(DI.meta)
|
||||||
private usersRepository: UsersRepository,
|
private readonly meta: MiMeta,
|
||||||
|
|
||||||
private systemAccountService: SystemAccountService,
|
private systemAccountService: SystemAccountService,
|
||||||
private metaService: MetaService,
|
private metaService: MetaService,
|
||||||
private notesChart: NotesChart,
|
private notesChart: NotesChart,
|
||||||
private usersChart: UsersChart,
|
private usersChart: UsersChart,
|
||||||
|
private readonly instanceStatsService: InstanceStatsService,
|
||||||
) {
|
) {
|
||||||
//this.createServer = this.createServer.bind(this);
|
//this.createServer = this.createServer.bind(this);
|
||||||
}
|
}
|
||||||
|
|
@ -51,38 +52,28 @@ export class NodeinfoServerService {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
|
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
|
||||||
const nodeinfo2 = async (version: number) => {
|
const nodeinfo2 = async (version: '2.0' | '2.1') => {
|
||||||
const now = Date.now();
|
const meta = this.meta;
|
||||||
|
const stats = await this.instanceStatsService.fetch();
|
||||||
const notesChart = await this.notesChart.getChart('hour', 1, null);
|
|
||||||
const localPosts = notesChart.local.total[0];
|
|
||||||
|
|
||||||
const usersChart = await this.usersChart.getChart('hour', 1, null);
|
|
||||||
const total = usersChart.local.total[0];
|
|
||||||
|
|
||||||
const [
|
|
||||||
meta,
|
|
||||||
activeHalfyear,
|
|
||||||
activeMonth,
|
|
||||||
] = await Promise.all([
|
|
||||||
this.metaService.fetch(true),
|
|
||||||
// 重い
|
|
||||||
this.usersRepository.count({ where: { host: IsNull(), isBot: false, lastActiveDate: MoreThan(new Date(now - 15552000000)) } }),
|
|
||||||
this.usersRepository.count({ where: { host: IsNull(), isBot: false, lastActiveDate: MoreThan(new Date(now - 2592000000)) } }),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const proxyAccount = await this.systemAccountService.fetch('proxy');
|
const proxyAccount = await this.systemAccountService.fetch('proxy');
|
||||||
|
|
||||||
const basePolicies = { ...DEFAULT_POLICIES, ...meta.policies };
|
const basePolicies = { ...DEFAULT_POLICIES, ...meta.policies };
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
const software = {
|
||||||
const document: any = {
|
name: 'sharkey',
|
||||||
software: {
|
version: this.config.version,
|
||||||
name: 'sharkey',
|
};
|
||||||
version: this.config.version,
|
|
||||||
homepage: nodeinfo_homepage,
|
if (version !== '2.0') {
|
||||||
|
Object.assign(software, {
|
||||||
|
homepage: meta.repositoryUrl ?? nodeinfo_homepage,
|
||||||
repository: meta.repositoryUrl,
|
repository: meta.repositoryUrl,
|
||||||
},
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
version,
|
||||||
|
software,
|
||||||
protocols: ['activitypub'],
|
protocols: ['activitypub'],
|
||||||
services: {
|
services: {
|
||||||
inbound: [] as string[],
|
inbound: [] as string[],
|
||||||
|
|
@ -90,8 +81,12 @@ export class NodeinfoServerService {
|
||||||
},
|
},
|
||||||
openRegistrations: !meta.disableRegistration,
|
openRegistrations: !meta.disableRegistration,
|
||||||
usage: {
|
usage: {
|
||||||
users: { total, activeHalfyear, activeMonth },
|
users: {
|
||||||
localPosts,
|
total: stats.usersTotal,
|
||||||
|
activeHalfyear: stats.usersActiveSixMonths,
|
||||||
|
activeMonth: stats.usersActiveMonth,
|
||||||
|
},
|
||||||
|
localPosts: stats.notesTotal,
|
||||||
localComments: 0,
|
localComments: 0,
|
||||||
},
|
},
|
||||||
metadata: {
|
metadata: {
|
||||||
|
|
@ -138,18 +133,9 @@ export class NodeinfoServerService {
|
||||||
themeColor: meta.themeColor ?? '#86b300',
|
themeColor: meta.themeColor ?? '#86b300',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (version >= 21) {
|
|
||||||
document.software.repository = meta.repositoryUrl;
|
|
||||||
document.software.homepage = meta.repositoryUrl;
|
|
||||||
}
|
|
||||||
return document;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const cache = new MemorySingleCache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10); // 10m
|
|
||||||
|
|
||||||
fastify.get(nodeinfo2_1path, async (request, reply) => {
|
fastify.get(nodeinfo2_1path, async (request, reply) => {
|
||||||
const base = await cache.fetch(() => nodeinfo2(21));
|
|
||||||
|
|
||||||
reply
|
reply
|
||||||
.type(
|
.type(
|
||||||
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
|
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
|
||||||
|
|
@ -159,14 +145,10 @@ export class NodeinfoServerService {
|
||||||
.header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
.header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
||||||
.header('Access-Control-Allow-Origin', '*')
|
.header('Access-Control-Allow-Origin', '*')
|
||||||
.header('Access-Control-Expose-Headers', 'Vary');
|
.header('Access-Control-Expose-Headers', 'Vary');
|
||||||
return { version: '2.1', ...base };
|
return await nodeinfo2('2.1');
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.get(nodeinfo2_0path, async (request, reply) => {
|
fastify.get(nodeinfo2_0path, async (request, reply) => {
|
||||||
const base = await cache.fetch(() => nodeinfo2(20));
|
|
||||||
|
|
||||||
delete (base as any).software.repository;
|
|
||||||
|
|
||||||
reply
|
reply
|
||||||
.type(
|
.type(
|
||||||
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"',
|
'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"',
|
||||||
|
|
@ -176,7 +158,7 @@ export class NodeinfoServerService {
|
||||||
.header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
.header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
||||||
.header('Access-Control-Allow-Origin', '*')
|
.header('Access-Control-Allow-Origin', '*')
|
||||||
.header('Access-Control-Expose-Headers', 'Vary');
|
.header('Access-Control-Expose-Headers', 'Vary');
|
||||||
return { version: '2.0', ...base };
|
return await nodeinfo2('2.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue