2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import { toASCII } from 'punycode';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UtilityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public getFullApAccount(username: string, host: string | null): string {
|
|
|
|
return host ? `${username}@${this.toPuny(host)}` : `${username}@${this.toPuny(this.config.host)}`;
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public isSelfHost(host: string | null): boolean {
|
|
|
|
if (host == null) return true;
|
|
|
|
return this.toPuny(this.config.host) === this.toPuny(host);
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:21:07 +09: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 20:11:27 +09: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}`));
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public extractDbHost(uri: string): string {
|
|
|
|
const url = new URL(uri);
|
|
|
|
return this.toPuny(url.hostname);
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public toPuny(host: string): string {
|
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public toPunyNullable(host: string | null | undefined): string | null {
|
|
|
|
if (host == null) return null;
|
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|
|
|
|
}
|