]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Fix comments deleted display
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
CommitLineData
ba430d75 1import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
db7af09b 2import { HttpClient } from '@angular/common/http'
7ce44a74 3import { Inject, Injectable, LOCALE_ID } from '@angular/core'
88a7f93f 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
ba430d75 5import { Observable, of, Subject } from 'rxjs'
74b7c6d4 6import { getCompleteLocale, ServerConfig } from '../../../../../shared'
63c4db6d 7import { environment } from '../../../environments/environment'
ba430d75 8import { VideoConstant } from '../../../../../shared/models/videos'
3dfa8494
C
9import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
ad774752 11import { sortBy } from '@app/shared/misc/utils'
b764380a 12import { ServerStats } from '@shared/models/server'
db7af09b
C
13
14@Injectable()
15export 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/'
830b4faf 18 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
7ce44a74 19 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
b764380a
C
20 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
21
36f9424f 22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
db7af09b 23
ba430d75 24 configReloaded = new Subject<void>()
baeefe22 25
ba430d75
C
26 private localeObservable: Observable<any>
27 private videoLicensesObservable: Observable<VideoConstant<number>[]>
28 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
29 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
30 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
31 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
32 private configObservable: Observable<ServerConfig>
33
34 private configReset = false
35
36 private configLoaded = false
db7af09b 37 private config: ServerConfig = {
36f9424f 38 instance: {
00b5556c 39 name: 'PeerTube',
63ac2857
C
40 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
41 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
901637bb 42 defaultClientRoute: '',
f8802489 43 isNSFW: false,
0883b324 44 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
00b5556c
C
45 customizations: {
46 javascript: '',
47 css: ''
48 }
36f9424f 49 },
9677fca7
RK
50 search: {
51 remoteUri: {
52 users: true,
53 anonymous: false
54 }
55 },
7cd4d2ba 56 plugin: {
4a8d113b
C
57 registered: [],
58 registeredExternalAuths: [],
59 registeredIdAndPassAuths: []
7cd4d2ba
C
60 },
61 theme: {
62 registered: [],
63 default: 'default'
64 },
3b3b1820
C
65 email: {
66 enabled: false
67 },
3866f1a0
C
68 contactForm: {
69 enabled: false
70 },
915c5bbe 71 serverVersion: 'Unknown',
db7af09b 72 signup: {
ff2c1fe8 73 allowed: false,
d9eaee39
JM
74 allowedForCurrentIP: false,
75 requiresEmailVerification: false
6a84aafd
C
76 },
77 transcoding: {
09209296
C
78 enabledResolutions: [],
79 hls: {
80 enabled: false
5a71acd2
C
81 },
82 webtorrent: {
83 enabled: true
09209296 84 }
01de67b9
C
85 },
86 avatar: {
87 file: {
88 size: { max: 0 },
89 extensions: []
90 }
91 },
92 video: {
6de36768
C
93 image: {
94 size: { max: 0 },
95 extensions: []
96 },
01de67b9
C
97 file: {
98 extensions: []
99 }
1869c875 100 },
40e87e9e
C
101 videoCaption: {
102 file: {
103 size: { max: 0 },
104 extensions: []
105 }
106 },
1869c875 107 user: {
bee0abff
FA
108 videoQuota: -1,
109 videoQuotaDaily: -1
5d08a6a7
C
110 },
111 import: {
b2977eec 112 videos: {
5d08a6a7
C
113 http: {
114 enabled: false
a84b8fa5
C
115 },
116 torrent: {
117 enabled: false
5d08a6a7
C
118 }
119 }
9b4b15f9
AB
120 },
121 trending: {
122 videos: {
123 intervalDays: 0
124 }
7ccddd7b
JM
125 },
126 autoBlacklist: {
127 videos: {
128 ofUsers: {
129 enabled: false
130 }
131 }
31b6ddf8
C
132 },
133 tracker: {
134 enabled: true
f24c8b14
RK
135 },
136 followings: {
137 instance: {
138 autoFollowIndex: {
139 indexUrl: 'https://instances.joinpeertube.org'
140 }
141 }
db7af09b
C
142 }
143 }
db7af09b 144
7ce44a74
C
145 constructor (
146 private http: HttpClient,
147 @Inject(LOCALE_ID) private localeId: string
148 ) {
74b7c6d4 149 this.loadConfigLocally()
36f9424f 150 }
db7af09b 151
ba430d75
C
152 getServerVersionAndCommit () {
153 const serverVersion = this.config.serverVersion
154 const commit = this.config.serverCommit || ''
00b5556c 155
ba430d75
C
156 let result = serverVersion
157 if (commit) result += '...' + commit
db7af09b 158
ba430d75 159 return result
db7af09b
C
160 }
161
ba430d75
C
162 resetConfig () {
163 this.configLoaded = false
164 this.configReset = true
db7af09b
C
165 }
166
ba430d75
C
167 getConfig () {
168 if (this.configLoaded) return of(this.config)
db7af09b 169
ba430d75
C
170 if (!this.configObservable) {
171 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
172 .pipe(
017fbe18
C
173 tap(config => this.saveConfigLocally(config)),
174 tap(config => {
175 this.config = config
176 this.configLoaded = true
177 }),
ba430d75
C
178 tap(() => {
179 if (this.configReset) {
180 this.configReloaded.next()
181 this.configReset = false
182 }
183 }),
184 share()
185 )
186 }
830b4faf 187
ba430d75 188 return this.configObservable
fd45e8f4
C
189 }
190
ba430d75
C
191 getTmpConfig () {
192 return this.config
dbdf2d51
C
193 }
194
db7af09b 195 getVideoCategories () {
ba430d75
C
196 if (!this.videoCategoriesObservable) {
197 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
198 }
199
200 return this.videoCategoriesObservable.pipe(first())
db7af09b
C
201 }
202
203 getVideoLicences () {
ba430d75
C
204 if (!this.videoLicensesObservable) {
205 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
206 }
207
208 return this.videoLicensesObservable.pipe(first())
db7af09b
C
209 }
210
211 getVideoLanguages () {
ba430d75
C
212 if (!this.videoLanguagesObservable) {
213 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
214 }
215
216 return this.videoLanguagesObservable.pipe(first())
db7af09b
C
217 }
218
fd45e8f4 219 getVideoPrivacies () {
ba430d75
C
220 if (!this.videoPrivaciesObservable) {
221 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
222 }
223
224 return this.videoPrivaciesObservable.pipe(first())
fd45e8f4
C
225 }
226
830b4faf 227 getVideoPlaylistPrivacies () {
ba430d75
C
228 if (!this.videoPlaylistPrivaciesObservable) {
229 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
230 }
231
232 return this.videoPlaylistPrivaciesObservable.pipe(first())
233 }
234
235 getServerLocale () {
236 if (!this.localeObservable) {
237 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
238
239 // Default locale, nothing to translate
240 if (isDefaultLocale(completeLocale)) {
241 this.localeObservable = of({}).pipe(shareReplay())
242 } else {
243 this.localeObservable = this.http
244 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
245 .pipe(shareReplay())
246 }
247 }
248
249 return this.localeObservable.pipe(first())
830b4faf
C
250 }
251
b764380a
C
252 getServerStats () {
253 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
254 }
255
ba430d75 256 private loadAttributeEnum <T extends string | number> (
830b4faf 257 baseUrl: string,
fd45e8f4 258 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
3580fc00 259 sort = false
fd45e8f4 260 ) {
ba430d75
C
261 return this.getServerLocale()
262 .pipe(
263 switchMap(translations => {
264 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
265 .pipe(map(data => ({ data, translations })))
266 }),
267 map(({ data, translations }) => {
111fdc26
C
268 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
269 .map(dataKey => {
270 const label = data[ dataKey ]
271
272 const id = attributeName === 'languages'
273 ? dataKey as T
274 : parseInt(dataKey, 10) as T
275
276 return {
277 id,
278 label: peertubeTranslate(label, translations)
279 }
280 })
ba430d75
C
281
282 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74 283
ba430d75
C
284 return hashToPopulate
285 }),
286 shareReplay()
287 )
db7af09b 288 }
36f9424f
C
289
290 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 291 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
292 }
293
294 private loadConfigLocally () {
0bd78bf3 295 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
296
297 if (configString) {
298 try {
299 const parsed = JSON.parse(configString)
300 Object.assign(this.config, parsed)
301 } catch (err) {
302 console.error('Cannot parse config saved in local storage.', err)
303 }
304 }
305 }
db7af09b 306}