Merge branch 'release/1.4.0' into develop
[oweals/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 '../../../typings/models'
14
15 export type ActivitypubFollowPayload = {
16   followerActorId: number
17   name: string
18   host: string
19   isAutoFollow?: boolean
20 }
21
22 async function processActivityPubFollow (job: Bull.Job) {
23   const payload = job.data as ActivitypubFollowPayload
24   const host = payload.host
25
26   logger.info('Processing ActivityPub follow in job %d.', job.id)
27
28   let targetActor: MActorFull
29   if (!host || host === WEBSERVER.HOST) {
30     targetActor = await ActorModel.loadLocalByName(payload.name)
31   } else {
32     const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
33     const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
34     targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
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 than 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         actorId: fromActor.id,
66         targetActorId: targetActor.id
67       },
68       transaction: t
69     })
70     actorFollow.ActorFollowing = targetActor
71     actorFollow.ActorFollower = fromActor
72
73     // Send a notification to remote server if our follow is not already accepted
74     if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
75
76     return actorFollow
77   })
78
79   const followerFull = await ActorModel.loadFull(fromActor.id)
80
81   const actorFollowFull = Object.assign(actorFollow, {
82     ActorFollowing: targetActor,
83     ActorFollower: followerFull
84   })
85
86   if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
87   if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
88
89   return actorFollow
90 }