From af971e06c620bd46a5aa64c8833364e7022b5e3d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 15:22:51 +0200 Subject: Introduce search command --- shared/extra-utils/index.ts | 4 +- shared/extra-utils/overviews/overviews-command.ts | 1 - shared/extra-utils/search/index.ts | 1 + shared/extra-utils/search/search-command.ts | 101 ++++++++++++++++++++++ shared/extra-utils/search/video-channels.ts | 36 -------- shared/extra-utils/search/video-playlists.ts | 36 -------- shared/extra-utils/search/videos.ts | 64 -------------- shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/videos/videos.ts | 8 +- 9 files changed, 110 insertions(+), 144 deletions(-) create mode 100644 shared/extra-utils/search/index.ts create mode 100644 shared/extra-utils/search/search-command.ts delete mode 100644 shared/extra-utils/search/video-channels.ts delete mode 100644 shared/extra-utils/search/video-playlists.ts delete mode 100644 shared/extra-utils/search/videos.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 38c8c4c1c..067e6fb65 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -11,9 +11,7 @@ export * from './overviews' export * from './requests/check-api-params' export * from './requests/requests' -export * from './search/video-channels' -export * from './search/video-playlists' -export * from './search/videos' +export * from './search' export * from './server/activitypub' export * from './server/clients' diff --git a/shared/extra-utils/overviews/overviews-command.ts b/shared/extra-utils/overviews/overviews-command.ts index 0ac3cbd33..6f56b0ce4 100644 --- a/shared/extra-utils/overviews/overviews-command.ts +++ b/shared/extra-utils/overviews/overviews-command.ts @@ -6,7 +6,6 @@ export class OverviewsCommand extends AbstractCommand { getVideos (options: OverrideCommandOptions & { page: number - token?: string }) { const { token, page } = options const path = '/api/v1/overviews/videos' diff --git a/shared/extra-utils/search/index.ts b/shared/extra-utils/search/index.ts new file mode 100644 index 000000000..48dbe8ae9 --- /dev/null +++ b/shared/extra-utils/search/index.ts @@ -0,0 +1 @@ +export * from './search-command' diff --git a/shared/extra-utils/search/search-command.ts b/shared/extra-utils/search/search-command.ts new file mode 100644 index 000000000..d4cfab32b --- /dev/null +++ b/shared/extra-utils/search/search-command.ts @@ -0,0 +1,101 @@ +import { + ResultList, + Video, + VideoChannel, + VideoChannelsSearchQuery, + VideoPlaylist, + VideoPlaylistsSearchQuery, + VideosSearchQuery +} from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class SearchCommand extends AbstractCommand { + + searchChannels (options: OverrideCommandOptions & { + search: string + }) { + return this.advancedChannelSearch({ + ...options, + + search: { search: options.search } + }) + } + + advancedChannelSearch (options: OverrideCommandOptions & { + search: VideoChannelsSearchQuery + }) { + const { search, token } = options + const path = '/api/v1/search/video-channels' + + return this.getRequestBody>({ + ...options, + + token: token || null, + + path, + query: search, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + searchPlaylists (options: OverrideCommandOptions & { + search: string + }) { + return this.advancedPlaylistSearch({ + ...options, + + search: { search: options.search } + }) + } + + advancedPlaylistSearch (options: OverrideCommandOptions & { + search: VideoPlaylistsSearchQuery + }) { + const { search, token } = options + const path = '/api/v1/search/video-playlists' + + return this.getRequestBody>({ + ...options, + + token: token || null, + + path, + query: search, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + searchVideos (options: OverrideCommandOptions & { + search: string + sort?: string + }) { + const { search, sort } = options + + return this.advancedVideoSearch({ + ...options, + + search: { + search: search, + sort: sort ?? '-publishedAt' + } + }) + } + + advancedVideoSearch (options: OverrideCommandOptions & { + search: VideosSearchQuery + }) { + const { search, token } = options + const path = '/api/v1/search/videos' + + return this.getRequestBody>({ + ...options, + + token: token || null, + + path, + query: search, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } +} diff --git a/shared/extra-utils/search/video-channels.ts b/shared/extra-utils/search/video-channels.ts deleted file mode 100644 index 8e0f42578..000000000 --- a/shared/extra-utils/search/video-channels.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { VideoChannelsSearchQuery } from '@shared/models' -import { makeGetRequest } from '../requests/requests' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function searchVideoChannel (url: string, search: string, token?: string, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/api/v1/search/video-channels' - - return makeGetRequest({ - url, - path, - query: { - sort: '-createdAt', - search - }, - token, - statusCodeExpected - }) -} - -function advancedVideoChannelSearch (url: string, search: VideoChannelsSearchQuery) { - const path = '/api/v1/search/video-channels' - - return makeGetRequest({ - url, - path, - query: search, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -// --------------------------------------------------------------------------- - -export { - searchVideoChannel, - advancedVideoChannelSearch -} diff --git a/shared/extra-utils/search/video-playlists.ts b/shared/extra-utils/search/video-playlists.ts deleted file mode 100644 index c22831df7..000000000 --- a/shared/extra-utils/search/video-playlists.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { VideoPlaylistsSearchQuery } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' -import { makeGetRequest } from '../requests/requests' - -function searchVideoPlaylists (url: string, search: string, token?: string, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/api/v1/search/video-playlists' - - return makeGetRequest({ - url, - path, - query: { - sort: '-createdAt', - search - }, - token, - statusCodeExpected - }) -} - -function advancedVideoPlaylistSearch (url: string, search: VideoPlaylistsSearchQuery) { - const path = '/api/v1/search/video-playlists' - - return makeGetRequest({ - url, - path, - query: search, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -// --------------------------------------------------------------------------- - -export { - searchVideoPlaylists, - advancedVideoPlaylistSearch -} diff --git a/shared/extra-utils/search/videos.ts b/shared/extra-utils/search/videos.ts deleted file mode 100644 index db6edbd58..000000000 --- a/shared/extra-utils/search/videos.ts +++ /dev/null @@ -1,64 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ - -import * as request from 'supertest' -import { VideosSearchQuery } from '../../models/search' -import { immutableAssign } from '../miscs/miscs' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function searchVideo (url: string, search: string, sort = '-publishedAt') { - const path = '/api/v1/search/videos' - - const query = { sort, search: search } - const req = request(url) - .get(path) - .query(query) - .set('Accept', 'application/json') - - return req.expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function searchVideoWithToken (url: string, search: string, token: string, query: { nsfw?: boolean } = {}) { - const path = '/api/v1/search/videos' - const req = request(url) - .get(path) - .set('Authorization', 'Bearer ' + token) - .query(immutableAssign(query, { sort: '-publishedAt', search })) - .set('Accept', 'application/json') - - return req.expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function searchVideoWithSort (url: string, search: string, sort: string) { - const path = '/api/v1/search/videos' - - const query = { search, sort } - - return request(url) - .get(path) - .query(query) - .set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function advancedVideosSearch (url: string, options: VideosSearchQuery) { - const path = '/api/v1/search/videos' - - return request(url) - .get(path) - .query(options) - .set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -// --------------------------------------------------------------------------- - -export { - searchVideo, - advancedVideosSearch, - searchVideoWithToken, - searchVideoWithSort -} diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 70be96cf6..8ccf790fc 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -15,6 +15,7 @@ import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../mi import { AbusesCommand } from '../moderation' import { OverviewsCommand } from '../overviews' import { makeGetRequest } from '../requests/requests' +import { SearchCommand } from '../search' interface ServerInfo { app: ChildProcess @@ -75,6 +76,7 @@ interface ServerInfo { logsCommand?: LogsCommand abusesCommand?: AbusesCommand overviewsCommand?: OverviewsCommand + searchCommand?: SearchCommand } function parallelTests () { @@ -287,6 +289,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.logsCommand = new LogsCommand(server) server.abusesCommand = new AbusesCommand(server) server.overviewsCommand = new OverviewsCommand(server) + server.searchCommand = new SearchCommand(server) res(server) }) diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index 469ea4d63..a45c0402a 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -10,7 +10,7 @@ import validator from 'validator' import { getLowercaseExtension } from '@server/helpers/core-utils' import { buildUUID } from '@server/helpers/uuid' import { HttpStatusCode } from '@shared/core-utils' -import { VideosCommonQuery } from '@shared/models' +import { BooleanBothQuery, VideosCommonQuery } from '@shared/models' import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' import { VideoDetails, VideoPrivacy } from '../../models/videos' import { @@ -159,7 +159,7 @@ function getVideosList (url: string) { .expect('Content-Type', /json/) } -function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) { +function getVideosListWithToken (url: string, token: string, query: { nsfw?: BooleanBothQuery } = {}) { const path = '/api/v1/videos' return request(url) @@ -219,7 +219,7 @@ function getAccountVideos ( count: number, sort?: string, query: { - nsfw?: boolean + nsfw?: BooleanBothQuery search?: string } = {} ) { @@ -245,7 +245,7 @@ function getVideoChannelVideos ( start: number, count: number, sort?: string, - query: { nsfw?: boolean } = {} + query: { nsfw?: BooleanBothQuery } = {} ) { const path = '/api/v1/video-channels/' + videoChannelName + '/videos' -- cgit v1.2.3