my-history.ts 1.7 KB

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