]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Add rejected state to follows
[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 // Already rejected
62 if (actorFollow.state === 'rejected') {
63 return { actorFollow: undefined as MActorFollowActors }
64 }
65
66 // Set the follow as accepted if the remote actor follows a channel or account
67 // Or if the instance automatically accepts followers
68 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
69 actorFollow.state = 'accepted'
70
71 await actorFollow.save({ transaction: t })
72 }
73
74 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
75 if (!actorFollow.url) {
76 actorFollow.url = activityId
77 await actorFollow.save({ transaction: t })
78 }
79
80 actorFollow.ActorFollower = byActor
81 actorFollow.ActorFollowing = targetActor
82
83 // Target sends to actor he accepted the follow request
84 if (actorFollow.state === 'accepted') {
85 sendAccept(actorFollow)
86
87 await autoFollowBackIfNeeded(actorFollow, t)
88 }
89
90 return { actorFollow, created, isFollowingInstance, targetActor }
91 })
92
93 // Rejected
94 if (!actorFollow) return
95
96 if (created) {
97 const follower = await ActorModel.loadFull(byActor.id)
98 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
99
100 if (isFollowingInstance) {
101 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
102 } else {
103 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
104 }
105 }
106
107 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
108 }