aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/watching.ts
blob: e8c28b6138fcfd84f443922d44918fc5ec5e0560 (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
43
44
45
import express from 'express'
import { UserWatchingVideo } from '../../../../shared'
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import {
  asyncMiddleware,
  asyncRetryTransactionMiddleware,
  authenticate,
  openapiOperationDoc,
  videoWatchingValidator
} from '../../../middlewares'
import { UserVideoHistoryModel } from '../../../models/user/user-video-history'

const watchingRouter = express.Router()

watchingRouter.put('/:videoId/watching',
  openapiOperationDoc({ operationId: 'setProgress' }),
  authenticate,
  asyncMiddleware(videoWatchingValidator),
  asyncRetryTransactionMiddleware(userWatchVideo)
)

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

export {
  watchingRouter
}

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

async function userWatchVideo (req: express.Request, res: express.Response) {
  const user = res.locals.oauth.token.User

  const body: UserWatchingVideo = req.body
  const { id: videoId } = res.locals.videoId

  await UserVideoHistoryModel.upsert({
    videoId,
    userId: user.id,
    currentTime: body.currentTime
  })

  return res.type('json')
            .status(HttpStatusCode.NO_CONTENT_204)
            .end()
}