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