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 { MActor, MActorFollowActors, MActorFull } from '../../../typings/models'
15 export type ActivitypubFollowPayload = {
16 followerActorId: number
19 isAutoFollow?: boolean
22 async function processActivityPubFollow (job: Bull.Job) {
23 const payload = job.data as ActivitypubFollowPayload
24 const host = payload.host
26 logger.info('Processing ActivityPub follow in job %d.', job.id)
28 let targetActor: MActorFull
29 if (!host || host === WEBSERVER.HOST) {
30 targetActor = await ActorModel.loadLocalByName(payload.name)
32 const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
33 const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
34 targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
37 const fromActor = await ActorModel.load(payload.followerActorId)
39 return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
41 // ---------------------------------------------------------------------------
44 processActivityPubFollow
47 // ---------------------------------------------------------------------------
49 async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
50 if (fromActor.id === targetActor.id) {
51 throw new Error('Follower is the same than target actor.')
54 // Same server, direct accept
55 const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
57 const actorFollow = await sequelizeTypescript.transaction(async t => {
58 const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
60 actorId: fromActor.id,
61 targetActorId: targetActor.id
65 actorId: fromActor.id,
66 targetActorId: targetActor.id
70 actorFollow.ActorFollowing = targetActor
71 actorFollow.ActorFollower = fromActor
73 // Send a notification to remote server if our follow is not already accepted
74 if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
79 const followerFull = await ActorModel.loadFull(fromActor.id)
81 const actorFollowFull = Object.assign(actorFollow, {
82 ActorFollowing: targetActor,
83 ActorFollower: followerFull
86 if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
87 if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)