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