]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-follow.ts
Fix player play exception on chromium
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
1 import { ActivityFollow } from '../../../../shared/models/activitypub'
2 import { logger, retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
6 import { getOrCreateActorAndServerAndModel } from '../actor'
7 import { sendAccept } from '../send'
8
9 async function processFollowActivity (activity: ActivityFollow) {
10 const activityObject = activity.object
11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
12
13 return processFollow(actor, activityObject)
14 }
15
16 // ---------------------------------------------------------------------------
17
18 export {
19 processFollowActivity
20 }
21
22 // ---------------------------------------------------------------------------
23
24 function processFollow (actor: ActorModel, targetActorURL: string) {
25 const options = {
26 arguments: [ actor, targetActorURL ],
27 errorMessage: 'Cannot follow with many retries.'
28 }
29
30 return retryTransactionWrapper(follow, options)
31 }
32
33 async function follow (actor: ActorModel, targetActorURL: string) {
34 await sequelizeTypescript.transaction(async t => {
35 const targetActor = await ActorModel.loadByUrl(targetActorURL, t)
36
37 if (!targetActor) throw new Error('Unknown actor')
38 if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
39
40 const [ actorFollow ] = await ActorFollowModel.findOrCreate({
41 where: {
42 actorId: actor.id,
43 targetActorId: targetActor.id
44 },
45 defaults: {
46 actorId: actor.id,
47 targetActorId: targetActor.id,
48 state: 'accepted'
49 },
50 transaction: t
51 })
52
53 if (actorFollow.state !== 'accepted') {
54 actorFollow.state = 'accepted'
55 await actorFollow.save({ transaction: t })
56 }
57
58 actorFollow.ActorFollower = actor
59 actorFollow.ActorFollowing = targetActor
60
61 // Target sends to actor he accepted the follow request
62 return sendAccept(actorFollow, t)
63 })
64
65 logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
66 }