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