1 import { NSFWQuery, SearchTargetType } from '@shared/models'
3 export class AdvancedSearch {
4 startDate: string // ISO 8601
5 endDate: string // ISO 8601
7 originallyPublishedStartDate: string // ISO 8601
8 originallyPublishedEndDate: string // ISO 8601
21 durationMin: number // seconds
22 durationMax: number // seconds
26 searchTarget: SearchTargetType
28 // Filters we don't want to count, because they are mandatory
29 private silentFilters = new Set([ 'sort', 'searchTarget' ])
31 constructor (options?: {
34 originallyPublishedStartDate?: string
35 originallyPublishedEndDate?: string
37 categoryOneOf?: string
39 languageOneOf?: string
47 searchTarget?: SearchTargetType
51 this.startDate = options.startDate || undefined
52 this.endDate = options.endDate || undefined
53 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
54 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
56 this.nsfw = options.nsfw || undefined
57 this.categoryOneOf = options.categoryOneOf || undefined
58 this.licenceOneOf = options.licenceOneOf || undefined
59 this.languageOneOf = options.languageOneOf || undefined
60 this.tagsOneOf = this.intoArray(options.tagsOneOf)
61 this.tagsAllOf = this.intoArray(options.tagsAllOf)
62 this.durationMin = parseInt(options.durationMin, 10)
63 this.durationMax = parseInt(options.durationMax, 10)
65 this.searchTarget = options.searchTarget || undefined
67 if (isNaN(this.durationMin)) this.durationMin = undefined
68 if (isNaN(this.durationMax)) this.durationMax = undefined
70 this.sort = options.sort || '-match'
74 const obj = this.toUrlObject()
75 for (const k of Object.keys(obj)) {
76 if (this.silentFilters.has(k)) continue
78 if (this.isValidValue(obj[k])) return true
85 this.startDate = undefined
86 this.endDate = undefined
87 this.originallyPublishedStartDate = undefined
88 this.originallyPublishedEndDate = undefined
90 this.categoryOneOf = undefined
91 this.licenceOneOf = undefined
92 this.languageOneOf = undefined
93 this.tagsOneOf = undefined
94 this.tagsAllOf = undefined
95 this.durationMin = undefined
96 this.durationMax = undefined
103 startDate: this.startDate,
104 endDate: this.endDate,
105 originallyPublishedStartDate: this.originallyPublishedStartDate,
106 originallyPublishedEndDate: this.originallyPublishedEndDate,
108 categoryOneOf: this.categoryOneOf,
109 licenceOneOf: this.licenceOneOf,
110 languageOneOf: this.languageOneOf,
111 tagsOneOf: this.tagsOneOf,
112 tagsAllOf: this.tagsAllOf,
113 durationMin: this.durationMin,
114 durationMax: this.durationMax,
116 searchTarget: this.searchTarget
122 startDate: this.startDate,
123 endDate: this.endDate,
124 originallyPublishedStartDate: this.originallyPublishedStartDate,
125 originallyPublishedEndDate: this.originallyPublishedEndDate,
127 categoryOneOf: this.intoArray(this.categoryOneOf),
128 licenceOneOf: this.intoArray(this.licenceOneOf),
129 languageOneOf: this.intoArray(this.languageOneOf),
130 tagsOneOf: this.tagsOneOf,
131 tagsAllOf: this.tagsAllOf,
132 durationMin: this.durationMin,
133 durationMax: this.durationMax,
135 searchTarget: this.searchTarget
142 const obj = this.toUrlObject()
143 for (const k of Object.keys(obj)) {
144 if (this.silentFilters.has(k)) continue
146 if (this.isValidValue(obj[k])) acc++
152 private isValidValue (val: any) {
153 if (val === undefined) return false
154 if (val === '') return false
155 if (Array.isArray(val) && val.length === 0) return false
160 private intoArray (value: any) {
161 if (!value) return undefined
162 if (Array.isArray(value)) return value
164 if (typeof value === 'string') return value.split(',')