my-history.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import * as express from 'express'
  2. import {
  3. asyncMiddleware,
  4. asyncRetryTransactionMiddleware,
  5. authenticate,
  6. paginationValidator,
  7. setDefaultPagination,
  8. userHistoryRemoveValidator
  9. } from '../../../middlewares'
  10. import { getFormattedObjects } from '../../../helpers/utils'
  11. import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
  12. import { sequelizeTypescript } from '../../../initializers'
  13. const myVideosHistoryRouter = express.Router()
  14. myVideosHistoryRouter.get('/me/history/videos',
  15. authenticate,
  16. paginationValidator,
  17. setDefaultPagination,
  18. asyncMiddleware(listMyVideosHistory)
  19. )
  20. myVideosHistoryRouter.post('/me/history/videos/remove',
  21. authenticate,
  22. userHistoryRemoveValidator,
  23. asyncRetryTransactionMiddleware(removeUserHistory)
  24. )
  25. // ---------------------------------------------------------------------------
  26. export {
  27. myVideosHistoryRouter
  28. }
  29. // ---------------------------------------------------------------------------
  30. async function listMyVideosHistory (req: express.Request, res: express.Response) {
  31. const user = res.locals.oauth.token.User
  32. const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count)
  33. return res.json(getFormattedObjects(resultList.data, resultList.total))
  34. }
  35. async function removeUserHistory (req: express.Request, res: express.Response) {
  36. const user = res.locals.oauth.token.User
  37. const beforeDate = req.body.beforeDate || null
  38. await sequelizeTypescript.transaction(t => {
  39. return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
  40. })
  41. // Do not send the delete to other instances, we delete OUR copy of this video abuse
  42. return res.type('json').status(204).end()
  43. }