1
0
Fork 0
peertube/client/src/app/shared/shared-search/advanced-search.model.ts

227 lines
6.3 KiB
TypeScript
Raw Normal View History

2022-08-17 13:36:03 +00:00
import { splitIntoArray } from '@app/helpers'
import {
BooleanBothQuery,
BooleanQuery,
SearchTargetType,
VideoChannelsSearchQuery,
VideoPlaylistsSearchQuery,
VideosSearchQuery
} from '@shared/models'
2018-07-20 16:31:49 +00:00
2021-12-10 10:02:42 +00:00
export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels'
2018-07-20 16:31:49 +00:00
export class AdvancedSearch {
startDate: string // ISO 8601
endDate: string // ISO 8601
originallyPublishedStartDate: string // ISO 8601
originallyPublishedEndDate: string // ISO 8601
nsfw: BooleanBothQuery
2018-07-20 16:31:49 +00:00
categoryOneOf: string
licenceOneOf: string
languageOneOf: string
2020-08-11 07:22:42 +00:00
tagsOneOf: string[]
tagsAllOf: string[]
2018-07-20 16:31:49 +00:00
durationMin: number // seconds
durationMax: number // seconds
2021-05-05 10:10:00 +00:00
isLive: BooleanQuery
host: string
2018-07-24 08:13:54 +00:00
sort: string
2020-05-29 14:16:24 +00:00
searchTarget: SearchTargetType
2021-12-10 10:02:42 +00:00
resultType: AdvancedSearchResultType
2020-05-29 14:16:24 +00:00
2018-07-20 16:31:49 +00:00
constructor (options?: {
startDate?: string
endDate?: string
originallyPublishedStartDate?: string
originallyPublishedEndDate?: string
nsfw?: BooleanBothQuery
2018-07-20 16:31:49 +00:00
categoryOneOf?: string
licenceOneOf?: string
languageOneOf?: string
2020-08-11 07:22:42 +00:00
tagsOneOf?: any
tagsAllOf?: any
2021-05-05 10:10:00 +00:00
isLive?: BooleanQuery
host?: string
2018-07-20 16:31:49 +00:00
durationMin?: string
durationMax?: string
2018-07-24 08:13:54 +00:00
sort?: string
2020-05-29 14:16:24 +00:00
searchTarget?: SearchTargetType
2021-12-10 10:02:42 +00:00
resultType?: AdvancedSearchResultType
2018-07-20 16:31:49 +00:00
}) {
if (!options) return
this.startDate = options.startDate || undefined
this.endDate = options.endDate || undefined
this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
this.nsfw = options.nsfw || undefined
2021-05-05 10:10:00 +00:00
this.isLive = options.isLive || undefined
this.categoryOneOf = options.categoryOneOf || undefined
this.licenceOneOf = options.licenceOneOf || undefined
this.languageOneOf = options.languageOneOf || undefined
2022-08-17 13:36:03 +00:00
this.tagsOneOf = splitIntoArray(options.tagsOneOf)
this.tagsAllOf = splitIntoArray(options.tagsAllOf)
2021-12-17 13:41:52 +00:00
this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined
this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined
2018-07-20 16:31:49 +00:00
this.host = options.host || undefined
2020-05-29 14:16:24 +00:00
this.searchTarget = options.searchTarget || undefined
2021-12-10 10:02:42 +00:00
this.resultType = options.resultType || undefined
if (!this.resultType && this.hasVideoFilter()) {
this.resultType = 'videos'
}
2018-07-20 16:31:49 +00:00
if (isNaN(this.durationMin)) this.durationMin = undefined
if (isNaN(this.durationMax)) this.durationMax = undefined
2018-07-24 08:13:54 +00:00
this.sort = options.sort || '-match'
2018-07-20 16:31:49 +00:00
}
containsValues () {
2022-06-10 07:40:20 +00:00
return this.size() !== 0
2018-07-20 16:31:49 +00:00
}
reset () {
this.startDate = undefined
this.endDate = undefined
this.originallyPublishedStartDate = undefined
this.originallyPublishedEndDate = undefined
2018-07-20 16:31:49 +00:00
this.nsfw = undefined
this.categoryOneOf = undefined
this.licenceOneOf = undefined
this.languageOneOf = undefined
this.tagsOneOf = undefined
this.tagsAllOf = undefined
this.durationMin = undefined
this.durationMax = undefined
2021-05-05 10:10:00 +00:00
this.isLive = undefined
this.host = undefined
2018-07-24 08:13:54 +00:00
this.sort = '-match'
2018-07-20 16:31:49 +00:00
}
toUrlObject () {
return {
startDate: this.startDate,
endDate: this.endDate,
originallyPublishedStartDate: this.originallyPublishedStartDate,
originallyPublishedEndDate: this.originallyPublishedEndDate,
2018-07-20 16:31:49 +00:00
nsfw: this.nsfw,
categoryOneOf: this.categoryOneOf,
licenceOneOf: this.licenceOneOf,
languageOneOf: this.languageOneOf,
tagsOneOf: this.tagsOneOf,
tagsAllOf: this.tagsAllOf,
durationMin: this.durationMin,
2018-07-24 08:13:54 +00:00
durationMax: this.durationMax,
2021-05-05 10:10:00 +00:00
isLive: this.isLive,
host: this.host,
2020-05-29 14:16:24 +00:00
sort: this.sort,
2021-12-10 10:02:42 +00:00
searchTarget: this.searchTarget,
resultType: this.resultType
2018-07-20 16:31:49 +00:00
}
}
toVideosAPIObject (): VideosSearchQuery {
2021-05-05 10:10:00 +00:00
let isLive: boolean
if (this.isLive) isLive = this.isLive === 'true'
2018-07-20 16:31:49 +00:00
return {
startDate: this.startDate,
endDate: this.endDate,
originallyPublishedStartDate: this.originallyPublishedStartDate,
originallyPublishedEndDate: this.originallyPublishedEndDate,
2018-07-20 16:31:49 +00:00
nsfw: this.nsfw,
2022-08-17 13:36:03 +00:00
categoryOneOf: splitIntoArray(this.categoryOneOf),
licenceOneOf: splitIntoArray(this.licenceOneOf),
languageOneOf: splitIntoArray(this.languageOneOf),
2020-08-11 07:22:42 +00:00
tagsOneOf: this.tagsOneOf,
tagsAllOf: this.tagsAllOf,
2018-07-20 16:31:49 +00:00
durationMin: this.durationMin,
2018-07-24 08:13:54 +00:00
durationMax: this.durationMax,
host: this.host,
2021-05-05 10:10:00 +00:00
isLive,
2020-05-29 14:16:24 +00:00
sort: this.sort,
searchTarget: this.searchTarget
2018-07-20 16:31:49 +00:00
}
}
toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
return {
host: this.host,
searchTarget: this.searchTarget
}
}
toChannelAPIObject (): VideoChannelsSearchQuery {
return {
host: this.host,
searchTarget: this.searchTarget
}
}
size () {
let acc = 0
2022-06-10 07:40:20 +00:00
if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++
if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) 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
}
2020-08-11 07:22:42 +00:00
private isValidValue (val: any) {
if (val === undefined) return false
if (val === '') return false
if (Array.isArray(val) && val.length === 0) return false
return true
}
2021-12-10 10:02:42 +00:00
private hasVideoFilter () {
return this.startDate !== undefined ||
this.endDate !== undefined ||
this.originallyPublishedStartDate !== undefined ||
this.originallyPublishedEndDate !== undefined ||
2021-12-17 13:41:52 +00:00
this.nsfw !== undefined ||
2021-12-10 10:02:42 +00:00
this.categoryOneOf !== undefined ||
this.licenceOneOf !== undefined ||
this.languageOneOf !== undefined ||
this.tagsOneOf !== undefined ||
this.tagsAllOf !== undefined ||
this.durationMin !== undefined ||
this.durationMax !== undefined ||
this.isLive !== undefined
}
2018-07-20 16:31:49 +00:00
}