1 import { intoArray, toBoolean } from '@app/helpers'
2 import { getAllPrivacies } from '@shared/core-utils'
3 import { AttributesOnly } from '@shared/typescript-utils'
4 import { BooleanBothQuery, NSFWPolicyType, VideoInclude, VideoPrivacy, VideoSortField } from '@shared/models'
6 type VideoFiltersKeys = {
7 [ id in keyof AttributesOnly<VideoFilters> ]: any
10 export type VideoFilterScope = 'local' | 'federated'
12 export class VideoFilters {
14 nsfw: BooleanBothQuery
16 languageOneOf: string[]
17 categoryOneOf: number[]
19 scope: VideoFilterScope
22 live: BooleanBothQuery
26 private defaultValues = new Map<keyof VideoFilters, any>([
27 [ 'sort', '-publishedAt' ],
29 [ 'languageOneOf', undefined ],
30 [ 'categoryOneOf', undefined ],
31 [ 'scope', 'federated' ],
32 [ 'allVideos', false ],
36 private activeFilters: { key: string, canRemove: boolean, label: string, value?: string }[] = []
37 private defaultNSFWPolicy: NSFWPolicyType
39 private onChangeCallbacks: Array<() => void> = []
40 private oldFormObjectString: string
42 private readonly hiddenFields: string[] = []
44 constructor (defaultSort: string, defaultScope: VideoFilterScope, hiddenFields: string[] = []) {
45 this.setDefaultSort(defaultSort)
46 this.setDefaultScope(defaultScope)
48 this.hiddenFields = hiddenFields
53 onChange (cb: () => void) {
54 this.onChangeCallbacks.push(cb)
58 // Don't run on change if the values did not change
59 const currentFormObjectString = JSON.stringify(this.toFormObject())
60 if (this.oldFormObjectString && currentFormObjectString === this.oldFormObjectString) return
62 this.oldFormObjectString = currentFormObjectString
64 for (const cb of this.onChangeCallbacks) {
69 setDefaultScope (scope: VideoFilterScope) {
70 this.defaultValues.set('scope', scope)
73 setDefaultSort (sort: string) {
74 this.defaultValues.set('sort', sort)
77 setNSFWPolicy (nsfwPolicy: NSFWPolicyType) {
78 this.updateDefaultNSFW(nsfwPolicy)
81 reset (specificKey?: string) {
82 for (const [ key, value ] of this.defaultValues) {
83 if (specificKey && specificKey !== key) continue
86 this[key as any] = value
89 this.buildActiveFilters()
92 load (obj: Partial<AttributesOnly<VideoFilters>>) {
93 if (obj.sort !== undefined) this.sort = obj.sort
95 if (obj.nsfw !== undefined) this.nsfw = obj.nsfw
97 if (obj.languageOneOf !== undefined) this.languageOneOf = intoArray(obj.languageOneOf)
98 if (obj.categoryOneOf !== undefined) this.categoryOneOf = intoArray(obj.categoryOneOf)
100 if (obj.scope !== undefined) this.scope = obj.scope
101 if (obj.allVideos !== undefined) this.allVideos = toBoolean(obj.allVideos)
103 if (obj.live !== undefined) this.live = obj.live
105 if (obj.search !== undefined) this.search = obj.search
107 this.buildActiveFilters()
110 buildActiveFilters () {
111 this.activeFilters = []
113 this.activeFilters.push({
116 label: $localize`Sensitive content`,
117 value: this.getNSFWValue()
120 this.activeFilters.push({
123 label: $localize`Scope`,
124 value: this.scope === 'federated'
125 ? $localize`Federated`
129 if (this.languageOneOf && this.languageOneOf.length !== 0) {
130 this.activeFilters.push({
131 key: 'languageOneOf',
133 label: $localize`Languages`,
134 value: this.languageOneOf.map(l => l.toUpperCase()).join(', ')
138 if (this.categoryOneOf && this.categoryOneOf.length !== 0) {
139 this.activeFilters.push({
140 key: 'categoryOneOf',
142 label: $localize`Categories`,
143 value: this.categoryOneOf.join(', ')
147 if (this.allVideos) {
148 this.activeFilters.push({
151 label: $localize`All videos`
155 if (this.live === 'true') {
156 this.activeFilters.push({
159 label: $localize`Live videos`
161 } else if (this.live === 'false') {
162 this.activeFilters.push({
165 label: $localize`VOD videos`
169 this.activeFilters = this.activeFilters
170 .filter(a => this.hiddenFields.includes(a.key) === false)
173 getActiveFilters () {
174 return this.activeFilters
177 toFormObject (): VideoFiltersKeys {
178 const result: Partial<VideoFiltersKeys> = {}
180 for (const [ key ] of this.defaultValues) {
181 result[key] = this[key]
184 return result as VideoFiltersKeys
188 const result: { [ id: string ]: any } = {}
190 for (const [ key, defaultValue ] of this.defaultValues) {
191 if (this[key] !== defaultValue) {
192 result[key] = this[key]
199 toVideosAPIObject () {
201 let include: VideoInclude
202 let privacyOneOf: VideoPrivacy[]
204 if (this.scope === 'local') {
208 if (this.allVideos) {
209 include = VideoInclude.NOT_PUBLISHED_STATE
210 privacyOneOf = getAllPrivacies()
214 if (this.live === 'true') isLive = true
215 else if (this.live === 'false') isLive = false
220 languageOneOf: this.languageOneOf,
221 categoryOneOf: this.categoryOneOf,
230 getNSFWDisplayLabel () {
231 if (this.defaultNSFWPolicy === 'blur') return $localize`Blurred`
233 return $localize`Displayed`
236 private getNSFWValue () {
237 if (this.nsfw === 'false') return $localize`hidden`
238 if (this.defaultNSFWPolicy === 'blur') return $localize`blurred`
240 return $localize`displayed`
243 private updateDefaultNSFW (nsfwPolicy: NSFWPolicyType) {
244 const nsfw = nsfwPolicy === 'do_not_list'
248 this.defaultValues.set('nsfw', nsfw)
249 this.defaultNSFWPolicy = nsfwPolicy