]>
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 | user: { | |
63 | videoQuota: -1 | |
64 | } | |
65 | } | |
66 | private videoCategories: Array<VideoConstant<number>> = [] | |
67 | private videoLicences: Array<VideoConstant<number>> = [] | |
68 | private videoLanguages: Array<VideoConstant<string>> = [] | |
69 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] | |
70 | ||
71 | constructor ( | |
72 | private http: HttpClient, | |
73 | @Inject(LOCALE_ID) private localeId: string | |
74 | ) { | |
75 | this.loadServerLocale() | |
76 | this.loadConfigLocally() | |
77 | } | |
78 | ||
79 | loadConfig () { | |
80 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
81 | .pipe(tap(this.saveConfigLocally)) | |
82 | .subscribe(data => { | |
83 | this.config = data | |
84 | ||
85 | this.configLoaded.next(true) | |
86 | }) | |
87 | } | |
88 | ||
89 | loadVideoCategories () { | |
90 | return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true) | |
91 | } | |
92 | ||
93 | loadVideoLicences () { | |
94 | return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded) | |
95 | } | |
96 | ||
97 | loadVideoLanguages () { | |
98 | return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true) | |
99 | } | |
100 | ||
101 | loadVideoPrivacies () { | |
102 | return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded) | |
103 | } | |
104 | ||
105 | getConfig () { | |
106 | return this.config | |
107 | } | |
108 | ||
109 | getVideoCategories () { | |
110 | return this.videoCategories | |
111 | } | |
112 | ||
113 | getVideoLicences () { | |
114 | return this.videoLicences | |
115 | } | |
116 | ||
117 | getVideoLanguages () { | |
118 | return this.videoLanguages | |
119 | } | |
120 | ||
121 | getVideoPrivacies () { | |
122 | return this.videoPrivacies | |
123 | } | |
124 | ||
125 | getAbout () { | |
126 | return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about') | |
127 | } | |
128 | ||
129 | private loadVideoAttributeEnum ( | |
130 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
131 | hashToPopulate: VideoConstant<number | string>[], | |
132 | notifier: ReplaySubject<boolean>, | |
133 | sort = false | |
134 | ) { | |
135 | this.localeObservable | |
136 | .pipe( | |
137 | switchMap(translations => { | |
138 | return this.http.get(ServerService.BASE_VIDEO_URL + attributeName) | |
139 | .pipe(map(data => ({ data, translations }))) | |
140 | }) | |
141 | ) | |
142 | .subscribe(({ data, translations }) => { | |
143 | Object.keys(data) | |
144 | .forEach(dataKey => { | |
145 | const label = data[ dataKey ] | |
146 | ||
147 | hashToPopulate.push({ | |
148 | id: dataKey, | |
149 | label: peertubeTranslate(label, translations) | |
150 | }) | |
151 | }) | |
152 | ||
153 | if (sort === true) { | |
154 | hashToPopulate.sort((a, b) => { | |
155 | if (a.label < b.label) return -1 | |
156 | if (a.label === b.label) return 0 | |
157 | return 1 | |
158 | }) | |
159 | } | |
160 | ||
161 | notifier.next(true) | |
162 | }) | |
163 | } | |
164 | ||
165 | private loadServerLocale () { | |
166 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
167 | ||
168 | // Default locale, nothing to translate | |
169 | if (isDefaultLocale(completeLocale)) { | |
170 | this.localeObservable = of({}).pipe(share()) | |
171 | return | |
172 | } | |
173 | ||
174 | this.localeObservable = this.http | |
175 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
176 | .pipe(share()) | |
177 | } | |
178 | ||
179 | private saveConfigLocally (config: ServerConfig) { | |
180 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) | |
181 | } | |
182 | ||
183 | private loadConfigLocally () { | |
184 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) | |
185 | ||
186 | if (configString) { | |
187 | try { | |
188 | const parsed = JSON.parse(configString) | |
189 | Object.assign(this.config, parsed) | |
190 | } catch (err) { | |
191 | console.error('Cannot parse config saved in local storage.', err) | |
192 | } | |
193 | } | |
194 | } | |
195 | } |