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