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