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