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 { MAccount, MActorFollowActors, MActorFollowFull, MActorSignature } from '../../../typings/models'
15 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
16 const { activity, byActor } = options
17 const activityObject = getAPId(activity.object)
19 return retryTransactionWrapper(processFollow, byActor, activityObject)
22 // ---------------------------------------------------------------------------
28 // ---------------------------------------------------------------------------
30 async function processFollow (byActor: MActorSignature, targetActorURL: string) {
31 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
32 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
34 if (!targetActor) throw new Error('Unknown actor')
35 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
37 const serverActor = await getServerActor()
38 const isFollowingInstance = targetActor.id === serverActor.id
40 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
41 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
43 await sendReject(byActor, targetActor)
45 return { actorFollow: undefined as MActorFollowActors }
48 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
51 targetActorId: targetActor.id
55 targetActorId: targetActor.id,
56 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
61 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
62 actorFollow.state = 'accepted'
63 await actorFollow.save({ transaction: t })
66 actorFollow.ActorFollower = byActor
67 actorFollow.ActorFollowing = targetActor
69 // Target sends to actor he accepted the follow request
70 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
72 return { actorFollow, created, isFollowingInstance }
76 if (!actorFollow) return
79 if (isFollowingInstance) {
80 Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
82 const actorFollowFull = actorFollow as MActorFollowFull
83 actorFollowFull.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as MAccount
85 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
89 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)