]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-follow.ts
Remove unnecessary actor existance check
[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 } from '../../../initializers'
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7import { sendAccept } from '../send'
8
9async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
10 const activityObject = activity.object
11
12 return retryTransactionWrapper(processFollow, byActor, activityObject)
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 processFollowActivity
19}
20
21// ---------------------------------------------------------------------------
22
23async function processFollow (actor: ActorModel, targetActorURL: string) {
24 await sequelizeTypescript.transaction(async t => {
25 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
26
27 if (!targetActor) throw new Error('Unknown actor')
28 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
29
30 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
31 where: {
32 actorId: actor.id,
33 targetActorId: targetActor.id
34 },
35 defaults: {
36 actorId: actor.id,
37 targetActorId: targetActor.id,
38 state: 'accepted'
39 },
40 transaction: t
41 })
42
43 actorFollow.ActorFollower = actor
44 actorFollow.ActorFollowing = targetActor
45
46 if (actorFollow.state !== 'accepted') {
47 actorFollow.state = 'accepted'
48 await actorFollow.save({ transaction: t })
49 }
50
51 actorFollow.ActorFollower = actor
52 actorFollow.ActorFollowing = targetActor
53
54 // Target sends to actor he accepted the follow request
55 return sendAccept(actorFollow)
56 })
57
58 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
59}