summary refs log tree commit diff
path: root/src/activitypub/webfinger/index.ts
blob: 0b82e1039173065800994280798f9e54403bcd18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { route } from "@spacebar/api";
import { Channel, Config, User, WebfingerResponse } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";

const router = Router();
export default router;

router.get(
	"/",
	route({
		query: {
			resource: {
				type: "string",
				description: "Resource to locate",
			},
		},
		responses: {
			200: {
				body: "WebfingerResponse",
			},
		},
	}),
	async (req: Request, res: Response<WebfingerResponse>) => {
		let resource = req.query.resource as string | undefined;
		if (!resource) throw new HTTPError("Must specify resource");

		// we know what you mean, bro
		resource = resource.replace("acct:", "");

		const [resourceId, resourceDomain] = resource.split("@");

		const { webDomain } = Config.get().federation;
		if (resourceDomain != webDomain)
			throw new HTTPError("Resource could not be found", 404);

		const found =
			(await User.findOne({
				where: { id: resourceId },
				select: ["id"],
			})) ||
			(await Channel.findOne({
				where: { id: resourceId },
				select: ["id"],
			}));

		if (!found) throw new HTTPError("Resource could not be found", 404);

		const type = found instanceof Channel ? "channel" : "user";

		return res.json({
			subject: `acct:${resourceId}@${webDomain}`, // mastodon always returns acct so might as well
			aliases: [`https://${webDomain}/fed/${type}/${resourceId}`],
			links: [
				{
					rel: "self",
					type: "application/activity+json",
					href: `https://${webDomain}/fed/${type}/${resourceId}`,
				},
			],
		});
	},
);