diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-29 11:54:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-29 11:54:38 +0200 |
commit | d6886027109af42be2e3ec5d14ad166199add11d (patch) | |
tree | 3a7603d36ff35a2d623d863f14ab93a2e1e6b7f2 /server/controllers/api/search/search-videos.ts | |
parent | b033851fb54241bb703f86add025229e68cc6f59 (diff) | |
download | PeerTube-d6886027109af42be2e3ec5d14ad166199add11d.tar.gz PeerTube-d6886027109af42be2e3ec5d14ad166199add11d.tar.zst PeerTube-d6886027109af42be2e3ec5d14ad166199add11d.zip |
Refactor search query options
Diffstat (limited to 'server/controllers/api/search/search-videos.ts')
-rw-r--r-- | server/controllers/api/search/search-videos.ts | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/server/controllers/api/search/search-videos.ts b/server/controllers/api/search/search-videos.ts index a4153f3f8..4a6ce0de4 100644 --- a/server/controllers/api/search/search-videos.ts +++ b/server/controllers/api/search/search-videos.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { sanitizeUrl } from '@server/helpers/core-utils' | 2 | import { sanitizeUrl } from '@server/helpers/core-utils' |
3 | import { pickSearchVideoQuery } from '@server/helpers/query' | ||
3 | import { doJSONRequest } from '@server/helpers/requests' | 4 | import { doJSONRequest } from '@server/helpers/requests' |
4 | import { CONFIG } from '@server/initializers/config' | 5 | import { CONFIG } from '@server/initializers/config' |
5 | import { WEBSERVER } from '@server/initializers/constants' | 6 | import { WEBSERVER } from '@server/initializers/constants' |
@@ -7,7 +8,7 @@ import { getOrCreateAPVideo } from '@server/lib/activitypub/videos' | |||
7 | import { Hooks } from '@server/lib/plugins/hooks' | 8 | import { Hooks } from '@server/lib/plugins/hooks' |
8 | import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search' | 9 | import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search' |
9 | import { HttpStatusCode, ResultList, Video } from '@shared/models' | 10 | import { HttpStatusCode, ResultList, Video } from '@shared/models' |
10 | import { VideosSearchQuery } from '../../../../shared/models/search' | 11 | import { VideosSearchQueryAfterSanitize } from '../../../../shared/models/search' |
11 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils' | 12 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils' |
12 | import { logger } from '../../../helpers/logger' | 13 | import { logger } from '../../../helpers/logger' |
13 | import { getFormattedObjects } from '../../../helpers/utils' | 14 | import { getFormattedObjects } from '../../../helpers/utils' |
@@ -46,7 +47,7 @@ export { searchVideosRouter } | |||
46 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
47 | 48 | ||
48 | function searchVideos (req: express.Request, res: express.Response) { | 49 | function searchVideos (req: express.Request, res: express.Response) { |
49 | const query: VideosSearchQuery = req.query | 50 | const query = pickSearchVideoQuery(req.query) |
50 | const search = query.search | 51 | const search = query.search |
51 | 52 | ||
52 | if (isURISearch(search)) { | 53 | if (isURISearch(search)) { |
@@ -60,10 +61,10 @@ function searchVideos (req: express.Request, res: express.Response) { | |||
60 | return searchVideosDB(query, res) | 61 | return searchVideosDB(query, res) |
61 | } | 62 | } |
62 | 63 | ||
63 | async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) { | 64 | async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) { |
64 | const result = await buildMutedForSearchIndex(res) | 65 | const result = await buildMutedForSearchIndex(res) |
65 | 66 | ||
66 | let body: VideosSearchQuery = Object.assign(query, result) | 67 | let body = { ...query, ...result } |
67 | 68 | ||
68 | // Use the default instance NSFW policy if not specified | 69 | // Use the default instance NSFW policy if not specified |
69 | if (!body.nsfw) { | 70 | if (!body.nsfw) { |
@@ -97,13 +98,18 @@ async function searchVideosIndex (query: VideosSearchQuery, res: express.Respons | |||
97 | } | 98 | } |
98 | } | 99 | } |
99 | 100 | ||
100 | async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { | 101 | async function searchVideosDB (query: VideosSearchQueryAfterSanitize, res: express.Response) { |
101 | const apiOptions = await Hooks.wrapObject(Object.assign(query, { | 102 | const apiOptions = await Hooks.wrapObject({ |
103 | ...query, | ||
104 | |||
102 | includeLocalVideos: true, | 105 | includeLocalVideos: true, |
103 | nsfw: buildNSFWFilter(res, query.nsfw), | ||
104 | filter: query.filter, | 106 | filter: query.filter, |
105 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined | 107 | |
106 | }), 'filter:api.search.videos.local.list.params') | 108 | nsfw: buildNSFWFilter(res, query.nsfw), |
109 | user: res.locals.oauth | ||
110 | ? res.locals.oauth.token.User | ||
111 | : undefined | ||
112 | }, 'filter:api.search.videos.local.list.params') | ||
107 | 113 | ||
108 | const resultList = await Hooks.wrapPromiseFun( | 114 | const resultList = await Hooks.wrapPromiseFun( |
109 | VideoModel.searchAndPopulateAccountAndServer, | 115 | VideoModel.searchAndPopulateAccountAndServer, |