X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-search%2Fadvanced-search.model.ts;h=29fe3e8dc9e7ed3b348cf48d6c23c1f8fb50af68;hb=52798aa5f277492d4dd2482bca9396d2e982fa19;hp=2c83f53b64c3a12d611b2a10ee2478b76f23fa83;hpb=ad35265d743e621d86f3f0796dd9d8795c599dca;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-search/advanced-search.model.ts b/client/src/app/shared/shared-search/advanced-search.model.ts index 2c83f53b6..29fe3e8dc 100644 --- a/client/src/app/shared/shared-search/advanced-search.model.ts +++ b/client/src/app/shared/shared-search/advanced-search.model.ts @@ -1,4 +1,14 @@ -import { BooleanBothQuery, BooleanQuery, SearchTargetType, VideosSearchQuery } from '@shared/models' +import { splitIntoArray } from '@app/helpers' +import { + BooleanBothQuery, + BooleanQuery, + SearchTargetType, + VideoChannelsSearchQuery, + VideoPlaylistsSearchQuery, + VideosSearchQuery +} from '@shared/models' + +export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels' export class AdvancedSearch { startDate: string // ISO 8601 @@ -23,12 +33,14 @@ export class AdvancedSearch { isLive: BooleanQuery + host: string + sort: string searchTarget: SearchTargetType + resultType: AdvancedSearchResultType - // Filters we don't want to count, because they are mandatory - private silentFilters = new Set([ 'sort', 'searchTarget' ]) + excludeAlreadyWatched?: boolean constructor (options?: { startDate?: string @@ -45,10 +57,15 @@ export class AdvancedSearch { isLive?: BooleanQuery + host?: string + durationMin?: string durationMax?: string sort?: string searchTarget?: SearchTargetType + resultType?: AdvancedSearchResultType + + excludeAlreadyWatched?: boolean }) { if (!options) return @@ -63,13 +80,23 @@ export class AdvancedSearch { this.categoryOneOf = options.categoryOneOf || undefined this.licenceOneOf = options.licenceOneOf || undefined this.languageOneOf = options.languageOneOf || undefined - this.tagsOneOf = this.intoArray(options.tagsOneOf) - this.tagsAllOf = this.intoArray(options.tagsAllOf) - this.durationMin = parseInt(options.durationMin, 10) - this.durationMax = parseInt(options.durationMax, 10) + this.tagsOneOf = splitIntoArray(options.tagsOneOf) + this.tagsAllOf = splitIntoArray(options.tagsAllOf) + this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined + this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined + + this.host = options.host || undefined this.searchTarget = options.searchTarget || undefined + this.resultType = options.resultType || undefined + + this.excludeAlreadyWatched = options.excludeAlreadyWatched || undefined + + if (!this.resultType && this.hasVideoFilter()) { + this.resultType = 'videos' + } + if (isNaN(this.durationMin)) this.durationMin = undefined if (isNaN(this.durationMax)) this.durationMax = undefined @@ -77,14 +104,7 @@ export class AdvancedSearch { } containsValues () { - const obj = this.toUrlObject() - for (const k of Object.keys(obj)) { - if (this.silentFilters.has(k)) continue - - if (this.isValidValue(obj[k])) return true - } - - return false + return this.size() !== 0 } reset () { @@ -101,6 +121,7 @@ export class AdvancedSearch { this.durationMin = undefined this.durationMax = undefined this.isLive = undefined + this.host = undefined this.sort = '-match' } @@ -120,12 +141,15 @@ export class AdvancedSearch { durationMin: this.durationMin, durationMax: this.durationMax, isLive: this.isLive, + host: this.host, sort: this.sort, - searchTarget: this.searchTarget + searchTarget: this.searchTarget, + resultType: this.resultType, + excludeAlreadyWatched: this.excludeAlreadyWatched } } - toAPIObject (): VideosSearchQuery { + toVideosAPIObject (): VideosSearchQuery { let isLive: boolean if (this.isLive) isLive = this.isLive === 'true' @@ -135,15 +159,31 @@ export class AdvancedSearch { originallyPublishedStartDate: this.originallyPublishedStartDate, originallyPublishedEndDate: this.originallyPublishedEndDate, nsfw: this.nsfw, - categoryOneOf: this.intoArray(this.categoryOneOf), - licenceOneOf: this.intoArray(this.licenceOneOf), - languageOneOf: this.intoArray(this.languageOneOf), + categoryOneOf: splitIntoArray(this.categoryOneOf), + licenceOneOf: splitIntoArray(this.licenceOneOf), + languageOneOf: splitIntoArray(this.languageOneOf), tagsOneOf: this.tagsOneOf, tagsAllOf: this.tagsAllOf, durationMin: this.durationMin, durationMax: this.durationMax, + host: this.host, isLive, sort: this.sort, + searchTarget: this.searchTarget, + excludeAlreadyWatched: this.excludeAlreadyWatched + } + } + + toPlaylistAPIObject (): VideoPlaylistsSearchQuery { + return { + host: this.host, + searchTarget: this.searchTarget + } + } + + toChannelAPIObject (): VideoChannelsSearchQuery { + return { + host: this.host, searchTarget: this.searchTarget } } @@ -151,12 +191,19 @@ export class AdvancedSearch { size () { let acc = 0 - const obj = this.toUrlObject() - for (const k of Object.keys(obj)) { - if (this.silentFilters.has(k)) continue + if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++ + if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) acc++ - if (this.isValidValue(obj[k])) acc++ - } + if (this.isValidValue(this.nsfw)) acc++ + if (this.isValidValue(this.categoryOneOf)) acc++ + if (this.isValidValue(this.licenceOneOf)) acc++ + if (this.isValidValue(this.languageOneOf)) acc++ + if (this.isValidValue(this.tagsOneOf)) acc++ + if (this.isValidValue(this.tagsAllOf)) acc++ + if (this.isValidValue(this.durationMin) || this.isValidValue(this.durationMax)) acc++ + if (this.isValidValue(this.isLive)) acc++ + if (this.isValidValue(this.host)) acc++ + if (this.isValidValue(this.resultType)) acc++ return acc } @@ -169,12 +216,19 @@ export class AdvancedSearch { return true } - private intoArray (value: any) { - if (!value) return undefined - if (Array.isArray(value)) return value - - if (typeof value === 'string') return value.split(',') - - return [ value ] + private hasVideoFilter () { + return this.startDate !== undefined || + this.endDate !== undefined || + this.originallyPublishedStartDate !== undefined || + this.originallyPublishedEndDate !== undefined || + this.nsfw !== undefined || + this.categoryOneOf !== undefined || + this.licenceOneOf !== undefined || + this.languageOneOf !== undefined || + this.tagsOneOf !== undefined || + this.tagsAllOf !== undefined || + this.durationMin !== undefined || + this.durationMax !== undefined || + this.isLive !== undefined } }