2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import { toASCII } from 'punycode';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-12-24 07:23:56 +01:00
|
|
|
import RE2 from 're2';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UtilityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public getFullApAccount(username: string, host: string | null): string {
|
|
|
|
return host ? `${username}@${this.toPuny(host)}` : `${username}@${this.toPuny(this.config.host)}`;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public isSelfHost(host: string | null): boolean {
|
|
|
|
if (host == null) return true;
|
|
|
|
return this.toPuny(this.config.host) === this.toPuny(host);
|
|
|
|
}
|
|
|
|
|
2023-01-13 10:21:07 +01:00
|
|
|
@bindThis
|
|
|
|
public isBlockedHost(blockedHosts: string[], host: string | null): boolean {
|
|
|
|
if (host == null) return false;
|
|
|
|
return blockedHosts.some(x => `.${host.toLowerCase()}`.endsWith(`.${x}`));
|
|
|
|
}
|
|
|
|
|
2023-10-16 13:11:27 +02:00
|
|
|
@bindThis
|
|
|
|
public isSilencedHost(silencedHosts: string[] | undefined, host: string | null): boolean {
|
|
|
|
if (!silencedHosts || host == null) return false;
|
|
|
|
return silencedHosts.some(x => `.${host.toLowerCase()}`.endsWith(`.${x}`));
|
|
|
|
}
|
|
|
|
|
2023-12-24 07:23:56 +01:00
|
|
|
@bindThis
|
2024-02-09 02:07:18 +01:00
|
|
|
public isKeyWordIncluded(text: string, keyWords: string[]): boolean {
|
|
|
|
if (keyWords.length === 0) return false;
|
2023-12-24 07:23:56 +01:00
|
|
|
if (text === '') return false;
|
|
|
|
|
|
|
|
const regexpregexp = /^\/(.+)\/(.*)$/;
|
|
|
|
|
2024-02-09 02:07:18 +01:00
|
|
|
const matched = keyWords.some(filter => {
|
2023-12-24 07:23:56 +01:00
|
|
|
// represents RegExp
|
|
|
|
const regexp = filter.match(regexpregexp);
|
|
|
|
// This should never happen due to input sanitisation.
|
|
|
|
if (!regexp) {
|
|
|
|
const words = filter.split(' ');
|
|
|
|
return words.every(keyword => text.includes(keyword));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// TODO: RE2インスタンスをキャッシュ
|
|
|
|
return new RE2(regexp[1], regexp[2]).test(text);
|
|
|
|
} catch (err) {
|
|
|
|
// This should never happen due to input sanitisation.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return matched;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public extractDbHost(uri: string): string {
|
|
|
|
const url = new URL(uri);
|
|
|
|
return this.toPuny(url.hostname);
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public toPuny(host: string): string {
|
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public toPunyNullable(host: string | null | undefined): string | null {
|
|
|
|
if (host == null) return null;
|
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|
|
|
|
}
|