]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/activitypub-follow.ts
Create a dedicated table to track video thumbnails
[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
14 export type ActivitypubFollowPayload = {
15 followerActorId: number
16 name: string
17 host: string
18 }
19
20 async 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
41 export {
42 processActivityPubFollow
43 }
44
45 // ---------------------------------------------------------------------------
46
47 async 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 }