mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2024-12-01 21:09:17 -06:00
b211106695
* Add "currentTime" and "event" body params to view endpoint * Merge watching and view endpoints * Introduce WatchAction AP activity * Add tables to store viewer information of local videos * Add endpoints to fetch video views/viewers stats of local videos * Refactor views/viewers handlers * Support "views" and "viewers" counters for both VOD and live videos
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
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
|
|
}
|