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