mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-30 21:14:12 +00:00 
			
		
		
		
	* wip * wip * wip * Update SystemAccountService.ts * Update 1740121393164-system-accounts.js * Update DeleteAccountService.ts * wip * wip * wip * wip * Update 1740121393164-system-accounts.js * Update RepositoryModule.ts * wip * wip * wip * Update ApRendererService.ts * wip * wip * Update SystemAccountService.ts * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * add print logs * ログが長すぎて出てないかもしれない * fix migration * refactor * fix fed-tests * Update RelayService.ts * merge * Update user.test.ts * chore: emit log * fix: tweak sleep duration * fix: exit 1 * fix: wait for misskey processes to become healthy * fix: longer sleep for user deletion * fix: make sleep longer again * デッドロック解消の試み https://github.com/misskey-dev/misskey/issues/15005 * Revert "デッドロック解消の試み" This reverts commit 266141f66fb584371bbb56ef7eba04e14bcff94d. * wip * Update SystemAccountService.ts --------- Co-authored-by: おさむのひと <46447427+samunohito@users.noreply.github.com> Co-authored-by: zyoshoka <107108195+zyoshoka@users.noreply.github.com>
		
			
				
	
	
		
			162 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			162 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /*
 | |
|  * SPDX-FileCopyrightText: syuilo and misskey-project
 | |
|  * SPDX-License-Identifier: AGPL-3.0-only
 | |
|  */
 | |
| 
 | |
| import { Inject, Injectable } from '@nestjs/common';
 | |
| import { DataSource } from 'typeorm';
 | |
| import * as Redis from 'ioredis';
 | |
| import { DI } from '@/di-symbols.js';
 | |
| import { MiMeta } from '@/models/Meta.js';
 | |
| import { GlobalEventService } from '@/core/GlobalEventService.js';
 | |
| import { bindThis } from '@/decorators.js';
 | |
| import type { GlobalEvents } from '@/core/GlobalEventService.js';
 | |
| import { FeaturedService } from '@/core/FeaturedService.js';
 | |
| import type { OnApplicationShutdown } from '@nestjs/common';
 | |
| 
 | |
| @Injectable()
 | |
| export class MetaService implements OnApplicationShutdown {
 | |
| 	private cache: MiMeta | undefined;
 | |
| 	private intervalId: NodeJS.Timeout;
 | |
| 
 | |
| 	constructor(
 | |
| 		@Inject(DI.redisForSub)
 | |
| 		private redisForSub: Redis.Redis,
 | |
| 
 | |
| 		@Inject(DI.db)
 | |
| 		private db: DataSource,
 | |
| 
 | |
| 		private featuredService: FeaturedService,
 | |
| 		private globalEventService: GlobalEventService,
 | |
| 	) {
 | |
| 		//this.onMessage = this.onMessage.bind(this);
 | |
| 
 | |
| 		if (process.env.NODE_ENV !== 'test') {
 | |
| 			this.intervalId = setInterval(() => {
 | |
| 				this.fetch(true).then(meta => {
 | |
| 					// fetch内でもセットしてるけど仕様変更の可能性もあるため一応
 | |
| 					this.cache = meta;
 | |
| 				});
 | |
| 			}, 1000 * 60 * 5);
 | |
| 		}
 | |
| 
 | |
| 		this.redisForSub.on('message', this.onMessage);
 | |
| 	}
 | |
| 
 | |
| 	@bindThis
 | |
| 	private async onMessage(_: string, data: string): Promise<void> {
 | |
| 		const obj = JSON.parse(data);
 | |
| 
 | |
| 		if (obj.channel === 'internal') {
 | |
| 			const { type, body } = obj.message as GlobalEvents['internal']['payload'];
 | |
| 			switch (type) {
 | |
| 				case 'metaUpdated': {
 | |
| 					this.cache = { // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
 | |
| 						...(body.after),
 | |
| 						rootUser: null, // joinなカラムは通常取ってこないので
 | |
| 					};
 | |
| 					break;
 | |
| 				}
 | |
| 				default:
 | |
| 					break;
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@bindThis
 | |
| 	public async fetch(noCache = false): Promise<MiMeta> {
 | |
| 		if (!noCache && this.cache) return this.cache;
 | |
| 
 | |
| 		return await this.db.transaction(async transactionalEntityManager => {
 | |
| 			// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
 | |
| 			const metas = await transactionalEntityManager.find(MiMeta, {
 | |
| 				order: {
 | |
| 					id: 'DESC',
 | |
| 				},
 | |
| 			});
 | |
| 
 | |
| 			const meta = metas[0];
 | |
| 
 | |
| 			if (meta) {
 | |
| 				this.cache = meta;
 | |
| 				return meta;
 | |
| 			} else {
 | |
| 				// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
 | |
| 				const saved = await transactionalEntityManager
 | |
| 					.upsert(
 | |
| 						MiMeta,
 | |
| 						{
 | |
| 							id: 'x',
 | |
| 						},
 | |
| 						['id'],
 | |
| 					)
 | |
| 					.then((x) => transactionalEntityManager.findOneByOrFail(MiMeta, x.identifiers[0]));
 | |
| 
 | |
| 				this.cache = saved;
 | |
| 				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);
 | |
| 			} else {
 | |
| 				await transactionalEntityManager.save(MiMeta, {
 | |
| 					...data,
 | |
| 					id: 'x',
 | |
| 				});
 | |
| 			}
 | |
| 
 | |
| 			const afters = await transactionalEntityManager.find(MiMeta, {
 | |
| 				order: {
 | |
| 					id: 'DESC',
 | |
| 				},
 | |
| 			});
 | |
| 
 | |
| 			return afters[0];
 | |
| 		});
 | |
| 
 | |
| 		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', { before, after: updated });
 | |
| 
 | |
| 		return updated;
 | |
| 	}
 | |
| 
 | |
| 	@bindThis
 | |
| 	public dispose(): void {
 | |
| 		clearInterval(this.intervalId);
 | |
| 		this.redisForSub.off('message', this.onMessage);
 | |
| 	}
 | |
| 
 | |
| 	@bindThis
 | |
| 	public onApplicationShutdown(signal?: string | undefined): void {
 | |
| 		this.dispose();
 | |
| 	}
 | |
| }
 |