]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Add stats route
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { HttpClient } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/do'
4 import { ReplaySubject } from 'rxjs/ReplaySubject'
5 import { ServerConfig } from '../../../../../shared'
6 import { About } from '../../../../../shared/models/server/about.model'
7 import { environment } from '../../../environments/environment'
8
9 @Injectable()
10 export class ServerService {
11 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
12 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
13 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
14
15 configLoaded = new ReplaySubject<boolean>(1)
16 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
17 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
18 videoLicencesLoaded = new ReplaySubject<boolean>(1)
19 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
20
21 private config: ServerConfig = {
22 instance: {
23 name: 'PeerTube',
24 customizations: {
25 javascript: '',
26 css: ''
27 }
28 },
29 serverVersion: 'Unknown',
30 signup: {
31 allowed: false
32 },
33 transcoding: {
34 enabledResolutions: []
35 },
36 avatar: {
37 file: {
38 size: { max: 0 },
39 extensions: []
40 }
41 },
42 video: {
43 image: {
44 size: { max: 0 },
45 extensions: []
46 },
47 file: {
48 extensions: []
49 }
50 }
51 }
52 private videoCategories: Array<{ id: number, label: string }> = []
53 private videoLicences: Array<{ id: number, label: string }> = []
54 private videoLanguages: Array<{ id: number, label: string }> = []
55 private videoPrivacies: Array<{ id: number, label: string }> = []
56
57 constructor (private http: HttpClient) {
58 this.loadConfigLocally()
59 }
60
61 loadConfig () {
62 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
63 .do(this.saveConfigLocally)
64 .subscribe(data => {
65 this.config = data
66
67 this.configLoaded.next(true)
68 })
69 }
70
71 loadVideoCategories () {
72 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
73 }
74
75 loadVideoLicences () {
76 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
77 }
78
79 loadVideoLanguages () {
80 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
81 }
82
83 loadVideoPrivacies () {
84 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
85 }
86
87 getConfig () {
88 return this.config
89 }
90
91 getVideoCategories () {
92 return this.videoCategories
93 }
94
95 getVideoLicences () {
96 return this.videoLicences
97 }
98
99 getVideoLanguages () {
100 return this.videoLanguages
101 }
102
103 getVideoPrivacies () {
104 return this.videoPrivacies
105 }
106
107 getAbout () {
108 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
109 }
110
111 private loadVideoAttributeEnum (
112 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
113 hashToPopulate: { id: number, label: string }[],
114 notifier: ReplaySubject<boolean>,
115 sort = false
116 ) {
117 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
118 .subscribe(data => {
119 Object.keys(data)
120 .forEach(dataKey => {
121 hashToPopulate.push({
122 id: parseInt(dataKey, 10),
123 label: data[dataKey]
124 })
125 })
126
127 if (sort === true) {
128 hashToPopulate.sort((a, b) => {
129 if (a.label < b.label) return -1
130 if (a.label === b.label) return 0
131 return 1
132 })
133 }
134
135 notifier.next(true)
136 })
137 }
138
139 private saveConfigLocally (config: ServerConfig) {
140 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
141 }
142
143 private loadConfigLocally () {
144 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
145
146 if (configString) {
147 try {
148 const parsed = JSON.parse(configString)
149 Object.assign(this.config, parsed)
150 } catch (err) {
151 console.error('Cannot parse config saved in local storage.', err)
152 }
153 }
154 }
155 }