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/database'
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 { CONFIG } from '../../../initializers/config'
11 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
12 import { MActorFollowActors, MActorSignature } from '../../../types/models'
13 import { autoFollowBackIfNeeded } from '../follow'
14 import { getServerActor } from '@server/models/application/application'
16 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
17 const { activity, byActor } = options
19 const activityId = activity.id
20 const objectId = getAPId(activity.object)
22 return retryTransactionWrapper(processFollow, byActor, activityId, objectId)
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 async function processFollow (byActor: MActorSignature, activityId: string, targetActorURL: string) {
34 const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => {
35 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
37 if (!targetActor) throw new Error('Unknown actor')
38 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
40 const serverActor = await getServerActor()
41 const isFollowingInstance = targetActor.id === serverActor.id
43 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
44 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
46 await sendReject(activityId, byActor, targetActor)
48 return { actorFollow: undefined as MActorFollowActors }
51 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
54 targetActorId: targetActor.id
58 targetActorId: targetActor.id,
61 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
68 // Set the follow as accepted if the remote actor follows a channel or account
69 // Or if the instance automatically accepts followers
70 if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) {
71 actorFollow.state = 'accepted'
72 await actorFollow.save({ transaction: t })
75 actorFollow.ActorFollower = byActor
76 actorFollow.ActorFollowing = targetActor
78 // Target sends to actor he accepted the follow request
79 if (actorFollow.state === 'accepted') {
80 await sendAccept(actorFollow)
81 await autoFollowBackIfNeeded(actorFollow)
84 return { actorFollow, created, isFollowingInstance, targetActor }
88 if (!actorFollow) return
91 const follower = await ActorModel.loadFull(byActor.id)
92 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
94 if (isFollowingInstance) {
95 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
97 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
101 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)