diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-20 18:31:49 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-24 14:04:05 +0200 |
commit | 0b18f4aa80df8868bf34605423c7a298dffbb2aa (patch) | |
tree | 25299da5d94fc73e88b21e87aeb2c156999c6fcd /server | |
parent | d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9 (diff) | |
download | PeerTube-0b18f4aa80df8868bf34605423c7a298dffbb2aa.tar.gz PeerTube-0b18f4aa80df8868bf34605423c7a298dffbb2aa.tar.zst PeerTube-0b18f4aa80df8868bf34605423c7a298dffbb2aa.zip |
Add advanced search in client
Diffstat (limited to 'server')
-rw-r--r-- | server/helpers/custom-validators/search.ts | 7 | ||||
-rw-r--r-- | server/helpers/express-utils.ts | 6 | ||||
-rw-r--r-- | server/initializers/database.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/search.ts | 5 | ||||
-rw-r--r-- | server/models/video/video.ts | 17 | ||||
-rw-r--r-- | server/tests/api/search/search-videos.ts | 8 | ||||
-rw-r--r-- | server/tests/api/videos/video-nsfw.ts | 11 |
7 files changed, 43 insertions, 13 deletions
diff --git a/server/helpers/custom-validators/search.ts b/server/helpers/custom-validators/search.ts index 2fde39160..15b389a58 100644 --- a/server/helpers/custom-validators/search.ts +++ b/server/helpers/custom-validators/search.ts | |||
@@ -11,9 +11,14 @@ function isStringArray (value: any) { | |||
11 | return isArray(value) && value.every(v => typeof v === 'string') | 11 | return isArray(value) && value.every(v => typeof v === 'string') |
12 | } | 12 | } |
13 | 13 | ||
14 | function isNSFWQueryValid (value: any) { | ||
15 | return value === 'true' || value === 'false' || value === 'both' | ||
16 | } | ||
17 | |||
14 | // --------------------------------------------------------------------------- | 18 | // --------------------------------------------------------------------------- |
15 | 19 | ||
16 | export { | 20 | export { |
17 | isNumberArray, | 21 | isNumberArray, |
18 | isStringArray | 22 | isStringArray, |
23 | isNSFWQueryValid | ||
19 | } | 24 | } |
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 5bf1e1a5f..76440348f 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts | |||
@@ -5,8 +5,10 @@ import { logger } from './logger' | |||
5 | import { User } from '../../shared/models/users' | 5 | import { User } from '../../shared/models/users' |
6 | import { generateRandomString } from './utils' | 6 | import { generateRandomString } from './utils' |
7 | 7 | ||
8 | function buildNSFWFilter (res: express.Response, paramNSFW?: boolean) { | 8 | function buildNSFWFilter (res: express.Response, paramNSFW?: string) { |
9 | if (paramNSFW === true || paramNSFW === false) return paramNSFW | 9 | if (paramNSFW === 'true') return true |
10 | if (paramNSFW === 'false') return false | ||
11 | if (paramNSFW === 'both') return undefined | ||
10 | 12 | ||
11 | if (res.locals.oauth) { | 13 | if (res.locals.oauth) { |
12 | const user: User = res.locals.oauth.token.User | 14 | const user: User = res.locals.oauth.token.User |
diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 045f41a96..d95e34bce 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts | |||
@@ -86,8 +86,6 @@ async function initDatabaseModels (silent: boolean) { | |||
86 | // Create custom PostgreSQL functions | 86 | // Create custom PostgreSQL functions |
87 | await createFunctions() | 87 | await createFunctions() |
88 | 88 | ||
89 | await sequelizeTypescript.query('CREATE EXTENSION IF NOT EXISTS pg_trgm', { raw: true }) | ||
90 | |||
91 | if (!silent) logger.info('Database %s is ready.', dbname) | 89 | if (!silent) logger.info('Database %s is ready.', dbname) |
92 | 90 | ||
93 | return | 91 | return |
diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts index fb2148eb3..a97f5b581 100644 --- a/server/middlewares/validators/search.ts +++ b/server/middlewares/validators/search.ts | |||
@@ -2,7 +2,7 @@ import * as express from 'express' | |||
2 | import { areValidationErrors } from './utils' | 2 | import { areValidationErrors } from './utils' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../helpers/logger' |
4 | import { query } from 'express-validator/check' | 4 | import { query } from 'express-validator/check' |
5 | import { isNumberArray, isStringArray } from '../../helpers/custom-validators/search' | 5 | import { isNumberArray, isStringArray, isNSFWQueryValid } from '../../helpers/custom-validators/search' |
6 | import { isBooleanValid, isDateValid, toArray } from '../../helpers/custom-validators/misc' | 6 | import { isBooleanValid, isDateValid, toArray } from '../../helpers/custom-validators/misc' |
7 | 7 | ||
8 | const searchValidator = [ | 8 | const searchValidator = [ |
@@ -46,8 +46,7 @@ const commonVideosFiltersValidator = [ | |||
46 | .custom(isStringArray).withMessage('Should have a valid all of tags array'), | 46 | .custom(isStringArray).withMessage('Should have a valid all of tags array'), |
47 | query('nsfw') | 47 | query('nsfw') |
48 | .optional() | 48 | .optional() |
49 | .toBoolean() | 49 | .custom(isNSFWQueryValid).withMessage('Should have a valid NSFW attribute'), |
50 | .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), | ||
51 | 50 | ||
52 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | 51 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
53 | logger.debug('Checking commons video filters query', { parameters: req.query }) | 52 | logger.debug('Checking commons video filters query', { parameters: req.query }) |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 68116e309..b97dfd96f 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -851,7 +851,22 @@ export class VideoModel extends Model<VideoModel> { | |||
851 | }) | 851 | }) |
852 | } | 852 | } |
853 | 853 | ||
854 | static async searchAndPopulateAccountAndServer (options: VideosSearchQuery) { | 854 | static async searchAndPopulateAccountAndServer (options: { |
855 | search: string | ||
856 | start?: number | ||
857 | count?: number | ||
858 | sort?: string | ||
859 | startDate?: string // ISO 8601 | ||
860 | endDate?: string // ISO 8601 | ||
861 | nsfw?: boolean | ||
862 | categoryOneOf?: number[] | ||
863 | licenceOneOf?: number[] | ||
864 | languageOneOf?: string[] | ||
865 | tagsOneOf?: string[] | ||
866 | tagsAllOf?: string[] | ||
867 | durationMin?: number // seconds | ||
868 | durationMax?: number // seconds | ||
869 | }) { | ||
855 | const whereAnd = [ ] | 870 | const whereAnd = [ ] |
856 | 871 | ||
857 | if (options.startDate || options.endDate) { | 872 | if (options.startDate || options.endDate) { |
diff --git a/server/tests/api/search/search-videos.ts b/server/tests/api/search/search-videos.ts index 7fc133b46..d2b0f0312 100644 --- a/server/tests/api/search/search-videos.ts +++ b/server/tests/api/search/search-videos.ts | |||
@@ -216,7 +216,7 @@ describe('Test a videos search', function () { | |||
216 | search: '1111 2222 3333', | 216 | search: '1111 2222 3333', |
217 | languageOneOf: [ 'pl', 'fr' ], | 217 | languageOneOf: [ 'pl', 'fr' ], |
218 | durationMax: 4, | 218 | durationMax: 4, |
219 | nsfw: false, | 219 | nsfw: 'false' as 'false', |
220 | licenceOneOf: [ 1, 4 ] | 220 | licenceOneOf: [ 1, 4 ] |
221 | } | 221 | } |
222 | 222 | ||
@@ -235,7 +235,7 @@ describe('Test a videos search', function () { | |||
235 | search: '1111 2222 3333', | 235 | search: '1111 2222 3333', |
236 | languageOneOf: [ 'pl', 'fr' ], | 236 | languageOneOf: [ 'pl', 'fr' ], |
237 | durationMax: 4, | 237 | durationMax: 4, |
238 | nsfw: false, | 238 | nsfw: 'false' as 'false', |
239 | licenceOneOf: [ 1, 4 ], | 239 | licenceOneOf: [ 1, 4 ], |
240 | sort: '-name' | 240 | sort: '-name' |
241 | } | 241 | } |
@@ -255,7 +255,7 @@ describe('Test a videos search', function () { | |||
255 | search: '1111 2222 3333', | 255 | search: '1111 2222 3333', |
256 | languageOneOf: [ 'pl', 'fr' ], | 256 | languageOneOf: [ 'pl', 'fr' ], |
257 | durationMax: 4, | 257 | durationMax: 4, |
258 | nsfw: false, | 258 | nsfw: 'false' as 'false', |
259 | licenceOneOf: [ 1, 4 ], | 259 | licenceOneOf: [ 1, 4 ], |
260 | sort: '-name', | 260 | sort: '-name', |
261 | start: 0, | 261 | start: 0, |
@@ -274,7 +274,7 @@ describe('Test a videos search', function () { | |||
274 | search: '1111 2222 3333', | 274 | search: '1111 2222 3333', |
275 | languageOneOf: [ 'pl', 'fr' ], | 275 | languageOneOf: [ 'pl', 'fr' ], |
276 | durationMax: 4, | 276 | durationMax: 4, |
277 | nsfw: false, | 277 | nsfw: 'false' as 'false', |
278 | licenceOneOf: [ 1, 4 ], | 278 | licenceOneOf: [ 1, 4 ], |
279 | sort: '-name', | 279 | sort: '-name', |
280 | start: 3, | 280 | start: 3, |
diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts index 38bdaa54e..370e69d2a 100644 --- a/server/tests/api/videos/video-nsfw.ts +++ b/server/tests/api/videos/video-nsfw.ts | |||
@@ -220,6 +220,17 @@ describe('Test video NSFW policy', function () { | |||
220 | expect(videos[ 0 ].name).to.equal('normal') | 220 | expect(videos[ 0 ].name).to.equal('normal') |
221 | } | 221 | } |
222 | }) | 222 | }) |
223 | |||
224 | it('Should display both videos when the nsfw param === both', async function () { | ||
225 | for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) { | ||
226 | expect(res.body.total).to.equal(2) | ||
227 | |||
228 | const videos = res.body.data | ||
229 | expect(videos).to.have.lengthOf(2) | ||
230 | expect(videos[ 0 ].name).to.equal('normal') | ||
231 | expect(videos[ 1 ].name).to.equal('nsfw') | ||
232 | } | ||
233 | }) | ||
223 | }) | 234 | }) |
224 | 235 | ||
225 | after(async function () { | 236 | after(async function () { |