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 constructor (defaultSort: string, defaultScope: VideoFilterScope) {
42 this.setDefaultSort(defaultSort)
43 this.setDefaultScope(defaultScope)
48 onChange (cb: () => void) {
49 this.onChangeCallbacks.push(cb)
53 // Don't run on change if the values did not change
54 const currentFormObjectString = JSON.stringify(this.toFormObject())
55 if (this.oldFormObjectString && currentFormObjectString === this.oldFormObjectString) return
57 this.oldFormObjectString = currentFormObjectString
59 for (const cb of this.onChangeCallbacks) {
64 setDefaultScope (scope: VideoFilterScope) {
65 this.defaultValues.set('scope', scope)
68 setDefaultSort (sort: string) {
69 this.defaultValues.set('sort', sort)
72 setNSFWPolicy (nsfwPolicy: NSFWPolicyType) {
73 this.updateDefaultNSFW(nsfwPolicy)
76 reset (specificKey?: string) {
77 for (const [ key, value ] of this.defaultValues) {
78 if (specificKey && specificKey !== key) continue
81 this[key as any] = value
84 this.buildActiveFilters()
87 load (obj: Partial<AttributesOnly<VideoFilters>>) {
88 if (obj.sort !== undefined) this.sort = obj.sort
90 if (obj.nsfw !== undefined) this.nsfw = obj.nsfw
92 if (obj.languageOneOf !== undefined) this.languageOneOf = intoArray(obj.languageOneOf)
93 if (obj.categoryOneOf !== undefined) this.categoryOneOf = intoArray(obj.categoryOneOf)
95 if (obj.scope !== undefined) this.scope = obj.scope
96 if (obj.allVideos !== undefined) this.allVideos = toBoolean(obj.allVideos)
98 if (obj.live !== undefined) this.live = obj.live
100 if (obj.search !== undefined) this.search = obj.search
102 this.buildActiveFilters()
105 buildActiveFilters () {
106 this.activeFilters = []
108 this.activeFilters.push({
111 label: $localize`Sensitive content`,
112 value: this.getNSFWValue()
115 this.activeFilters.push({
118 label: $localize`Scope`,
119 value: this.scope === 'federated'
120 ? $localize`Federated`
124 if (this.languageOneOf && this.languageOneOf.length !== 0) {
125 this.activeFilters.push({
126 key: 'languageOneOf',
128 label: $localize`Languages`,
129 value: this.languageOneOf.map(l => l.toUpperCase()).join(', ')
133 if (this.categoryOneOf && this.categoryOneOf.length !== 0) {
134 this.activeFilters.push({
135 key: 'categoryOneOf',
137 label: $localize`Categories`,
138 value: this.categoryOneOf.join(', ')
142 if (this.allVideos) {
143 this.activeFilters.push({
146 label: $localize`All videos`
150 if (this.live === 'true') {
151 this.activeFilters.push({
154 label: $localize`Live videos`
156 } else if (this.live === 'false') {
157 this.activeFilters.push({
160 label: $localize`VOD videos`
165 getActiveFilters () {
166 return this.activeFilters
169 toFormObject (): VideoFiltersKeys {
170 const result: Partial<VideoFiltersKeys> = {}
172 for (const [ key ] of this.defaultValues) {
173 result[key] = this[key]
176 return result as VideoFiltersKeys
180 const result: { [ id: string ]: any } = {}
182 for (const [ key, defaultValue ] of this.defaultValues) {
183 if (this[key] !== defaultValue) {
184 result[key] = this[key]
191 toVideosAPIObject () {
192 let filter: VideoFilter
194 if (this.scope === 'local' && this.allVideos) {
196 } else if (this.scope === 'federated' && this.allVideos) {
198 } else if (this.scope === 'local') {
203 if (this.live === 'true') isLive = true
204 else if (this.live === 'false') isLive = false
209 languageOneOf: this.languageOneOf,
210 categoryOneOf: this.categoryOneOf,
217 getNSFWDisplayLabel () {
218 if (this.defaultNSFWPolicy === 'blur') return $localize`Blurred`
220 return $localize`Displayed`
223 private getNSFWValue () {
224 if (this.nsfw === 'false') return $localize`hidden`
225 if (this.defaultNSFWPolicy === 'blur') return $localize`blurred`
227 return $localize`displayed`
230 private updateDefaultNSFW (nsfwPolicy: NSFWPolicyType) {
231 const nsfw = nsfwPolicy === 'do_not_list'
235 this.defaultValues.set('nsfw', nsfw)
236 this.defaultNSFWPolicy = nsfwPolicy