]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { SortMeta } from 'primeng/api'
2 import { HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ComponentPaginationLight } from './component-pagination.model'
5 import { RestPagination } from './rest-pagination'
6
7 interface QueryStringFilterPrefixes {
8 [key: string]: {
9 prefix: string
10 handler?: (v: string) => string | number
11 multiple?: boolean
12 isBoolean?: boolean
13 }
14 }
15
16 type ParseQueryStringFilters <K extends keyof any> = Partial<Record<K, string | number | boolean | (string | number | boolean)[]>>
17 type ParseQueryStringFiltersResult <K extends keyof any> = ParseQueryStringFilters<K> & { search?: string }
18
19 @Injectable()
20 export class RestService {
21
22 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
23 let newParams = params
24
25 if (pagination !== undefined) {
26 newParams = newParams.set('start', pagination.start.toString())
27 .set('count', pagination.count.toString())
28 }
29
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)
41 }
42
43 return newParams
44 }
45
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
54 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
55 for (const name of Object.keys(object)) {
56 const value = object[name]
57 if (value === undefined || value === null) continue
58
59 if (Array.isArray(value)) {
60 params = this.addArrayParams(params, name, value)
61 } else {
62 params = params.append(name, value)
63 }
64 }
65
66 return params
67 }
68
69 componentToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
70 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
71 const count: number = componentPagination.itemsPerPage
72
73 return { start, count }
74 }
75
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> {
80 if (!q) return {}
81
82 // Tokenize the strings using spaces that are not in quotes
83 const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
84 .filter(token => !!token)
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
95 const additionalFilters: ParseQueryStringFilters<keyof T> = {}
96
97 for (const prefixKey of Object.keys(prefixes) as (keyof T)[]) {
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
103 .map(t => t.replace(/^"|"$/g, '')) // Remove ""
104 .map(t => {
105 if (prefixObj.handler) return prefixObj.handler(t)
106
107 if (prefixObj.isBoolean) {
108 if (t === 'true') return true
109 if (t === 'false') return false
110
111 return undefined
112 }
113
114 return t
115 })
116 .filter(t => t !== null && t !== undefined)
117
118 if (matchedTokens.length === 0) continue
119
120 additionalFilters[prefixKey] = prefixObj.multiple === true
121 ? matchedTokens
122 : matchedTokens[0]
123 }
124
125 return {
126 search: searchTokens.join(' ') || undefined,
127
128 ...additionalFilters
129 }
130 }
131 }