2021-08-19 07:24:29 +00:00
|
|
|
import { intoArray } from '@app/helpers'
|
2021-07-29 13:19:22 +00:00
|
|
|
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
|
|
|
|
|
2018-11-27 17:57:02 +00:00
|
|
|
originallyPublishedStartDate: string // ISO 8601
|
|
|
|
originallyPublishedEndDate: string // ISO 8601
|
|
|
|
|
2021-05-03 09:06:19 +00:00
|
|
|
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
|
|
|
|
|
2021-07-29 13:19:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Filters we don't want to count, because they are mandatory
|
|
|
|
private silentFilters = new Set([ 'sort', 'searchTarget' ])
|
|
|
|
|
2018-07-20 16:31:49 +00:00
|
|
|
constructor (options?: {
|
|
|
|
startDate?: string
|
|
|
|
endDate?: string
|
2018-11-27 17:57:02 +00:00
|
|
|
originallyPublishedStartDate?: string
|
|
|
|
originallyPublishedEndDate?: string
|
2021-05-03 09:06:19 +00:00
|
|
|
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
|
|
|
|
|
2021-07-29 13:19:22 +00:00
|
|
|
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
|
|
|
|
|
2018-07-23 09:12:03 +00:00
|
|
|
this.startDate = options.startDate || undefined
|
|
|
|
this.endDate = options.endDate || undefined
|
2018-11-27 17:57:02 +00:00
|
|
|
this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
|
|
|
|
this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
|
|
|
|
|
2018-07-23 09:12:03 +00:00
|
|
|
this.nsfw = options.nsfw || undefined
|
2021-05-05 10:10:00 +00:00
|
|
|
this.isLive = options.isLive || undefined
|
|
|
|
|
2018-07-23 09:12:03 +00:00
|
|
|
this.categoryOneOf = options.categoryOneOf || undefined
|
|
|
|
this.licenceOneOf = options.licenceOneOf || undefined
|
|
|
|
this.languageOneOf = options.languageOneOf || undefined
|
2021-08-19 07:24:29 +00:00
|
|
|
this.tagsOneOf = intoArray(options.tagsOneOf)
|
|
|
|
this.tagsAllOf = intoArray(options.tagsAllOf)
|
2018-07-20 16:31:49 +00:00
|
|
|
this.durationMin = parseInt(options.durationMin, 10)
|
|
|
|
this.durationMax = parseInt(options.durationMax, 10)
|
|
|
|
|
2021-07-29 13:19:22 +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 () {
|
2018-10-18 12:35:31 +00:00
|
|
|
const obj = this.toUrlObject()
|
2018-07-20 16:31:49 +00:00
|
|
|
for (const k of Object.keys(obj)) {
|
2020-05-29 14:16:24 +00:00
|
|
|
if (this.silentFilters.has(k)) continue
|
2018-07-24 08:13:54 +00:00
|
|
|
|
2020-08-11 07:22:42 +00:00
|
|
|
if (this.isValidValue(obj[k])) return true
|
2018-07-20 16:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
reset () {
|
|
|
|
this.startDate = undefined
|
|
|
|
this.endDate = undefined
|
2018-11-27 17:57:02 +00:00
|
|
|
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
|
2021-07-29 13:19:22 +00:00
|
|
|
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,
|
2018-11-27 17:57:02 +00:00
|
|
|
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,
|
2021-07-29 13:19:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:19:22 +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,
|
2018-11-27 17:57:02 +00:00
|
|
|
originallyPublishedStartDate: this.originallyPublishedStartDate,
|
|
|
|
originallyPublishedEndDate: this.originallyPublishedEndDate,
|
2018-07-20 16:31:49 +00:00
|
|
|
nsfw: this.nsfw,
|
2021-08-19 07:24:29 +00:00
|
|
|
categoryOneOf: intoArray(this.categoryOneOf),
|
|
|
|
licenceOneOf: intoArray(this.licenceOneOf),
|
|
|
|
languageOneOf: intoArray(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,
|
2021-07-29 13:19:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-07-24 09:40:04 +00:00
|
|
|
|
2021-07-29 13:19:22 +00:00
|
|
|
toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
|
|
|
|
return {
|
|
|
|
host: this.host,
|
|
|
|
searchTarget: this.searchTarget
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toChannelAPIObject (): VideoChannelsSearchQuery {
|
|
|
|
return {
|
|
|
|
host: this.host,
|
|
|
|
searchTarget: this.searchTarget
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:21:03 +00:00
|
|
|
size () {
|
|
|
|
let acc = 0
|
|
|
|
|
2018-10-18 12:35:31 +00:00
|
|
|
const obj = this.toUrlObject()
|
2018-10-01 15:21:03 +00:00
|
|
|
for (const k of Object.keys(obj)) {
|
2020-05-29 14:16:24 +00:00
|
|
|
if (this.silentFilters.has(k)) continue
|
2018-10-01 15:21:03 +00:00
|
|
|
|
2020-08-11 07:22:42 +00:00
|
|
|
if (this.isValidValue(obj[k])) acc++
|
2018-10-01 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ||
|
|
|
|
this.nsfw !== undefined !== undefined ||
|
|
|
|
this.categoryOneOf !== undefined ||
|
|
|
|
this.licenceOneOf !== undefined ||
|
|
|
|
this.languageOneOf !== undefined ||
|
|
|
|
this.tagsOneOf !== undefined ||
|
|
|
|
this.tagsAllOf !== undefined ||
|
|
|
|
this.durationMin !== undefined ||
|
|
|
|
this.durationMax !== undefined ||
|
|
|
|
this.host !== undefined ||
|
|
|
|
this.isLive !== undefined
|
|
|
|
}
|
2018-07-20 16:31:49 +00:00
|
|
|
}
|