1 import { splitIntoArray, toBoolean } from '@app/helpers'
2 import { getAllPrivacies } from '@shared/core-utils'
3 import { escapeHTML } from '@shared/core-utils/renderer'
4 import { BooleanBothQuery, NSFWPolicyType, VideoInclude, VideoPrivacy, VideoSortField } from '@shared/models'
5 import { AttributesOnly } from '@shared/typescript-utils'
7 type VideoFiltersKeys = {
8 [ id in keyof AttributesOnly<VideoFilters> ]: any
11 export type VideoFilterScope = 'local' | 'federated'
13 export class VideoFilters {
15 nsfw: BooleanBothQuery
17 languageOneOf: string[]
18 categoryOneOf: number[]
20 scope: VideoFilterScope
23 live: BooleanBothQuery
27 private defaultValues = new Map<keyof VideoFilters, any>([
28 [ 'sort', '-publishedAt' ],
30 [ 'languageOneOf', undefined ],
31 [ 'categoryOneOf', undefined ],
32 [ 'scope', 'federated' ],
33 [ 'allVideos', false ],
37 private activeFilters: { key: string, canRemove: boolean, label: string, value?: string }[] = []
38 private defaultNSFWPolicy: NSFWPolicyType
40 private onChangeCallbacks: Array<() => void> = []
41 private oldFormObjectString: string
43 private readonly hiddenFields: string[] = []
45 constructor (defaultSort: string, defaultScope: VideoFilterScope, hiddenFields: string[] = []) {
46 this.setDefaultSort(defaultSort)
47 this.setDefaultScope(defaultScope)
49 this.hiddenFields = hiddenFields
54 onChange (cb: () => void) {
55 this.onChangeCallbacks.push(cb)
59 // Don't run on change if the values did not change
60 const currentFormObjectString = JSON.stringify(this.toFormObject())
61 if (this.oldFormObjectString && currentFormObjectString === this.oldFormObjectString) return
63 this.oldFormObjectString = currentFormObjectString
65 for (const cb of this.onChangeCallbacks) {
70 setDefaultScope (scope: VideoFilterScope) {
71 this.defaultValues.set('scope', scope)
74 setDefaultSort (sort: string) {
75 this.defaultValues.set('sort', sort)
78 setNSFWPolicy (nsfwPolicy: NSFWPolicyType) {
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 // FIXME: We may use <ng-option> that doesn't escape HTML so prefer to escape things
95 // https://github.com/ng-select/ng-select/issues/1363
97 const escapeIfNeeded = (value: any) => {
98 if (typeof value === 'string') return escapeHTML(value)
103 if (obj.sort !== undefined) this.sort = escapeIfNeeded(obj.sort) as VideoSortField
105 if (obj.nsfw !== undefined) this.nsfw = escapeIfNeeded(obj.nsfw) as BooleanBothQuery
107 if (obj.languageOneOf !== undefined) this.languageOneOf = splitIntoArray(escapeIfNeeded(obj.languageOneOf))
108 if (obj.categoryOneOf !== undefined) this.categoryOneOf = splitIntoArray(escapeIfNeeded(obj.categoryOneOf))
110 if (obj.scope !== undefined) this.scope = escapeIfNeeded(obj.scope) as VideoFilterScope
111 if (obj.allVideos !== undefined) this.allVideos = toBoolean(obj.allVideos)
113 if (obj.live !== undefined) this.live = escapeIfNeeded(obj.live) as BooleanBothQuery
115 if (obj.search !== undefined) this.search = escapeIfNeeded(obj.search)
117 this.buildActiveFilters()
120 buildActiveFilters () {
121 this.activeFilters = []
123 this.activeFilters.push({
126 label: $localize`Sensitive content`,
127 value: this.getNSFWValue()
130 this.activeFilters.push({
133 label: $localize`Scope`,
134 value: this.scope === 'federated'
135 ? $localize`Federated`
139 if (this.languageOneOf && this.languageOneOf.length !== 0) {
140 this.activeFilters.push({
141 key: 'languageOneOf',
143 label: $localize`Languages`,
144 value: this.languageOneOf.map(l => l.toUpperCase()).join(', ')
148 if (this.categoryOneOf && this.categoryOneOf.length !== 0) {
149 this.activeFilters.push({
150 key: 'categoryOneOf',
152 label: $localize`Categories`,
153 value: this.categoryOneOf.join(', ')
157 if (this.allVideos) {
158 this.activeFilters.push({
161 label: $localize`All videos`
165 if (this.live === 'true') {
166 this.activeFilters.push({
169 label: $localize`Live videos`
171 } else if (this.live === 'false') {
172 this.activeFilters.push({
175 label: $localize`VOD videos`
179 this.activeFilters = this.activeFilters
180 .filter(a => this.hiddenFields.includes(a.key) === false)
183 getActiveFilters () {
184 return this.activeFilters
187 toFormObject (): VideoFiltersKeys {
188 const result: Partial<VideoFiltersKeys> = {}
190 for (const [ key ] of this.defaultValues) {
191 result[key] = this[key]
194 return result as VideoFiltersKeys
198 const result: { [ id: string ]: any } = {}
200 for (const [ key, defaultValue ] of this.defaultValues) {
201 if (this[key] !== defaultValue) {
202 result[key] = this[key]
209 toVideosAPIObject () {
211 let include: VideoInclude
212 let privacyOneOf: VideoPrivacy[]
214 if (this.scope === 'local') {
218 if (this.allVideos) {
219 include = VideoInclude.NOT_PUBLISHED_STATE
220 privacyOneOf = getAllPrivacies()
224 if (this.live === 'true') isLive = true
225 else if (this.live === 'false') isLive = false
230 languageOneOf: this.languageOneOf,
231 categoryOneOf: this.categoryOneOf,
240 getNSFWDisplayLabel () {
241 if (this.defaultNSFWPolicy === 'blur') return $localize`Blurred`
243 return $localize`Displayed`
246 private getNSFWValue () {
247 if (this.nsfw === 'false') return $localize`hidden`
248 if (this.defaultNSFWPolicy === 'blur') return $localize`blurred`
250 return $localize`displayed`
253 private updateDefaultNSFW (nsfwPolicy: NSFWPolicyType) {
254 const nsfw = nsfwPolicy === 'do_not_list'
258 this.defaultValues.set('nsfw', nsfw)
259 this.defaultNSFWPolicy = nsfwPolicy