aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-search/advanced-search.model.ts
blob: e8bb00fd326618b745948af417b07f5f3d1bd3ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { splitIntoArray } from '@app/helpers'
import {
  BooleanBothQuery,
  BooleanQuery,
  SearchTargetType,
  VideoChannelsSearchQuery,
  VideoPlaylistsSearchQuery,
  VideosSearchQuery
} from '@shared/models'

export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels'

export class AdvancedSearch {
  startDate: string // ISO 8601
  endDate: string // ISO 8601

  originallyPublishedStartDate: string // ISO 8601
  originallyPublishedEndDate: string // ISO 8601

  nsfw: BooleanBothQuery

  categoryOneOf: string

  licenceOneOf: string

  languageOneOf: string

  tagsOneOf: string[]
  tagsAllOf: string[]

  durationMin: number // seconds
  durationMax: number // seconds

  isLive: BooleanQuery

  host: string

  sort: string

  searchTarget: SearchTargetType
  resultType: AdvancedSearchResultType

  constructor (options?: {
    startDate?: string
    endDate?: string
    originallyPublishedStartDate?: string
    originallyPublishedEndDate?: string
    nsfw?: BooleanBothQuery
    categoryOneOf?: string
    licenceOneOf?: string
    languageOneOf?: string

    tagsOneOf?: any
    tagsAllOf?: any

    isLive?: BooleanQuery

    host?: string

    durationMin?: string
    durationMax?: string
    sort?: string
    searchTarget?: SearchTargetType
    resultType?: AdvancedSearchResultType
  }) {
    if (!options) return

    this.startDate = options.startDate || undefined
    this.endDate = options.endDate || undefined
    this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
    this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined

    this.nsfw = options.nsfw || undefined
    this.isLive = options.isLive || undefined

    this.categoryOneOf = options.categoryOneOf || undefined
    this.licenceOneOf = options.licenceOneOf || undefined
    this.languageOneOf = options.languageOneOf || undefined
    this.tagsOneOf = splitIntoArray(options.tagsOneOf)
    this.tagsAllOf = splitIntoArray(options.tagsAllOf)
    this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined
    this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined

    this.host = options.host || undefined

    this.searchTarget = options.searchTarget || undefined

    this.resultType = options.resultType || undefined

    if (!this.resultType && this.hasVideoFilter()) {
      this.resultType = 'videos'
    }

    if (isNaN(this.durationMin)) this.durationMin = undefined
    if (isNaN(this.durationMax)) this.durationMax = undefined

    this.sort = options.sort || '-match'
  }

  containsValues () {
    return this.size() !== 0
  }

  reset () {
    this.startDate = undefined
    this.endDate = undefined
    this.originallyPublishedStartDate = undefined
    this.originallyPublishedEndDate = undefined
    this.nsfw = undefined
    this.categoryOneOf = undefined
    this.licenceOneOf = undefined
    this.languageOneOf = undefined
    this.tagsOneOf = undefined
    this.tagsAllOf = undefined
    this.durationMin = undefined
    this.durationMax = undefined
    this.isLive = undefined
    this.host = undefined

    this.sort = '-match'
  }

  toUrlObject () {
    return {
      startDate: this.startDate,
      endDate: this.endDate,
      originallyPublishedStartDate: this.originallyPublishedStartDate,
      originallyPublishedEndDate: this.originallyPublishedEndDate,
      nsfw: this.nsfw,
      categoryOneOf: this.categoryOneOf,
      licenceOneOf: this.licenceOneOf,
      languageOneOf: this.languageOneOf,
      tagsOneOf: this.tagsOneOf,
      tagsAllOf: this.tagsAllOf,
      durationMin: this.durationMin,
      durationMax: this.durationMax,
      isLive: this.isLive,
      host: this.host,
      sort: this.sort,
      searchTarget: this.searchTarget,
      resultType: this.resultType
    }
  }

  toVideosAPIObject (): VideosSearchQuery {
    let isLive: boolean
    if (this.isLive) isLive = this.isLive === 'true'

    return {
      startDate: this.startDate,
      endDate: this.endDate,
      originallyPublishedStartDate: this.originallyPublishedStartDate,
      originallyPublishedEndDate: this.originallyPublishedEndDate,
      nsfw: this.nsfw,
      categoryOneOf: splitIntoArray(this.categoryOneOf),
      licenceOneOf: splitIntoArray(this.licenceOneOf),
      languageOneOf: splitIntoArray(this.languageOneOf),
      tagsOneOf: this.tagsOneOf,
      tagsAllOf: this.tagsAllOf,
      durationMin: this.durationMin,
      durationMax: this.durationMax,
      host: this.host,
      isLive,
      sort: this.sort,
      searchTarget: this.searchTarget
    }
  }

  toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
    return {
      host: this.host,
      searchTarget: this.searchTarget
    }
  }

  toChannelAPIObject (): VideoChannelsSearchQuery {
    return {
      host: this.host,
      searchTarget: this.searchTarget
    }
  }

  size () {
    let acc = 0

    if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++
    if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) acc++

    if (this.isValidValue(this.nsfw)) acc++
    if (this.isValidValue(this.categoryOneOf)) acc++
    if (this.isValidValue(this.licenceOneOf)) acc++
    if (this.isValidValue(this.languageOneOf)) acc++
    if (this.isValidValue(this.tagsOneOf)) acc++
    if (this.isValidValue(this.tagsAllOf)) acc++
    if (this.isValidValue(this.durationMin) || this.isValidValue(this.durationMax)) acc++
    if (this.isValidValue(this.isLive)) acc++
    if (this.isValidValue(this.host)) acc++
    if (this.isValidValue(this.resultType)) acc++

    return acc
  }

  private isValidValue (val: any) {
    if (val === undefined) return false
    if (val === '') return false
    if (Array.isArray(val) && val.length === 0) return false

    return true
  }

  private hasVideoFilter () {
    return this.startDate !== undefined ||
      this.endDate !== undefined ||
      this.originallyPublishedStartDate !== undefined ||
      this.originallyPublishedEndDate !== undefined ||
      this.nsfw !== undefined ||
      this.categoryOneOf !== undefined ||
      this.licenceOneOf !== undefined ||
      this.languageOneOf !== undefined ||
      this.tagsOneOf !== undefined ||
      this.tagsAllOf !== undefined ||
      this.durationMin !== undefined ||
      this.durationMax !== undefined ||
      this.isLive !== undefined
  }
}