Add contact form checkbox in admin form
[oweals/peertube.git] / client / src / app / core / server / server.service.ts
1 import { map, 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-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, VideoPrivacy } from '../../../../../shared/models/videos'
10 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale } 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     email: {
41       enabled: false
42     },
43     contactForm: {
44       enabled: false
45     },
46     serverVersion: 'Unknown',
47     signup: {
48       allowed: false,
49       allowedForCurrentIP: false,
50       requiresEmailVerification: false
51     },
52     transcoding: {
53       enabledResolutions: []
54     },
55     avatar: {
56       file: {
57         size: { max: 0 },
58         extensions: []
59       }
60     },
61     video: {
62       image: {
63         size: { max: 0 },
64         extensions: []
65       },
66       file: {
67         extensions: []
68       }
69     },
70     videoCaption: {
71       file: {
72         size: { max: 0 },
73         extensions: []
74       }
75     },
76     user: {
77       videoQuota: -1,
78       videoQuotaDaily: -1
79     },
80     import: {
81       videos: {
82         http: {
83           enabled: false
84         },
85         torrent: {
86           enabled: false
87         }
88       }
89     }
90   }
91   private videoCategories: Array<VideoConstant<number>> = []
92   private videoLicences: Array<VideoConstant<number>> = []
93   private videoLanguages: Array<VideoConstant<string>> = []
94   private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
95
96   constructor (
97     private http: HttpClient,
98     @Inject(LOCALE_ID) private localeId: string
99   ) {
100     this.loadServerLocale()
101     this.loadConfigLocally()
102   }
103
104   loadConfig () {
105     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
106         .pipe(tap(this.saveConfigLocally))
107         .subscribe(data => {
108           this.config = data
109
110           this.configLoaded.next(true)
111         })
112   }
113
114   loadVideoCategories () {
115     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
116   }
117
118   loadVideoLicences () {
119     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
120   }
121
122   loadVideoLanguages () {
123     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
124   }
125
126   loadVideoPrivacies () {
127     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
128   }
129
130   getConfig () {
131     return this.config
132   }
133
134   getVideoCategories () {
135     return this.videoCategories
136   }
137
138   getVideoLicences () {
139     return this.videoLicences
140   }
141
142   getVideoLanguages () {
143     return this.videoLanguages
144   }
145
146   getVideoPrivacies () {
147     return this.videoPrivacies
148   }
149
150   getAbout () {
151     return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
152   }
153
154   private loadVideoAttributeEnum (
155     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
156     hashToPopulate: VideoConstant<string | number>[],
157     notifier: ReplaySubject<boolean>,
158     sort = false
159   ) {
160     this.localeObservable
161         .pipe(
162           switchMap(translations => {
163             return this.http.get<{ [id: string]: string }>(ServerService.BASE_VIDEO_URL + attributeName)
164                        .pipe(map(data => ({ data, translations })))
165           })
166         )
167         .subscribe(({ data, translations }) => {
168           Object.keys(data)
169                 .forEach(dataKey => {
170                   const label = data[ dataKey ]
171
172                   hashToPopulate.push({
173                     id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
174                     label: peertubeTranslate(label, translations)
175                   })
176                 })
177
178           if (sort === true) sortBy(hashToPopulate, 'label')
179
180           notifier.next(true)
181         })
182   }
183
184   private loadServerLocale () {
185     const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
186
187     // Default locale, nothing to translate
188     if (isDefaultLocale(completeLocale)) {
189       this.localeObservable = of({}).pipe(shareReplay())
190       return
191     }
192
193     this.localeObservable = this.http
194                                   .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
195                                   .pipe(shareReplay())
196   }
197
198   private saveConfigLocally (config: ServerConfig) {
199     peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
200   }
201
202   private loadConfigLocally () {
203     const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
204
205     if (configString) {
206       try {
207         const parsed = JSON.parse(configString)
208         Object.assign(this.config, parsed)
209       } catch (err) {
210         console.error('Cannot parse config saved in local storage.', err)
211       }
212     }
213   }
214 }