]>
Commit | Line | Data |
---|---|---|
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 | 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 | }, | |
70 | avatar: { | |
71 | file: { | |
72 | size: { max: 0 }, | |
73 | extensions: [] | |
74 | } | |
75 | }, | |
76 | video: { | |
77 | image: { | |
78 | size: { max: 0 }, | |
79 | extensions: [] | |
80 | }, | |
81 | file: { | |
82 | extensions: [] | |
83 | } | |
84 | }, | |
85 | videoCaption: { | |
86 | file: { | |
87 | size: { max: 0 }, | |
88 | extensions: [] | |
89 | } | |
90 | }, | |
91 | user: { | |
92 | videoQuota: -1, | |
93 | videoQuotaDaily: -1 | |
94 | }, | |
95 | import: { | |
96 | videos: { | |
97 | http: { | |
98 | enabled: false | |
99 | }, | |
100 | torrent: { | |
101 | enabled: false | |
102 | } | |
103 | } | |
104 | }, | |
105 | trending: { | |
106 | videos: { | |
107 | intervalDays: 0 | |
108 | } | |
109 | }, | |
110 | autoBlacklist: { | |
111 | videos: { | |
112 | ofUsers: { | |
113 | enabled: false | |
114 | } | |
115 | } | |
116 | }, | |
117 | tracker: { | |
118 | enabled: true | |
119 | } | |
120 | } | |
121 | private videoCategories: Array<VideoConstant<number>> = [] | |
122 | private videoLicences: Array<VideoConstant<number>> = [] | |
123 | private videoLanguages: Array<VideoConstant<string>> = [] | |
124 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] | |
125 | private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = [] | |
126 | ||
127 | constructor ( | |
128 | private http: HttpClient, | |
129 | @Inject(LOCALE_ID) private localeId: string | |
130 | ) { | |
131 | this.loadServerLocale() | |
132 | this.loadConfigLocally() | |
133 | } | |
134 | ||
135 | loadConfig () { | |
136 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
137 | .pipe(tap(this.saveConfigLocally)) | |
138 | .subscribe(data => { | |
139 | this.config = data | |
140 | ||
141 | this.configLoaded.next(true) | |
142 | }) | |
143 | } | |
144 | ||
145 | loadVideoCategories () { | |
146 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true) | |
147 | } | |
148 | ||
149 | loadVideoLicences () { | |
150 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded) | |
151 | } | |
152 | ||
153 | loadVideoLanguages () { | |
154 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true) | |
155 | } | |
156 | ||
157 | loadVideoPrivacies () { | |
158 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded) | |
159 | } | |
160 | ||
161 | loadVideoPlaylistPrivacies () { | |
162 | return this.loadAttributeEnum( | |
163 | ServerService.BASE_VIDEO_PLAYLIST_URL, | |
164 | 'privacies', | |
165 | this.videoPlaylistPrivacies, | |
166 | this.videoPlaylistPrivaciesLoaded | |
167 | ) | |
168 | } | |
169 | ||
170 | getConfig () { | |
171 | return cloneDeep(this.config) | |
172 | } | |
173 | ||
174 | getVideoCategories () { | |
175 | return cloneDeep(this.videoCategories) | |
176 | } | |
177 | ||
178 | getVideoLicences () { | |
179 | return cloneDeep(this.videoLicences) | |
180 | } | |
181 | ||
182 | getVideoLanguages () { | |
183 | return cloneDeep(this.videoLanguages) | |
184 | } | |
185 | ||
186 | getVideoPrivacies () { | |
187 | return cloneDeep(this.videoPrivacies) | |
188 | } | |
189 | ||
190 | getVideoPlaylistPrivacies () { | |
191 | return cloneDeep(this.videoPlaylistPrivacies) | |
192 | } | |
193 | ||
194 | private loadAttributeEnum ( | |
195 | baseUrl: string, | |
196 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
197 | hashToPopulate: VideoConstant<string | number>[], | |
198 | notifier: ReplaySubject<boolean>, | |
199 | sort = false | |
200 | ) { | |
201 | this.localeObservable | |
202 | .pipe( | |
203 | switchMap(translations => { | |
204 | return this.http.get<{ [id: string]: string }>(baseUrl + attributeName) | |
205 | .pipe(map(data => ({ data, translations }))) | |
206 | }) | |
207 | ) | |
208 | .subscribe(({ data, translations }) => { | |
209 | Object.keys(data) | |
210 | .forEach(dataKey => { | |
211 | const label = data[ dataKey ] | |
212 | ||
213 | hashToPopulate.push({ | |
214 | id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10), | |
215 | label: peertubeTranslate(label, translations) | |
216 | }) | |
217 | }) | |
218 | ||
219 | if (sort === true) sortBy(hashToPopulate, 'label') | |
220 | ||
221 | notifier.next(true) | |
222 | }) | |
223 | } | |
224 | ||
225 | private loadServerLocale () { | |
226 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
227 | ||
228 | // Default locale, nothing to translate | |
229 | if (isDefaultLocale(completeLocale)) { | |
230 | this.localeObservable = of({}).pipe(shareReplay()) | |
231 | return | |
232 | } | |
233 | ||
234 | this.localeObservable = this.http | |
235 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
236 | .pipe(shareReplay()) | |
237 | } | |
238 | ||
239 | private saveConfigLocally (config: ServerConfig) { | |
240 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) | |
241 | } | |
242 | ||
243 | private loadConfigLocally () { | |
244 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) | |
245 | ||
246 | if (configString) { | |
247 | try { | |
248 | const parsed = JSON.parse(configString) | |
249 | Object.assign(this.config, parsed) | |
250 | } catch (err) { | |
251 | console.error('Cannot parse config saved in local storage.', err) | |
252 | } | |
253 | } | |
254 | } | |
255 | } |