]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Fix broken follow notification
[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
13 async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
14 const activityObject = getAPId(activity.object)
15
16 return retryTransactionWrapper(processFollow, byActor, activityObject)
17 }
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 processFollowActivity
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function processFollow (actor: ActorModel, targetActorURL: string) {
28 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
29 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
30
31 if (!targetActor) throw new Error('Unknown actor')
32 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
33
34 const serverActor = await getServerActor()
35 const isFollowingInstance = targetActor.id === serverActor.id
36
37 if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
38 logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
39
40 await sendReject(actor, targetActor)
41
42 return { actorFollow: undefined }
43 }
44
45 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
46 where: {
47 actorId: actor.id,
48 targetActorId: targetActor.id
49 },
50 defaults: {
51 actorId: actor.id,
52 targetActorId: targetActor.id,
53 state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
54 },
55 transaction: t
56 })
57
58 if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
59 actorFollow.state = 'accepted'
60 await actorFollow.save({ transaction: t })
61 }
62
63 actorFollow.ActorFollower = actor
64 actorFollow.ActorFollowing = targetActor
65
66 // Target sends to actor he accepted the follow request
67 if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
68
69 return { actorFollow, created, isFollowingInstance }
70 })
71
72 // Rejected
73 if (!actorFollow) return
74
75 if (created) {
76 if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
77 else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
78 }
79
80 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
81 }