]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Merge branch 'release/5.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
CommitLineData
073deef8
C
1import { Transaction } from 'sequelize/types'
2import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
3import { AccountModel } from '@server/models/account/account'
7d9ba5c0 4import { getServerActor } from '@server/models/application/application'
3fd3ab2d 5import { ActivityFollow } from '../../../../shared/models/activitypub'
da854ddd
C
6import { retryTransactionWrapper } from '../../../helpers/database-utils'
7import { logger } from '../../../helpers/logger'
6dd9de95 8import { CONFIG } from '../../../initializers/config'
7d9ba5c0 9import { sequelizeTypescript } from '../../../initializers/database'
7e98a7df 10import { getAPId } from '../../../lib/activitypub/activity'
7d9ba5c0
C
11import { ActorModel } from '../../../models/actor/actor'
12import { ActorFollowModel } from '../../../models/actor/actor-follow'
26d6bf65 13import { APProcessorOptions } from '../../../types/activitypub-processor.model'
073deef8 14import { MActorFollow, MActorFull, MActorId, MActorSignature } from '../../../types/models'
7d9ba5c0 15import { Notifier } from '../../notifier'
8424c402 16import { autoFollowBackIfNeeded } from '../follow'
7d9ba5c0 17import { sendAccept, sendReject } from '../send'
7a7724e6 18
1198edf4
C
19async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
20 const { activity, byActor } = options
7a7724e6 21
de94ac86
C
22 const activityId = activity.id
23 const objectId = getAPId(activity.object)
24
25 return retryTransactionWrapper(processFollow, byActor, activityId, objectId)
7a7724e6
C
26}
27
28// ---------------------------------------------------------------------------
29
30export {
31 processFollowActivity
32}
33
34// ---------------------------------------------------------------------------
35
de94ac86 36async function processFollow (byActor: MActorSignature, activityId: string, targetActorURL: string) {
073deef8 37 const { actorFollow, created, targetActor } = await sequelizeTypescript.transaction(async t => {
e587e0ec 38 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
ce548a10 39
50d6de9c
C
40 if (!targetActor) throw new Error('Unknown actor')
41 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
7a7724e6 42
50cc1ee4 43 if (await rejectIfInstanceFollowDisabled(byActor, activityId, targetActor)) return { actorFollow: undefined }
073deef8 44 if (await rejectIfMuted(byActor, activityId, targetActor)) return { actorFollow: undefined }
5b9c965d 45
e1a570ab
C
46 const [ actorFollow, created ] = await ActorFollowModel.findOrCreateCustom({
47 byActor,
48 targetActor,
49 activityId,
a6b26afc 50 state: await isFollowingInstance(targetActor) && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
e1a570ab
C
51 ? 'pending'
52 : 'accepted',
53 transaction: t
54 })
40ff5707 55
073deef8 56 if (rejectIfAlreadyRejected(actorFollow, byActor, activityId, targetActor)) return { actorFollow: undefined }
4cbdcf44 57
073deef8 58 await acceptIfNeeded(actorFollow, targetActor, t)
4cbdcf44 59
073deef8 60 await fixFollowURLIfNeeded(actorFollow, activityId, t)
40ff5707 61
5224c394 62 actorFollow.ActorFollower = byActor
50d6de9c 63 actorFollow.ActorFollowing = targetActor
ce548a10 64
50d6de9c 65 // Target sends to actor he accepted the follow request
8424c402 66 if (actorFollow.state === 'accepted') {
eae0365b
C
67 sendAccept(actorFollow)
68
69 await autoFollowBackIfNeeded(actorFollow, t)
8424c402 70 }
f7cc67b4 71
073deef8 72 return { actorFollow, created, targetActor }
7a7724e6 73 })
ce548a10 74
883993c8
C
75 // Rejected
76 if (!actorFollow) return
77
78 if (created) {
8424c402
C
79 const follower = await ActorModel.loadFull(byActor.id)
80 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
81
50cc1ee4 82 if (await isFollowingInstance(targetActor)) {
8424c402 83 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
453e83ea 84 } else {
453e83ea
C
85 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
86 }
883993c8 87 }
f7cc67b4 88
5224c394 89 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
7a7724e6 90}
073deef8 91
50cc1ee4
C
92async function rejectIfInstanceFollowDisabled (byActor: MActorSignature, activityId: string, targetActor: MActorFull) {
93 if (await isFollowingInstance(targetActor) && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
073deef8
C
94 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
95
96 sendReject(activityId, byActor, targetActor)
97
98 return true
99 }
100
101 return false
102}
103
104async function rejectIfMuted (byActor: MActorSignature, activityId: string, targetActor: MActorFull) {
105 const followerAccount = await AccountModel.load(byActor.Account.id)
106 const followingAccountId = targetActor.Account
107
108 if (followerAccount && await isBlockedByServerOrAccount(followerAccount, followingAccountId)) {
109 logger.info('Rejecting %s because follower is muted.', byActor.url)
110
111 sendReject(activityId, byActor, targetActor)
112
113 return true
114 }
115
116 return false
117}
118
119function rejectIfAlreadyRejected (actorFollow: MActorFollow, byActor: MActorSignature, activityId: string, targetActor: MActorFull) {
120 // Already rejected
121 if (actorFollow.state === 'rejected') {
122 logger.info('Rejecting %s because follow is already rejected.', byActor.url)
123
124 sendReject(activityId, byActor, targetActor)
125
126 return true
127 }
128
129 return false
130}
131
132async function acceptIfNeeded (actorFollow: MActorFollow, targetActor: MActorFull, transaction: Transaction) {
133 // Set the follow as accepted if the remote actor follows a channel or account
134 // Or if the instance automatically accepts followers
135 if (actorFollow.state === 'accepted') return
50cc1ee4 136 if (!await isFollowingInstance(targetActor)) return
a6b26afc 137 if (CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === true && await isFollowingInstance(targetActor)) return
073deef8
C
138
139 actorFollow.state = 'accepted'
140
141 await actorFollow.save({ transaction })
142}
143
144async function fixFollowURLIfNeeded (actorFollow: MActorFollow, activityId: string, transaction: Transaction) {
145 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
146 if (!actorFollow.url) {
147 actorFollow.url = activityId
148 await actorFollow.save({ transaction })
149 }
150}
151
152async function isFollowingInstance (targetActor: MActorId) {
153 const serverActor = await getServerActor()
154
155 return targetActor.id === serverActor.id
156}