2018-07-10 11:02:20 -04:00
|
|
|
import * as Bull from 'bull'
|
2018-04-18 09:32:40 -04:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
|
2018-04-18 09:32:40 -04:00
|
|
|
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'
|
2019-01-04 02:56:20 -05:00
|
|
|
import { Notifier } from '../../notifier'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2018-04-18 09:32:40 -04:00
|
|
|
|
|
|
|
export type ActivitypubFollowPayload = {
|
2018-08-16 09:25:20 -04:00
|
|
|
followerActorId: number
|
|
|
|
name: string
|
2018-04-18 09:32:40 -04:00
|
|
|
host: string
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
async function processActivityPubFollow (job: Bull.Job) {
|
2018-04-18 09:32:40 -04:00
|
|
|
const payload = job.data as ActivitypubFollowPayload
|
|
|
|
const host = payload.host
|
|
|
|
|
|
|
|
logger.info('Processing ActivityPub follow in job %d.', job.id)
|
|
|
|
|
2018-08-21 10:18:59 -04:00
|
|
|
let targetActor: ActorModel
|
2019-04-11 05:33:44 -04:00
|
|
|
if (!host || host === WEBSERVER.HOST) {
|
2018-08-21 10:18:59 -04:00
|
|
|
targetActor = await ActorModel.loadLocalByName(payload.name)
|
|
|
|
} else {
|
|
|
|
const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
|
|
|
|
const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
|
|
|
|
targetActor = await getOrCreateActorAndServerAndModel(actorUrl)
|
|
|
|
}
|
2018-04-18 09:32:40 -04:00
|
|
|
|
2018-08-16 09:25:20 -04:00
|
|
|
const fromActor = await ActorModel.load(payload.followerActorId)
|
2018-04-18 09:32:40 -04:00
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
return retryTransactionWrapper(follow, fromActor, targetActor)
|
2018-04-18 09:32:40 -04:00
|
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processActivityPubFollow
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-01-04 02:56:20 -05:00
|
|
|
async function follow (fromActor: ActorModel, targetActor: ActorModel) {
|
2018-04-18 09:32:40 -04:00
|
|
|
if (fromActor.id === targetActor.id) {
|
|
|
|
throw new Error('Follower is the same than target actor.')
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:25:20 -04:00
|
|
|
// Same server, direct accept
|
|
|
|
const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
|
|
|
|
|
2019-01-04 02:56:20 -05:00
|
|
|
const actorFollow = await sequelizeTypescript.transaction(async t => {
|
2018-04-18 09:32:40 -04:00
|
|
|
const [ actorFollow ] = await ActorFollowModel.findOrCreate({
|
|
|
|
where: {
|
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
defaults: {
|
2018-08-16 09:25:20 -04:00
|
|
|
state,
|
2018-04-18 09:32:40 -04:00
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
actorFollow.ActorFollowing = targetActor
|
|
|
|
actorFollow.ActorFollower = fromActor
|
|
|
|
|
2018-07-31 05:15:35 -04:00
|
|
|
// Send a notification to remote server if our follow is not already accepted
|
|
|
|
if (actorFollow.state !== 'accepted') await sendFollow(actorFollow)
|
2019-01-04 02:56:20 -05:00
|
|
|
|
|
|
|
return actorFollow
|
2018-04-18 09:32:40 -04:00
|
|
|
})
|
2019-01-04 02:56:20 -05:00
|
|
|
|
2019-04-08 11:26:01 -04:00
|
|
|
if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow)
|
2018-04-18 09:32:40 -04:00
|
|
|
}
|