summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/kernel/undo/follow.ts
blob: d75f055640457dbbdae1e0e86f76a872e7d5a3ba (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
import config from '../../../../config';
import unfollow from '../../../../services/following/delete';
import cancelRequest from '../../../../services/following/requests/cancel';
import { IFollow } from '../../type';
import { IRemoteUser } from '../../../../models/entities/user';
import { Users, FollowRequests, Followings } from '../../../../models';

export default async (actor: IRemoteUser, activity: IFollow): Promise<void> => {
	const id = typeof activity.object == 'string' ? activity.object : activity.object.id;
	if (id == null) throw new Error('missing id');

	if (!id.startsWith(config.url + '/')) {
		return;
	}

	const followee = await Users.findOne(id.split('/').pop());

	if (followee == null) {
		throw new Error('followee not found');
	}

	if (followee.host != null) {
		throw new Error('フォロー解除しようとしているユーザーはローカルユーザーではありません');
	}

	const req = await FollowRequests.findOne({
		followerId: actor.id,
		followeeId: followee.id
	});

	const following = await Followings.findOne({
		followerId: actor.id,
		followeeId: followee.id
	});

	if (req) {
		await cancelRequest(followee, actor);
	}

	if (following) {
		await unfollow(actor, followee);
	}
};