]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Instance homepage support (#4007)
[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 }
2539932e
C
176 },
177 homepage: {
178 enabled: false
db7af09b
C
179 }
180 }
db7af09b 181
7ce44a74
C
182 constructor (
183 private http: HttpClient,
184 @Inject(LOCALE_ID) private localeId: string
185 ) {
74b7c6d4 186 this.loadConfigLocally()
36f9424f 187 }
db7af09b 188
ba430d75
C
189 getServerVersionAndCommit () {
190 const serverVersion = this.config.serverVersion
191 const commit = this.config.serverCommit || ''
00b5556c 192
ba430d75
C
193 let result = serverVersion
194 if (commit) result += '...' + commit
db7af09b 195
ba430d75 196 return result
db7af09b
C
197 }
198
ba430d75
C
199 resetConfig () {
200 this.configLoaded = false
201 this.configReset = true
72c33e71
C
202
203 // Notify config update
2539932e 204 return this.getConfig()
db7af09b
C
205 }
206
ba430d75
C
207 getConfig () {
208 if (this.configLoaded) return of(this.config)
db7af09b 209
ba430d75
C
210 if (!this.configObservable) {
211 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
212 .pipe(
017fbe18
C
213 tap(config => {
214 this.config = config
215 this.configLoaded = true
216 }),
72c33e71 217 tap(config => {
ba430d75 218 if (this.configReset) {
72c33e71 219 this.configReloaded.next(config)
ba430d75
C
220 this.configReset = false
221 }
222 }),
223 share()
224 )
225 }
830b4faf 226
ba430d75 227 return this.configObservable
fd45e8f4
C
228 }
229
ba430d75
C
230 getTmpConfig () {
231 return this.config
dbdf2d51
C
232 }
233
db7af09b 234 getVideoCategories () {
ba430d75
C
235 if (!this.videoCategoriesObservable) {
236 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
237 }
238
239 return this.videoCategoriesObservable.pipe(first())
db7af09b
C
240 }
241
242 getVideoLicences () {
ba430d75
C
243 if (!this.videoLicensesObservable) {
244 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
245 }
246
247 return this.videoLicensesObservable.pipe(first())
db7af09b
C
248 }
249
250 getVideoLanguages () {
ba430d75
C
251 if (!this.videoLanguagesObservable) {
252 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
253 }
254
255 return this.videoLanguagesObservable.pipe(first())
db7af09b
C
256 }
257
fd45e8f4 258 getVideoPrivacies () {
ba430d75
C
259 if (!this.videoPrivaciesObservable) {
260 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
261 }
262
263 return this.videoPrivaciesObservable.pipe(first())
fd45e8f4
C
264 }
265
830b4faf 266 getVideoPlaylistPrivacies () {
ba430d75
C
267 if (!this.videoPlaylistPrivaciesObservable) {
268 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
269 }
270
271 return this.videoPlaylistPrivaciesObservable.pipe(first())
272 }
273
274 getServerLocale () {
275 if (!this.localeObservable) {
276 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
277
278 // Default locale, nothing to translate
279 if (isDefaultLocale(completeLocale)) {
280 this.localeObservable = of({}).pipe(shareReplay())
281 } else {
282 this.localeObservable = this.http
283 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
284 .pipe(shareReplay())
285 }
286 }
287
288 return this.localeObservable.pipe(first())
830b4faf
C
289 }
290
b764380a
C
291 getServerStats () {
292 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
293 }
294
5fb2e288
C
295 getDefaultSearchTarget (): Promise<SearchTargetType> {
296 return this.getConfig().pipe(
297 map(config => {
298 const searchIndexConfig = config.search.searchIndex
299
300 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
301 return 'search-index'
302 }
303
304 return 'local'
305 })
306 ).toPromise()
307 }
308
ba430d75 309 private loadAttributeEnum <T extends string | number> (
830b4faf 310 baseUrl: string,
fd45e8f4 311 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
3580fc00 312 sort = false
fd45e8f4 313 ) {
ba430d75
C
314 return this.getServerLocale()
315 .pipe(
316 switchMap(translations => {
317 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
318 .pipe(map(data => ({ data, translations })))
319 }),
320 map(({ data, translations }) => {
111fdc26
C
321 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
322 .map(dataKey => {
323 const label = data[ dataKey ]
324
325 const id = attributeName === 'languages'
326 ? dataKey as T
327 : parseInt(dataKey, 10) as T
328
329 return {
330 id,
331 label: peertubeTranslate(label, translations)
332 }
333 })
ba430d75
C
334
335 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74 336
ba430d75
C
337 return hashToPopulate
338 }),
339 shareReplay()
340 )
db7af09b 341 }
36f9424f 342
36f9424f 343 private loadConfigLocally () {
aea0b0e7
C
344 const configString = window['PeerTubeServerConfig']
345 if (!configString) return
346
347 try {
348 const parsed = JSON.parse(configString)
349 Object.assign(this.config, parsed)
350 } catch (err) {
351 console.error('Cannot parse config saved in from index.html.', err)
36f9424f
C
352 }
353 }
db7af09b 354}