Update Angular -> 8.2.0
[oweals/peertube.git] / client / src / app / search / advanced-search.model.ts
1 import { NSFWQuery } from '../../../../shared/models/search'
2
3 export class AdvancedSearch {
4   startDate: string // ISO 8601
5   endDate: string // ISO 8601
6
7   originallyPublishedStartDate: string // ISO 8601
8   originallyPublishedEndDate: string // ISO 8601
9
10   nsfw: NSFWQuery
11
12   categoryOneOf: string
13
14   licenceOneOf: string
15
16   languageOneOf: string
17
18   tagsOneOf: string
19   tagsAllOf: string
20
21   durationMin: number // seconds
22   durationMax: number // seconds
23
24   sort: string
25
26   constructor (options?: {
27     startDate?: string
28     endDate?: string
29     originallyPublishedStartDate?: string
30     originallyPublishedEndDate?: string
31     nsfw?: NSFWQuery
32     categoryOneOf?: string
33     licenceOneOf?: string
34     languageOneOf?: string
35     tagsOneOf?: string
36     tagsAllOf?: string
37     durationMin?: string
38     durationMax?: string
39     sort?: string
40   }) {
41     if (!options) return
42
43     this.startDate = options.startDate || undefined
44     this.endDate = options.endDate || undefined
45     this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
46     this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
47
48     this.nsfw = options.nsfw || undefined
49     this.categoryOneOf = options.categoryOneOf || undefined
50     this.licenceOneOf = options.licenceOneOf || undefined
51     this.languageOneOf = options.languageOneOf || undefined
52     this.tagsOneOf = options.tagsOneOf || undefined
53     this.tagsAllOf = options.tagsAllOf || undefined
54     this.durationMin = parseInt(options.durationMin, 10)
55     this.durationMax = parseInt(options.durationMax, 10)
56
57     if (isNaN(this.durationMin)) this.durationMin = undefined
58     if (isNaN(this.durationMax)) this.durationMax = undefined
59
60     this.sort = options.sort || '-match'
61   }
62
63   containsValues () {
64     const obj = this.toUrlObject()
65     for (const k of Object.keys(obj)) {
66       if (k === 'sort') continue // Exception
67
68       if (obj[k] !== undefined) return true
69     }
70
71     return false
72   }
73
74   reset () {
75     this.startDate = undefined
76     this.endDate = undefined
77     this.originallyPublishedStartDate = undefined
78     this.originallyPublishedEndDate = undefined
79     this.nsfw = undefined
80     this.categoryOneOf = undefined
81     this.licenceOneOf = undefined
82     this.languageOneOf = undefined
83     this.tagsOneOf = undefined
84     this.tagsAllOf = undefined
85     this.durationMin = undefined
86     this.durationMax = undefined
87
88     this.sort = '-match'
89   }
90
91   toUrlObject () {
92     return {
93       startDate: this.startDate,
94       endDate: this.endDate,
95       originallyPublishedStartDate: this.originallyPublishedStartDate,
96       originallyPublishedEndDate: this.originallyPublishedEndDate,
97       nsfw: this.nsfw,
98       categoryOneOf: this.categoryOneOf,
99       licenceOneOf: this.licenceOneOf,
100       languageOneOf: this.languageOneOf,
101       tagsOneOf: this.tagsOneOf,
102       tagsAllOf: this.tagsAllOf,
103       durationMin: this.durationMin,
104       durationMax: this.durationMax,
105       sort: this.sort
106     }
107   }
108
109   toAPIObject () {
110     return {
111       startDate: this.startDate,
112       endDate: this.endDate,
113       originallyPublishedStartDate: this.originallyPublishedStartDate,
114       originallyPublishedEndDate: this.originallyPublishedEndDate,
115       nsfw: this.nsfw,
116       categoryOneOf: this.intoArray(this.categoryOneOf),
117       licenceOneOf: this.intoArray(this.licenceOneOf),
118       languageOneOf: this.intoArray(this.languageOneOf),
119       tagsOneOf: this.intoArray(this.tagsOneOf),
120       tagsAllOf: this.intoArray(this.tagsAllOf),
121       durationMin: this.durationMin,
122       durationMax: this.durationMax,
123       sort: this.sort
124     }
125   }
126
127   size () {
128     let acc = 0
129
130     const obj = this.toUrlObject()
131     for (const k of Object.keys(obj)) {
132       if (k === 'sort') continue // Exception
133
134       if (obj[k] !== undefined) acc++
135     }
136
137     return acc
138   }
139
140   private intoArray (value: any) {
141     if (!value) return undefined
142
143     if (typeof value === 'string') return value.split(',')
144
145     return [ value ]
146   }
147 }