Add import.video.torrent 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       videos: {
74         http: {
75           enabled: false
76         },
77         torrent: {
78           enabled: false
79         }
80       }
81     }
82   }
83   private videoCategories: Array<VideoConstant<string>> = []
84   private videoLicences: Array<VideoConstant<string>> = []
85   private videoLanguages: Array<VideoConstant<string>> = []
86   private videoPrivacies: Array<VideoConstant<string>> = []
87
88   constructor (
89     private http: HttpClient,
90     @Inject(LOCALE_ID) private localeId: string
91   ) {
92     this.loadServerLocale()
93     this.loadConfigLocally()
94   }
95
96   loadConfig () {
97     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
98         .pipe(tap(this.saveConfigLocally))
99         .subscribe(data => {
100           this.config = data
101
102           this.configLoaded.next(true)
103         })
104   }
105
106   loadVideoCategories () {
107     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
108   }
109
110   loadVideoLicences () {
111     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
112   }
113
114   loadVideoLanguages () {
115     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
116   }
117
118   loadVideoPrivacies () {
119     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
120   }
121
122   getConfig () {
123     return this.config
124   }
125
126   getVideoCategories () {
127     return this.videoCategories
128   }
129
130   getVideoLicences () {
131     return this.videoLicences
132   }
133
134   getVideoLanguages () {
135     return this.videoLanguages
136   }
137
138   getVideoPrivacies () {
139     return this.videoPrivacies
140   }
141
142   getAbout () {
143     return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
144   }
145
146   private loadVideoAttributeEnum (
147     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
148     hashToPopulate: VideoConstant<string>[],
149     notifier: ReplaySubject<boolean>,
150     sort = false
151   ) {
152     this.localeObservable
153         .pipe(
154           switchMap(translations => {
155             return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
156                        .pipe(map(data => ({ data, translations })))
157           })
158         )
159         .subscribe(({ data, translations }) => {
160           Object.keys(data)
161                 .forEach(dataKey => {
162                   const label = data[ dataKey ]
163
164                   hashToPopulate.push({
165                     id: dataKey,
166                     label: peertubeTranslate(label, translations)
167                   })
168                 })
169
170           if (sort === true) sortBy(hashToPopulate, 'label')
171
172           notifier.next(true)
173         })
174   }
175
176   private loadServerLocale () {
177     const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
178
179     // Default locale, nothing to translate
180     if (isDefaultLocale(completeLocale)) {
181       this.localeObservable = of({}).pipe(share())
182       return
183     }
184
185     this.localeObservable = this.http
186                                   .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
187                                   .pipe(share())
188   }
189
190   private saveConfigLocally (config: ServerConfig) {
191     peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
192   }
193
194   private loadConfigLocally () {
195     const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
196
197     if (configString) {
198       try {
199         const parsed = JSON.parse(configString)
200         Object.assign(this.config, parsed)
201       } catch (err) {
202         console.error('Cannot parse config saved in local storage.', err)
203       }
204     }
205   }
206 }