]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Add banner tests
[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'
80fdaf06 4import { sequelizeTypescript } from '../../../initializers/database'
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'
6dd9de95 10import { CONFIG } from '../../../initializers/config'
26d6bf65
C
11import { APProcessorOptions } from '../../../types/activitypub-processor.model'
12import { MActorFollowActors, MActorSignature } from '../../../types/models'
8424c402 13import { autoFollowBackIfNeeded } from '../follow'
8dc8a34e 14import { getServerActor } from '@server/models/application/application'
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
de94ac86 46 await sendReject(activityId, byActor, targetActor)
1735c825 47
453e83ea 48 return { actorFollow: undefined as MActorFollowActors }
5b9c965d
C
49 }
50
453e83ea 51 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
350e31d6 52 where: {
5224c394 53 actorId: byActor.id,
50d6de9c 54 targetActorId: targetActor.id
350e31d6
C
55 },
56 defaults: {
5224c394 57 actorId: byActor.id,
50d6de9c 58 targetActorId: targetActor.id,
de94ac86
C
59 url: activityId,
60
61 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
62 ? 'pending'
63 : 'accepted'
350e31d6 64 },
ce548a10 65 transaction: t
453e83ea 66 })
40ff5707 67
2db48acc
C
68 // Set the follow as accepted if the remote actor follows a channel or account
69 // Or if the instance automatically accepts followers
70 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
50d6de9c 71 actorFollow.state = 'accepted'
4cbdcf44
C
72
73 await actorFollow.save({ transaction: t })
74 }
75
76 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
77 if (!actorFollow.url) {
78 actorFollow.url = activityId
50d6de9c 79 await actorFollow.save({ transaction: t })
40ff5707
C
80 }
81
5224c394 82 actorFollow.ActorFollower = byActor
50d6de9c 83 actorFollow.ActorFollowing = targetActor
ce548a10 84
50d6de9c 85 // Target sends to actor he accepted the follow request
8424c402
C
86 if (actorFollow.state === 'accepted') {
87 await sendAccept(actorFollow)
88 await autoFollowBackIfNeeded(actorFollow)
89 }
f7cc67b4 90
8424c402 91 return { actorFollow, created, isFollowingInstance, targetActor }
7a7724e6 92 })
ce548a10 93
883993c8
C
94 // Rejected
95 if (!actorFollow) return
96
97 if (created) {
8424c402
C
98 const follower = await ActorModel.loadFull(byActor.id)
99 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
100
453e83ea 101 if (isFollowingInstance) {
8424c402 102 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
453e83ea 103 } else {
453e83ea
C
104 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
105 }
883993c8 106 }
f7cc67b4 107
5224c394 108 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
7a7724e6 109}