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