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