2021-03-24 02:05:37 +00:00
|
|
|
import { URL } from 'url';
|
2021-03-23 08:43:07 +00:00
|
|
|
import { getJson } from '@/misc/fetch';
|
2019-04-10 06:07:21 +00:00
|
|
|
import { query as urlQuery } from '../prelude/url';
|
2018-03-31 10:55:00 +00:00
|
|
|
|
|
|
|
type ILink = {
|
2018-04-08 19:08:56 +00:00
|
|
|
href: string;
|
2019-04-12 16:43:22 +00:00
|
|
|
rel?: string;
|
2018-04-01 12:24:25 +00:00
|
|
|
};
|
2018-03-31 10:55:00 +00:00
|
|
|
|
|
|
|
type IWebFinger = {
|
2018-04-08 19:08:56 +00:00
|
|
|
links: ILink[];
|
|
|
|
subject: string;
|
2018-04-01 12:24:25 +00:00
|
|
|
};
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2019-04-10 06:07:21 +00:00
|
|
|
export default async function(query: string): Promise<IWebFinger> {
|
|
|
|
const url = genUrl(query);
|
|
|
|
|
2020-04-09 14:42:23 +00:00
|
|
|
return await getJson(url, 'application/jrd+json, application/json');
|
2019-04-10 06:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function genUrl(query: string) {
|
|
|
|
if (query.match(/^https?:\/\//)) {
|
|
|
|
const u = new URL(query);
|
|
|
|
return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
|
|
|
|
}
|
|
|
|
|
|
|
|
const m = query.match(/^([^@]+)@(.*)/);
|
|
|
|
if (m) {
|
|
|
|
const hostname = m[2];
|
|
|
|
return `https://${hostname}/.well-known/webfinger?` + urlQuery({ resource: `acct:${query}` });
|
|
|
|
}
|
2018-04-02 09:36:47 +00:00
|
|
|
|
2019-09-15 14:27:33 +00:00
|
|
|
throw new Error(`Invalid query (${query})`);
|
2018-04-02 09:36:47 +00:00
|
|
|
}
|