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