aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/local-video-viewer.ts
blob: 738083adc9b00472a0a039487cce955366c14a3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Transaction } from 'sequelize'
import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
import { MVideo } from '@server/types/models'
import { WatchActionObject } from '@shared/models'
import { getDurationFromActivityStream } from './activity'

async function createOrUpdateLocalVideoViewer (watchAction: WatchActionObject, video: MVideo, t: Transaction) {
  const stats = await LocalVideoViewerModel.loadByUrl(watchAction.id)
  if (stats) await stats.destroy({ transaction: t })

  const localVideoViewer = await LocalVideoViewerModel.create({
    url: watchAction.id,
    uuid: watchAction.uuid,

    watchTime: getDurationFromActivityStream(watchAction.duration),

    startDate: new Date(watchAction.startTime),
    endDate: new Date(watchAction.endTime),

    country: watchAction.location
      ? watchAction.location.addressCountry
      : null,

    videoId: video.id
  })

  await LocalVideoViewerWatchSectionModel.bulkCreateSections({
    localVideoViewerId: localVideoViewer.id,

    watchSections: watchAction.watchSections.map(s => ({
      start: s.startTimestamp,
      end: s.endTimestamp
    }))
  })
}

// ---------------------------------------------------------------------------

export {
  createOrUpdateLocalVideoViewer
}