]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
5350fd8e | 2 | import { logger } from '../../../helpers/logger' |
22a16e36 | 3 | import { CONFIG, REMOTE_SCHEME, sequelizeTypescript } from '../../../initializers' |
5350fd8e C |
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' | |
f7cc67b4 | 11 | import { Notifier } from '../../notifier' |
5350fd8e C |
12 | |
13 | export type ActivitypubFollowPayload = { | |
06a05d5f C |
14 | followerActorId: number |
15 | name: string | |
5350fd8e C |
16 | host: string |
17 | } | |
18 | ||
94831479 | 19 | async function processActivityPubFollow (job: Bull.Job) { |
5350fd8e C |
20 | const payload = job.data as ActivitypubFollowPayload |
21 | const host = payload.host | |
22 | ||
23 | logger.info('Processing ActivityPub follow in job %d.', job.id) | |
24 | ||
22a16e36 C |
25 | let targetActor: ActorModel |
26 | if (!host || host === CONFIG.WEBSERVER.HOST) { | |
27 | targetActor = await ActorModel.loadLocalByName(payload.name) | |
28 | } else { | |
29 | const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) | |
30 | const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) | |
31 | targetActor = await getOrCreateActorAndServerAndModel(actorUrl) | |
32 | } | |
5350fd8e | 33 | |
06a05d5f | 34 | const fromActor = await ActorModel.load(payload.followerActorId) |
5350fd8e | 35 | |
90d4bb81 | 36 | return retryTransactionWrapper(follow, fromActor, targetActor) |
5350fd8e C |
37 | } |
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | export { | |
41 | processActivityPubFollow | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
f7cc67b4 | 46 | async function follow (fromActor: ActorModel, targetActor: ActorModel) { |
5350fd8e C |
47 | if (fromActor.id === targetActor.id) { |
48 | throw new Error('Follower is the same than target actor.') | |
49 | } | |
50 | ||
06a05d5f C |
51 | // Same server, direct accept |
52 | const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending' | |
53 | ||
f7cc67b4 | 54 | const actorFollow = await sequelizeTypescript.transaction(async t => { |
5350fd8e C |
55 | const [ actorFollow ] = await ActorFollowModel.findOrCreate({ |
56 | where: { | |
57 | actorId: fromActor.id, | |
58 | targetActorId: targetActor.id | |
59 | }, | |
60 | defaults: { | |
06a05d5f | 61 | state, |
5350fd8e C |
62 | actorId: fromActor.id, |
63 | targetActorId: targetActor.id | |
64 | }, | |
65 | transaction: t | |
66 | }) | |
67 | actorFollow.ActorFollowing = targetActor | |
68 | actorFollow.ActorFollower = fromActor | |
69 | ||
6104adc3 C |
70 | // Send a notification to remote server if our follow is not already accepted |
71 | if (actorFollow.state !== 'accepted') await sendFollow(actorFollow) | |
f7cc67b4 C |
72 | |
73 | return actorFollow | |
5350fd8e | 74 | }) |
f7cc67b4 C |
75 | |
76 | if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewFollow(actorFollow) | |
5350fd8e | 77 | } |