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