]>
Commit | Line | Data |
---|---|---|
231ff4af | 1 | import * as debug from 'debug' |
f77eb73b | 2 | import { SortMeta } from 'primeng/api' |
feb34f6b C |
3 | import { HttpParams } from '@angular/common/http' |
4 | import { Injectable } from '@angular/core' | |
5 | import { ComponentPaginationLight } from './component-pagination.model' | |
df98563e | 6 | import { RestPagination } from './rest-pagination' |
de59c48f | 7 | |
231ff4af C |
8 | const logger = debug('peertube:rest') |
9 | ||
feb34f6b C |
10 | interface 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 |
19 | type ParseQueryStringFilters <K extends keyof any> = Partial<Record<K, string | number | boolean | (string | number | boolean)[]>> |
20 | type ParseQueryStringFiltersResult <K extends keyof any> = ParseQueryStringFilters<K> & { search?: string } | |
feb34f6b | 21 | |
de59c48f C |
22 | @Injectable() |
23 | export 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 | ||
dd6d2a7c | 85 | const tokens = this.tokenizeString(q) |
feb34f6b C |
86 | |
87 | // Build prefix array | |
88 | const prefixeStrings = Object.values(prefixes) | |
dd6d2a7c | 89 | .map(p => p.prefix) |
feb34f6b | 90 | |
231ff4af C |
91 | logger(`Built tokens "${tokens.join(', ')}" for prefixes "${prefixeStrings.join(', ')}"`) |
92 | ||
feb34f6b C |
93 | // Search is the querystring minus defined filters |
94 | const searchTokens = tokens.filter(t => { | |
95 | return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false) | |
96 | }) | |
97 | ||
4beda9e1 | 98 | const additionalFilters: ParseQueryStringFilters<keyof T> = {} |
feb34f6b | 99 | |
4beda9e1 | 100 | for (const prefixKey of Object.keys(prefixes) as (keyof T)[]) { |
feb34f6b C |
101 | const prefixObj = prefixes[prefixKey] |
102 | const prefix = prefixObj.prefix | |
103 | ||
104 | const matchedTokens = tokens.filter(t => t.startsWith(prefix)) | |
105 | .map(t => t.slice(prefix.length)) // Keep the value filter | |
1a7d0887 | 106 | .map(t => t.replace(/^"|"$/g, '')) // Remove "" |
feb34f6b C |
107 | .map(t => { |
108 | if (prefixObj.handler) return prefixObj.handler(t) | |
109 | ||
1a7d0887 C |
110 | if (prefixObj.isBoolean) { |
111 | if (t === 'true') return true | |
112 | if (t === 'false') return false | |
113 | ||
114 | return undefined | |
115 | } | |
116 | ||
feb34f6b C |
117 | return t |
118 | }) | |
1a7d0887 | 119 | .filter(t => t !== null && t !== undefined) |
feb34f6b C |
120 | |
121 | if (matchedTokens.length === 0) continue | |
122 | ||
123 | additionalFilters[prefixKey] = prefixObj.multiple === true | |
124 | ? matchedTokens | |
125 | : matchedTokens[0] | |
126 | } | |
127 | ||
231ff4af C |
128 | const search = searchTokens.join(' ') || undefined |
129 | ||
130 | logger('Built search: ' + search, additionalFilters) | |
131 | ||
feb34f6b | 132 | return { |
231ff4af | 133 | search, |
feb34f6b C |
134 | |
135 | ...additionalFilters | |
136 | } | |
137 | } | |
dd6d2a7c C |
138 | |
139 | tokenizeString (q: string) { | |
140 | if (!q) return [] | |
141 | ||
142 | // Tokenize the strings using spaces that are not in quotes | |
143 | return q.match(/(?:[^\s"]+|"[^"]*")+/g) | |
144 | .filter(token => !!token) | |
145 | } | |
de59c48f | 146 | } |