]>
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: { | |
09209296 C |
54 | enabledResolutions: [], |
55 | hls: { | |
56 | enabled: false | |
57 | } | |
01de67b9 C |
58 | }, |
59 | avatar: { | |
60 | file: { | |
61 | size: { max: 0 }, | |
62 | extensions: [] | |
63 | } | |
64 | }, | |
65 | video: { | |
6de36768 C |
66 | image: { |
67 | size: { max: 0 }, | |
68 | extensions: [] | |
69 | }, | |
01de67b9 C |
70 | file: { |
71 | extensions: [] | |
72 | } | |
1869c875 | 73 | }, |
40e87e9e C |
74 | videoCaption: { |
75 | file: { | |
76 | size: { max: 0 }, | |
77 | extensions: [] | |
78 | } | |
79 | }, | |
1869c875 | 80 | user: { |
bee0abff FA |
81 | videoQuota: -1, |
82 | videoQuotaDaily: -1 | |
5d08a6a7 C |
83 | }, |
84 | import: { | |
b2977eec | 85 | videos: { |
5d08a6a7 C |
86 | http: { |
87 | enabled: false | |
a84b8fa5 C |
88 | }, |
89 | torrent: { | |
90 | enabled: false | |
5d08a6a7 C |
91 | } |
92 | } | |
9b4b15f9 AB |
93 | }, |
94 | trending: { | |
95 | videos: { | |
96 | intervalDays: 0 | |
97 | } | |
db7af09b C |
98 | } |
99 | } | |
8cd7faaa C |
100 | private videoCategories: Array<VideoConstant<number>> = [] |
101 | private videoLicences: Array<VideoConstant<number>> = [] | |
9d3ef9fe | 102 | private videoLanguages: Array<VideoConstant<string>> = [] |
8cd7faaa | 103 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] |
db7af09b | 104 | |
7ce44a74 C |
105 | constructor ( |
106 | private http: HttpClient, | |
107 | @Inject(LOCALE_ID) private localeId: string | |
108 | ) { | |
7ce44a74 | 109 | this.loadServerLocale() |
74b7c6d4 | 110 | this.loadConfigLocally() |
36f9424f | 111 | } |
db7af09b C |
112 | |
113 | loadConfig () { | |
114 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
db400f44 C |
115 | .pipe(tap(this.saveConfigLocally)) |
116 | .subscribe(data => { | |
117 | this.config = data | |
00b5556c | 118 | |
db400f44 C |
119 | this.configLoaded.next(true) |
120 | }) | |
db7af09b C |
121 | } |
122 | ||
123 | loadVideoCategories () { | |
3580fc00 | 124 | return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true) |
db7af09b C |
125 | } |
126 | ||
127 | loadVideoLicences () { | |
baeefe22 | 128 | return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded) |
db7af09b C |
129 | } |
130 | ||
131 | loadVideoLanguages () { | |
3580fc00 | 132 | return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true) |
db7af09b C |
133 | } |
134 | ||
fd45e8f4 | 135 | loadVideoPrivacies () { |
baeefe22 | 136 | return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded) |
fd45e8f4 C |
137 | } |
138 | ||
db7af09b C |
139 | getConfig () { |
140 | return this.config | |
141 | } | |
142 | ||
143 | getVideoCategories () { | |
144 | return this.videoCategories | |
145 | } | |
146 | ||
147 | getVideoLicences () { | |
148 | return this.videoLicences | |
149 | } | |
150 | ||
151 | getVideoLanguages () { | |
152 | return this.videoLanguages | |
153 | } | |
154 | ||
fd45e8f4 C |
155 | getVideoPrivacies () { |
156 | return this.videoPrivacies | |
157 | } | |
158 | ||
159 | private loadVideoAttributeEnum ( | |
160 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
8cd7faaa | 161 | hashToPopulate: VideoConstant<string | number>[], |
3580fc00 C |
162 | notifier: ReplaySubject<boolean>, |
163 | sort = false | |
fd45e8f4 | 164 | ) { |
7ce44a74 C |
165 | this.localeObservable |
166 | .pipe( | |
167 | switchMap(translations => { | |
c199c427 C |
168 | return this.http.get<{ [id: string]: string }>(ServerService.BASE_VIDEO_URL + attributeName) |
169 | .pipe(map(data => ({ data, translations }))) | |
7ce44a74 C |
170 | }) |
171 | ) | |
172 | .subscribe(({ data, translations }) => { | |
173 | Object.keys(data) | |
174 | .forEach(dataKey => { | |
175 | const label = data[ dataKey ] | |
176 | ||
177 | hashToPopulate.push({ | |
8cd7faaa | 178 | id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10), |
7ce44a74 C |
179 | label: peertubeTranslate(label, translations) |
180 | }) | |
181 | }) | |
182 | ||
ad774752 | 183 | if (sort === true) sortBy(hashToPopulate, 'label') |
7ce44a74 C |
184 | |
185 | notifier.next(true) | |
186 | }) | |
187 | } | |
188 | ||
189 | private loadServerLocale () { | |
74b7c6d4 | 190 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) |
7ce44a74 C |
191 | |
192 | // Default locale, nothing to translate | |
74b7c6d4 | 193 | if (isDefaultLocale(completeLocale)) { |
4bda2e47 | 194 | this.localeObservable = of({}).pipe(shareReplay()) |
74b7c6d4 C |
195 | return |
196 | } | |
7ce44a74 C |
197 | |
198 | this.localeObservable = this.http | |
74b7c6d4 | 199 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') |
4bda2e47 | 200 | .pipe(shareReplay()) |
db7af09b | 201 | } |
36f9424f C |
202 | |
203 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 204 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
205 | } |
206 | ||
207 | private loadConfigLocally () { | |
0bd78bf3 | 208 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
209 | |
210 | if (configString) { | |
211 | try { | |
212 | const parsed = JSON.parse(configString) | |
213 | Object.assign(this.config, parsed) | |
214 | } catch (err) { | |
215 | console.error('Cannot parse config saved in local storage.', err) | |
216 | } | |
217 | } | |
218 | } | |
db7af09b | 219 | } |