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