]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-miniature/video-filters.model.ts
Reorder playlists when adding an element
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-filters.model.ts
index 920dc826c7018fd20555006b6e4f96b4f9607d0c..4069ab4b5db8732b7c581fee93a8a6f878a16228 100644 (file)
@@ -1,6 +1,8 @@
-import { intoArray, toBoolean } from '@app/helpers'
-import { AttributesOnly } from '@shared/core-utils'
-import { BooleanBothQuery, NSFWPolicyType, VideoFilter, VideoSortField } from '@shared/models'
+import { splitIntoArray, toBoolean } from '@app/helpers'
+import { getAllPrivacies } from '@shared/core-utils'
+import { escapeHTML } from '@shared/core-utils/renderer'
+import { BooleanBothQuery, NSFWPolicyType, VideoInclude, VideoPrivacy, VideoSortField } from '@shared/models'
+import { AttributesOnly } from '@shared/typescript-utils'
 
 type VideoFiltersKeys = {
   [ id in keyof AttributesOnly<VideoFilters> ]: any
@@ -89,19 +91,28 @@ export class VideoFilters {
   }
 
   load (obj: Partial<AttributesOnly<VideoFilters>>) {
-    if (obj.sort !== undefined) this.sort = obj.sort
+    // FIXME: We may use <ng-option> that doesn't escape HTML so prefer to escape things
+    // https://github.com/ng-select/ng-select/issues/1363
 
-    if (obj.nsfw !== undefined) this.nsfw = obj.nsfw
+    const escapeIfNeeded = (value: any) => {
+      if (typeof value === 'string') return escapeHTML(value)
 
-    if (obj.languageOneOf !== undefined) this.languageOneOf = intoArray(obj.languageOneOf)
-    if (obj.categoryOneOf !== undefined) this.categoryOneOf = intoArray(obj.categoryOneOf)
+      return value
+    }
+
+    if (obj.sort !== undefined) this.sort = escapeIfNeeded(obj.sort) as VideoSortField
+
+    if (obj.nsfw !== undefined) this.nsfw = escapeIfNeeded(obj.nsfw) as BooleanBothQuery
+
+    if (obj.languageOneOf !== undefined) this.languageOneOf = splitIntoArray(escapeIfNeeded(obj.languageOneOf))
+    if (obj.categoryOneOf !== undefined) this.categoryOneOf = splitIntoArray(escapeIfNeeded(obj.categoryOneOf))
 
-    if (obj.scope !== undefined) this.scope = obj.scope
+    if (obj.scope !== undefined) this.scope = escapeIfNeeded(obj.scope) as VideoFilterScope
     if (obj.allVideos !== undefined) this.allVideos = toBoolean(obj.allVideos)
 
-    if (obj.live !== undefined) this.live = obj.live
+    if (obj.live !== undefined) this.live = escapeIfNeeded(obj.live) as BooleanBothQuery
 
-    if (obj.search !== undefined) this.search = obj.search
+    if (obj.search !== undefined) this.search = escapeIfNeeded(obj.search)
 
     this.buildActiveFilters()
   }
@@ -196,14 +207,17 @@ export class VideoFilters {
   }
 
   toVideosAPIObject () {
-    let filter: VideoFilter
-
-    if (this.scope === 'local' && this.allVideos) {
-      filter = 'all-local'
-    } else if (this.scope === 'federated' && this.allVideos) {
-      filter = 'all'
-    } else if (this.scope === 'local') {
-      filter = 'local'
+    let isLocal: boolean
+    let include: VideoInclude
+    let privacyOneOf: VideoPrivacy[]
+
+    if (this.scope === 'local') {
+      isLocal = true
+    }
+
+    if (this.allVideos) {
+      include = VideoInclude.NOT_PUBLISHED_STATE
+      privacyOneOf = getAllPrivacies()
     }
 
     let isLive: boolean
@@ -216,7 +230,9 @@ export class VideoFilters {
       languageOneOf: this.languageOneOf,
       categoryOneOf: this.categoryOneOf,
       search: this.search,
-      filter,
+      isLocal,
+      include,
+      privacyOneOf,
       isLive
     }
   }