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