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
20 assertIsChannel?: boolean
23 async function processActivityPubFollow (job: Bull.Job) {
24 const payload = job.data as ActivitypubFollowPayload
25 const host = payload.host
27 logger.info('Processing ActivityPub follow in job %d.', job.id)
29 let targetActor: MActorFull
30 if (!host || host === WEBSERVER.HOST) {
31 targetActor = await ActorModel.loadLocalByName(payload.name)
33 const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
34 const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
35 targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
38 if (payload.assertIsChannel && !targetActor.VideoChannel) {
39 logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
43 const fromActor = await ActorModel.load(payload.followerActorId)
45 return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
47 // ---------------------------------------------------------------------------
50 processActivityPubFollow
53 // ---------------------------------------------------------------------------
55 async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
56 if (fromActor.id === targetActor.id) {
57 throw new Error('Follower is the same as target actor.')
60 // Same server, direct accept
61 const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
63 const actorFollow = await sequelizeTypescript.transaction(async t => {
64 const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
66 actorId: fromActor.id,
67 targetActorId: targetActor.id
71 actorId: fromActor.id,
72 targetActorId: targetActor.id
76 actorFollow.ActorFollowing = targetActor
77 actorFollow.ActorFollower = fromActor
79 // Send a notification to remote server if our follow is not already accepted
80 if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
85 const followerFull = await ActorModel.loadFull(fromActor.id)
87 const actorFollowFull = Object.assign(actorFollow, {
88 ActorFollowing: targetActor,
89 ActorFollower: followerFull
92 if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
93 if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)