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 } from '../send'
9 async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
10 const activityObject = activity.object
12 return retryTransactionWrapper(processFollow, byActor, activityObject)
15 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
23 async function processFollow (actor: ActorModel, targetActorURL: string) {
24 await sequelizeTypescript.transaction(async t => {
25 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
27 if (!targetActor) throw new Error('Unknown actor')
28 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
30 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
33 targetActorId: targetActor.id
37 targetActorId: targetActor.id,
43 actorFollow.ActorFollower = actor
44 actorFollow.ActorFollowing = targetActor
46 if (actorFollow.state !== 'accepted') {
47 actorFollow.state = 'accepted'
48 await actorFollow.save({ transaction: t })
51 actorFollow.ActorFollower = actor
52 actorFollow.ActorFollowing = targetActor
54 // Target sends to actor he accepted the follow request
55 return sendAccept(actorFollow)
58 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)