egirlskey/packages/backend/src/core/MetaService.ts

160 lines
4.1 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
2023-04-14 04:50:05 +00:00
import * as Redis from 'ioredis';
2022-09-17 18:27:08 +00:00
import { DI } from '@/di-symbols.js';
import { MiMeta } from '@/models/Meta.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { bindThis } from '@/decorators.js';
2023-09-29 02:29:54 +00:00
import type { GlobalEvents } from '@/core/GlobalEventService.js';
import { FeaturedService } from '@/core/FeaturedService.js';
import type { OnApplicationShutdown } from '@nestjs/common';
2022-09-17 18:27:08 +00:00
@Injectable()
export class MetaService implements OnApplicationShutdown {
private cache: MiMeta | undefined;
private intervalId: NodeJS.Timeout;
2022-09-17 18:27:08 +00:00
constructor(
@Inject(DI.redisForSub)
private redisForSub: Redis.Redis,
2022-09-17 18:27:08 +00:00
@Inject(DI.db)
private db: DataSource,
private featuredService: FeaturedService,
private globalEventService: GlobalEventService,
2022-09-17 18:27:08 +00:00
) {
//this.onMessage = this.onMessage.bind(this);
2022-09-17 18:27:08 +00:00
if (process.env.NODE_ENV !== 'test') {
2022-09-18 18:11:50 +00:00
this.intervalId = setInterval(() => {
2022-09-17 18:27:08 +00:00
this.fetch(true).then(meta => {
// fetch内でもセットしてるけど仕様変更の可能性もあるため一応
2022-09-18 18:11:50 +00:00
this.cache = meta;
2022-09-17 18:27:08 +00:00
});
}, 1000 * 60 * 5);
2022-09-17 18:27:08 +00:00
}
this.redisForSub.on('message', this.onMessage);
2022-09-17 18:27:08 +00:00
}
@bindThis
2022-09-23 22:12:11 +00:00
private async onMessage(_: string, data: string): Promise<void> {
const obj = JSON.parse(data);
if (obj.channel === 'internal') {
2023-09-29 02:29:54 +00:00
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
switch (type) {
case 'metaUpdated': {
2024-01-22 08:44:03 +00:00
this.cache = { // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
...body,
proxyAccount: null, // joinなカラムは通常取ってこないので
};
break;
}
default:
break;
}
}
}
@bindThis
public async fetch(noCache = false): Promise<MiMeta> {
2022-09-18 18:11:50 +00:00
if (!noCache && this.cache) return this.cache;
2022-09-17 18:27:08 +00:00
return await this.db.transaction(async transactionalEntityManager => {
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
const metas = await transactionalEntityManager.find(MiMeta, {
2022-09-17 18:27:08 +00:00
order: {
id: 'DESC',
},
});
2022-09-17 18:27:08 +00:00
const meta = metas[0];
2022-09-17 18:27:08 +00:00
if (meta) {
2022-09-18 18:11:50 +00:00
this.cache = meta;
2022-09-17 18:27:08 +00:00
return meta;
} else {
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
const saved = await transactionalEntityManager
.upsert(
MiMeta,
2022-09-17 18:27:08 +00:00
{
id: 'x',
},
['id'],
)
.then((x) => transactionalEntityManager.findOneByOrFail(MiMeta, x.identifiers[0]));
2022-09-18 18:11:50 +00:00
this.cache = saved;
2022-09-17 18:27:08 +00:00
return saved;
}
});
}
@bindThis
public async update(data: Partial<MiMeta>): Promise<MiMeta> {
let before: MiMeta | undefined;
const updated = await this.db.transaction(async transactionalEntityManager => {
const metas = await transactionalEntityManager.find(MiMeta, {
order: {
id: 'DESC',
},
});
before = metas[0];
if (before) {
await transactionalEntityManager.update(MiMeta, before.id, data);
const metas = await transactionalEntityManager.find(MiMeta, {
order: {
id: 'DESC',
},
});
return metas[0];
} else {
return await transactionalEntityManager.save(MiMeta, data);
}
});
if (data.hiddenTags) {
process.nextTick(() => {
const hiddenTags = new Set<string>(data.hiddenTags);
if (before) {
for (const previousHiddenTag of before.hiddenTags) {
hiddenTags.delete(previousHiddenTag);
}
}
for (const hiddenTag of hiddenTags) {
this.featuredService.removeHashtagsFromRanking(hiddenTag);
}
});
}
this.globalEventService.publishInternalEvent('metaUpdated', updated);
return updated;
}
@bindThis
2023-05-29 04:21:26 +00:00
public dispose(): void {
2022-09-18 18:11:50 +00:00
clearInterval(this.intervalId);
this.redisForSub.off('message', this.onMessage);
2022-09-17 18:27:08 +00:00
}
2023-05-29 04:21:26 +00:00
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
2022-09-17 18:27:08 +00:00
}