]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import * as Bull from 'bull'
2import { logger } from '../../../helpers/logger'
3import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
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'
11import { Notifier } from '../../notifier'
12import { sequelizeTypescript } from '../../../initializers/database'
13
14export type ActivitypubFollowPayload = {
15 followerActorId: number
16 name: string
17 host: string
18}
19
20async function processActivityPubFollow (job: Bull.Job) {
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
26 let targetActor: ActorModel
27 if (!host || host === WEBSERVER.HOST) {
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 }
34
35 const fromActor = await ActorModel.load(payload.followerActorId)
36
37 return retryTransactionWrapper(follow, fromActor, targetActor)
38}
39// ---------------------------------------------------------------------------
40
41export {
42 processActivityPubFollow
43}
44
45// ---------------------------------------------------------------------------
46
47async function follow (fromActor: ActorModel, targetActor: ActorModel) {
48 if (fromActor.id === targetActor.id) {
49 throw new Error('Follower is the same than target actor.')
50 }
51
52 // Same server, direct accept
53 const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
54
55 const actorFollow = await sequelizeTypescript.transaction(async t => {
56 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
57 where: {
58 actorId: fromActor.id,
59 targetActorId: targetActor.id
60 },
61 defaults: {
62 state,
63 actorId: fromActor.id,
64 targetActorId: targetActor.id
65 },
66 transaction: t
67 })
68 actorFollow.ActorFollowing = targetActor
69 actorFollow.ActorFollower = fromActor
70
71 // Send a notification to remote server if our follow is not already accepted
72 if (actorFollow.state !== 'accepted') await sendFollow(actorFollow)
73
74 return actorFollow
75 })
76
77 if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow)
78}