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