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