watching.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import * as express from 'express'
  2. import { UserWatchingVideo } from '../../../../shared'
  3. import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoWatchingValidator } from '../../../middlewares'
  4. import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
  5. const watchingRouter = express.Router()
  6. watchingRouter.put('/:videoId/watching',
  7. authenticate,
  8. asyncMiddleware(videoWatchingValidator),
  9. asyncRetryTransactionMiddleware(userWatchVideo)
  10. )
  11. // ---------------------------------------------------------------------------
  12. export {
  13. watchingRouter
  14. }
  15. // ---------------------------------------------------------------------------
  16. async function userWatchVideo (req: express.Request, res: express.Response) {
  17. const user = res.locals.oauth.token.User
  18. const body: UserWatchingVideo = req.body
  19. const { id: videoId } = res.locals.video as { id: number }
  20. await UserVideoHistoryModel.upsert({
  21. videoId,
  22. userId: user.id,
  23. currentTime: body.currentTime
  24. })
  25. return res.type('json').status(204).end()
  26. }