1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
4 import { sendFollow } from '../../activitypub/send'
5 import { sanitizeHost } from '../../../helpers/core-utils'
6 import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
7 import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
8 import { retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
10 import { ActorModel } from '../../../models/activitypub/actor'
11 import { Notifier } from '../../notifier'
12 import { sequelizeTypescript } from '../../../initializers/database'
13 import { MAccount, MActor, MActorFollowActors, MActorFollowFull, MActorFull } from '../../../typings/models'
15 export type ActivitypubFollowPayload = {
16 followerActorId: number
21 async function processActivityPubFollow (job: Bull.Job) {
22 const payload = job.data as ActivitypubFollowPayload
23 const host = payload.host
25 logger.info('Processing ActivityPub follow in job %d.', job.id)
27 let targetActor: MActorFull
28 if (!host || host === WEBSERVER.HOST) {
29 targetActor = await ActorModel.loadLocalByName(payload.name)
31 const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
32 const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
33 targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
36 const fromActor = await ActorModel.load(payload.followerActorId)
38 return retryTransactionWrapper(follow, fromActor, targetActor)
40 // ---------------------------------------------------------------------------
43 processActivityPubFollow
46 // ---------------------------------------------------------------------------
48 async function follow (fromActor: MActor, targetActor: MActorFull) {
49 if (fromActor.id === targetActor.id) {
50 throw new Error('Follower is the same than target actor.')
53 // Same server, direct accept
54 const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
56 const actorFollow = await sequelizeTypescript.transaction(async t => {
57 const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
59 actorId: fromActor.id,
60 targetActorId: targetActor.id
64 actorId: fromActor.id,
65 targetActorId: targetActor.id
69 actorFollow.ActorFollowing = targetActor
70 actorFollow.ActorFollower = fromActor
72 // Send a notification to remote server if our follow is not already accepted
73 if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
78 if (actorFollow.state === 'accepted') {
79 const followerFull = Object.assign(fromActor, { Account: await actorFollow.ActorFollower.$get('Account') as MAccount })
81 const actorFollowFull = Object.assign(actorFollow, {
82 ActorFollowing: targetActor,
83 ActorFollower: followerFull
86 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)