]>
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 | }, |
3b3b1820 C |
45 | email: { |
46 | enabled: false | |
47 | }, | |
3866f1a0 C |
48 | contactForm: { |
49 | enabled: false | |
50 | }, | |
915c5bbe | 51 | serverVersion: 'Unknown', |
db7af09b | 52 | signup: { |
ff2c1fe8 | 53 | allowed: false, |
d9eaee39 JM |
54 | allowedForCurrentIP: false, |
55 | requiresEmailVerification: false | |
6a84aafd C |
56 | }, |
57 | transcoding: { | |
09209296 C |
58 | enabledResolutions: [], |
59 | hls: { | |
60 | enabled: false | |
61 | } | |
01de67b9 C |
62 | }, |
63 | avatar: { | |
64 | file: { | |
65 | size: { max: 0 }, | |
66 | extensions: [] | |
67 | } | |
68 | }, | |
69 | video: { | |
6de36768 C |
70 | image: { |
71 | size: { max: 0 }, | |
72 | extensions: [] | |
73 | }, | |
01de67b9 C |
74 | file: { |
75 | extensions: [] | |
76 | } | |
1869c875 | 77 | }, |
40e87e9e C |
78 | videoCaption: { |
79 | file: { | |
80 | size: { max: 0 }, | |
81 | extensions: [] | |
82 | } | |
83 | }, | |
1869c875 | 84 | user: { |
bee0abff FA |
85 | videoQuota: -1, |
86 | videoQuotaDaily: -1 | |
5d08a6a7 C |
87 | }, |
88 | import: { | |
b2977eec | 89 | videos: { |
5d08a6a7 C |
90 | http: { |
91 | enabled: false | |
a84b8fa5 C |
92 | }, |
93 | torrent: { | |
94 | enabled: false | |
5d08a6a7 C |
95 | } |
96 | } | |
9b4b15f9 AB |
97 | }, |
98 | trending: { | |
99 | videos: { | |
100 | intervalDays: 0 | |
101 | } | |
7ccddd7b JM |
102 | }, |
103 | autoBlacklist: { | |
104 | videos: { | |
105 | ofUsers: { | |
106 | enabled: false | |
107 | } | |
108 | } | |
31b6ddf8 C |
109 | }, |
110 | tracker: { | |
111 | enabled: true | |
db7af09b C |
112 | } |
113 | } | |
8cd7faaa C |
114 | private videoCategories: Array<VideoConstant<number>> = [] |
115 | private videoLicences: Array<VideoConstant<number>> = [] | |
9d3ef9fe | 116 | private videoLanguages: Array<VideoConstant<string>> = [] |
8cd7faaa | 117 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] |
830b4faf | 118 | private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = [] |
db7af09b | 119 | |
7ce44a74 C |
120 | constructor ( |
121 | private http: HttpClient, | |
122 | @Inject(LOCALE_ID) private localeId: string | |
123 | ) { | |
7ce44a74 | 124 | this.loadServerLocale() |
74b7c6d4 | 125 | this.loadConfigLocally() |
36f9424f | 126 | } |
db7af09b C |
127 | |
128 | loadConfig () { | |
129 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
db400f44 C |
130 | .pipe(tap(this.saveConfigLocally)) |
131 | .subscribe(data => { | |
132 | this.config = data | |
00b5556c | 133 | |
db400f44 C |
134 | this.configLoaded.next(true) |
135 | }) | |
db7af09b C |
136 | } |
137 | ||
138 | loadVideoCategories () { | |
830b4faf | 139 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true) |
db7af09b C |
140 | } |
141 | ||
142 | loadVideoLicences () { | |
830b4faf | 143 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded) |
db7af09b C |
144 | } |
145 | ||
146 | loadVideoLanguages () { | |
830b4faf | 147 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true) |
db7af09b C |
148 | } |
149 | ||
fd45e8f4 | 150 | loadVideoPrivacies () { |
830b4faf C |
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 | ) | |
fd45e8f4 C |
161 | } |
162 | ||
db7af09b | 163 | getConfig () { |
12bec528 | 164 | return cloneDeep(this.config) |
db7af09b C |
165 | } |
166 | ||
167 | getVideoCategories () { | |
12bec528 | 168 | return cloneDeep(this.videoCategories) |
db7af09b C |
169 | } |
170 | ||
171 | getVideoLicences () { | |
12bec528 | 172 | return cloneDeep(this.videoLicences) |
db7af09b C |
173 | } |
174 | ||
175 | getVideoLanguages () { | |
12bec528 | 176 | return cloneDeep(this.videoLanguages) |
db7af09b C |
177 | } |
178 | ||
fd45e8f4 | 179 | getVideoPrivacies () { |
12bec528 | 180 | return cloneDeep(this.videoPrivacies) |
fd45e8f4 C |
181 | } |
182 | ||
830b4faf | 183 | getVideoPlaylistPrivacies () { |
12bec528 | 184 | return cloneDeep(this.videoPlaylistPrivacies) |
830b4faf C |
185 | } |
186 | ||
187 | private loadAttributeEnum ( | |
188 | baseUrl: string, | |
fd45e8f4 | 189 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
8cd7faaa | 190 | hashToPopulate: VideoConstant<string | number>[], |
3580fc00 C |
191 | notifier: ReplaySubject<boolean>, |
192 | sort = false | |
fd45e8f4 | 193 | ) { |
7ce44a74 C |
194 | this.localeObservable |
195 | .pipe( | |
196 | switchMap(translations => { | |
830b4faf | 197 | return this.http.get<{ [id: string]: string }>(baseUrl + attributeName) |
c199c427 | 198 | .pipe(map(data => ({ data, translations }))) |
7ce44a74 C |
199 | }) |
200 | ) | |
201 | .subscribe(({ data, translations }) => { | |
202 | Object.keys(data) | |
203 | .forEach(dataKey => { | |
204 | const label = data[ dataKey ] | |
205 | ||
206 | hashToPopulate.push({ | |
8cd7faaa | 207 | id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10), |
7ce44a74 C |
208 | label: peertubeTranslate(label, translations) |
209 | }) | |
210 | }) | |
211 | ||
ad774752 | 212 | if (sort === true) sortBy(hashToPopulate, 'label') |
7ce44a74 C |
213 | |
214 | notifier.next(true) | |
215 | }) | |
216 | } | |
217 | ||
218 | private loadServerLocale () { | |
74b7c6d4 | 219 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) |
7ce44a74 C |
220 | |
221 | // Default locale, nothing to translate | |
74b7c6d4 | 222 | if (isDefaultLocale(completeLocale)) { |
4bda2e47 | 223 | this.localeObservable = of({}).pipe(shareReplay()) |
74b7c6d4 C |
224 | return |
225 | } | |
7ce44a74 C |
226 | |
227 | this.localeObservable = this.http | |
74b7c6d4 | 228 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') |
4bda2e47 | 229 | .pipe(shareReplay()) |
db7af09b | 230 | } |
36f9424f C |
231 | |
232 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 233 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
234 | } |
235 | ||
236 | private loadConfigLocally () { | |
0bd78bf3 | 237 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
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 | } | |
db7af09b | 248 | } |