misskey/src/remote/webfinger.ts

34 lines
793 B
TypeScript
Raw Normal View History

2021-03-23 09:43:07 +01:00
import { getJson } from '@/misc/fetch';
import { query as urlQuery } from '../prelude/url';
2018-03-31 12:55:00 +02:00
type ILink = {
2018-04-08 21:08:56 +02:00
href: string;
rel?: string;
2018-04-01 14:24:25 +02:00
};
2018-03-31 12:55:00 +02:00
type IWebFinger = {
2018-04-08 21:08:56 +02:00
links: ILink[];
subject: string;
2018-04-01 14:24:25 +02:00
};
2018-03-31 12:55:00 +02:00
export default async function(query: string): Promise<IWebFinger> {
const url = genUrl(query);
return await getJson(url, 'application/jrd+json, application/json');
}
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 11:36:47 +02:00
throw new Error(`Invalid query (${query})`);
2018-04-02 11:36:47 +02:00
}