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'
13 async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
14 const activityObject = getAPId(activity.object)
16 return retryTransactionWrapper(processFollow, byActor, activityObject)
19 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 async function processFollow (actor: ActorModel, targetActorURL: string) {
28 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
29 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
31 if (!targetActor) throw new Error('Unknown actor')
32 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
34 const serverActor = await getServerActor()
35 const isFollowingInstance = targetActor.id === serverActor.id
37 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
38 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
40 await sendReject(actor, targetActor)
42 return { actorFollow: undefined }
45 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
48 targetActorId: targetActor.id
52 targetActorId: targetActor.id,
53 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
58 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
59 actorFollow.state = 'accepted'
60 await actorFollow.save({ transaction: t })
63 actorFollow.ActorFollower = actor
64 actorFollow.ActorFollowing = targetActor
66 // Target sends to actor he accepted the follow request
67 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
69 return { actorFollow, created, isFollowingInstance }
73 if (!actorFollow) return
76 if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
77 else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
80 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)