]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/local-video-viewer.ts
Fix peertube runner concurrency
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / local-video-viewer.ts
1 import { Transaction } from 'sequelize'
2 import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
3 import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
4 import { MVideo } from '@server/types/models'
5 import { WatchActionObject } from '@shared/models'
6 import { getDurationFromActivityStream } from './activity'
7
8 async function createOrUpdateLocalVideoViewer (watchAction: WatchActionObject, video: MVideo, t: Transaction) {
9 const stats = await LocalVideoViewerModel.loadByUrl(watchAction.id)
10 if (stats) await stats.destroy({ transaction: t })
11
12 const localVideoViewer = await LocalVideoViewerModel.create({
13 url: watchAction.id,
14 uuid: watchAction.uuid,
15
16 watchTime: getDurationFromActivityStream(watchAction.duration),
17
18 startDate: new Date(watchAction.startTime),
19 endDate: new Date(watchAction.endTime),
20
21 country: watchAction.location
22 ? watchAction.location.addressCountry
23 : null,
24
25 videoId: video.id
26 }, { transaction: t })
27
28 await LocalVideoViewerWatchSectionModel.bulkCreateSections({
29 localVideoViewerId: localVideoViewer.id,
30
31 watchSections: watchAction.watchSections.map(s => ({
32 start: s.startTimestamp,
33 end: s.endTimestamp
34 })),
35
36 transaction: t
37 })
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 createOrUpdateLocalVideoViewer
44 }