]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-accept.ts
c55b57820e698fcc635d2f4e469e6de17c5ecd69
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-accept.ts
1 import { ActivityAccept } from '../../../../shared/models/activitypub'
2 import { getActorUrl } from '../../../helpers/activitypub'
3 import { ActorModel } from '../../../models/activitypub/actor'
4 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
5 import { addFetchOutboxJob } from '../fetch'
6
7 async function processAcceptActivity (activity: ActivityAccept, inboxActor?: ActorModel) {
8 if (inboxActor === undefined) throw new Error('Need to accept on explicit inbox.')
9
10 const actorUrl = getActorUrl(activity.actor)
11 const targetActor = await ActorModel.loadByUrl(actorUrl)
12
13 return processAccept(inboxActor, targetActor)
14 }
15
16 // ---------------------------------------------------------------------------
17
18 export {
19 processAcceptActivity
20 }
21
22 // ---------------------------------------------------------------------------
23
24 async function processAccept (actor: ActorModel, targetActor: ActorModel) {
25 const follow = await ActorFollowModel.loadByActorAndTarget(actor.id, targetActor.id)
26 if (!follow) throw new Error('Cannot find associated follow.')
27
28 if (follow.state !== 'accepted') {
29 follow.set('state', 'accepted')
30 await follow.save()
31 await addFetchOutboxJob(targetActor)
32 }
33 }