]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
3997ce6db02b5bbb320a5f6d8ba8e0c67c58a659
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
5 import { Observable, of, Subject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12
13 @Injectable()
14 export class ServerService {
15 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
16 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
17 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
18 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
19 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
20
21 configReloaded = new Subject<void>()
22
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
34 private config: ServerConfig = {
35 instance: {
36 name: 'PeerTube',
37 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
38 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
39 defaultClientRoute: '',
40 isNSFW: false,
41 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
42 customizations: {
43 javascript: '',
44 css: ''
45 }
46 },
47 plugin: {
48 registered: []
49 },
50 theme: {
51 registered: [],
52 default: 'default'
53 },
54 email: {
55 enabled: false
56 },
57 contactForm: {
58 enabled: false
59 },
60 serverVersion: 'Unknown',
61 signup: {
62 allowed: false,
63 allowedForCurrentIP: false,
64 requiresEmailVerification: false
65 },
66 transcoding: {
67 enabledResolutions: [],
68 hls: {
69 enabled: false
70 },
71 webtorrent: {
72 enabled: true
73 }
74 },
75 avatar: {
76 file: {
77 size: { max: 0 },
78 extensions: []
79 }
80 },
81 video: {
82 image: {
83 size: { max: 0 },
84 extensions: []
85 },
86 file: {
87 extensions: []
88 }
89 },
90 videoCaption: {
91 file: {
92 size: { max: 0 },
93 extensions: []
94 }
95 },
96 user: {
97 videoQuota: -1,
98 videoQuotaDaily: -1
99 },
100 import: {
101 videos: {
102 http: {
103 enabled: false
104 },
105 torrent: {
106 enabled: false
107 }
108 }
109 },
110 trending: {
111 videos: {
112 intervalDays: 0
113 }
114 },
115 autoBlacklist: {
116 videos: {
117 ofUsers: {
118 enabled: false
119 }
120 }
121 },
122 tracker: {
123 enabled: true
124 },
125 followings: {
126 instance: {
127 autoFollowIndex: {
128 indexUrl: 'https://instances.joinpeertube.org'
129 }
130 }
131 }
132 }
133
134 constructor (
135 private http: HttpClient,
136 @Inject(LOCALE_ID) private localeId: string
137 ) {
138 this.loadConfigLocally()
139 }
140
141 getServerVersionAndCommit () {
142 const serverVersion = this.config.serverVersion
143 const commit = this.config.serverCommit || ''
144
145 let result = serverVersion
146 if (commit) result += '...' + commit
147
148 return result
149 }
150
151 resetConfig () {
152 this.configLoaded = false
153 this.configReset = true
154 }
155
156 getConfig () {
157 if (this.configLoaded) return of(this.config)
158
159 if (!this.configObservable) {
160 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
161 .pipe(
162 tap(config => this.saveConfigLocally(config)),
163 tap(config => {
164 this.config = config
165 this.configLoaded = true
166 }),
167 tap(() => {
168 if (this.configReset) {
169 this.configReloaded.next()
170 this.configReset = false
171 }
172 }),
173 share()
174 )
175 }
176
177 return this.configObservable
178 }
179
180 getTmpConfig () {
181 return this.config
182 }
183
184 getVideoCategories () {
185 if (!this.videoCategoriesObservable) {
186 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
187 }
188
189 return this.videoCategoriesObservable.pipe(first())
190 }
191
192 getVideoLicences () {
193 if (!this.videoLicensesObservable) {
194 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
195 }
196
197 return this.videoLicensesObservable.pipe(first())
198 }
199
200 getVideoLanguages () {
201 if (!this.videoLanguagesObservable) {
202 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
203 }
204
205 return this.videoLanguagesObservable.pipe(first())
206 }
207
208 getVideoPrivacies () {
209 if (!this.videoPrivaciesObservable) {
210 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
211 }
212
213 return this.videoPrivaciesObservable.pipe(first())
214 }
215
216 getVideoPlaylistPrivacies () {
217 if (!this.videoPlaylistPrivaciesObservable) {
218 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
219 }
220
221 return this.videoPlaylistPrivaciesObservable.pipe(first())
222 }
223
224 getServerLocale () {
225 if (!this.localeObservable) {
226 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
227
228 // Default locale, nothing to translate
229 if (isDefaultLocale(completeLocale)) {
230 this.localeObservable = of({}).pipe(shareReplay())
231 } else {
232 this.localeObservable = this.http
233 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
234 .pipe(shareReplay())
235 }
236 }
237
238 return this.localeObservable.pipe(first())
239 }
240
241 private loadAttributeEnum <T extends string | number> (
242 baseUrl: string,
243 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
244 sort = false
245 ) {
246 return this.getServerLocale()
247 .pipe(
248 switchMap(translations => {
249 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
250 .pipe(map(data => ({ data, translations })))
251 }),
252 map(({ data, translations }) => {
253 const hashToPopulate: VideoConstant<T>[] = []
254
255 Object.keys(data)
256 .forEach(dataKey => {
257 const label = data[ dataKey ]
258
259 hashToPopulate.push({
260 id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T,
261 label: peertubeTranslate(label, translations)
262 })
263 })
264
265 if (sort === true) sortBy(hashToPopulate, 'label')
266
267 return hashToPopulate
268 }),
269 shareReplay()
270 )
271 }
272
273 private saveConfigLocally (config: ServerConfig) {
274 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
275 }
276
277 private loadConfigLocally () {
278 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
279
280 if (configString) {
281 try {
282 const parsed = JSON.parse(configString)
283 Object.assign(this.config, parsed)
284 } catch (err) {
285 console.error('Cannot parse config saved in local storage.', err)
286 }
287 }
288 }
289 }