2018-07-10 17:02:20 +02:00
|
|
|
import * as Bull from 'bull'
|
2018-04-18 15:32:40 +02:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getServerActor } from '../../../helpers/utils'
|
|
|
|
import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers'
|
|
|
|
import { sendFollow } from '../../activitypub/send'
|
|
|
|
import { sanitizeHost } from '../../../helpers/core-utils'
|
|
|
|
import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
|
|
|
|
import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
|
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
|
|
|
|
export type ActivitypubFollowPayload = {
|
|
|
|
host: string
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
async function processActivityPubFollow (job: Bull.Job) {
|
2018-04-18 15:32:40 +02:00
|
|
|
const payload = job.data as ActivitypubFollowPayload
|
|
|
|
const host = payload.host
|
|
|
|
|
|
|
|
logger.info('Processing ActivityPub follow in job %d.', job.id)
|
|
|
|
|
|
|
|
const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
|
|
|
|
|
|
|
|
const actorUrl = await loadActorUrlOrGetFromWebfinger(SERVER_ACTOR_NAME, sanitizedHost)
|
|
|
|
const targetActor = await getOrCreateActorAndServerAndModel(actorUrl)
|
|
|
|
|
|
|
|
const fromActor = await getServerActor()
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
return retryTransactionWrapper(follow, fromActor, targetActor)
|
2018-04-18 15:32:40 +02:00
|
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processActivityPubFollow
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function follow (fromActor: ActorModel, targetActor: ActorModel) {
|
|
|
|
if (fromActor.id === targetActor.id) {
|
|
|
|
throw new Error('Follower is the same than target actor.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const [ actorFollow ] = await ActorFollowModel.findOrCreate({
|
|
|
|
where: {
|
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
state: 'pending',
|
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
actorFollow.ActorFollowing = targetActor
|
|
|
|
actorFollow.ActorFollower = fromActor
|
|
|
|
|
|
|
|
// Send a notification to remote server
|
|
|
|
await sendFollow(actorFollow)
|
|
|
|
})
|
|
|
|
}
|