Merge from upstream
[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   nsfw: NSFWQuery
8
9   categoryOneOf: string
10
11   licenceOneOf: string
12
13   languageOneOf: string
14
15   tagsOneOf: string
16   tagsAllOf: string
17
18   durationMin: number // seconds
19   durationMax: number // seconds
20
21   sort: string
22
23   constructor (options?: {
24     startDate?: string
25     endDate?: string
26     nsfw?: NSFWQuery
27     categoryOneOf?: string
28     licenceOneOf?: string
29     languageOneOf?: string
30     tagsOneOf?: string
31     tagsAllOf?: string
32     durationMin?: string
33     durationMax?: string
34     sort?: string
35   }) {
36     if (!options) return
37
38     this.startDate = options.startDate || undefined
39     this.endDate = options.endDate || undefined
40     this.nsfw = options.nsfw || undefined
41     this.categoryOneOf = options.categoryOneOf || undefined
42     this.licenceOneOf = options.licenceOneOf || undefined
43     this.languageOneOf = options.languageOneOf || undefined
44     this.tagsOneOf = options.tagsOneOf || undefined
45     this.tagsAllOf = options.tagsAllOf || undefined
46     this.durationMin = parseInt(options.durationMin, 10)
47     this.durationMax = parseInt(options.durationMax, 10)
48
49     if (isNaN(this.durationMin)) this.durationMin = undefined
50     if (isNaN(this.durationMax)) this.durationMax = undefined
51
52     this.sort = options.sort || '-match'
53   }
54
55   containsValues () {
56     const obj = this.toUrlObject()
57     for (const k of Object.keys(obj)) {
58       if (k === 'sort') continue // Exception
59
60       if (obj[k] !== undefined) return true
61     }
62
63     return false
64   }
65
66   reset () {
67     this.startDate = undefined
68     this.endDate = undefined
69     this.nsfw = undefined
70     this.categoryOneOf = undefined
71     this.licenceOneOf = undefined
72     this.languageOneOf = undefined
73     this.tagsOneOf = undefined
74     this.tagsAllOf = undefined
75     this.durationMin = undefined
76     this.durationMax = undefined
77
78     this.sort = '-match'
79   }
80
81   toUrlObject () {
82     return {
83       startDate: this.startDate,
84       endDate: this.endDate,
85       nsfw: this.nsfw,
86       categoryOneOf: this.categoryOneOf,
87       licenceOneOf: this.licenceOneOf,
88       languageOneOf: this.languageOneOf,
89       tagsOneOf: this.tagsOneOf,
90       tagsAllOf: this.tagsAllOf,
91       durationMin: this.durationMin,
92       durationMax: this.durationMax,
93       sort: this.sort
94     }
95   }
96
97   toAPIObject () {
98     return {
99       startDate: this.startDate,
100       endDate: this.endDate,
101       nsfw: this.nsfw,
102       categoryOneOf: this.intoArray(this.categoryOneOf),
103       licenceOneOf: this.intoArray(this.licenceOneOf),
104       languageOneOf: this.intoArray(this.languageOneOf),
105       tagsOneOf: this.intoArray(this.tagsOneOf),
106       tagsAllOf: this.intoArray(this.tagsAllOf),
107       durationMin: this.durationMin,
108       durationMax: this.durationMax,
109       sort: this.sort
110     }
111   }
112
113   size () {
114     let acc = 0
115
116     const obj = this.toUrlObject()
117     for (const k of Object.keys(obj)) {
118       if (k === 'sort') continue // Exception
119
120       if (obj[k] !== undefined) acc++
121     }
122
123     return acc
124   }
125
126   private intoArray (value: any) {
127     if (!value) return undefined
128
129     if (typeof value === 'string') return value.split(',')
130
131     return [ value ]
132   }
133 }