2020-06-23 12:10:17 +00:00
|
|
|
import { NSFWQuery, SearchTargetType } from '@shared/models'
|
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
|
|
|
|
|
2018-07-20 16:31:49 +00:00
|
|
|
nsfw: NSFWQuery
|
|
|
|
|
|
|
|
categoryOneOf: string
|
|
|
|
|
|
|
|
licenceOneOf: string
|
|
|
|
|
|
|
|
languageOneOf: string
|
|
|
|
|
|
|
|
tagsOneOf: string
|
|
|
|
tagsAllOf: string
|
|
|
|
|
|
|
|
durationMin: number // seconds
|
|
|
|
durationMax: number // seconds
|
|
|
|
|
2018-07-24 08:13:54 +00:00
|
|
|
sort: string
|
|
|
|
|
2020-05-29 14:16:24 +00:00
|
|
|
searchTarget: SearchTargetType
|
|
|
|
|
|
|
|
// 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
|
2018-07-20 16:31:49 +00:00
|
|
|
nsfw?: NSFWQuery
|
|
|
|
categoryOneOf?: string
|
|
|
|
licenceOneOf?: string
|
|
|
|
languageOneOf?: string
|
|
|
|
tagsOneOf?: string
|
|
|
|
tagsAllOf?: string
|
|
|
|
durationMin?: string
|
|
|
|
durationMax?: string
|
2018-07-24 08:13:54 +00:00
|
|
|
sort?: string
|
2020-05-29 14:16:24 +00:00
|
|
|
searchTarget?: SearchTargetType
|
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
|
|
|
|
this.categoryOneOf = options.categoryOneOf || undefined
|
|
|
|
this.licenceOneOf = options.licenceOneOf || undefined
|
|
|
|
this.languageOneOf = options.languageOneOf || undefined
|
|
|
|
this.tagsOneOf = options.tagsOneOf || undefined
|
|
|
|
this.tagsAllOf = options.tagsAllOf || undefined
|
2018-07-20 16:31:49 +00:00
|
|
|
this.durationMin = parseInt(options.durationMin, 10)
|
|
|
|
this.durationMax = parseInt(options.durationMax, 10)
|
|
|
|
|
2020-05-29 14:16:24 +00:00
|
|
|
this.searchTarget = options.searchTarget || undefined
|
|
|
|
|
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 () {
|
2020-05-29 14:16:24 +00:00
|
|
|
const exceptions = new Set([ 'sort', 'searchTarget' ])
|
|
|
|
|
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
|
|
|
|
2019-12-04 16:12:23 +00:00
|
|
|
if (obj[k] !== undefined && 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
|
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,
|
2020-05-29 14:16:24 +00:00
|
|
|
sort: this.sort,
|
|
|
|
searchTarget: this.searchTarget
|
2018-07-20 16:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toAPIObject () {
|
|
|
|
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,
|
2018-07-24 09:40:04 +00:00
|
|
|
categoryOneOf: this.intoArray(this.categoryOneOf),
|
|
|
|
licenceOneOf: this.intoArray(this.licenceOneOf),
|
|
|
|
languageOneOf: this.intoArray(this.languageOneOf),
|
|
|
|
tagsOneOf: this.intoArray(this.tagsOneOf),
|
|
|
|
tagsAllOf: this.intoArray(this.tagsAllOf),
|
2018-07-20 16:31:49 +00:00
|
|
|
durationMin: this.durationMin,
|
2018-07-24 08:13:54 +00:00
|
|
|
durationMax: this.durationMax,
|
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
|
|
|
|
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
|
|
|
|
2019-12-04 16:12:23 +00:00
|
|
|
if (obj[k] !== undefined && obj[k] !== '') acc++
|
2018-10-01 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:40:04 +00:00
|
|
|
private intoArray (value: any) {
|
|
|
|
if (!value) return undefined
|
2019-10-21 11:31:58 +00:00
|
|
|
if (Array.isArray(value)) return value
|
2018-07-24 09:40:04 +00:00
|
|
|
|
|
|
|
if (typeof value === 'string') return value.split(',')
|
|
|
|
|
|
|
|
return [ value ]
|
|
|
|
}
|
2018-07-20 16:31:49 +00:00
|
|
|
}
|