mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
66
server/core/controllers/api/videos/view.ts
Normal file
66
server/core/controllers/api/videos/view.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import express from 'express'
|
||||
import { HttpStatusCode, VideoView } from '@peertube/peertube-models'
|
||||
import { Hooks } from '@server/lib/plugins/hooks.js'
|
||||
import { VideoViewsManager } from '@server/lib/views/video-views-manager.js'
|
||||
import { MVideoId } from '@server/types/models/index.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
methodsValidator,
|
||||
openapiOperationDoc,
|
||||
optionalAuthenticate,
|
||||
videoViewValidator
|
||||
} from '../../../middlewares/index.js'
|
||||
import { UserVideoHistoryModel } from '../../../models/user/user-video-history.js'
|
||||
|
||||
const viewRouter = express.Router()
|
||||
|
||||
viewRouter.all(
|
||||
[ '/:videoId/views', '/:videoId/watching' ],
|
||||
openapiOperationDoc({ operationId: 'addView' }),
|
||||
methodsValidator([ 'PUT', 'POST' ]),
|
||||
optionalAuthenticate,
|
||||
asyncMiddleware(videoViewValidator),
|
||||
asyncMiddleware(viewVideo)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
viewRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function viewVideo (req: express.Request, res: express.Response) {
|
||||
const video = res.locals.onlyImmutableVideo
|
||||
|
||||
const body = req.body as VideoView
|
||||
|
||||
const ip = req.ip
|
||||
const { successView } = await VideoViewsManager.Instance.processLocalView({
|
||||
video,
|
||||
ip,
|
||||
currentTime: body.currentTime,
|
||||
viewEvent: body.viewEvent
|
||||
})
|
||||
|
||||
if (successView) {
|
||||
Hooks.runAction('action:api.video.viewed', { video, ip, req, res })
|
||||
}
|
||||
|
||||
await updateUserHistoryIfNeeded(body, video, res)
|
||||
|
||||
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
||||
}
|
||||
|
||||
async function updateUserHistoryIfNeeded (body: VideoView, video: MVideoId, res: express.Response) {
|
||||
const user = res.locals.oauth?.token.User
|
||||
if (!user) return
|
||||
if (user.videosHistoryEnabled !== true) return
|
||||
|
||||
await UserVideoHistoryModel.upsert({
|
||||
videoId: video.id,
|
||||
userId: user.id,
|
||||
currentTime: body.currentTime
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue