]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Fix production configuration
[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'
7d9ba5c0 3import { getAPId } from '../../../helpers/activitypub'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
6dd9de95 6import { CONFIG } from '../../../initializers/config'
7d9ba5c0
C
7import { sequelizeTypescript } from '../../../initializers/database'
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
1cf0df02
C
51 // Don't use findOrCreate by sequelize that breaks our actor follow hooks
52 let created = false
53 let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, t)
54
55 if (!actorFollow) {
56 created = true
57
58 actorFollow = await ActorFollowModel.create({
5224c394 59 actorId: byActor.id,
50d6de9c 60 targetActorId: targetActor.id,
de94ac86
C
61 url: activityId,
62
63 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
64 ? 'pending'
65 : 'accepted'
1cf0df02
C
66 }, { transaction: t })
67 }
40ff5707 68
2db48acc
C
69 // Set the follow as accepted if the remote actor follows a channel or account
70 // Or if the instance automatically accepts followers
71 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
50d6de9c 72 actorFollow.state = 'accepted'
4cbdcf44
C
73
74 await actorFollow.save({ transaction: t })
75 }
76
77 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
78 if (!actorFollow.url) {
79 actorFollow.url = activityId
50d6de9c 80 await actorFollow.save({ transaction: t })
40ff5707
C
81 }
82
5224c394 83 actorFollow.ActorFollower = byActor
50d6de9c 84 actorFollow.ActorFollowing = targetActor
ce548a10 85
50d6de9c 86 // Target sends to actor he accepted the follow request
8424c402 87 if (actorFollow.state === 'accepted') {
eae0365b
C
88 sendAccept(actorFollow)
89
90 await autoFollowBackIfNeeded(actorFollow, t)
8424c402 91 }
f7cc67b4 92
8424c402 93 return { actorFollow, created, isFollowingInstance, targetActor }
7a7724e6 94 })
ce548a10 95
883993c8
C
96 // Rejected
97 if (!actorFollow) return
98
99 if (created) {
8424c402
C
100 const follower = await ActorModel.loadFull(byActor.id)
101 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
102
453e83ea 103 if (isFollowingInstance) {
8424c402 104 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
453e83ea 105 } else {
453e83ea
C
106 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
107 }
883993c8 108 }
f7cc67b4 109
5224c394 110 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
7a7724e6 111}