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