From 978c87e7f58b6673fe60f04f1767bc9e02ea4936 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 20 Oct 2021 09:05:43 +0200 Subject: Add channel filters for my videos/followers --- server/controllers/api/users/me.ts | 4 +++- server/middlewares/validators/users.ts | 27 +++++++++++++++++++++++++-- server/models/video/video.ts | 9 ++++++++- server/tests/api/check-params/videos.ts | 14 ++++++++++++++ server/tests/api/users/users.ts | 25 +++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 83b774d3c..6bacdbbb6 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -25,7 +25,7 @@ import { usersUpdateMeValidator, usersVideoRatingValidator } from '../../../middlewares' -import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' +import { deleteMeValidator, usersVideosValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' import { updateAvatarValidator } from '../../../middlewares/validators/actor-image' import { AccountModel } from '../../../models/account/account' import { AccountVideoRateModel } from '../../../models/account/account-video-rate' @@ -69,6 +69,7 @@ meRouter.get('/me/videos', videosSortValidator, setDefaultVideosSort, setDefaultPagination, + asyncMiddleware(usersVideosValidator), asyncMiddleware(getUserVideos) ) @@ -113,6 +114,7 @@ async function getUserVideos (req: express.Request, res: express.Response) { count: req.query.count, sort: req.query.sort, search: req.query.search, + channelId: res.locals.videoChannel?.id, isLive: req.query.isLive }, 'filter:api.user.me.videos.list.params') diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index c6eeeaf18..8f1a7801f 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -4,7 +4,7 @@ import { omit } from 'lodash' import { Hooks } from '@server/lib/plugins/hooks' import { MUserDefault } from '@server/types/models' import { HttpStatusCode, UserRegister, UserRole } from '@shared/models' -import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' +import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { isUserAdminFlagsValid, @@ -31,7 +31,7 @@ import { Redis } from '../../lib/redis' import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../lib/signup' import { ActorModel } from '../../models/actor/actor' import { UserModel } from '../../models/user/user' -import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared' +import { areValidationErrors, doesVideoChannelIdExist, doesVideoExist, isValidVideoIdParam } from './shared' const usersListValidator = [ query('blocked') @@ -318,6 +318,28 @@ const usersVideoRatingValidator = [ } ] +const usersVideosValidator = [ + query('isLive') + .optional() + .customSanitizer(toBooleanOrNull) + .custom(isBooleanValid).withMessage('Should have a valid live boolean'), + + query('channelId') + .optional() + .customSanitizer(toIntOrNull) + .custom(isIdValid).withMessage('Should have a valid channel id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersVideosValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + if (req.query.channelId && !await doesVideoChannelIdExist(req.query.channelId, res)) return + + return next() + } +] + const ensureUserRegistrationAllowed = [ async (req: express.Request, res: express.Response, next: express.NextFunction) => { const allowedParams = { @@ -513,6 +535,7 @@ export { ensureUserRegistrationAllowed, ensureUserRegistrationAllowedForIP, usersGetValidator, + usersVideosValidator, usersAskResetPasswordValidator, usersResetPasswordValidator, usersAskSendVerifyEmailValidator, diff --git a/server/models/video/video.ts b/server/models/video/video.ts index d2daf18ee..4044287ee 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -978,10 +978,12 @@ export class VideoModel extends Model>> { start: number count: number sort: string + + channelId?: number isLive?: boolean search?: string }) { - const { accountId, start, count, sort, search, isLive } = options + const { accountId, channelId, start, count, sort, search, isLive } = options function buildBaseQuery (): FindOptions { const where: WhereOptions = {} @@ -996,6 +998,10 @@ export class VideoModel extends Model>> { where.isLive = isLive } + const channelWhere = channelId + ? { id: channelId } + : {} + const baseQuery = { offset: start, limit: count, @@ -1005,6 +1011,7 @@ export class VideoModel extends Model>> { { model: VideoChannelModel, required: true, + where: channelWhere, include: [ { model: AccountModel, diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts index e11ca0c82..d02b6e156 100644 --- a/server/tests/api/check-params/videos.ts +++ b/server/tests/api/check-params/videos.ts @@ -119,6 +119,20 @@ describe('Test videos API validator', function () { await checkBadSortPagination(server.url, path, server.accessToken) }) + it('Should fail with an invalid channel', async function () { + await makeGetRequest({ url: server.url, token: server.accessToken, path, query: { channelId: 'toto' } }) + }) + + it('Should fail with an unknown channel', async function () { + await makeGetRequest({ + url: server.url, + token: server.accessToken, + path, + query: { channelId: 89898 }, + expectedStatus: HttpStatusCode.NOT_FOUND_404 + }) + }) + it('Should success with the correct parameters', async function () { await makeGetRequest({ url: server.url, token: server.accessToken, path, expectedStatus: HttpStatusCode.OK_200 }) }) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 085d9d870..6c41e7d56 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -318,6 +318,8 @@ describe('Test users', function () { fixture: 'video_short.webm' } await server.videos.upload({ token: userToken, attributes }) + + await server.channels.create({ token: userToken, attributes: { name: 'other_channel' } }) }) it('Should have video quota updated', async function () { @@ -340,6 +342,29 @@ describe('Test users', function () { expect(video.previewPath).to.not.be.null }) + it('Should be able to filter by channel in my videos', async function () { + const myInfo = await server.users.getMyInfo({ token: userToken }) + const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel') + const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel') + + { + const { total, data } = await server.videos.listMyVideos({ token: userToken, channelId: mainChannel.id }) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) + + const video: Video = data[0] + expect(video.name).to.equal('super user video') + expect(video.thumbnailPath).to.not.be.null + expect(video.previewPath).to.not.be.null + } + + { + const { total, data } = await server.videos.listMyVideos({ token: userToken, channelId: otherChannel.id }) + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) + } + }) + it('Should be able to search in my videos', async function () { { const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'user video' }) -- cgit v1.2.3