]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Merge branch 'release/3.2.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 { 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 minimumAge: 16
65 },
66 transcoding: {
67 profile: 'default',
68 availableProfiles: [ 'default' ],
69 enabledResolutions: [],
70 hls: {
71 enabled: false
72 },
73 webtorrent: {
74 enabled: true
75 }
76 },
77 live: {
78 enabled: false,
79 allowReplay: true,
80 maxDuration: null,
81 maxInstanceLives: -1,
82 maxUserLives: -1,
83 transcoding: {
84 enabled: false,
85 profile: 'default',
86 availableProfiles: [ 'default' ],
87 enabledResolutions: []
88 },
89 rtmp: {
90 port: 1935
91 }
92 },
93 avatar: {
94 file: {
95 size: { max: 0 },
96 extensions: []
97 }
98 },
99 banner: {
100 file: {
101 size: { max: 0 },
102 extensions: []
103 }
104 },
105 video: {
106 image: {
107 size: { max: 0 },
108 extensions: []
109 },
110 file: {
111 extensions: []
112 }
113 },
114 videoCaption: {
115 file: {
116 size: { max: 0 },
117 extensions: []
118 }
119 },
120 user: {
121 videoQuota: -1,
122 videoQuotaDaily: -1
123 },
124 import: {
125 videos: {
126 http: {
127 enabled: false
128 },
129 torrent: {
130 enabled: false
131 }
132 }
133 },
134 trending: {
135 videos: {
136 intervalDays: 0,
137 algorithms: {
138 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
139 default: 'most-viewed'
140 }
141 }
142 },
143 autoBlacklist: {
144 videos: {
145 ofUsers: {
146 enabled: false
147 }
148 }
149 },
150 tracker: {
151 enabled: true
152 },
153 followings: {
154 instance: {
155 autoFollowIndex: {
156 indexUrl: 'https://instances.joinpeertube.org'
157 }
158 }
159 },
160 broadcastMessage: {
161 enabled: false,
162 message: '',
163 level: 'info',
164 dismissable: false
165 },
166 search: {
167 remoteUri: {
168 users: true,
169 anonymous: false
170 },
171 searchIndex: {
172 enabled: false,
173 url: '',
174 disableLocalSearch: false,
175 isDefaultSearch: false
176 }
177 },
178 homepage: {
179 enabled: false
180 }
181 }
182
183 constructor (
184 private http: HttpClient,
185 @Inject(LOCALE_ID) private localeId: string
186 ) {
187 this.loadConfigLocally()
188 }
189
190 getServerVersionAndCommit () {
191 const serverVersion = this.config.serverVersion
192 const commit = this.config.serverCommit || ''
193
194 let result = serverVersion
195 if (commit) result += '...' + commit
196
197 return result
198 }
199
200 resetConfig () {
201 this.configLoaded = false
202 this.configReset = true
203
204 // Notify config update
205 return this.getConfig()
206 }
207
208 getConfig () {
209 if (this.configLoaded) return of(this.config)
210
211 if (!this.configObservable) {
212 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
213 .pipe(
214 tap(config => {
215 this.config = config
216 this.configLoaded = true
217 }),
218 tap(config => {
219 if (this.configReset) {
220 this.configReloaded.next(config)
221 this.configReset = false
222 }
223 }),
224 share()
225 )
226 }
227
228 return this.configObservable
229 }
230
231 getTmpConfig () {
232 return this.config
233 }
234
235 getVideoCategories () {
236 if (!this.videoCategoriesObservable) {
237 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
238 }
239
240 return this.videoCategoriesObservable.pipe(first())
241 }
242
243 getVideoLicences () {
244 if (!this.videoLicensesObservable) {
245 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
246 }
247
248 return this.videoLicensesObservable.pipe(first())
249 }
250
251 getVideoLanguages () {
252 if (!this.videoLanguagesObservable) {
253 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
254 }
255
256 return this.videoLanguagesObservable.pipe(first())
257 }
258
259 getVideoPrivacies () {
260 if (!this.videoPrivaciesObservable) {
261 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
262 }
263
264 return this.videoPrivaciesObservable.pipe(first())
265 }
266
267 getVideoPlaylistPrivacies () {
268 if (!this.videoPlaylistPrivaciesObservable) {
269 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
270 }
271
272 return this.videoPlaylistPrivaciesObservable.pipe(first())
273 }
274
275 getServerLocale () {
276 if (!this.localeObservable) {
277 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
278
279 // Default locale, nothing to translate
280 if (isDefaultLocale(completeLocale)) {
281 this.localeObservable = of({}).pipe(shareReplay())
282 } else {
283 this.localeObservable = this.http
284 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
285 .pipe(shareReplay())
286 }
287 }
288
289 return this.localeObservable.pipe(first())
290 }
291
292 getServerStats () {
293 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
294 }
295
296 getDefaultSearchTarget (): Promise<SearchTargetType> {
297 return this.getConfig().pipe(
298 map(config => {
299 const searchIndexConfig = config.search.searchIndex
300
301 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
302 return 'search-index'
303 }
304
305 return 'local'
306 })
307 ).toPromise()
308 }
309
310 private loadAttributeEnum <T extends string | number> (
311 baseUrl: string,
312 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
313 sort = false
314 ) {
315 return this.getServerLocale()
316 .pipe(
317 switchMap(translations => {
318 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
319 .pipe(map(data => ({ data, translations })))
320 }),
321 map(({ data, translations }) => {
322 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
323 .map(dataKey => {
324 const label = data[ dataKey ]
325
326 const id = attributeName === 'languages'
327 ? dataKey as T
328 : parseInt(dataKey, 10) as T
329
330 return {
331 id,
332 label: peertubeTranslate(label, translations)
333 }
334 })
335
336 if (sort === true) sortBy(hashToPopulate, 'label')
337
338 return hashToPopulate
339 }),
340 shareReplay()
341 )
342 }
343
344 private loadConfigLocally () {
345 const configString = window['PeerTubeServerConfig']
346 if (!configString) return
347
348 try {
349 const parsed = JSON.parse(configString)
350 Object.assign(this.config, parsed)
351 } catch (err) {
352 console.error('Cannot parse config saved in from index.html.', err)
353 }
354 }
355 }