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'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
|
2018-04-18 15:32:40 +02: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 08:56:20 +01:00
|
|
|
import { Notifier } from '../../notifier'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2019-08-30 16:50:12 +02:00
|
|
|
import { MActor, MActorFollowActors, MActorFull } from '../../../typings/models'
|
2018-04-18 15:32:40 +02:00
|
|
|
|
|
|
|
export type ActivitypubFollowPayload = {
|
2018-08-16 15:25:20 +02:00
|
|
|
followerActorId: number
|
|
|
|
name: string
|
2018-04-18 15:32:40 +02:00
|
|
|
host: string
|
2019-08-30 16:50:12 +02:00
|
|
|
isAutoFollow?: boolean
|
2020-04-16 16:36:43 +02:00
|
|
|
assertIsChannel?: boolean
|
2018-04-18 15:32:40 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
let targetActor: MActorFull
|
2019-04-11 11:33:44 +02:00
|
|
|
if (!host || host === WEBSERVER.HOST) {
|
2018-08-21 16:18:59 +02:00
|
|
|
targetActor = await ActorModel.loadLocalByName(payload.name)
|
|
|
|
} else {
|
|
|
|
const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
|
|
|
|
const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
|
2019-08-15 11:53:26 +02:00
|
|
|
targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
|
2018-08-21 16:18:59 +02:00
|
|
|
}
|
2018-04-18 15:32:40 +02:00
|
|
|
|
2020-04-16 16:36:43 +02:00
|
|
|
if (payload.assertIsChannel && !targetActor.VideoChannel) {
|
2020-04-20 09:04:38 +02:00
|
|
|
logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
|
2020-04-16 16:36:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:25:20 +02:00
|
|
|
const fromActor = await ActorModel.load(payload.followerActorId)
|
2018-04-18 15:32:40 +02:00
|
|
|
|
2019-08-30 16:50:12 +02:00
|
|
|
return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
|
2018-04-18 15:32:40 +02:00
|
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processActivityPubFollow
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-30 16:50:12 +02:00
|
|
|
async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
|
2018-04-18 15:32:40 +02:00
|
|
|
if (fromActor.id === targetActor.id) {
|
2019-09-23 08:17:42 +02:00
|
|
|
throw new Error('Follower is the same as target actor.')
|
2018-04-18 15:32:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 15:25:20 +02:00
|
|
|
// Same server, direct accept
|
|
|
|
const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
|
|
|
|
|
2019-01-04 08:56:20 +01:00
|
|
|
const actorFollow = await sequelizeTypescript.transaction(async t => {
|
2019-08-20 13:52:49 +02:00
|
|
|
const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
|
2018-04-18 15:32:40 +02:00
|
|
|
where: {
|
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
defaults: {
|
2018-08-16 15:25:20 +02:00
|
|
|
state,
|
2018-04-18 15:32:40 +02:00
|
|
|
actorId: fromActor.id,
|
|
|
|
targetActorId: targetActor.id
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
actorFollow.ActorFollowing = targetActor
|
|
|
|
actorFollow.ActorFollower = fromActor
|
|
|
|
|
2018-07-31 11:15:35 +02:00
|
|
|
// Send a notification to remote server if our follow is not already accepted
|
2019-07-29 11:59:29 +02:00
|
|
|
if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
|
2019-01-04 08:56:20 +01:00
|
|
|
|
|
|
|
return actorFollow
|
2018-04-18 15:32:40 +02:00
|
|
|
})
|
2019-01-04 08:56:20 +01:00
|
|
|
|
2019-08-30 16:50:12 +02:00
|
|
|
const followerFull = await ActorModel.loadFull(fromActor.id)
|
2019-08-20 13:52:49 +02:00
|
|
|
|
2019-08-30 16:50:12 +02:00
|
|
|
const actorFollowFull = Object.assign(actorFollow, {
|
|
|
|
ActorFollowing: targetActor,
|
|
|
|
ActorFollower: followerFull
|
|
|
|
})
|
2019-08-20 13:52:49 +02:00
|
|
|
|
2019-08-30 16:50:12 +02:00
|
|
|
if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
|
|
|
|
if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
|
|
|
|
|
|
|
|
return actorFollow
|
2018-04-18 15:32:40 +02:00
|
|
|
}
|