1 import { ActivityFollow } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7 import { sendAccept, sendReject } from '../send'
8 import { Notifier } from '../../notifier'
9 import { getAPId } from '../../../helpers/activitypub'
10 import { getServerActor } from '../../../helpers/utils'
11 import { CONFIG } from '../../../initializers/config'
12 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 import { MActorFollowActors, MActorSignature } from '../../../typings/models'
14 import { autoFollowBackIfNeeded } from '../follow'
16 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
18 const activityObject = getAPId(activity.object)
20 return retryTransactionWrapper(processFollow, byActor, activityObject)
23 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
31 async 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)
35 if (!targetActor) throw new Error('Unknown actor')
36 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
38 const serverActor = await getServerActor()
39 const isFollowingInstance = targetActor.id === serverActor.id
41 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
42 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
44 await sendReject(byActor, targetActor)
46 return { actorFollow: undefined as MActorFollowActors }
49 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
52 targetActorId: targetActor.id
56 targetActorId: targetActor.id,
57 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
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 })
69 actorFollow.ActorFollower = byActor
70 actorFollow.ActorFollowing = targetActor
72 // Target sends to actor he accepted the follow request
73 if (actorFollow.state === 'accepted') {
74 await sendAccept(actorFollow)
75 await autoFollowBackIfNeeded(actorFollow)
78 return { actorFollow, created, isFollowingInstance, targetActor }
82 if (!actorFollow) return
85 const follower = await ActorModel.loadFull(byActor.id)
86 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
88 if (isFollowingInstance) {
89 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
91 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
95 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)