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