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