]>
Commit | Line | Data |
---|---|---|
4bda2e47 | 1 | import { map, shareReplay, switchMap, tap } from 'rxjs/operators' |
db7af09b | 2 | import { HttpClient } from '@angular/common/http' |
7ce44a74 | 3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
0bd78bf3 | 4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
ad774752 | 5 | import { Observable, of, ReplaySubject } from 'rxjs' |
74b7c6d4 | 6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' |
63c4db6d | 7 | import { environment } from '../../../environments/environment' |
8cd7faaa | 8 | import { VideoConstant, VideoPrivacy } 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' |
830b4faf | 12 | import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model' |
12bec528 | 13 | import { cloneDeep } from 'lodash-es' |
db7af09b C |
14 | |
15 | @Injectable() | |
16 | export class ServerService { | |
d3e56c0c | 17 | private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/' |
63c4db6d C |
18 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
19 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
830b4faf | 20 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' |
7ce44a74 | 21 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' |
36f9424f | 22 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' |
db7af09b | 23 | |
00b5556c | 24 | configLoaded = new ReplaySubject<boolean>(1) |
baeefe22 | 25 | videoPrivaciesLoaded = new ReplaySubject<boolean>(1) |
830b4faf | 26 | videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1) |
baeefe22 C |
27 | videoCategoriesLoaded = new ReplaySubject<boolean>(1) |
28 | videoLicencesLoaded = new ReplaySubject<boolean>(1) | |
29 | videoLanguagesLoaded = new ReplaySubject<boolean>(1) | |
7ce44a74 | 30 | localeObservable: Observable<any> |
baeefe22 | 31 | |
db7af09b | 32 | private config: ServerConfig = { |
36f9424f | 33 | instance: { |
00b5556c | 34 | name: 'PeerTube', |
63ac2857 C |
35 | shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' + |
36 | 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.', | |
901637bb | 37 | defaultClientRoute: '', |
f8802489 | 38 | isNSFW: false, |
0883b324 | 39 | defaultNSFWPolicy: 'do_not_list' as 'do_not_list', |
00b5556c C |
40 | customizations: { |
41 | javascript: '', | |
42 | css: '' | |
43 | } | |
36f9424f | 44 | }, |
7cd4d2ba C |
45 | plugin: { |
46 | registered: [] | |
47 | }, | |
48 | theme: { | |
49 | registered: [], | |
50 | default: 'default' | |
51 | }, | |
3b3b1820 C |
52 | email: { |
53 | enabled: false | |
54 | }, | |
3866f1a0 C |
55 | contactForm: { |
56 | enabled: false | |
57 | }, | |
915c5bbe | 58 | serverVersion: 'Unknown', |
db7af09b | 59 | signup: { |
ff2c1fe8 | 60 | allowed: false, |
d9eaee39 JM |
61 | allowedForCurrentIP: false, |
62 | requiresEmailVerification: false | |
6a84aafd C |
63 | }, |
64 | transcoding: { | |
09209296 C |
65 | enabledResolutions: [], |
66 | hls: { | |
67 | enabled: false | |
68 | } | |
01de67b9 C |
69 | }, |
70 | avatar: { | |
71 | file: { | |
72 | size: { max: 0 }, | |
73 | extensions: [] | |
74 | } | |
75 | }, | |
76 | video: { | |
6de36768 C |
77 | image: { |
78 | size: { max: 0 }, | |
79 | extensions: [] | |
80 | }, | |
01de67b9 C |
81 | file: { |
82 | extensions: [] | |
83 | } | |
1869c875 | 84 | }, |
40e87e9e C |
85 | videoCaption: { |
86 | file: { | |
87 | size: { max: 0 }, | |
88 | extensions: [] | |
89 | } | |
90 | }, | |
1869c875 | 91 | user: { |
bee0abff FA |
92 | videoQuota: -1, |
93 | videoQuotaDaily: -1 | |
5d08a6a7 C |
94 | }, |
95 | import: { | |
b2977eec | 96 | videos: { |
5d08a6a7 C |
97 | http: { |
98 | enabled: false | |
a84b8fa5 C |
99 | }, |
100 | torrent: { | |
101 | enabled: false | |
5d08a6a7 C |
102 | } |
103 | } | |
9b4b15f9 AB |
104 | }, |
105 | trending: { | |
106 | videos: { | |
107 | intervalDays: 0 | |
108 | } | |
7ccddd7b JM |
109 | }, |
110 | autoBlacklist: { | |
111 | videos: { | |
112 | ofUsers: { | |
113 | enabled: false | |
114 | } | |
115 | } | |
31b6ddf8 C |
116 | }, |
117 | tracker: { | |
118 | enabled: true | |
db7af09b C |
119 | } |
120 | } | |
8cd7faaa C |
121 | private videoCategories: Array<VideoConstant<number>> = [] |
122 | private videoLicences: Array<VideoConstant<number>> = [] | |
9d3ef9fe | 123 | private videoLanguages: Array<VideoConstant<string>> = [] |
8cd7faaa | 124 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] |
830b4faf | 125 | private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = [] |
db7af09b | 126 | |
7ce44a74 C |
127 | constructor ( |
128 | private http: HttpClient, | |
129 | @Inject(LOCALE_ID) private localeId: string | |
130 | ) { | |
7ce44a74 | 131 | this.loadServerLocale() |
74b7c6d4 | 132 | this.loadConfigLocally() |
36f9424f | 133 | } |
db7af09b C |
134 | |
135 | loadConfig () { | |
136 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
db400f44 C |
137 | .pipe(tap(this.saveConfigLocally)) |
138 | .subscribe(data => { | |
139 | this.config = data | |
00b5556c | 140 | |
db400f44 C |
141 | this.configLoaded.next(true) |
142 | }) | |
db7af09b C |
143 | } |
144 | ||
145 | loadVideoCategories () { | |
830b4faf | 146 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true) |
db7af09b C |
147 | } |
148 | ||
149 | loadVideoLicences () { | |
830b4faf | 150 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded) |
db7af09b C |
151 | } |
152 | ||
153 | loadVideoLanguages () { | |
830b4faf | 154 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true) |
db7af09b C |
155 | } |
156 | ||
fd45e8f4 | 157 | loadVideoPrivacies () { |
830b4faf C |
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 | ) | |
fd45e8f4 C |
168 | } |
169 | ||
db7af09b | 170 | getConfig () { |
12bec528 | 171 | return cloneDeep(this.config) |
db7af09b C |
172 | } |
173 | ||
174 | getVideoCategories () { | |
12bec528 | 175 | return cloneDeep(this.videoCategories) |
db7af09b C |
176 | } |
177 | ||
178 | getVideoLicences () { | |
12bec528 | 179 | return cloneDeep(this.videoLicences) |
db7af09b C |
180 | } |
181 | ||
182 | getVideoLanguages () { | |
12bec528 | 183 | return cloneDeep(this.videoLanguages) |
db7af09b C |
184 | } |
185 | ||
fd45e8f4 | 186 | getVideoPrivacies () { |
12bec528 | 187 | return cloneDeep(this.videoPrivacies) |
fd45e8f4 C |
188 | } |
189 | ||
830b4faf | 190 | getVideoPlaylistPrivacies () { |
12bec528 | 191 | return cloneDeep(this.videoPlaylistPrivacies) |
830b4faf C |
192 | } |
193 | ||
194 | private loadAttributeEnum ( | |
195 | baseUrl: string, | |
fd45e8f4 | 196 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
8cd7faaa | 197 | hashToPopulate: VideoConstant<string | number>[], |
3580fc00 C |
198 | notifier: ReplaySubject<boolean>, |
199 | sort = false | |
fd45e8f4 | 200 | ) { |
7ce44a74 C |
201 | this.localeObservable |
202 | .pipe( | |
203 | switchMap(translations => { | |
830b4faf | 204 | return this.http.get<{ [id: string]: string }>(baseUrl + attributeName) |
c199c427 | 205 | .pipe(map(data => ({ data, translations }))) |
7ce44a74 C |
206 | }) |
207 | ) | |
208 | .subscribe(({ data, translations }) => { | |
209 | Object.keys(data) | |
210 | .forEach(dataKey => { | |
211 | const label = data[ dataKey ] | |
212 | ||
213 | hashToPopulate.push({ | |
8cd7faaa | 214 | id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10), |
7ce44a74 C |
215 | label: peertubeTranslate(label, translations) |
216 | }) | |
217 | }) | |
218 | ||
ad774752 | 219 | if (sort === true) sortBy(hashToPopulate, 'label') |
7ce44a74 C |
220 | |
221 | notifier.next(true) | |
222 | }) | |
223 | } | |
224 | ||
225 | private loadServerLocale () { | |
74b7c6d4 | 226 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) |
7ce44a74 C |
227 | |
228 | // Default locale, nothing to translate | |
74b7c6d4 | 229 | if (isDefaultLocale(completeLocale)) { |
4bda2e47 | 230 | this.localeObservable = of({}).pipe(shareReplay()) |
74b7c6d4 C |
231 | return |
232 | } | |
7ce44a74 C |
233 | |
234 | this.localeObservable = this.http | |
74b7c6d4 | 235 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') |
4bda2e47 | 236 | .pipe(shareReplay()) |
db7af09b | 237 | } |
36f9424f C |
238 | |
239 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 240 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
241 | } |
242 | ||
243 | private loadConfigLocally () { | |
0bd78bf3 | 244 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
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 | } | |
db7af09b | 255 | } |