]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-follow.ts
Add ability to forbid followers
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
... / ...
CommitLineData
1import { ActivityFollow } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
4import { sequelizeTypescript, CONFIG } from '../../../initializers'
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7import { sendAccept, sendReject } from '../send'
8import { Notifier } from '../../notifier'
9import { getAPId } from '../../../helpers/activitypub'
10import { getServerActor } from '../../../helpers/utils'
11
12async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
13 const activityObject = getAPId(activity.object)
14
15 return retryTransactionWrapper(processFollow, byActor, activityObject)
16}
17
18// ---------------------------------------------------------------------------
19
20export {
21 processFollowActivity
22}
23
24// ---------------------------------------------------------------------------
25
26async function processFollow (actor: ActorModel, targetActorURL: string) {
27 const { actorFollow, created } = await sequelizeTypescript.transaction(async t => {
28 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
29
30 if (!targetActor) throw new Error('Unknown actor')
31 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
32
33 const serverActor = await getServerActor()
34 if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
35 return sendReject(actor, targetActor)
36 }
37
38 const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
39 where: {
40 actorId: actor.id,
41 targetActorId: targetActor.id
42 },
43 defaults: {
44 actorId: actor.id,
45 targetActorId: targetActor.id,
46 state: 'accepted'
47 },
48 transaction: t
49 })
50
51 actorFollow.ActorFollower = actor
52 actorFollow.ActorFollowing = targetActor
53
54 if (actorFollow.state !== 'accepted') {
55 actorFollow.state = 'accepted'
56 await actorFollow.save({ transaction: t })
57 }
58
59 actorFollow.ActorFollower = actor
60 actorFollow.ActorFollowing = targetActor
61
62 // Target sends to actor he accepted the follow request
63 await sendAccept(actorFollow)
64
65 return { actorFollow, created }
66 })
67
68 if (created) Notifier.Instance.notifyOfNewFollow(actorFollow)
69
70 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
71}