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