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'
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 })
12 const localVideoViewer = await LocalVideoViewerModel.create({
14 uuid: watchAction.uuid,
16 watchTime: getDurationFromActivityStream(watchAction.duration),
18 startDate: new Date(watchAction.startTime),
19 endDate: new Date(watchAction.endTime),
21 country: watchAction.location
22 ? watchAction.location.addressCountry
26 }, { transaction: t })
28 await LocalVideoViewerWatchSectionModel.bulkCreateSections({
29 localVideoViewerId: localVideoViewer.id,
31 watchSections: watchAction.watchSections.map(s => ({
32 start: s.startTimestamp,
40 // ---------------------------------------------------------------------------
43 createOrUpdateLocalVideoViewer