]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/rest/rest.service.ts
Don't display account setup modal on signup
[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 ParseQueryStringFilterResult = {
17 [key: string]: string | number | boolean | (string | number | boolean)[]
18 }
19
20 @Injectable()
21 export class RestService {
22
23 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
24 let newParams = params
25
26 if (pagination !== undefined) {
27 newParams = newParams.set('start', pagination.start.toString())
28 .set('count', pagination.count.toString())
29 }
30
31 if (sort !== undefined) {
32 let sortString = ''
33
34 if (typeof sort === 'string') {
35 sortString = sort
36 } else {
37 const sortPrefix = sort.order === 1 ? '' : '-'
38 sortString = sortPrefix + sort.field
39 }
40
41 newParams = newParams.set('sort', sortString)
42 }
43
44 return newParams
45 }
46
47 addArrayParams (params: HttpParams, name: string, values: (string | number)[]) {
48 for (const v of values) {
49 params = params.append(name, v)
50 }
51
52 return params
53 }
54
55 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
56 for (const name of Object.keys(object)) {
57 const value = object[name]
58 if (value === undefined || value === null) continue
59
60 if (Array.isArray(value)) {
61 params = this.addArrayParams(params, name, value)
62 } else {
63 params = params.append(name, value)
64 }
65 }
66
67 return params
68 }
69
70 componentPaginationToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
71 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
72 const count: number = componentPagination.itemsPerPage
73
74 return { start, count }
75 }
76
77 parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult {
78 if (!q) return {}
79
80 // Tokenize the strings using spaces that are not in quotes
81 const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
82 .filter(token => !!token)
83
84 // Build prefix array
85 const prefixeStrings = Object.values(prefixes)
86 .map(p => p.prefix)
87
88 // Search is the querystring minus defined filters
89 const searchTokens = tokens.filter(t => {
90 return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
91 })
92
93 const additionalFilters: ParseQueryStringFilterResult = {}
94
95 for (const prefixKey of Object.keys(prefixes)) {
96 const prefixObj = prefixes[prefixKey]
97 const prefix = prefixObj.prefix
98
99 const matchedTokens = tokens.filter(t => t.startsWith(prefix))
100 .map(t => t.slice(prefix.length)) // Keep the value filter
101 .map(t => t.replace(/^"|"$/g, '')) // Remove ""
102 .map(t => {
103 if (prefixObj.handler) return prefixObj.handler(t)
104
105 if (prefixObj.isBoolean) {
106 if (t === 'true') return true
107 if (t === 'false') return false
108
109 return undefined
110 }
111
112 return t
113 })
114 .filter(t => t !== null && t !== undefined)
115
116 if (matchedTokens.length === 0) continue
117
118 additionalFilters[prefixKey] = prefixObj.multiple === true
119 ? matchedTokens
120 : matchedTokens[0]
121 }
122
123 return {
124 search: searchTokens.join(' ') || undefined,
125
126 ...additionalFilters
127 }
128 }
129 }