]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { getServerActor } from '@server/models/application/application'
2 import { ActivityFollow } from '../../../../shared/models/activitypub'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { CONFIG } from '../../../initializers/config'
6 import { sequelizeTypescript } from '../../../initializers/database'
7 import { getAPId } from '../../../lib/activitypub/activity'
8 import { ActorModel } from '../../../models/actor/actor'
9 import { ActorFollowModel } from '../../../models/actor/actor-follow'
10 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
11 import { MActorFollowActors, MActorSignature } from '../../../types/models'
12 import { Notifier } from '../../notifier'
13 import { autoFollowBackIfNeeded } from '../follow'
14 import { sendAccept, sendReject } from '../send'
15
16 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
18
19 const activityId = activity.id
20 const objectId = getAPId(activity.object)
21
22 return retryTransactionWrapper(processFollow, byActor, activityId, objectId)
23 }
24
25 // ---------------------------------------------------------------------------
26
27 export {
28 processFollowActivity
29 }
30
31 // ---------------------------------------------------------------------------
32
33 async function processFollow (byActor: MActorSignature, activityId: string, targetActorURL: string) {
34 const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => {
35 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
36
37 if (!targetActor) throw new Error('Unknown actor')
38 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
39
40 const serverActor = await getServerActor()
41 const isFollowingInstance = targetActor.id === serverActor.id
42
43 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
44 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
45
46 sendReject(activityId, byActor, targetActor)
47
48 return { actorFollow: undefined as MActorFollowActors }
49 }
50
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 })
60
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)) {
64 actorFollow.state = 'accepted'
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
72 await actorFollow.save({ transaction: t })
73 }
74
75 actorFollow.ActorFollower = byActor
76 actorFollow.ActorFollowing = targetActor
77
78 // Target sends to actor he accepted the follow request
79 if (actorFollow.state === 'accepted') {
80 sendAccept(actorFollow)
81
82 await autoFollowBackIfNeeded(actorFollow, t)
83 }
84
85 return { actorFollow, created, isFollowingInstance, targetActor }
86 })
87
88 // Rejected
89 if (!actorFollow) return
90
91 if (created) {
92 const follower = await ActorModel.loadFull(byActor.id)
93 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
94
95 if (isFollowingInstance) {
96 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
97 } else {
98 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
99 }
100 }
101
102 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
103 }