]>
Commit | Line | Data |
---|---|---|
ba430d75 | 1 | import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators' |
db7af09b | 2 | import { HttpClient } from '@angular/common/http' |
7ce44a74 | 3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
88a7f93f | 4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
ba430d75 | 5 | import { Observable, of, Subject } from 'rxjs' |
74b7c6d4 | 6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' |
63c4db6d | 7 | import { environment } from '../../../environments/environment' |
ba430d75 | 8 | import { VideoConstant } from '../../../../../shared/models/videos' |
3dfa8494 C |
9 | import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' |
10 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' | |
ad774752 | 11 | import { sortBy } from '@app/shared/misc/utils' |
db7af09b C |
12 | |
13 | @Injectable() | |
14 | export class ServerService { | |
63c4db6d C |
15 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
16 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
830b4faf | 17 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' |
7ce44a74 | 18 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' |
36f9424f | 19 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' |
db7af09b | 20 | |
ba430d75 | 21 | configReloaded = new Subject<void>() |
baeefe22 | 22 | |
ba430d75 C |
23 | private localeObservable: Observable<any> |
24 | private videoLicensesObservable: Observable<VideoConstant<number>[]> | |
25 | private videoCategoriesObservable: Observable<VideoConstant<number>[]> | |
26 | private videoPrivaciesObservable: Observable<VideoConstant<number>[]> | |
27 | private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]> | |
28 | private videoLanguagesObservable: Observable<VideoConstant<string>[]> | |
29 | private configObservable: Observable<ServerConfig> | |
30 | ||
31 | private configReset = false | |
32 | ||
33 | private configLoaded = false | |
db7af09b | 34 | private config: ServerConfig = { |
36f9424f | 35 | instance: { |
00b5556c | 36 | name: 'PeerTube', |
63ac2857 C |
37 | shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' + |
38 | 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.', | |
901637bb | 39 | defaultClientRoute: '', |
f8802489 | 40 | isNSFW: false, |
0883b324 | 41 | defaultNSFWPolicy: 'do_not_list' as 'do_not_list', |
00b5556c C |
42 | customizations: { |
43 | javascript: '', | |
44 | css: '' | |
45 | } | |
36f9424f | 46 | }, |
7cd4d2ba C |
47 | plugin: { |
48 | registered: [] | |
49 | }, | |
50 | theme: { | |
51 | registered: [], | |
52 | default: 'default' | |
53 | }, | |
3b3b1820 C |
54 | email: { |
55 | enabled: false | |
56 | }, | |
3866f1a0 C |
57 | contactForm: { |
58 | enabled: false | |
59 | }, | |
915c5bbe | 60 | serverVersion: 'Unknown', |
db7af09b | 61 | signup: { |
ff2c1fe8 | 62 | allowed: false, |
d9eaee39 JM |
63 | allowedForCurrentIP: false, |
64 | requiresEmailVerification: false | |
6a84aafd C |
65 | }, |
66 | transcoding: { | |
09209296 C |
67 | enabledResolutions: [], |
68 | hls: { | |
69 | enabled: false | |
5a71acd2 C |
70 | }, |
71 | webtorrent: { | |
72 | enabled: true | |
09209296 | 73 | } |
01de67b9 C |
74 | }, |
75 | avatar: { | |
76 | file: { | |
77 | size: { max: 0 }, | |
78 | extensions: [] | |
79 | } | |
80 | }, | |
81 | video: { | |
6de36768 C |
82 | image: { |
83 | size: { max: 0 }, | |
84 | extensions: [] | |
85 | }, | |
01de67b9 C |
86 | file: { |
87 | extensions: [] | |
88 | } | |
1869c875 | 89 | }, |
40e87e9e C |
90 | videoCaption: { |
91 | file: { | |
92 | size: { max: 0 }, | |
93 | extensions: [] | |
94 | } | |
95 | }, | |
1869c875 | 96 | user: { |
bee0abff FA |
97 | videoQuota: -1, |
98 | videoQuotaDaily: -1 | |
5d08a6a7 C |
99 | }, |
100 | import: { | |
b2977eec | 101 | videos: { |
5d08a6a7 C |
102 | http: { |
103 | enabled: false | |
a84b8fa5 C |
104 | }, |
105 | torrent: { | |
106 | enabled: false | |
5d08a6a7 C |
107 | } |
108 | } | |
9b4b15f9 AB |
109 | }, |
110 | trending: { | |
111 | videos: { | |
112 | intervalDays: 0 | |
113 | } | |
7ccddd7b JM |
114 | }, |
115 | autoBlacklist: { | |
116 | videos: { | |
117 | ofUsers: { | |
118 | enabled: false | |
119 | } | |
120 | } | |
31b6ddf8 C |
121 | }, |
122 | tracker: { | |
123 | enabled: true | |
f24c8b14 RK |
124 | }, |
125 | followings: { | |
126 | instance: { | |
127 | autoFollowIndex: { | |
128 | indexUrl: 'https://instances.joinpeertube.org' | |
129 | } | |
130 | } | |
db7af09b C |
131 | } |
132 | } | |
db7af09b | 133 | |
7ce44a74 C |
134 | constructor ( |
135 | private http: HttpClient, | |
136 | @Inject(LOCALE_ID) private localeId: string | |
137 | ) { | |
74b7c6d4 | 138 | this.loadConfigLocally() |
36f9424f | 139 | } |
db7af09b | 140 | |
ba430d75 C |
141 | getServerVersionAndCommit () { |
142 | const serverVersion = this.config.serverVersion | |
143 | const commit = this.config.serverCommit || '' | |
00b5556c | 144 | |
ba430d75 C |
145 | let result = serverVersion |
146 | if (commit) result += '...' + commit | |
db7af09b | 147 | |
ba430d75 | 148 | return result |
db7af09b C |
149 | } |
150 | ||
ba430d75 C |
151 | resetConfig () { |
152 | this.configLoaded = false | |
153 | this.configReset = true | |
db7af09b C |
154 | } |
155 | ||
ba430d75 C |
156 | getConfig () { |
157 | if (this.configLoaded) return of(this.config) | |
db7af09b | 158 | |
ba430d75 C |
159 | if (!this.configObservable) { |
160 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
161 | .pipe( | |
162 | tap(this.saveConfigLocally), | |
163 | tap(() => this.configLoaded = true), | |
164 | tap(() => { | |
165 | if (this.configReset) { | |
166 | this.configReloaded.next() | |
167 | this.configReset = false | |
168 | } | |
169 | }), | |
170 | share() | |
171 | ) | |
172 | } | |
830b4faf | 173 | |
ba430d75 | 174 | return this.configObservable |
fd45e8f4 C |
175 | } |
176 | ||
ba430d75 C |
177 | getTmpConfig () { |
178 | return this.config | |
dbdf2d51 C |
179 | } |
180 | ||
db7af09b | 181 | getVideoCategories () { |
ba430d75 C |
182 | if (!this.videoCategoriesObservable) { |
183 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
184 | } | |
185 | ||
186 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
187 | } |
188 | ||
189 | getVideoLicences () { | |
ba430d75 C |
190 | if (!this.videoLicensesObservable) { |
191 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
192 | } | |
193 | ||
194 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
195 | } |
196 | ||
197 | getVideoLanguages () { | |
ba430d75 C |
198 | if (!this.videoLanguagesObservable) { |
199 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
200 | } | |
201 | ||
202 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
203 | } |
204 | ||
fd45e8f4 | 205 | getVideoPrivacies () { |
ba430d75 C |
206 | if (!this.videoPrivaciesObservable) { |
207 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
208 | } | |
209 | ||
210 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
211 | } |
212 | ||
830b4faf | 213 | getVideoPlaylistPrivacies () { |
ba430d75 C |
214 | if (!this.videoPlaylistPrivaciesObservable) { |
215 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
216 | } | |
217 | ||
218 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
219 | } | |
220 | ||
221 | getServerLocale () { | |
222 | if (!this.localeObservable) { | |
223 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
224 | ||
225 | // Default locale, nothing to translate | |
226 | if (isDefaultLocale(completeLocale)) { | |
227 | this.localeObservable = of({}).pipe(shareReplay()) | |
228 | } else { | |
229 | this.localeObservable = this.http | |
230 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
231 | .pipe(shareReplay()) | |
232 | } | |
233 | } | |
234 | ||
235 | return this.localeObservable.pipe(first()) | |
830b4faf C |
236 | } |
237 | ||
ba430d75 | 238 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 239 | baseUrl: string, |
fd45e8f4 | 240 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 241 | sort = false |
fd45e8f4 | 242 | ) { |
ba430d75 C |
243 | return this.getServerLocale() |
244 | .pipe( | |
245 | switchMap(translations => { | |
246 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
247 | .pipe(map(data => ({ data, translations }))) | |
248 | }), | |
249 | map(({ data, translations }) => { | |
250 | const hashToPopulate: VideoConstant<T>[] = [] | |
7ce44a74 | 251 | |
ba430d75 C |
252 | Object.keys(data) |
253 | .forEach(dataKey => { | |
254 | const label = data[ dataKey ] | |
7ce44a74 | 255 | |
ba430d75 C |
256 | hashToPopulate.push({ |
257 | id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T, | |
258 | label: peertubeTranslate(label, translations) | |
259 | }) | |
260 | }) | |
261 | ||
262 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 263 | |
ba430d75 C |
264 | return hashToPopulate |
265 | }), | |
266 | shareReplay() | |
267 | ) | |
db7af09b | 268 | } |
36f9424f C |
269 | |
270 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 271 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
272 | } |
273 | ||
274 | private loadConfigLocally () { | |
0bd78bf3 | 275 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
276 | |
277 | if (configString) { | |
278 | try { | |
279 | const parsed = JSON.parse(configString) | |
280 | Object.assign(this.config, parsed) | |
281 | } catch (err) { | |
282 | console.error('Cannot parse config saved in local storage.', err) | |
283 | } | |
284 | } | |
285 | } | |
db7af09b | 286 | } |