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 { SignatureActorModel } from '../../../typings/models'
14 import { ActorFollowModelLight } from '../../../typings/models/actor-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: SignatureActorModel, targetActorURL: string) {
32 const { actorFollow, created, isFollowingInstance } = 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 }
49 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
52 targetActorId: targetActor.id
56 targetActorId: targetActor.id,
57 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
60 }) as [ ActorFollowModelLight, boolean ]
62 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
63 actorFollow.state = 'accepted'
64 await actorFollow.save({ transaction: t })
67 actorFollow.ActorFollower = byActor
68 actorFollow.ActorFollowing = targetActor
70 // Target sends to actor he accepted the follow request
71 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
73 return { actorFollow, created, isFollowingInstance }
77 if (!actorFollow) return
80 if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
81 else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
84 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)