]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Merge branch 'develop' into cli-wrapper
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
1 import { ActivityFollow } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7 import { sendAccept } from '../send'
8
9 async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
10 const activityObject = activity.object
11
12 return retryTransactionWrapper(processFollow, byActor, activityObject)
13 }
14
15 // ---------------------------------------------------------------------------
16
17 export {
18 processFollowActivity
19 }
20
21 // ---------------------------------------------------------------------------
22
23 async function processFollow (actor: ActorModel, targetActorURL: string) {
24 await sequelizeTypescript.transaction(async t => {
25 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
26
27 if (!targetActor) throw new Error('Unknown actor')
28 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
29
30 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
31 where: {
32 actorId: actor.id,
33 targetActorId: targetActor.id
34 },
35 defaults: {
36 actorId: actor.id,
37 targetActorId: targetActor.id,
38 state: 'accepted'
39 },
40 transaction: t
41 })
42
43 actorFollow.ActorFollower = actor
44 actorFollow.ActorFollowing = targetActor
45
46 if (actorFollow.state !== 'accepted') {
47 actorFollow.state = 'accepted'
48 await actorFollow.save({ transaction: t })
49 }
50
51 actorFollow.ActorFollower = actor
52 actorFollow.ActorFollowing = targetActor
53
54 // Target sends to actor he accepted the follow request
55 return sendAccept(actorFollow)
56 })
57
58 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
59 }