]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
paginate response for abuse list in openapi spec
[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 },
cdeddff1
C
101 banner: {
102 file: {
103 size: { max: 0 },
104 extensions: []
105 }
106 },
01de67b9 107 video: {
6de36768
C
108 image: {
109 size: { max: 0 },
110 extensions: []
111 },
01de67b9
C
112 file: {
113 extensions: []
114 }
1869c875 115 },
40e87e9e
C
116 videoCaption: {
117 file: {
118 size: { max: 0 },
119 extensions: []
120 }
121 },
1869c875 122 user: {
bee0abff
FA
123 videoQuota: -1,
124 videoQuotaDaily: -1
5d08a6a7
C
125 },
126 import: {
b2977eec 127 videos: {
5d08a6a7
C
128 http: {
129 enabled: false
a84b8fa5
C
130 },
131 torrent: {
132 enabled: false
5d08a6a7
C
133 }
134 }
9b4b15f9
AB
135 },
136 trending: {
137 videos: {
ba5d4a84
RK
138 intervalDays: 0,
139 algorithms: {
3d4e112d 140 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
ba5d4a84
RK
141 default: 'most-viewed'
142 }
9b4b15f9 143 }
7ccddd7b
JM
144 },
145 autoBlacklist: {
146 videos: {
147 ofUsers: {
148 enabled: false
149 }
150 }
31b6ddf8
C
151 },
152 tracker: {
153 enabled: true
f24c8b14
RK
154 },
155 followings: {
156 instance: {
157 autoFollowIndex: {
158 indexUrl: 'https://instances.joinpeertube.org'
159 }
160 }
72c33e71
C
161 },
162 broadcastMessage: {
163 enabled: false,
164 message: '',
165 level: 'info',
166 dismissable: false
5fb2e288
C
167 },
168 search: {
169 remoteUri: {
170 users: true,
171 anonymous: false
172 },
173 searchIndex: {
174 enabled: false,
175 url: '',
176 disableLocalSearch: false,
177 isDefaultSearch: false
178 }
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
204 this.getConfig().subscribe(() => {
205 // empty, to fire a reset config event
206 })
db7af09b
C
207 }
208
ba430d75
C
209 getConfig () {
210 if (this.configLoaded) return of(this.config)
db7af09b 211
ba430d75
C
212 if (!this.configObservable) {
213 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
214 .pipe(
017fbe18
C
215 tap(config => this.saveConfigLocally(config)),
216 tap(config => {
217 this.config = config
218 this.configLoaded = true
219 }),
72c33e71 220 tap(config => {
ba430d75 221 if (this.configReset) {
72c33e71 222 this.configReloaded.next(config)
ba430d75
C
223 this.configReset = false
224 }
225 }),
226 share()
227 )
228 }
830b4faf 229
ba430d75 230 return this.configObservable
fd45e8f4
C
231 }
232
ba430d75
C
233 getTmpConfig () {
234 return this.config
dbdf2d51
C
235 }
236
db7af09b 237 getVideoCategories () {
ba430d75
C
238 if (!this.videoCategoriesObservable) {
239 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
240 }
241
242 return this.videoCategoriesObservable.pipe(first())
db7af09b
C
243 }
244
245 getVideoLicences () {
ba430d75
C
246 if (!this.videoLicensesObservable) {
247 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
248 }
249
250 return this.videoLicensesObservable.pipe(first())
db7af09b
C
251 }
252
253 getVideoLanguages () {
ba430d75
C
254 if (!this.videoLanguagesObservable) {
255 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
256 }
257
258 return this.videoLanguagesObservable.pipe(first())
db7af09b
C
259 }
260
fd45e8f4 261 getVideoPrivacies () {
ba430d75
C
262 if (!this.videoPrivaciesObservable) {
263 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
264 }
265
266 return this.videoPrivaciesObservable.pipe(first())
fd45e8f4
C
267 }
268
830b4faf 269 getVideoPlaylistPrivacies () {
ba430d75
C
270 if (!this.videoPlaylistPrivaciesObservable) {
271 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
272 }
273
274 return this.videoPlaylistPrivaciesObservable.pipe(first())
275 }
276
277 getServerLocale () {
278 if (!this.localeObservable) {
279 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
280
281 // Default locale, nothing to translate
282 if (isDefaultLocale(completeLocale)) {
283 this.localeObservable = of({}).pipe(shareReplay())
284 } else {
285 this.localeObservable = this.http
286 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
287 .pipe(shareReplay())
288 }
289 }
290
291 return this.localeObservable.pipe(first())
830b4faf
C
292 }
293
b764380a
C
294 getServerStats () {
295 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
296 }
297
5fb2e288
C
298 getDefaultSearchTarget (): Promise<SearchTargetType> {
299 return this.getConfig().pipe(
300 map(config => {
301 const searchIndexConfig = config.search.searchIndex
302
303 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
304 return 'search-index'
305 }
306
307 return 'local'
308 })
309 ).toPromise()
310 }
311
ba430d75 312 private loadAttributeEnum <T extends string | number> (
830b4faf 313 baseUrl: string,
fd45e8f4 314 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
3580fc00 315 sort = false
fd45e8f4 316 ) {
ba430d75
C
317 return this.getServerLocale()
318 .pipe(
319 switchMap(translations => {
320 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
321 .pipe(map(data => ({ data, translations })))
322 }),
323 map(({ data, translations }) => {
111fdc26
C
324 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
325 .map(dataKey => {
326 const label = data[ dataKey ]
327
328 const id = attributeName === 'languages'
329 ? dataKey as T
330 : parseInt(dataKey, 10) as T
331
332 return {
333 id,
334 label: peertubeTranslate(label, translations)
335 }
336 })
ba430d75
C
337
338 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74 339
ba430d75
C
340 return hashToPopulate
341 }),
342 shareReplay()
343 )
db7af09b 344 }
36f9424f
C
345
346 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 347 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
348 }
349
350 private loadConfigLocally () {
0bd78bf3 351 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
352
353 if (configString) {
354 try {
355 const parsed = JSON.parse(configString)
356 Object.assign(this.config, parsed)
357 } catch (err) {
358 console.error('Cannot parse config saved in local storage.', err)
359 }
360 }
361 }
db7af09b 362}