]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-follow.ts
Avoir some circular dependencies
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
... / ...
CommitLineData
1import { ActivityFollow } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
4import { sequelizeTypescript } from '../../../initializers'
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7import { sendAccept, sendReject } from '../send'
8import { Notifier } from '../../notifier'
9import { getAPId } from '../../../helpers/activitypub'
10import { CONFIG } from '../../../initializers/config'
11import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
12import { MActorFollowActors, MActorSignature } from '../../../typings/models'
13import { autoFollowBackIfNeeded } from '../follow'
14import { getServerActor } from '@server/models/application/application'
15
16async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
18 const activityObject = getAPId(activity.object)
19
20 return retryTransactionWrapper(processFollow, byActor, activityObject)
21}
22
23// ---------------------------------------------------------------------------
24
25export {
26 processFollowActivity
27}
28
29// ---------------------------------------------------------------------------
30
31async function processFollow (byActor: MActorSignature, targetActorURL: string) {
32 const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => {
33 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
34
35 if (!targetActor) throw new Error('Unknown actor')
36 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
37
38 const serverActor = await getServerActor()
39 const isFollowingInstance = targetActor.id === serverActor.id
40
41 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
42 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
43
44 await sendReject(byActor, targetActor)
45
46 return { actorFollow: undefined as MActorFollowActors }
47 }
48
49 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
50 where: {
51 actorId: byActor.id,
52 targetActorId: targetActor.id
53 },
54 defaults: {
55 actorId: byActor.id,
56 targetActorId: targetActor.id,
57 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
58 },
59 transaction: t
60 })
61
62 // Set the follow as accepted if the remote actor follows a channel or account
63 // Or if the instance automatically accepts followers
64 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
65 actorFollow.state = 'accepted'
66 await actorFollow.save({ transaction: t })
67 }
68
69 actorFollow.ActorFollower = byActor
70 actorFollow.ActorFollowing = targetActor
71
72 // Target sends to actor he accepted the follow request
73 if (actorFollow.state === 'accepted') {
74 await sendAccept(actorFollow)
75 await autoFollowBackIfNeeded(actorFollow)
76 }
77
78 return { actorFollow, created, isFollowingInstance, targetActor }
79 })
80
81 // Rejected
82 if (!actorFollow) return
83
84 if (created) {
85 const follower = await ActorModel.loadFull(byActor.id)
86 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
87
88 if (isFollowingInstance) {
89 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
90 } else {
91 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
92 }
93 }
94
95 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
96}