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