]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
CommitLineData
7d9ba5c0 1import { getServerActor } from '@server/models/application/application'
3fd3ab2d 2import { ActivityFollow } from '../../../../shared/models/activitypub'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
6dd9de95 5import { CONFIG } from '../../../initializers/config'
7d9ba5c0 6import { sequelizeTypescript } from '../../../initializers/database'
7e98a7df 7import { getAPId } from '../../../lib/activitypub/activity'
7d9ba5c0
C
8import { ActorModel } from '../../../models/actor/actor'
9import { ActorFollowModel } from '../../../models/actor/actor-follow'
26d6bf65
C
10import { APProcessorOptions } from '../../../types/activitypub-processor.model'
11import { MActorFollowActors, MActorSignature } from '../../../types/models'
7d9ba5c0 12import { Notifier } from '../../notifier'
8424c402 13import { autoFollowBackIfNeeded } from '../follow'
7d9ba5c0 14import { sendAccept, sendReject } from '../send'
7a7724e6 15
1198edf4
C
16async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
7a7724e6 18
de94ac86
C
19 const activityId = activity.id
20 const objectId = getAPId(activity.object)
21
22 return retryTransactionWrapper(processFollow, byActor, activityId, objectId)
7a7724e6
C
23}
24
25// ---------------------------------------------------------------------------
26
27export {
28 processFollowActivity
29}
30
31// ---------------------------------------------------------------------------
32
de94ac86 33async function processFollow (byActor: MActorSignature, activityId: string, targetActorURL: string) {
8424c402 34 const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => {
e587e0ec 35 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
ce548a10 36
50d6de9c
C
37 if (!targetActor) throw new Error('Unknown actor')
38 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
7a7724e6 39
5b9c965d 40 const serverActor = await getServerActor()
883993c8
C
41 const isFollowingInstance = targetActor.id === serverActor.id
42
43 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
14893eb7
C
44 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
45
eae0365b 46 sendReject(activityId, byActor, targetActor)
1735c825 47
453e83ea 48 return { actorFollow: undefined as MActorFollowActors }
5b9c965d
C
49 }
50
e1a570ab
C
51 const [ actorFollow, created ] = await ActorFollowModel.findOrCreateCustom({
52 byActor,
53 targetActor,
54 activityId,
55 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
56 ? 'pending'
57 : 'accepted',
58 transaction: t
59 })
40ff5707 60
2db48acc
C
61 // Set the follow as accepted if the remote actor follows a channel or account
62 // Or if the instance automatically accepts followers
63 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
50d6de9c 64 actorFollow.state = 'accepted'
4cbdcf44
C
65
66 await actorFollow.save({ transaction: t })
67 }
68
69 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
70 if (!actorFollow.url) {
71 actorFollow.url = activityId
50d6de9c 72 await actorFollow.save({ transaction: t })
40ff5707
C
73 }
74
5224c394 75 actorFollow.ActorFollower = byActor
50d6de9c 76 actorFollow.ActorFollowing = targetActor
ce548a10 77
50d6de9c 78 // Target sends to actor he accepted the follow request
8424c402 79 if (actorFollow.state === 'accepted') {
eae0365b
C
80 sendAccept(actorFollow)
81
82 await autoFollowBackIfNeeded(actorFollow, t)
8424c402 83 }
f7cc67b4 84
8424c402 85 return { actorFollow, created, isFollowingInstance, targetActor }
7a7724e6 86 })
ce548a10 87
883993c8
C
88 // Rejected
89 if (!actorFollow) return
90
91 if (created) {
8424c402
C
92 const follower = await ActorModel.loadFull(byActor.id)
93 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
94
453e83ea 95 if (isFollowingInstance) {
8424c402 96 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
453e83ea 97 } else {
453e83ea
C
98 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
99 }
883993c8 100 }
f7cc67b4 101
5224c394 102 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
7a7724e6 103}