summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/MetaService.ts
blob: 07f82dc23e097da8ef3aaff84b75a6fd05a9d154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Inject, Injectable } from '@nestjs/common';
import { DataSource, EntityManager } 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 { MiInstance } from '@/models/Instance.js';
import { diffArrays } from '@/misc/diff-arrays.js';
import type { MetasRepository } from '@/models/_.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,

		@Inject(DI.metasRepository)
		private readonly metasRepository: MetasRepository,

		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;

		// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
		let meta = await this.metasRepository.createQueryBuilder('meta')
			.select()
			.orderBy({
				id: 'DESC',
			})
			.limit(1)
			.getOne();

		if (!meta) {
			await this.metasRepository.createQueryBuilder('meta')
				.insert()
				.values({
					id: 'x',
				})
				.orIgnore()
				.execute();

			meta = await this.metasRepository.createQueryBuilder('meta')
				.select()
				.orderBy({
					id: 'DESC',
				})
				.limit(1)
				.getOneOrFail();
		}

		this.cache = meta;
		return meta;
	}

	@bindThis
	public async update(data: Partial<MiMeta>): Promise<MiMeta> {
		let before: MiMeta | undefined;

		const updated = await this.db.transaction(async transactionalEntityManager => {
			const metas: (MiMeta | undefined)[] = 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',
				},
			});

			// Propagate changes to blockedHosts, silencedHosts, mediaSilencedHosts, federationInstances, and bubbleInstances to the relevant instance rows
			// Do this inside the transaction to avoid potential race condition (when an instance gets registered while we're updating).
			await this.persistBlocks(transactionalEntityManager, before ?? {}, afters[0]);

			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();
	}

	private async persistBlocks(tem: EntityManager, before: Partial<MiMeta>, after: Partial<MiMeta>): Promise<void> {
		await this.persistBlock(tem, before.blockedHosts, after.blockedHosts, 'isBlocked');
		await this.persistBlock(tem, before.silencedHosts, after.silencedHosts, 'isSilenced');
		await this.persistBlock(tem, before.mediaSilencedHosts, after.mediaSilencedHosts, 'isMediaSilenced');
		await this.persistBlock(tem, before.federationHosts, after.federationHosts, 'isAllowListed');
		await this.persistBlock(tem, before.bubbleInstances, after.bubbleInstances, 'isBubbled');
	}

	private async persistBlock(tem: EntityManager, before: string[] | undefined, after: string[] | undefined, field: keyof MiInstance): Promise<void> {
		const { added, removed } = diffArrays(before, after);

		if (removed.length > 0) {
			await this.updateInstancesByHost(tem, field, false, removed);
		}

		if (added.length > 0) {
			await this.updateInstancesByHost(tem, field, true, added);
		}
	}

	private async updateInstancesByHost(tem: EntityManager, field: keyof MiInstance, value: boolean, hosts: string[]): Promise<void> {
		// Use non-array queries when possible, as they are indexed and can be much faster.
		if (hosts.length === 1) {
			const pattern = genHostPattern(hosts[0]);
			await tem
				.createQueryBuilder(MiInstance, 'instance')
				.update()
				.set({ [field]: value })
				.where('(lower(reverse("host")) || \'.\') LIKE :pattern', { pattern })
				.execute();
		} else if (hosts.length > 1) {
			const patterns = hosts.map(host => genHostPattern(host));
			await tem
				.createQueryBuilder(MiInstance, 'instance')
				.update()
				.set({ [field]: value })
				.where('(lower(reverse("host")) || \'.\') LIKE ANY (:patterns)', { patterns })
				.execute();
		}
	}
}

function genHostPattern(host: string): string {
	return host.toLowerCase().split('').reverse().join('') + '.%';
}