1
0
Fork 0
peertube/server/controllers/api/users/my-history.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as express from 'express'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
paginationValidator,
setDefaultPagination,
userHistoryRemoveValidator
} from '../../../middlewares'
import { getFormattedObjects } from '../../../helpers/utils'
import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
2020-05-07 08:58:24 -04:00
import { sequelizeTypescript } from '../../../initializers/database'
const myVideosHistoryRouter = express.Router()
myVideosHistoryRouter.get('/me/history/videos',
authenticate,
paginationValidator,
setDefaultPagination,
asyncMiddleware(listMyVideosHistory)
)
myVideosHistoryRouter.post('/me/history/videos/remove',
authenticate,
userHistoryRemoveValidator,
asyncRetryTransactionMiddleware(removeUserHistory)
)
// ---------------------------------------------------------------------------
export {
myVideosHistoryRouter
}
// ---------------------------------------------------------------------------
async function listMyVideosHistory (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count)
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
async function removeUserHistory (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
const beforeDate = req.body.beforeDate || null
await sequelizeTypescript.transaction(t => {
return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
})
return res.type('json').status(204).end()
}