]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest.service.ts
Add ability to filter by file type
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest.service.ts
CommitLineData
231ff4af 1import * as debug from 'debug'
f77eb73b 2import { SortMeta } from 'primeng/api'
feb34f6b
C
3import { HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
5import { ComponentPaginationLight } from './component-pagination.model'
df98563e 6import { RestPagination } from './rest-pagination'
de59c48f 7
231ff4af
C
8const logger = debug('peertube:rest')
9
feb34f6b
C
10interface QueryStringFilterPrefixes {
11 [key: string]: {
12 prefix: string
231ff4af 13 handler?: (v: string) => string | number | boolean
feb34f6b 14 multiple?: boolean
8491293b 15 isBoolean?: boolean
feb34f6b
C
16 }
17}
18
4beda9e1
C
19type ParseQueryStringFilters <K extends keyof any> = Partial<Record<K, string | number | boolean | (string | number | boolean)[]>>
20type ParseQueryStringFiltersResult <K extends keyof any> = ParseQueryStringFilters<K> & { search?: string }
feb34f6b 21
de59c48f
C
22@Injectable()
23export class RestService {
24
d592e0a9
C
25 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
26 let newParams = params
de59c48f 27
d592e0a9
C
28 if (pagination !== undefined) {
29 newParams = newParams.set('start', pagination.start.toString())
30 .set('count', pagination.count.toString())
de59c48f
C
31 }
32
d592e0a9
C
33 if (sort !== undefined) {
34 let sortString = ''
35
36 if (typeof sort === 'string') {
37 sortString = sort
38 } else {
39 const sortPrefix = sort.order === 1 ? '' : '-'
40 sortString = sortPrefix + sort.field
41 }
42
43 newParams = newParams.set('sort', sortString)
de59c48f
C
44 }
45
d592e0a9 46 return newParams
de59c48f
C
47 }
48
3da38d6e
C
49 addArrayParams (params: HttpParams, name: string, values: (string | number)[]) {
50 for (const v of values) {
51 params = params.append(name, v)
52 }
53
54 return params
55 }
56
c199c427 57 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
f37dc0dd
C
58 for (const name of Object.keys(object)) {
59 const value = object[name]
1ebddadd 60 if (value === undefined || value === null) continue
f37dc0dd 61
9abd170d 62 if (Array.isArray(value)) {
3da38d6e 63 params = this.addArrayParams(params, name, value)
f37dc0dd
C
64 } else {
65 params = params.append(name, value)
66 }
67 }
68
69 return params
70 }
71
4beda9e1 72 componentToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
4635f59d
C
73 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
74 const count: number = componentPagination.itemsPerPage
75
76 return { start, count }
77 }
feb34f6b 78
4beda9e1
C
79 /*
80 * Returns an object containing the filters and the remaining search
81 */
82 parseQueryStringFilter <T extends QueryStringFilterPrefixes> (q: string, prefixes: T): ParseQueryStringFiltersResult<keyof T> {
feb34f6b
C
83 if (!q) return {}
84
cc0e0d32
C
85 // Tokenize the strings using spaces that are not in quotes
86 const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
87 .filter(token => !!token)
feb34f6b
C
88
89 // Build prefix array
90 const prefixeStrings = Object.values(prefixes)
91 .map(p => p.prefix)
92
231ff4af
C
93 logger(`Built tokens "${tokens.join(', ')}" for prefixes "${prefixeStrings.join(', ')}"`)
94
feb34f6b
C
95 // Search is the querystring minus defined filters
96 const searchTokens = tokens.filter(t => {
97 return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
98 })
99
4beda9e1 100 const additionalFilters: ParseQueryStringFilters<keyof T> = {}
feb34f6b 101
4beda9e1 102 for (const prefixKey of Object.keys(prefixes) as (keyof T)[]) {
feb34f6b
C
103 const prefixObj = prefixes[prefixKey]
104 const prefix = prefixObj.prefix
105
106 const matchedTokens = tokens.filter(t => t.startsWith(prefix))
107 .map(t => t.slice(prefix.length)) // Keep the value filter
1a7d0887 108 .map(t => t.replace(/^"|"$/g, '')) // Remove ""
feb34f6b
C
109 .map(t => {
110 if (prefixObj.handler) return prefixObj.handler(t)
111
1a7d0887
C
112 if (prefixObj.isBoolean) {
113 if (t === 'true') return true
114 if (t === 'false') return false
115
116 return undefined
117 }
118
feb34f6b
C
119 return t
120 })
1a7d0887 121 .filter(t => t !== null && t !== undefined)
feb34f6b
C
122
123 if (matchedTokens.length === 0) continue
124
125 additionalFilters[prefixKey] = prefixObj.multiple === true
126 ? matchedTokens
127 : matchedTokens[0]
128 }
129
231ff4af
C
130 const search = searchTokens.join(' ') || undefined
131
132 logger('Built search: ' + search, additionalFilters)
133
feb34f6b 134 return {
231ff4af 135 search,
feb34f6b
C
136
137 ...additionalFilters
138 }
139 }
de59c48f 140}