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 return sendReject(actor, targetActor)
43 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
46 targetActorId: targetActor.id
50 targetActorId: targetActor.id,
51 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
56 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
57 actorFollow.state = 'accepted'
58 await actorFollow.save({ transaction: t })
61 actorFollow.ActorFollower = actor
62 actorFollow.ActorFollowing = targetActor
64 // Target sends to actor he accepted the follow request
65 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
67 return { actorFollow, created, isFollowingInstance }
71 if (!actorFollow) return
74 if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
75 else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
78 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)