summaryrefslogtreecommitdiff
path: root/src/server/activitypub.ts
blob: 6cc31de7b5c491cd0d662e0ed2701b2103801e20 (plain)
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
import config from '../conf';
import { extractPublic } from '../crypto_key';
import parseAcct from '../common/user/parse-acct';
import User, { ILocalAccount } from '../models/user';
const express = require('express');

const app = express();

app.get('/@:user', async (req, res, next) => {
	const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
	if (!['application/activity+json', 'application/ld+json'].includes(accepted)) {
		return next();
	}

	const { username, host } = parseAcct(req.params.user);
	if (host !== null) {
		return res.send(422);
	}

	const user = await User.findOne({
		usernameLower: username.toLowerCase(),
		host: null
	});
	if (user === null) {
		return res.send(404);
	}

	const id = `${config.url}/@${user.username}`;

	if (username !== user.username) {
		return res.redirect(id);
	}

	res.json({
		'@context': [
			'https://www.w3.org/ns/activitystreams',
			'https://w3id.org/security/v1'
		],
		type: 'Person',
		id,
		preferredUsername: user.username,
		name: user.name,
		summary: user.description,
		icon: user.avatarId && {
			type: 'Image',
			url: `${config.drive_url}/${user.avatarId}`
		},
		image: user.bannerId && {
			type: 'Image',
			url: `${config.drive_url}/${user.bannerId}`
		},
		publicKey: {
			type: 'Key',
			owner: id,
			publicKeyPem: extractPublic((user.account as ILocalAccount).keypair)
		}
	});
});

export default app;