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