Add import http enabled configuration
[oweals/peertube.git] / client / src / app / core / server / server.service.ts
1 import { map, share, 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-local-storage'
5 import { Observable, of, ReplaySubject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { environment } from '../../../environments/environment'
9 import { VideoConstant } from '../../../../../shared/models/videos'
10 import { isDefaultLocale } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-utils'
12 import { sortBy } from '@app/shared/misc/utils'
13
14 @Injectable()
15 export class ServerService {
16   private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
17   private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
18   private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
19   private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
20
21   configLoaded = new ReplaySubject<boolean>(1)
22   videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
23   videoCategoriesLoaded = new ReplaySubject<boolean>(1)
24   videoLicencesLoaded = new ReplaySubject<boolean>(1)
25   videoLanguagesLoaded = new ReplaySubject<boolean>(1)
26   localeObservable: Observable<any>
27
28   private config: ServerConfig = {
29     instance: {
30       name: 'PeerTube',
31       shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform  ' +
32                         'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
33       defaultClientRoute: '',
34       defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
35       customizations: {
36         javascript: '',
37         css: ''
38       }
39     },
40     serverVersion: 'Unknown',
41     signup: {
42       allowed: false,
43       allowedForCurrentIP: false
44     },
45     transcoding: {
46       enabledResolutions: []
47     },
48     avatar: {
49       file: {
50         size: { max: 0 },
51         extensions: []
52       }
53     },
54     video: {
55       image: {
56         size: { max: 0 },
57         extensions: []
58       },
59       file: {
60         extensions: []
61       }
62     },
63     videoCaption: {
64       file: {
65         size: { max: 0 },
66         extensions: []
67       }
68     },
69     user: {
70       videoQuota: -1
71     },
72     import: {
73       video: {
74         http: {
75           enabled: false
76         }
77       }
78     }
79   }
80   private videoCategories: Array<VideoConstant<string>> = []
81   private videoLicences: Array<VideoConstant<string>> = []
82   private videoLanguages: Array<VideoConstant<string>> = []
83   private videoPrivacies: Array<VideoConstant<string>> = []
84
85   constructor (
86     private http: HttpClient,
87     @Inject(LOCALE_ID) private localeId: string
88   ) {
89     this.loadServerLocale()
90     this.loadConfigLocally()
91   }
92
93   loadConfig () {
94     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
95         .pipe(tap(this.saveConfigLocally))
96         .subscribe(data => {
97           this.config = data
98
99           this.configLoaded.next(true)
100         })
101   }
102
103   loadVideoCategories () {
104     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
105   }
106
107   loadVideoLicences () {
108     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
109   }
110
111   loadVideoLanguages () {
112     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
113   }
114
115   loadVideoPrivacies () {
116     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
117   }
118
119   getConfig () {
120     return this.config
121   }
122
123   getVideoCategories () {
124     return this.videoCategories
125   }
126
127   getVideoLicences () {
128     return this.videoLicences
129   }
130
131   getVideoLanguages () {
132     return this.videoLanguages
133   }
134
135   getVideoPrivacies () {
136     return this.videoPrivacies
137   }
138
139   getAbout () {
140     return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
141   }
142
143   private loadVideoAttributeEnum (
144     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
145     hashToPopulate: VideoConstant<string>[],
146     notifier: ReplaySubject<boolean>,
147     sort = false
148   ) {
149     this.localeObservable
150         .pipe(
151           switchMap(translations => {
152             return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
153                        .pipe(map(data => ({ data, translations })))
154           })
155         )
156         .subscribe(({ data, translations }) => {
157           Object.keys(data)
158                 .forEach(dataKey => {
159                   const label = data[ dataKey ]
160
161                   hashToPopulate.push({
162                     id: dataKey,
163                     label: peertubeTranslate(label, translations)
164                   })
165                 })
166
167           if (sort === true) sortBy(hashToPopulate, 'label')
168
169           notifier.next(true)
170         })
171   }
172
173   private loadServerLocale () {
174     const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
175
176     // Default locale, nothing to translate
177     if (isDefaultLocale(completeLocale)) {
178       this.localeObservable = of({}).pipe(share())
179       return
180     }
181
182     this.localeObservable = this.http
183                                   .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
184                                   .pipe(share())
185   }
186
187   private saveConfigLocally (config: ServerConfig) {
188     peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
189   }
190
191   private loadConfigLocally () {
192     const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
193
194     if (configString) {
195       try {
196         const parsed = JSON.parse(configString)
197         Object.assign(this.config, parsed)
198       } catch (err) {
199         console.error('Cannot parse config saved in local storage.', err)
200       }
201     }
202   }
203 }