aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-04-18 15:32:40 +0200
committerChocobozzz <me@florianbigard.com>2018-04-18 15:32:40 +0200
commit5350fd8e5b2b2d017b16d97828893a8a4a40bd89 (patch)
treeeddc20eeb19e1501ead0ad72b2fdaffc92708478 /server/lib/job-queue/handlers
parentbf6e8e3e3dd6516a6125c9c2c12587e7bac8c107 (diff)
downloadPeerTube-5350fd8e5b2b2d017b16d97828893a8a4a40bd89.tar.gz
PeerTube-5350fd8e5b2b2d017b16d97828893a8a4a40bd89.tar.zst
PeerTube-5350fd8e5b2b2d017b16d97828893a8a4a40bd89.zip
Move server follow in the job queue
It helps to track follow errors
Diffstat (limited to 'server/lib/job-queue/handlers')
-rw-r--r--server/lib/job-queue/handlers/activitypub-follow.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts
new file mode 100644
index 000000000..6764a4037
--- /dev/null
+++ b/server/lib/job-queue/handlers/activitypub-follow.ts
@@ -0,0 +1,68 @@
1import * as kue from 'kue'
2import { logger } from '../../../helpers/logger'
3import { getServerActor } from '../../../helpers/utils'
4import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers'
5import { sendFollow } from '../../activitypub/send'
6import { sanitizeHost } from '../../../helpers/core-utils'
7import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
8import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
9import { retryTransactionWrapper } from '../../../helpers/database-utils'
10import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
11import { ActorModel } from '../../../models/activitypub/actor'
12
13export type ActivitypubFollowPayload = {
14 host: string
15}
16
17async function processActivityPubFollow (job: kue.Job) {
18 const payload = job.data as ActivitypubFollowPayload
19 const host = payload.host
20
21 logger.info('Processing ActivityPub follow in job %d.', job.id)
22
23 const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
24
25 const actorUrl = await loadActorUrlOrGetFromWebfinger(SERVER_ACTOR_NAME, sanitizedHost)
26 const targetActor = await getOrCreateActorAndServerAndModel(actorUrl)
27
28 const fromActor = await getServerActor()
29 const options = {
30 arguments: [ fromActor, targetActor ],
31 errorMessage: 'Cannot follow with many retries.'
32 }
33
34 return retryTransactionWrapper(follow, options)
35}
36// ---------------------------------------------------------------------------
37
38export {
39 processActivityPubFollow
40}
41
42// ---------------------------------------------------------------------------
43
44function follow (fromActor: ActorModel, targetActor: ActorModel) {
45 if (fromActor.id === targetActor.id) {
46 throw new Error('Follower is the same than target actor.')
47 }
48
49 return sequelizeTypescript.transaction(async t => {
50 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
51 where: {
52 actorId: fromActor.id,
53 targetActorId: targetActor.id
54 },
55 defaults: {
56 state: 'pending',
57 actorId: fromActor.id,
58 targetActorId: targetActor.id
59 },
60 transaction: t
61 })
62 actorFollow.ActorFollowing = targetActor
63 actorFollow.ActorFollower = fromActor
64
65 // Send a notification to remote server
66 await sendFollow(actorFollow)
67 })
68}