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