]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Fix actor follow counts calculation
[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 { getAPId } from '../../../helpers/activitypub'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { CONFIG } from '../../../initializers/config'
7 import { sequelizeTypescript } from '../../../initializers/database'
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 // 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({
59 actorId: byActor.id,
60 targetActorId: targetActor.id,
61 url: activityId,
62
63 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
64 ? 'pending'
65 : 'accepted'
66 }, { transaction: t })
67 }
68
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)) {
72 actorFollow.state = 'accepted'
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
80 await actorFollow.save({ transaction: t })
81 }
82
83 actorFollow.ActorFollower = byActor
84 actorFollow.ActorFollowing = targetActor
85
86 // Target sends to actor he accepted the follow request
87 if (actorFollow.state === 'accepted') {
88 sendAccept(actorFollow)
89
90 await autoFollowBackIfNeeded(actorFollow, t)
91 }
92
93 return { actorFollow, created, isFollowingInstance, targetActor }
94 })
95
96 // Rejected
97 if (!actorFollow) return
98
99 if (created) {
100 const follower = await ActorModel.loadFull(byActor.id)
101 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
102
103 if (isFollowingInstance) {
104 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
105 } else {
106 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
107 }
108 }
109
110 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
111 }