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