Add history on server side

Add ability to disable, clear and list user videos history
This commit is contained in:
Chocobozzz
2018-12-17 15:52:38 +01:00
parent 583cd0d212
commit 8b9a525a18
19 changed files with 385 additions and 29 deletions

View File

@@ -38,6 +38,7 @@ import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../h
import { meRouter } from './me'
import { deleteUserToken } from '../../../lib/oauth-model'
import { myBlocklistRouter } from './my-blocklist'
import { myVideosHistoryRouter } from './my-history'
const auditLogger = auditLoggerFactory('users')
@@ -55,6 +56,7 @@ const askSendEmailLimiter = new RateLimit({
const usersRouter = express.Router()
usersRouter.use('/', myBlocklistRouter)
usersRouter.use('/', myVideosHistoryRouter)
usersRouter.use('/', meRouter)
usersRouter.get('/autocomplete',

View File

@@ -330,6 +330,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr
if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled
if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled
await sequelizeTypescript.transaction(async t => {
const userAccount = await AccountModel.load(user.Account.id)

View File

@@ -0,0 +1,57 @@
import * as express from 'express'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
paginationValidator,
setDefaultPagination,
userHistoryRemoveValidator
} from '../../../middlewares'
import { UserModel } from '../../../models/account/user'
import { getFormattedObjects } from '../../../helpers/utils'
import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
import { sequelizeTypescript } from '../../../initializers'
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) {
const user: UserModel = 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) {
const user: UserModel = res.locals.oauth.token.User
const beforeDate = req.body.beforeDate || null
await sequelizeTypescript.transaction(t => {
return UserVideoHistoryModel.removeHistoryBefore(user, beforeDate, t)
})
// Do not send the delete to other instances, we delete OUR copy of this video abuse
return res.type('json').status(204).end()
}