PeerTube/server/lib/job-queue/handlers/activitypub-follow.ts

83 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-08-08 10:42:08 +02:00
import { Job } from 'bullmq'
2021-05-11 11:15:29 +02:00
import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url'
import { ActivitypubFollowPayload } from '@shared/models'
import { sanitizeHost } from '../../../helpers/core-utils'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
2021-05-11 11:15:29 +02:00
import { logger } from '../../../helpers/logger'
import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
import { sequelizeTypescript } from '../../../initializers/database'
2021-05-11 11:15:29 +02:00
import { ActorModel } from '../../../models/actor/actor'
import { ActorFollowModel } from '../../../models/actor/actor-follow'
2021-10-14 14:14:57 +02:00
import { MActor, MActorFull } from '../../../types/models'
import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../activitypub/actors'
2021-05-11 11:15:29 +02:00
import { sendFollow } from '../../activitypub/send'
import { Notifier } from '../../notifier'
2021-08-27 14:32:44 +02:00
async function processActivityPubFollow (job: Job) {
const payload = job.data as ActivitypubFollowPayload
const host = payload.host
2022-08-08 15:48:17 +02:00
logger.info('Processing ActivityPub follow in job %s.', 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)
2021-06-03 16:02:29 +02:00
targetActor = await getOrCreateAPActor(actorUrl, 'all')
2018-08-21 16:18:59 +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)
return
}
const fromActor = await ActorModel.load(payload.followerActorId)
return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
}
// ---------------------------------------------------------------------------
export {
processActivityPubFollow
}
// ---------------------------------------------------------------------------
async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
if (fromActor.id === targetActor.id) {
throw new Error('Follower is the same as target actor.')
}
// Same server, direct accept
const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
const actorFollow = await sequelizeTypescript.transaction(async t => {
2021-10-14 10:52:15 +02:00
const [ actorFollow ] = await ActorFollowModel.findOrCreateCustom({
byActor: fromActor,
state,
targetActor,
activityId: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
transaction: t
})
// 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)
return actorFollow
})
const followerFull = await ActorModel.loadFull(fromActor.id)
2019-08-20 13:52:49 +02:00
const actorFollowFull = Object.assign(actorFollow, {
ActorFollowing: targetActor,
ActorFollower: followerFull
})
2019-08-20 13:52:49 +02:00
if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
return actorFollow
}