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