]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/activitypub-follow.ts
Add user notification base code
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-follow.ts
CommitLineData
94831479 1import * as Bull from 'bull'
5350fd8e 2import { logger } from '../../../helpers/logger'
22a16e36 3import { CONFIG, REMOTE_SCHEME, sequelizeTypescript } from '../../../initializers'
5350fd8e
C
4import { sendFollow } from '../../activitypub/send'
5import { sanitizeHost } from '../../../helpers/core-utils'
6import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
7import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
8import { retryTransactionWrapper } from '../../../helpers/database-utils'
9import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
10import { ActorModel } from '../../../models/activitypub/actor'
11
12export type ActivitypubFollowPayload = {
06a05d5f
C
13 followerActorId: number
14 name: string
5350fd8e
C
15 host: string
16}
17
94831479 18async function processActivityPubFollow (job: Bull.Job) {
5350fd8e
C
19 const payload = job.data as ActivitypubFollowPayload
20 const host = payload.host
21
22 logger.info('Processing ActivityPub follow in job %d.', job.id)
23
22a16e36
C
24 let targetActor: ActorModel
25 if (!host || host === CONFIG.WEBSERVER.HOST) {
26 targetActor = await ActorModel.loadLocalByName(payload.name)
27 } else {
28 const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
29 const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
30 targetActor = await getOrCreateActorAndServerAndModel(actorUrl)
31 }
5350fd8e 32
06a05d5f 33 const fromActor = await ActorModel.load(payload.followerActorId)
5350fd8e 34
90d4bb81 35 return retryTransactionWrapper(follow, fromActor, targetActor)
5350fd8e
C
36}
37// ---------------------------------------------------------------------------
38
39export {
40 processActivityPubFollow
41}
42
43// ---------------------------------------------------------------------------
44
45function follow (fromActor: ActorModel, targetActor: ActorModel) {
46 if (fromActor.id === targetActor.id) {
47 throw new Error('Follower is the same than target actor.')
48 }
49
06a05d5f
C
50 // Same server, direct accept
51 const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
52
5350fd8e
C
53 return sequelizeTypescript.transaction(async t => {
54 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
55 where: {
56 actorId: fromActor.id,
57 targetActorId: targetActor.id
58 },
59 defaults: {
06a05d5f 60 state,
5350fd8e
C
61 actorId: fromActor.id,
62 targetActorId: targetActor.id
63 },
64 transaction: t
65 })
66 actorFollow.ActorFollowing = targetActor
67 actorFollow.ActorFollower = fromActor
68
6104adc3
C
69 // Send a notification to remote server if our follow is not already accepted
70 if (actorFollow.state !== 'accepted') await sendFollow(actorFollow)
5350fd8e
C
71 })
72}