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