]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
5fb2e288 1import { Observable, of, Subject } from 'rxjs'
ba430d75 2import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
db7af09b 3import { HttpClient } from '@angular/common/http'
7ce44a74 4import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4504f09f 5import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers'
bd45d503
C
6import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
7import { SearchTargetType, ServerConfig, ServerStats, VideoConstant } from '@shared/models'
5fb2e288 8import { environment } from '../../../environments/environment'
db7af09b
C
9
10@Injectable()
11export class ServerService {
63c4db6d
C
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
830b4faf 14 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
7ce44a74 15 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
b764380a
C
16 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
17
72c33e71 18 configReloaded = new Subject<ServerConfig>()
baeefe22 19
ba430d75
C
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
db7af09b 31 private config: ServerConfig = {
36f9424f 32 instance: {
00b5556c 33 name: 'PeerTube',
63ac2857
C
34 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
35 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
f8802489 36 isNSFW: false,
0883b324 37 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
3da68f0a 38 defaultClientRoute: '',
00b5556c
C
39 customizations: {
40 javascript: '',
41 css: ''
42 }
36f9424f 43 },
7cd4d2ba 44 plugin: {
4a8d113b
C
45 registered: [],
46 registeredExternalAuths: [],
47 registeredIdAndPassAuths: []
7cd4d2ba
C
48 },
49 theme: {
50 registered: [],
51 default: 'default'
52 },
3b3b1820
C
53 email: {
54 enabled: false
55 },
3866f1a0
C
56 contactForm: {
57 enabled: false
58 },
915c5bbe 59 serverVersion: 'Unknown',
db7af09b 60 signup: {
ff2c1fe8 61 allowed: false,
d9eaee39
JM
62 allowedForCurrentIP: false,
63 requiresEmailVerification: false
6a84aafd
C
64 },
65 transcoding: {
80ac2e55
C
66 profile: 'default',
67 availableProfiles: [ 'default' ],
09209296
C
68 enabledResolutions: [],
69 hls: {
70 enabled: false
5a71acd2
C
71 },
72 webtorrent: {
73 enabled: true
09209296 74 }
01de67b9 75 },
c6c0fa6c
C
76 live: {
77 enabled: false,
fb719404
C
78 allowReplay: true,
79 maxDuration: null,
a056ca48
C
80 maxInstanceLives: -1,
81 maxUserLives: -1,
c6c0fa6c
C
82 transcoding: {
83 enabled: false,
80ac2e55
C
84 profile: 'default',
85 availableProfiles: [ 'default' ],
c6c0fa6c 86 enabledResolutions: []
4f20856e
C
87 },
88 rtmp: {
89 port: 1935
c6c0fa6c
C
90 }
91 },
01de67b9
C
92 avatar: {
93 file: {
94 size: { max: 0 },
95 extensions: []
96 }
97 },
cdeddff1
C
98 banner: {
99 file: {
100 size: { max: 0 },
101 extensions: []
102 }
103 },
01de67b9 104 video: {
6de36768
C
105 image: {
106 size: { max: 0 },
107 extensions: []
108 },
01de67b9
C
109 file: {
110 extensions: []
111 }
1869c875 112 },
40e87e9e
C
113 videoCaption: {
114 file: {
115 size: { max: 0 },
116 extensions: []
117 }
118 },
1869c875 119 user: {
bee0abff
FA
120 videoQuota: -1,
121 videoQuotaDaily: -1
5d08a6a7
C
122 },
123 import: {
b2977eec 124 videos: {
5d08a6a7
C
125 http: {
126 enabled: false
a84b8fa5
C
127 },
128 torrent: {
129 enabled: false
5d08a6a7
C
130 }
131 }
9b4b15f9
AB
132 },
133 trending: {
134 videos: {
ba5d4a84
RK
135 intervalDays: 0,
136 algorithms: {
3d4e112d 137 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
ba5d4a84
RK
138 default: 'most-viewed'
139 }
9b4b15f9 140 }
7ccddd7b
JM
141 },
142 autoBlacklist: {
143 videos: {
144 ofUsers: {
145 enabled: false
146 }
147 }
31b6ddf8
C
148 },
149 tracker: {
150 enabled: true
f24c8b14
RK
151 },
152 followings: {
153 instance: {
154 autoFollowIndex: {
155 indexUrl: 'https://instances.joinpeertube.org'
156 }
157 }
72c33e71
C
158 },
159 broadcastMessage: {
160 enabled: false,
161 message: '',
162 level: 'info',
163 dismissable: false
5fb2e288
C
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 }
db7af09b
C
176 }
177 }
db7af09b 178
7ce44a74
C
179 constructor (
180 private http: HttpClient,
181 @Inject(LOCALE_ID) private localeId: string
182 ) {
74b7c6d4 183 this.loadConfigLocally()
36f9424f 184 }
db7af09b 185
ba430d75
C
186 getServerVersionAndCommit () {
187 const serverVersion = this.config.serverVersion
188 const commit = this.config.serverCommit || ''
00b5556c 189
ba430d75
C
190 let result = serverVersion
191 if (commit) result += '...' + commit
db7af09b 192
ba430d75 193 return result
db7af09b
C
194 }
195
ba430d75
C
196 resetConfig () {
197 this.configLoaded = false
198 this.configReset = true
72c33e71
C
199
200 // Notify config update
201 this.getConfig().subscribe(() => {
202 // empty, to fire a reset config event
203 })
db7af09b
C
204 }
205
ba430d75
C
206 getConfig () {
207 if (this.configLoaded) return of(this.config)
db7af09b 208
ba430d75
C
209 if (!this.configObservable) {
210 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
211 .pipe(
017fbe18
C
212 tap(config => {
213 this.config = config
214 this.configLoaded = true
215 }),
72c33e71 216 tap(config => {
ba430d75 217 if (this.configReset) {
72c33e71 218 this.configReloaded.next(config)
ba430d75
C
219 this.configReset = false
220 }
221 }),
222 share()
223 )
224 }
830b4faf 225
ba430d75 226 return this.configObservable
fd45e8f4
C
227 }
228
ba430d75
C
229 getTmpConfig () {
230 return this.config
dbdf2d51
C
231 }
232
db7af09b 233 getVideoCategories () {
ba430d75
C
234 if (!this.videoCategoriesObservable) {
235 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
236 }
237
238 return this.videoCategoriesObservable.pipe(first())
db7af09b
C
239 }
240
241 getVideoLicences () {
ba430d75
C
242 if (!this.videoLicensesObservable) {
243 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
244 }
245
246 return this.videoLicensesObservable.pipe(first())
db7af09b
C
247 }
248
249 getVideoLanguages () {
ba430d75
C
250 if (!this.videoLanguagesObservable) {
251 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
252 }
253
254 return this.videoLanguagesObservable.pipe(first())
db7af09b
C
255 }
256
fd45e8f4 257 getVideoPrivacies () {
ba430d75
C
258 if (!this.videoPrivaciesObservable) {
259 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
260 }
261
262 return this.videoPrivaciesObservable.pipe(first())
fd45e8f4
C
263 }
264
830b4faf 265 getVideoPlaylistPrivacies () {
ba430d75
C
266 if (!this.videoPlaylistPrivaciesObservable) {
267 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
268 }
269
270 return this.videoPlaylistPrivaciesObservable.pipe(first())
271 }
272
273 getServerLocale () {
274 if (!this.localeObservable) {
275 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
276
277 // Default locale, nothing to translate
278 if (isDefaultLocale(completeLocale)) {
279 this.localeObservable = of({}).pipe(shareReplay())
280 } else {
281 this.localeObservable = this.http
282 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
283 .pipe(shareReplay())
284 }
285 }
286
287 return this.localeObservable.pipe(first())
830b4faf
C
288 }
289
b764380a
C
290 getServerStats () {
291 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
292 }
293
5fb2e288
C
294 getDefaultSearchTarget (): Promise<SearchTargetType> {
295 return this.getConfig().pipe(
296 map(config => {
297 const searchIndexConfig = config.search.searchIndex
298
299 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
300 return 'search-index'
301 }
302
303 return 'local'
304 })
305 ).toPromise()
306 }
307
ba430d75 308 private loadAttributeEnum <T extends string | number> (
830b4faf 309 baseUrl: string,
fd45e8f4 310 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
3580fc00 311 sort = false
fd45e8f4 312 ) {
ba430d75
C
313 return this.getServerLocale()
314 .pipe(
315 switchMap(translations => {
316 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
317 .pipe(map(data => ({ data, translations })))
318 }),
319 map(({ data, translations }) => {
111fdc26
C
320 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
321 .map(dataKey => {
322 const label = data[ dataKey ]
323
324 const id = attributeName === 'languages'
325 ? dataKey as T
326 : parseInt(dataKey, 10) as T
327
328 return {
329 id,
330 label: peertubeTranslate(label, translations)
331 }
332 })
ba430d75
C
333
334 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74 335
ba430d75
C
336 return hashToPopulate
337 }),
338 shareReplay()
339 )
db7af09b 340 }
36f9424f 341
36f9424f 342 private loadConfigLocally () {
aea0b0e7
C
343 const configString = window['PeerTubeServerConfig']
344 if (!configString) return
345
346 try {
347 const parsed = JSON.parse(configString)
348 Object.assign(this.config, parsed)
349 } catch (err) {
350 console.error('Cannot parse config saved in from index.html.', err)
36f9424f
C
351 }
352 }
db7af09b 353}