1 import { intoArray, toBoolean } from '@app/helpers'
2 import { AttributesOnly } from '@shared/core-utils'
3 import { BooleanBothQuery, NSFWPolicyType, VideoFilter, VideoSortField } from '@shared/models'
5 type VideoFiltersKeys = {
6 [ id in keyof AttributesOnly<VideoFilters> ]: any
9 export type VideoFilterScope = 'local' | 'federated'
11 export class VideoFilters {
13 nsfw: BooleanBothQuery
15 languageOneOf: string[]
16 categoryOneOf: number[]
18 scope: VideoFilterScope
21 live: BooleanBothQuery
25 private defaultValues = new Map<keyof VideoFilters, any>([
26 [ 'sort', '-publishedAt' ],
28 [ 'languageOneOf', undefined ],
29 [ 'categoryOneOf', undefined ],
30 [ 'scope', 'federated' ],
31 [ 'allVideos', false ],
35 private activeFilters: { key: string, canRemove: boolean, label: string, value?: string }[] = []
36 private defaultNSFWPolicy: NSFWPolicyType
38 private onChangeCallbacks: Array<() => void> = []
39 private oldFormObjectString: string
41 private readonly hiddenFields: string[] = []
43 constructor (defaultSort: string, defaultScope: VideoFilterScope, hiddenFields: string[] = []) {
44 this.setDefaultSort(defaultSort)
45 this.setDefaultScope(defaultScope)
47 this.hiddenFields = hiddenFields
52 onChange (cb: () => void) {
53 this.onChangeCallbacks.push(cb)
57 // Don't run on change if the values did not change
58 const currentFormObjectString = JSON.stringify(this.toFormObject())
59 if (this.oldFormObjectString && currentFormObjectString === this.oldFormObjectString) return
61 this.oldFormObjectString = currentFormObjectString
63 for (const cb of this.onChangeCallbacks) {
68 setDefaultScope (scope: VideoFilterScope) {
69 this.defaultValues.set('scope', scope)
72 setDefaultSort (sort: string) {
73 this.defaultValues.set('sort', sort)
76 setNSFWPolicy (nsfwPolicy: NSFWPolicyType) {
77 console.log(nsfwPolicy)
79 this.updateDefaultNSFW(nsfwPolicy)
82 reset (specificKey?: string) {
83 for (const [ key, value ] of this.defaultValues) {
84 if (specificKey && specificKey !== key) continue
87 this[key as any] = value
90 this.buildActiveFilters()
93 load (obj: Partial<AttributesOnly<VideoFilters>>) {
94 if (obj.sort !== undefined) this.sort = obj.sort
96 if (obj.nsfw !== undefined) this.nsfw = obj.nsfw
98 if (obj.languageOneOf !== undefined) this.languageOneOf = intoArray(obj.languageOneOf)
99 if (obj.categoryOneOf !== undefined) this.categoryOneOf = intoArray(obj.categoryOneOf)
101 if (obj.scope !== undefined) this.scope = obj.scope
102 if (obj.allVideos !== undefined) this.allVideos = toBoolean(obj.allVideos)
104 if (obj.live !== undefined) this.live = obj.live
106 if (obj.search !== undefined) this.search = obj.search
108 this.buildActiveFilters()
111 buildActiveFilters () {
112 this.activeFilters = []
114 this.activeFilters.push({
117 label: $localize`Sensitive content`,
118 value: this.getNSFWValue()
121 this.activeFilters.push({
124 label: $localize`Scope`,
125 value: this.scope === 'federated'
126 ? $localize`Federated`
130 if (this.languageOneOf && this.languageOneOf.length !== 0) {
131 this.activeFilters.push({
132 key: 'languageOneOf',
134 label: $localize`Languages`,
135 value: this.languageOneOf.map(l => l.toUpperCase()).join(', ')
139 if (this.categoryOneOf && this.categoryOneOf.length !== 0) {
140 this.activeFilters.push({
141 key: 'categoryOneOf',
143 label: $localize`Categories`,
144 value: this.categoryOneOf.join(', ')
148 if (this.allVideos) {
149 this.activeFilters.push({
152 label: $localize`All videos`
156 if (this.live === 'true') {
157 this.activeFilters.push({
160 label: $localize`Live videos`
162 } else if (this.live === 'false') {
163 this.activeFilters.push({
166 label: $localize`VOD videos`
170 this.activeFilters = this.activeFilters
171 .filter(a => this.hiddenFields.includes(a.key) === false)
174 getActiveFilters () {
175 return this.activeFilters
178 toFormObject (): VideoFiltersKeys {
179 const result: Partial<VideoFiltersKeys> = {}
181 for (const [ key ] of this.defaultValues) {
182 result[key] = this[key]
185 return result as VideoFiltersKeys
189 const result: { [ id: string ]: any } = {}
191 for (const [ key, defaultValue ] of this.defaultValues) {
192 if (this[key] !== defaultValue) {
193 result[key] = this[key]
200 toVideosAPIObject () {
201 let filter: VideoFilter
203 if (this.scope === 'local' && this.allVideos) {
205 } else if (this.scope === 'federated' && this.allVideos) {
207 } else if (this.scope === 'local') {
212 if (this.live === 'true') isLive = true
213 else if (this.live === 'false') isLive = false
218 languageOneOf: this.languageOneOf,
219 categoryOneOf: this.categoryOneOf,
226 getNSFWDisplayLabel () {
227 if (this.defaultNSFWPolicy === 'blur') return $localize`Blurred`
229 return $localize`Displayed`
232 private getNSFWValue () {
233 if (this.nsfw === 'false') return $localize`hidden`
234 if (this.defaultNSFWPolicy === 'blur') return $localize`blurred`
236 return $localize`displayed`
239 private updateDefaultNSFW (nsfwPolicy: NSFWPolicyType) {
240 const nsfw = nsfwPolicy === 'do_not_list'
244 this.defaultValues.set('nsfw', nsfw)
245 this.defaultNSFWPolicy = nsfwPolicy