aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/passwords.ts
blob: d11cf5bcccb7c26568bbf6958c91fb01b7e0f69a (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import express from 'express'

import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import { getFormattedObjects } from '../../../helpers/utils'
import {
  asyncMiddleware,
  asyncRetryTransactionMiddleware,
  authenticate,
  setDefaultPagination,
  setDefaultSort
} from '../../../middlewares'
import {
  listVideoPasswordValidator,
  paginationValidator,
  removeVideoPasswordValidator,
  updateVideoPasswordListValidator,
  videoPasswordsSortValidator
} from '../../../middlewares/validators'
import { VideoPasswordModel } from '@server/models/video/video-password'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { Transaction } from 'sequelize'
import { getVideoWithAttributes } from '@server/helpers/video'

const lTags = loggerTagsFactory('api', 'video')
const videoPasswordRouter = express.Router()

videoPasswordRouter.get('/:videoId/passwords',
  authenticate,
  paginationValidator,
  videoPasswordsSortValidator,
  setDefaultSort,
  setDefaultPagination,
  asyncMiddleware(listVideoPasswordValidator),
  asyncMiddleware(listVideoPasswords)
)

videoPasswordRouter.put('/:videoId/passwords',
  authenticate,
  asyncMiddleware(updateVideoPasswordListValidator),
  asyncMiddleware(updateVideoPasswordList)
)

videoPasswordRouter.delete('/:videoId/passwords/:passwordId',
  authenticate,
  asyncMiddleware(removeVideoPasswordValidator),
  asyncRetryTransactionMiddleware(removeVideoPassword)
)

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

export {
  videoPasswordRouter
}

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

async function listVideoPasswords (req: express.Request, res: express.Response) {
  const options = {
    videoId: res.locals.videoAll.id,
    start: req.query.start,
    count: req.query.count,
    sort: req.query.sort
  }

  const resultList = await VideoPasswordModel.listPasswords(options)

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}

async function updateVideoPasswordList (req: express.Request, res: express.Response) {
  const videoInstance = getVideoWithAttributes(res)
  const videoId = videoInstance.id

  const passwordArray = req.body.passwords as string[]

  await VideoPasswordModel.sequelize.transaction(async (t: Transaction) => {
    await VideoPasswordModel.deleteAllPasswords(videoId, t)
    await VideoPasswordModel.addPasswords(passwordArray, videoId, t)
  })

  logger.info(
    `Video passwords for video with name %s and uuid %s have been updated`,
    videoInstance.name,
    videoInstance.uuid,
    lTags(videoInstance.uuid)
  )

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}

async function removeVideoPassword (req: express.Request, res: express.Response) {
  const videoInstance = getVideoWithAttributes(res)
  const password = res.locals.videoPassword

  await VideoPasswordModel.deletePassword(password.id)
  logger.info(
    'Password with id %d of video named %s and uuid %s has been deleted.',
    password.id,
    videoInstance.name,
    videoInstance.uuid,
    lTags(videoInstance.uuid)
  )

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}