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