Stronger actor association typing in AP functions
[oweals/peertube.git] / server / lib / activitypub / process / process-view.ts
1 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
2 import { forwardVideoRelatedActivity } from '../send/utils'
3 import { Redis } from '../../redis'
4 import { ActivityCreate, ActivityView, ViewObject } from '../../../../shared/models/activitypub'
5 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
6 import { SignatureActorModel } from '../../../typings/models'
7
8 async function processViewActivity (options: APProcessorOptions<ActivityCreate | ActivityView>) {
9   const { activity, byActor } = options
10   return processCreateView(activity, byActor)
11 }
12
13 // ---------------------------------------------------------------------------
14
15 export {
16   processViewActivity
17 }
18
19 // ---------------------------------------------------------------------------
20
21 async function processCreateView (activity: ActivityView | ActivityCreate, byActor: SignatureActorModel) {
22   const videoObject = activity.type === 'View' ? activity.object : (activity.object as ViewObject).object
23
24   const options = {
25     videoObject: videoObject,
26     fetchType: 'only-video' as 'only-video'
27   }
28   const { video } = await getOrCreateVideoAndAccountAndChannel(options)
29
30   await Redis.Instance.addVideoView(video.id)
31
32   if (video.isOwned()) {
33     // Don't resend the activity to the sender
34     const exceptions = [ byActor ]
35     await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
36   }
37 }