]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
80c52164d578f3bb6323380e3c559ed952f7c2ad
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { map, shareReplay, 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, of, ReplaySubject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
13 import { cloneDeep } from 'lodash-es'
14
15 @Injectable()
16 export class ServerService {
17 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
18 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
19 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
20 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
21 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
23
24 configLoaded = new ReplaySubject<boolean>(1)
25 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
26 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
27 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
28 videoLicencesLoaded = new ReplaySubject<boolean>(1)
29 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
30 localeObservable: Observable<any>
31
32 private config: ServerConfig = {
33 instance: {
34 name: 'PeerTube',
35 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
36 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
37 defaultClientRoute: '',
38 isNSFW: false,
39 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
40 customizations: {
41 javascript: '',
42 css: ''
43 }
44 },
45 plugins: [],
46 email: {
47 enabled: false
48 },
49 contactForm: {
50 enabled: false
51 },
52 serverVersion: 'Unknown',
53 signup: {
54 allowed: false,
55 allowedForCurrentIP: false,
56 requiresEmailVerification: false
57 },
58 transcoding: {
59 enabledResolutions: [],
60 hls: {
61 enabled: false
62 }
63 },
64 avatar: {
65 file: {
66 size: { max: 0 },
67 extensions: []
68 }
69 },
70 video: {
71 image: {
72 size: { max: 0 },
73 extensions: []
74 },
75 file: {
76 extensions: []
77 }
78 },
79 videoCaption: {
80 file: {
81 size: { max: 0 },
82 extensions: []
83 }
84 },
85 user: {
86 videoQuota: -1,
87 videoQuotaDaily: -1
88 },
89 import: {
90 videos: {
91 http: {
92 enabled: false
93 },
94 torrent: {
95 enabled: false
96 }
97 }
98 },
99 trending: {
100 videos: {
101 intervalDays: 0
102 }
103 },
104 autoBlacklist: {
105 videos: {
106 ofUsers: {
107 enabled: false
108 }
109 }
110 },
111 tracker: {
112 enabled: true
113 }
114 }
115 private videoCategories: Array<VideoConstant<number>> = []
116 private videoLicences: Array<VideoConstant<number>> = []
117 private videoLanguages: Array<VideoConstant<string>> = []
118 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
119 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
120
121 constructor (
122 private http: HttpClient,
123 @Inject(LOCALE_ID) private localeId: string
124 ) {
125 this.loadServerLocale()
126 this.loadConfigLocally()
127 }
128
129 loadConfig () {
130 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
131 .pipe(tap(this.saveConfigLocally))
132 .subscribe(data => {
133 this.config = data
134
135 this.configLoaded.next(true)
136 })
137 }
138
139 loadVideoCategories () {
140 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
141 }
142
143 loadVideoLicences () {
144 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
145 }
146
147 loadVideoLanguages () {
148 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
149 }
150
151 loadVideoPrivacies () {
152 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
153 }
154
155 loadVideoPlaylistPrivacies () {
156 return this.loadAttributeEnum(
157 ServerService.BASE_VIDEO_PLAYLIST_URL,
158 'privacies',
159 this.videoPlaylistPrivacies,
160 this.videoPlaylistPrivaciesLoaded
161 )
162 }
163
164 getConfig () {
165 return cloneDeep(this.config)
166 }
167
168 getVideoCategories () {
169 return cloneDeep(this.videoCategories)
170 }
171
172 getVideoLicences () {
173 return cloneDeep(this.videoLicences)
174 }
175
176 getVideoLanguages () {
177 return cloneDeep(this.videoLanguages)
178 }
179
180 getVideoPrivacies () {
181 return cloneDeep(this.videoPrivacies)
182 }
183
184 getVideoPlaylistPrivacies () {
185 return cloneDeep(this.videoPlaylistPrivacies)
186 }
187
188 private loadAttributeEnum (
189 baseUrl: string,
190 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
191 hashToPopulate: VideoConstant<string | number>[],
192 notifier: ReplaySubject<boolean>,
193 sort = false
194 ) {
195 this.localeObservable
196 .pipe(
197 switchMap(translations => {
198 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
199 .pipe(map(data => ({ data, translations })))
200 })
201 )
202 .subscribe(({ data, translations }) => {
203 Object.keys(data)
204 .forEach(dataKey => {
205 const label = data[ dataKey ]
206
207 hashToPopulate.push({
208 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
209 label: peertubeTranslate(label, translations)
210 })
211 })
212
213 if (sort === true) sortBy(hashToPopulate, 'label')
214
215 notifier.next(true)
216 })
217 }
218
219 private loadServerLocale () {
220 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
221
222 // Default locale, nothing to translate
223 if (isDefaultLocale(completeLocale)) {
224 this.localeObservable = of({}).pipe(shareReplay())
225 return
226 }
227
228 this.localeObservable = this.http
229 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
230 .pipe(shareReplay())
231 }
232
233 private saveConfigLocally (config: ServerConfig) {
234 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
235 }
236
237 private loadConfigLocally () {
238 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
239
240 if (configString) {
241 try {
242 const parsed = JSON.parse(configString)
243 Object.assign(this.config, parsed)
244 } catch (err) {
245 console.error('Cannot parse config saved in local storage.', err)
246 }
247 }
248 }
249 }