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