]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Stronger actor association typing in AP functions
[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'
6dd9de95 4import { sequelizeTypescript } from '../../../initializers'
50d6de9c
C
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
5b9c965d 7import { sendAccept, sendReject } from '../send'
f7cc67b4 8import { Notifier } from '../../notifier'
848f499d 9import { getAPId } from '../../../helpers/activitypub'
5b9c965d 10import { getServerActor } from '../../../helpers/utils'
6dd9de95 11import { CONFIG } from '../../../initializers/config'
1198edf4 12import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
5224c394
C
13import { SignatureActorModel } from '../../../typings/models'
14import { ActorFollowModelLight } from '../../../typings/models/actor-follow'
7a7724e6 15
1198edf4
C
16async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
848f499d 18 const activityObject = getAPId(activity.object)
7a7724e6 19
e587e0ec 20 return retryTransactionWrapper(processFollow, byActor, activityObject)
7a7724e6
C
21}
22
23// ---------------------------------------------------------------------------
24
25export {
26 processFollowActivity
27}
28
29// ---------------------------------------------------------------------------
30
5224c394 31async function processFollow (byActor: SignatureActorModel, targetActorURL: string) {
883993c8 32 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
e587e0ec 33 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
ce548a10 34
50d6de9c
C
35 if (!targetActor) throw new Error('Unknown actor')
36 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
7a7724e6 37
5b9c965d 38 const serverActor = await getServerActor()
883993c8
C
39 const isFollowingInstance = targetActor.id === serverActor.id
40
41 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
14893eb7
C
42 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
43
5224c394 44 await sendReject(byActor, targetActor)
1735c825
C
45
46 return { actorFollow: undefined }
5b9c965d
C
47 }
48
f7cc67b4 49 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
350e31d6 50 where: {
5224c394 51 actorId: byActor.id,
50d6de9c 52 targetActorId: targetActor.id
350e31d6
C
53 },
54 defaults: {
5224c394 55 actorId: byActor.id,
50d6de9c 56 targetActorId: targetActor.id,
14893eb7 57 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
350e31d6 58 },
ce548a10 59 transaction: t
5224c394 60 }) as [ ActorFollowModelLight, boolean ]
40ff5707 61
14893eb7 62 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
50d6de9c
C
63 actorFollow.state = 'accepted'
64 await actorFollow.save({ transaction: t })
40ff5707
C
65 }
66
5224c394 67 actorFollow.ActorFollower = byActor
50d6de9c 68 actorFollow.ActorFollowing = targetActor
ce548a10 69
50d6de9c 70 // Target sends to actor he accepted the follow request
14893eb7 71 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
f7cc67b4 72
883993c8 73 return { actorFollow, created, isFollowingInstance }
7a7724e6 74 })
ce548a10 75
883993c8
C
76 // Rejected
77 if (!actorFollow) return
78
79 if (created) {
80 if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
81 else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
82 }
f7cc67b4 83
5224c394 84 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
7a7724e6 85}