]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
5 import { Observable, of, Subject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { ServerStats } from '@shared/models/server'
13
14 @Injectable()
15 export class ServerService {
16 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
17 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
18 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
19 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
20 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
21
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
23
24 configReloaded = new Subject<void>()
25
26 private localeObservable: Observable<any>
27 private videoLicensesObservable: Observable<VideoConstant<number>[]>
28 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
29 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
30 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
31 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
32 private configObservable: Observable<ServerConfig>
33
34 private configReset = false
35
36 private configLoaded = false
37 private config: ServerConfig = {
38 instance: {
39 name: 'PeerTube',
40 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
41 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
42 defaultClientRoute: '',
43 isNSFW: false,
44 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
45 customizations: {
46 javascript: '',
47 css: ''
48 }
49 },
50 plugin: {
51 registered: []
52 },
53 theme: {
54 registered: [],
55 default: 'default'
56 },
57 email: {
58 enabled: false
59 },
60 contactForm: {
61 enabled: false
62 },
63 serverVersion: 'Unknown',
64 signup: {
65 allowed: false,
66 allowedForCurrentIP: false,
67 requiresEmailVerification: false
68 },
69 transcoding: {
70 enabledResolutions: [],
71 hls: {
72 enabled: false
73 },
74 webtorrent: {
75 enabled: true
76 }
77 },
78 avatar: {
79 file: {
80 size: { max: 0 },
81 extensions: []
82 }
83 },
84 video: {
85 image: {
86 size: { max: 0 },
87 extensions: []
88 },
89 file: {
90 extensions: []
91 }
92 },
93 videoCaption: {
94 file: {
95 size: { max: 0 },
96 extensions: []
97 }
98 },
99 user: {
100 videoQuota: -1,
101 videoQuotaDaily: -1
102 },
103 import: {
104 videos: {
105 http: {
106 enabled: false
107 },
108 torrent: {
109 enabled: false
110 }
111 }
112 },
113 trending: {
114 videos: {
115 intervalDays: 0
116 }
117 },
118 autoBlacklist: {
119 videos: {
120 ofUsers: {
121 enabled: false
122 }
123 }
124 },
125 tracker: {
126 enabled: true
127 },
128 followings: {
129 instance: {
130 autoFollowIndex: {
131 indexUrl: 'https://instances.joinpeertube.org'
132 }
133 }
134 }
135 }
136
137 constructor (
138 private http: HttpClient,
139 @Inject(LOCALE_ID) private localeId: string
140 ) {
141 this.loadConfigLocally()
142 }
143
144 getServerVersionAndCommit () {
145 const serverVersion = this.config.serverVersion
146 const commit = this.config.serverCommit || ''
147
148 let result = serverVersion
149 if (commit) result += '...' + commit
150
151 return result
152 }
153
154 resetConfig () {
155 this.configLoaded = false
156 this.configReset = true
157 }
158
159 getConfig () {
160 if (this.configLoaded) return of(this.config)
161
162 if (!this.configObservable) {
163 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
164 .pipe(
165 tap(this.saveConfigLocally),
166 tap(() => this.configLoaded = true),
167 tap(() => {
168 if (this.configReset) {
169 this.configReloaded.next()
170 this.configReset = false
171 }
172 }),
173 share()
174 )
175 }
176
177 return this.configObservable
178 }
179
180 getTmpConfig () {
181 return this.config
182 }
183
184 getVideoCategories () {
185 if (!this.videoCategoriesObservable) {
186 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
187 }
188
189 return this.videoCategoriesObservable.pipe(first())
190 }
191
192 getVideoLicences () {
193 if (!this.videoLicensesObservable) {
194 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
195 }
196
197 return this.videoLicensesObservable.pipe(first())
198 }
199
200 getVideoLanguages () {
201 if (!this.videoLanguagesObservable) {
202 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
203 }
204
205 return this.videoLanguagesObservable.pipe(first())
206 }
207
208 getVideoPrivacies () {
209 if (!this.videoPrivaciesObservable) {
210 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
211 }
212
213 return this.videoPrivaciesObservable.pipe(first())
214 }
215
216 getVideoPlaylistPrivacies () {
217 if (!this.videoPlaylistPrivaciesObservable) {
218 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
219 }
220
221 return this.videoPlaylistPrivaciesObservable.pipe(first())
222 }
223
224 getServerLocale () {
225 if (!this.localeObservable) {
226 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
227
228 // Default locale, nothing to translate
229 if (isDefaultLocale(completeLocale)) {
230 this.localeObservable = of({}).pipe(shareReplay())
231 } else {
232 this.localeObservable = this.http
233 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
234 .pipe(shareReplay())
235 }
236 }
237
238 return this.localeObservable.pipe(first())
239 }
240
241 getServerStats () {
242 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
243 }
244
245 private loadAttributeEnum <T extends string | number> (
246 baseUrl: string,
247 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
248 sort = false
249 ) {
250 return this.getServerLocale()
251 .pipe(
252 switchMap(translations => {
253 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
254 .pipe(map(data => ({ data, translations })))
255 }),
256 map(({ data, translations }) => {
257 const hashToPopulate: VideoConstant<T>[] = []
258
259 Object.keys(data)
260 .forEach(dataKey => {
261 const label = data[ dataKey ]
262
263 hashToPopulate.push({
264 id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T,
265 label: peertubeTranslate(label, translations)
266 })
267 })
268
269 if (sort === true) sortBy(hashToPopulate, 'label')
270
271 return hashToPopulate
272 }),
273 shareReplay()
274 )
275 }
276
277 private saveConfigLocally (config: ServerConfig) {
278 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
279 }
280
281 private loadConfigLocally () {
282 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
283
284 if (configString) {
285 try {
286 const parsed = JSON.parse(configString)
287 Object.assign(this.config, parsed)
288 } catch (err) {
289 console.error('Cannot parse config saved in local storage.', err)
290 }
291 }
292 }
293 }