]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Refresh remote actors on GET enpoints
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
CommitLineData
3fd3ab2d 1import { ActivityFollow } from '../../../../shared/models/activitypub'
da854ddd
C
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { sequelizeTypescript } from '../../../initializers'
50d6de9c
C
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3fd3ab2d 7import { sendAccept } from '../send'
f7cc67b4 8import { Notifier } from '../../notifier'
7a7724e6 9
e587e0ec 10async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
7a7724e6 11 const activityObject = activity.object
7a7724e6 12
e587e0ec 13 return retryTransactionWrapper(processFollow, byActor, activityObject)
7a7724e6
C
14}
15
16// ---------------------------------------------------------------------------
17
18export {
19 processFollowActivity
20}
21
22// ---------------------------------------------------------------------------
23
90d4bb81 24async function processFollow (actor: ActorModel, targetActorURL: string) {
f7cc67b4 25 const { actorFollow, created } = await sequelizeTypescript.transaction(async t => {
e587e0ec 26 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
ce548a10 27
50d6de9c
C
28 if (!targetActor) throw new Error('Unknown actor')
29 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
7a7724e6 30
f7cc67b4 31 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
350e31d6 32 where: {
50d6de9c
C
33 actorId: actor.id,
34 targetActorId: targetActor.id
350e31d6
C
35 },
36 defaults: {
50d6de9c
C
37 actorId: actor.id,
38 targetActorId: targetActor.id,
350e31d6
C
39 state: 'accepted'
40 },
ce548a10 41 transaction: t
350e31d6 42 })
40ff5707 43
32b2b43c
C
44 actorFollow.ActorFollower = actor
45 actorFollow.ActorFollowing = targetActor
46
50d6de9c
C
47 if (actorFollow.state !== 'accepted') {
48 actorFollow.state = 'accepted'
49 await actorFollow.save({ transaction: t })
40ff5707
C
50 }
51
50d6de9c
C
52 actorFollow.ActorFollower = actor
53 actorFollow.ActorFollowing = targetActor
ce548a10 54
50d6de9c 55 // Target sends to actor he accepted the follow request
f7cc67b4
C
56 await sendAccept(actorFollow)
57
58 return { actorFollow, created }
7a7724e6 59 })
ce548a10 60
f7cc67b4
C
61 if (created) Notifier.Instance.notifyOfNewFollow(actorFollow)
62
85414add 63 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
7a7724e6 64}