]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/search/advanced-search.model.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / search / advanced-search.model.ts
index aad4367881f8c0f37771114e270cf666822978a5..5b713e14550120785bff6dca2ee3604883d25eb9 100644 (file)
@@ -4,6 +4,9 @@ export class AdvancedSearch {
   startDate: string // ISO 8601
   endDate: string // ISO 8601
 
+  originallyPublishedStartDate: string // ISO 8601
+  originallyPublishedEndDate: string // ISO 8601
+
   nsfw: NSFWQuery
 
   categoryOneOf: string
@@ -18,9 +21,13 @@ export class AdvancedSearch {
   durationMin: number // seconds
   durationMax: number // seconds
 
+  sort: string
+
   constructor (options?: {
     startDate?: string
     endDate?: string
+    originallyPublishedStartDate?: string
+    originallyPublishedEndDate?: string
     nsfw?: NSFWQuery
     categoryOneOf?: string
     licenceOneOf?: string
@@ -29,11 +36,15 @@ export class AdvancedSearch {
     tagsAllOf?: string
     durationMin?: string
     durationMax?: string
+    sort?: string
   }) {
     if (!options) return
 
     this.startDate = options.startDate || undefined
     this.endDate = options.endDate || undefined
+    this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
+    this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
+
     this.nsfw = options.nsfw || undefined
     this.categoryOneOf = options.categoryOneOf || undefined
     this.licenceOneOf = options.licenceOneOf || undefined
@@ -45,11 +56,15 @@ export class AdvancedSearch {
 
     if (isNaN(this.durationMin)) this.durationMin = undefined
     if (isNaN(this.durationMax)) this.durationMax = undefined
+
+    this.sort = options.sort || '-match'
   }
 
   containsValues () {
     const obj = this.toUrlObject()
     for (const k of Object.keys(obj)) {
+      if (k === 'sort') continue // Exception
+
       if (obj[k] !== undefined) return true
     }
 
@@ -59,6 +74,8 @@ export class AdvancedSearch {
   reset () {
     this.startDate = undefined
     this.endDate = undefined
+    this.originallyPublishedStartDate = undefined
+    this.originallyPublishedEndDate = undefined
     this.nsfw = undefined
     this.categoryOneOf = undefined
     this.licenceOneOf = undefined
@@ -67,12 +84,16 @@ export class AdvancedSearch {
     this.tagsAllOf = undefined
     this.durationMin = undefined
     this.durationMax = undefined
+
+    this.sort = '-match'
   }
 
   toUrlObject () {
     return {
       startDate: this.startDate,
       endDate: this.endDate,
+      originallyPublishedStartDate: this.originallyPublishedStartDate,
+      originallyPublishedEndDate: this.originallyPublishedEndDate,
       nsfw: this.nsfw,
       categoryOneOf: this.categoryOneOf,
       licenceOneOf: this.licenceOneOf,
@@ -80,7 +101,8 @@ export class AdvancedSearch {
       tagsOneOf: this.tagsOneOf,
       tagsAllOf: this.tagsAllOf,
       durationMin: this.durationMin,
-      durationMax: this.durationMax
+      durationMax: this.durationMax,
+      sort: this.sort
     }
   }
 
@@ -88,14 +110,38 @@ export class AdvancedSearch {
     return {
       startDate: this.startDate,
       endDate: this.endDate,
+      originallyPublishedStartDate: this.originallyPublishedStartDate,
+      originallyPublishedEndDate: this.originallyPublishedEndDate,
       nsfw: this.nsfw,
-      categoryOneOf: this.categoryOneOf ? this.categoryOneOf.split(',') : undefined,
-      licenceOneOf: this.licenceOneOf ? this.licenceOneOf.split(',') : undefined,
-      languageOneOf: this.languageOneOf ? this.languageOneOf.split(',') : undefined,
-      tagsOneOf: this.tagsOneOf ? this.tagsOneOf.split(',') : undefined,
-      tagsAllOf: this.tagsAllOf ? this.tagsAllOf.split(',') : undefined,
+      categoryOneOf: this.intoArray(this.categoryOneOf),
+      licenceOneOf: this.intoArray(this.licenceOneOf),
+      languageOneOf: this.intoArray(this.languageOneOf),
+      tagsOneOf: this.intoArray(this.tagsOneOf),
+      tagsAllOf: this.intoArray(this.tagsAllOf),
       durationMin: this.durationMin,
-      durationMax: this.durationMax
+      durationMax: this.durationMax,
+      sort: this.sort
+    }
+  }
+
+  size () {
+    let acc = 0
+
+    const obj = this.toUrlObject()
+    for (const k of Object.keys(obj)) {
+      if (k === 'sort') continue // Exception
+
+      if (obj[k] !== undefined) acc++
     }
+
+    return acc
+  }
+
+  private intoArray (value: any) {
+    if (!value) return undefined
+
+    if (typeof value === 'string') return value.split(',')
+
+    return [ value ]
   }
 }