]>
Commit | Line | Data |
---|---|---|
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 { MActor, MActorFollowActors, MActorFull } from '../../../typings/models' | |
14 | ||
15 | export type ActivitypubFollowPayload = { | |
16 | followerActorId: number | |
17 | name: string | |
18 | host: string | |
19 | isAutoFollow?: boolean | |
20 | assertIsChannel?: boolean | |
21 | } | |
22 | ||
23 | async function processActivityPubFollow (job: Bull.Job) { | |
24 | const payload = job.data as ActivitypubFollowPayload | |
25 | const host = payload.host | |
26 | ||
27 | logger.info('Processing ActivityPub follow in job %d.', job.id) | |
28 | ||
29 | let targetActor: MActorFull | |
30 | if (!host || host === WEBSERVER.HOST) { | |
31 | targetActor = await ActorModel.loadLocalByName(payload.name) | |
32 | } else { | |
33 | const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) | |
34 | const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) | |
35 | targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all') | |
36 | } | |
37 | ||
38 | if (payload.assertIsChannel && !targetActor.VideoChannel) { | |
39 | logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host) | |
40 | return | |
41 | } | |
42 | ||
43 | const fromActor = await ActorModel.load(payload.followerActorId) | |
44 | ||
45 | return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow) | |
46 | } | |
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | processActivityPubFollow | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) { | |
56 | if (fromActor.id === targetActor.id) { | |
57 | throw new Error('Follower is the same as target actor.') | |
58 | } | |
59 | ||
60 | // Same server, direct accept | |
61 | const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending' | |
62 | ||
63 | const actorFollow = await sequelizeTypescript.transaction(async t => { | |
64 | const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({ | |
65 | where: { | |
66 | actorId: fromActor.id, | |
67 | targetActorId: targetActor.id | |
68 | }, | |
69 | defaults: { | |
70 | state, | |
71 | actorId: fromActor.id, | |
72 | targetActorId: targetActor.id | |
73 | }, | |
74 | transaction: t | |
75 | }) | |
76 | actorFollow.ActorFollowing = targetActor | |
77 | actorFollow.ActorFollower = fromActor | |
78 | ||
79 | // Send a notification to remote server if our follow is not already accepted | |
80 | if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t) | |
81 | ||
82 | return actorFollow | |
83 | }) | |
84 | ||
85 | const followerFull = await ActorModel.loadFull(fromActor.id) | |
86 | ||
87 | const actorFollowFull = Object.assign(actorFollow, { | |
88 | ActorFollowing: targetActor, | |
89 | ActorFollower: followerFull | |
90 | }) | |
91 | ||
92 | if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) | |
93 | if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull) | |
94 | ||
95 | return actorFollow | |
96 | } |