summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/act/follow.ts
blob: 3dd029af54c5a896489ddffd4949e7fa82c1fb1a (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
import parseAcct from '../../../acct/parse';
import User, { IRemoteUser } from '../../../models/user';
import config from '../../../config';
import follow from '../../../services/following/create';
import { IFollow } from '../type';

export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
	const prefix = config.url + '/@';
	const id = typeof activity == 'string' ? activity : activity.id;

	if (!id.startsWith(prefix)) {
		return null;
	}

	const { username, host } = parseAcct(id.slice(prefix.length));
	if (host !== null) {
		throw new Error();
	}

	const followee = await User.findOne({ username, host });
	if (followee === null) {
		throw new Error();
	}

	await follow(actor, followee, activity);
};