]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest.service.ts
align feed icons to the right for video listings
[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
12 }
13}
14
15type ParseQueryStringFilterResult = {
16 [key: string]: string | number | (string | number)[]
17}
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
c199c427 46 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
f37dc0dd
C
47 for (const name of Object.keys(object)) {
48 const value = object[name]
1ebddadd 49 if (value === undefined || value === null) continue
f37dc0dd
C
50
51 if (Array.isArray(value) && value.length !== 0) {
52 for (const v of value) params = params.append(name, v)
53 } else {
54 params = params.append(name, value)
55 }
56 }
57
58 return params
59 }
60
440d39c5 61 componentPaginationToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
4635f59d
C
62 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
63 const count: number = componentPagination.itemsPerPage
64
65 return { start, count }
66 }
feb34f6b
C
67
68 parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult {
69 if (!q) return {}
70
cc0e0d32
C
71 // Tokenize the strings using spaces that are not in quotes
72 const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
73 .filter(token => !!token)
feb34f6b
C
74
75 // Build prefix array
76 const prefixeStrings = Object.values(prefixes)
77 .map(p => p.prefix)
78
79 // Search is the querystring minus defined filters
80 const searchTokens = tokens.filter(t => {
81 return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
82 })
83
84 const additionalFilters: ParseQueryStringFilterResult = {}
85
86 for (const prefixKey of Object.keys(prefixes)) {
87 const prefixObj = prefixes[prefixKey]
88 const prefix = prefixObj.prefix
89
90 const matchedTokens = tokens.filter(t => t.startsWith(prefix))
91 .map(t => t.slice(prefix.length)) // Keep the value filter
cc0e0d32 92 .map(t => t.replace(/^"|"$/g, ''))
feb34f6b
C
93 .map(t => {
94 if (prefixObj.handler) return prefixObj.handler(t)
95
96 return t
97 })
1ebddadd 98 .filter(t => !!t || t === 0)
feb34f6b
C
99
100 if (matchedTokens.length === 0) continue
101
102 additionalFilters[prefixKey] = prefixObj.multiple === true
103 ? matchedTokens
104 : matchedTokens[0]
105 }
106
107 return {
1ebddadd 108 search: searchTokens.join(' ') || undefined,
feb34f6b
C
109
110 ...additionalFilters
111 }
112 }
de59c48f 113}