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'
16 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
19 const activityId = activity.id
20 const objectId = getAPId(activity.object)
22 return retryTransactionWrapper(processFollow, byActor, activityId, objectId)
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
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)
37 if (!targetActor) throw new Error('Unknown actor')
38 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
40 const serverActor = await getServerActor()
41 const isFollowingInstance = targetActor.id === serverActor.id
43 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
44 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
46 sendReject(activityId, byActor, targetActor)
48 return { actorFollow: undefined as MActorFollowActors }
51 const [ actorFollow, created ] = await ActorFollowModel.findOrCreateCustom({
55 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
61 // Set the follow as accepted if the remote actor follows a channel or account
62 // Or if the instance automatically accepts followers
63 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
64 actorFollow.state = 'accepted'
66 await actorFollow.save({ transaction: t })
69 // Before PeerTube V3 we did not save the follow ID. Try to fix these old follows
70 if (!actorFollow.url) {
71 actorFollow.url = activityId
72 await actorFollow.save({ transaction: t })
75 actorFollow.ActorFollower = byActor
76 actorFollow.ActorFollowing = targetActor
78 // Target sends to actor he accepted the follow request
79 if (actorFollow.state === 'accepted') {
80 sendAccept(actorFollow)
82 await autoFollowBackIfNeeded(actorFollow, t)
85 return { actorFollow, created, isFollowingInstance, targetActor }
89 if (!actorFollow) return
92 const follower = await ActorModel.loadFull(byActor.id)
93 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
95 if (isFollowingInstance) {
96 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
98 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
102 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)