]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
cf9c411a4fe7ead08f3544256f67d4e0cccc8247
[github/Chocobozzz/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-web-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 import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
13 import { cloneDeep } from 'lodash-es'
14
15 @Injectable()
16 export class ServerService {
17 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
18 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
19 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
20 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
21 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
23
24 configLoaded = new ReplaySubject<boolean>(1)
25 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
26 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
27 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
28 videoLicencesLoaded = new ReplaySubject<boolean>(1)
29 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
30 localeObservable: Observable<any>
31
32 private config: ServerConfig = {
33 instance: {
34 name: 'PeerTube',
35 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
36 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
37 defaultClientRoute: '',
38 isNSFW: false,
39 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
40 customizations: {
41 javascript: '',
42 css: ''
43 }
44 },
45 plugin: {
46 registered: []
47 },
48 theme: {
49 registered: [],
50 default: 'default'
51 },
52 email: {
53 enabled: false
54 },
55 contactForm: {
56 enabled: false
57 },
58 serverVersion: 'Unknown',
59 signup: {
60 allowed: false,
61 allowedForCurrentIP: false,
62 requiresEmailVerification: false
63 },
64 transcoding: {
65 enabledResolutions: [],
66 hls: {
67 enabled: false
68 },
69 webtorrent: {
70 enabled: true
71 }
72 },
73 avatar: {
74 file: {
75 size: { max: 0 },
76 extensions: []
77 }
78 },
79 video: {
80 image: {
81 size: { max: 0 },
82 extensions: []
83 },
84 file: {
85 extensions: []
86 }
87 },
88 videoCaption: {
89 file: {
90 size: { max: 0 },
91 extensions: []
92 }
93 },
94 user: {
95 videoQuota: -1,
96 videoQuotaDaily: -1
97 },
98 import: {
99 videos: {
100 http: {
101 enabled: false
102 },
103 torrent: {
104 enabled: false
105 }
106 }
107 },
108 trending: {
109 videos: {
110 intervalDays: 0
111 }
112 },
113 autoBlacklist: {
114 videos: {
115 ofUsers: {
116 enabled: false
117 }
118 }
119 },
120 tracker: {
121 enabled: true
122 }
123 }
124 private videoCategories: Array<VideoConstant<number>> = []
125 private videoLicences: Array<VideoConstant<number>> = []
126 private videoLanguages: Array<VideoConstant<string>> = []
127 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
128 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
129
130 constructor (
131 private http: HttpClient,
132 @Inject(LOCALE_ID) private localeId: string
133 ) {
134 this.loadServerLocale()
135 this.loadConfigLocally()
136 }
137
138 loadConfig () {
139 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
140 .pipe(tap(this.saveConfigLocally))
141 .subscribe(data => {
142 this.config = data
143
144 this.configLoaded.next(true)
145 })
146 }
147
148 loadVideoCategories () {
149 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
150 }
151
152 loadVideoLicences () {
153 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
154 }
155
156 loadVideoLanguages () {
157 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
158 }
159
160 loadVideoPrivacies () {
161 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
162 }
163
164 loadVideoPlaylistPrivacies () {
165 return this.loadAttributeEnum(
166 ServerService.BASE_VIDEO_PLAYLIST_URL,
167 'privacies',
168 this.videoPlaylistPrivacies,
169 this.videoPlaylistPrivaciesLoaded
170 )
171 }
172
173 getConfig () {
174 return cloneDeep(this.config)
175 }
176
177 getVideoCategories () {
178 return cloneDeep(this.videoCategories)
179 }
180
181 getVideoLicences () {
182 return cloneDeep(this.videoLicences)
183 }
184
185 getVideoLanguages () {
186 return cloneDeep(this.videoLanguages)
187 }
188
189 getVideoPrivacies () {
190 return cloneDeep(this.videoPrivacies)
191 }
192
193 getVideoPlaylistPrivacies () {
194 return cloneDeep(this.videoPlaylistPrivacies)
195 }
196
197 private loadAttributeEnum (
198 baseUrl: string,
199 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
200 hashToPopulate: VideoConstant<string | number>[],
201 notifier: ReplaySubject<boolean>,
202 sort = false
203 ) {
204 this.localeObservable
205 .pipe(
206 switchMap(translations => {
207 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
208 .pipe(map(data => ({ data, translations })))
209 })
210 )
211 .subscribe(({ data, translations }) => {
212 Object.keys(data)
213 .forEach(dataKey => {
214 const label = data[ dataKey ]
215
216 hashToPopulate.push({
217 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
218 label: peertubeTranslate(label, translations)
219 })
220 })
221
222 if (sort === true) sortBy(hashToPopulate, 'label')
223
224 notifier.next(true)
225 })
226 }
227
228 private loadServerLocale () {
229 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
230
231 // Default locale, nothing to translate
232 if (isDefaultLocale(completeLocale)) {
233 this.localeObservable = of({}).pipe(shareReplay())
234 return
235 }
236
237 this.localeObservable = this.http
238 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
239 .pipe(shareReplay())
240 }
241
242 private saveConfigLocally (config: ServerConfig) {
243 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
244 }
245
246 private loadConfigLocally () {
247 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
248
249 if (configString) {
250 try {
251 const parsed = JSON.parse(configString)
252 Object.assign(this.config, parsed)
253 } catch (err) {
254 console.error('Cannot parse config saved in local storage.', err)
255 }
256 }
257 }
258 }