]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Merge branch 'feature/strong-model-types' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
1 import { ActivityFollow } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7 import { sendAccept, sendReject } from '../send'
8 import { Notifier } from '../../notifier'
9 import { getAPId } from '../../../helpers/activitypub'
10 import { getServerActor } from '../../../helpers/utils'
11 import { CONFIG } from '../../../initializers/config'
12 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 import { MAccount, MActorFollowActors, MActorFollowFull, MActorSignature } from '../../../typings/models'
14
15 async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
16 const { activity, byActor } = options
17 const activityObject = getAPId(activity.object)
18
19 return retryTransactionWrapper(processFollow, byActor, activityObject)
20 }
21
22 // ---------------------------------------------------------------------------
23
24 export {
25 processFollowActivity
26 }
27
28 // ---------------------------------------------------------------------------
29
30 async function processFollow (byActor: MActorSignature, targetActorURL: string) {
31 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
32 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
33
34 if (!targetActor) throw new Error('Unknown actor')
35 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
36
37 const serverActor = await getServerActor()
38 const isFollowingInstance = targetActor.id === serverActor.id
39
40 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
41 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
42
43 await sendReject(byActor, targetActor)
44
45 return { actorFollow: undefined as MActorFollowActors }
46 }
47
48 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
49 where: {
50 actorId: byActor.id,
51 targetActorId: targetActor.id
52 },
53 defaults: {
54 actorId: byActor.id,
55 targetActorId: targetActor.id,
56 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
57 },
58 transaction: t
59 })
60
61 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
62 actorFollow.state = 'accepted'
63 await actorFollow.save({ transaction: t })
64 }
65
66 actorFollow.ActorFollower = byActor
67 actorFollow.ActorFollowing = targetActor
68
69 // Target sends to actor he accepted the follow request
70 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
71
72 return { actorFollow, created, isFollowingInstance }
73 })
74
75 // Rejected
76 if (!actorFollow) return
77
78 if (created) {
79 if (isFollowingInstance) {
80 Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
81 } else {
82 const actorFollowFull = actorFollow as MActorFollowFull
83 actorFollowFull.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as MAccount
84
85 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
86 }
87 }
88
89 logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
90 }