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