]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/rest/rest.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest.service.ts
index 93b5f56b2b402d8e7135b92c3fd4f3be7f7de754..d8b5ffb1851755aee06a8373a2c035c4cc5bc1d9 100644 (file)
@@ -1,13 +1,16 @@
+import * as debug from 'debug'
 import { SortMeta } from 'primeng/api'
 import { HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { ComponentPaginationLight } from './component-pagination.model'
 import { RestPagination } from './rest-pagination'
 
+const debugLogger = debug('peertube:rest')
+
 interface QueryStringFilterPrefixes {
   [key: string]: {
     prefix: string
-    handler?: (v: string) => string | number
+    handler?: (v: string) => string | number | boolean
     multiple?: boolean
     isBoolean?: boolean
   }
@@ -28,19 +31,19 @@ export class RestService {
     }
 
     if (sort !== undefined) {
-      let sortString = ''
+      newParams = newParams.set('sort', this.buildSortString(sort))
+    }
 
-      if (typeof sort === 'string') {
-        sortString = sort
-      } else {
-        const sortPrefix = sort.order === 1 ? '' : '-'
-        sortString = sortPrefix + sort.field
-      }
+    return newParams
+  }
 
-      newParams = newParams.set('sort', sortString)
+  buildSortString (sort: SortMeta | string) {
+    if (typeof sort === 'string') {
+      return sort
     }
 
-    return newParams
+    const sortPrefix = sort.order === 1 ? '' : '-'
+    return sortPrefix + sort.field
   }
 
   addArrayParams (params: HttpParams, name: string, values: (string | number)[]) {
@@ -79,13 +82,13 @@ export class RestService {
   parseQueryStringFilter <T extends QueryStringFilterPrefixes> (q: string, prefixes: T): ParseQueryStringFiltersResult<keyof T> {
     if (!q) return {}
 
-    // Tokenize the strings using spaces that are not in quotes
-    const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
-                    .filter(token => !!token)
+    const tokens = this.tokenizeString(q)
 
     // Build prefix array
     const prefixeStrings = Object.values(prefixes)
-                           .map(p => p.prefix)
+                                 .map(p => p.prefix)
+
+    debugLogger(`Built tokens "${tokens.join(', ')}" for prefixes "${prefixeStrings.join(', ')}"`)
 
     // Search is the querystring minus defined filters
     const searchTokens = tokens.filter(t => {
@@ -122,10 +125,22 @@ export class RestService {
         : matchedTokens[0]
     }
 
+    const search = searchTokens.join(' ') || undefined
+
+    debugLogger('Built search: ' + search, additionalFilters)
+
     return {
-      search: searchTokens.join(' ') || undefined,
+      search,
 
       ...additionalFilters
     }
   }
+
+  tokenizeString (q: string) {
+    if (!q) return []
+
+    // Tokenize the strings using spaces that are not in quotes
+    return q.match(/(?:[^\s"]+|"[^"]*")+/g)
+            .filter(token => !!token)
+  }
 }