bf9e74ca05
* fix: resolving alias for local users (#9199) Signed-off-by: xtex <xtexchooser@duck.com> * style: return type for RemoteUserResolveService#resolveSelf Signed-off-by: xtex <xtexchooser@duck.com> * docs: update CHANGELOG Signed-off-by: xtex <xtexchooser@duck.com> * style: fix typecheck Signed-off-by: xtex <xtexchooser@duck.com> --------- Signed-off-by: xtex <xtexchooser@duck.com> Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { URL } from 'node:url';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { DI } from '@/di-symbols.js';
|
|
import type { Config } from '@/config.js';
|
|
import { query as urlQuery } from '@/misc/prelude/url.js';
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
export type ILink = {
|
|
href: string;
|
|
rel?: string;
|
|
};
|
|
|
|
export type IWebFinger = {
|
|
links: ILink[];
|
|
subject: string;
|
|
};
|
|
|
|
const urlRegex = /^https?:\/\//;
|
|
const mRegex = /^([^@]+)@(.*)/;
|
|
|
|
@Injectable()
|
|
export class WebfingerService {
|
|
constructor(
|
|
@Inject(DI.config)
|
|
private config: Config,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
) {
|
|
}
|
|
|
|
@bindThis
|
|
public async webfinger(query: string): Promise<IWebFinger> {
|
|
const url = this.genUrl(query);
|
|
|
|
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
|
|
}
|
|
|
|
@bindThis
|
|
private genUrl(query: string): string {
|
|
if (query.match(urlRegex)) {
|
|
const u = new URL(query);
|
|
return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
|
|
}
|
|
|
|
const m = query.match(mRegex);
|
|
if (m) {
|
|
const hostname = m[2];
|
|
const useHttp = process.env.MISSKEY_WEBFINGER_USE_HTTP && process.env.MISSKEY_WEBFINGER_USE_HTTP.toLowerCase() === 'true';
|
|
return `http${useHttp ? '' : 's'}://${hostname}/.well-known/webfinger?${urlQuery({ resource: `acct:${query}` })}`;
|
|
}
|
|
|
|
throw new Error(`Invalid query (${query})`);
|
|
}
|
|
}
|