]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
4bda2e47 1import { map, shareReplay, switchMap, tap } from 'rxjs/operators'
db7af09b 2import { HttpClient } from '@angular/common/http'
7ce44a74 3import { Inject, Injectable, LOCALE_ID } from '@angular/core'
0bd78bf3 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
ad774752 5import { Observable, of, ReplaySubject } from 'rxjs'
74b7c6d4 6import { getCompleteLocale, ServerConfig } from '../../../../../shared'
63c4db6d 7import { environment } from '../../../environments/environment'
8cd7faaa 8import { VideoConstant, VideoPrivacy } 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'
830b4faf 12import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
db7af09b
C
13
14@Injectable()
15export class ServerService {
d3e56c0c 16 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
63c4db6d
C
17 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
18 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
830b4faf 19 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
7ce44a74 20 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
36f9424f 21 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
db7af09b 22
00b5556c 23 configLoaded = new ReplaySubject<boolean>(1)
baeefe22 24 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
830b4faf 25 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
baeefe22
C
26 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
27 videoLicencesLoaded = new ReplaySubject<boolean>(1)
28 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
7ce44a74 29 localeObservable: Observable<any>
baeefe22 30
db7af09b 31 private config: ServerConfig = {
36f9424f 32 instance: {
00b5556c 33 name: 'PeerTube',
63ac2857
C
34 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
35 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
901637bb 36 defaultClientRoute: '',
f8802489 37 isNSFW: false,
0883b324 38 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
00b5556c
C
39 customizations: {
40 javascript: '',
41 css: ''
42 }
36f9424f 43 },
3b3b1820
C
44 email: {
45 enabled: false
46 },
3866f1a0
C
47 contactForm: {
48 enabled: false
49 },
915c5bbe 50 serverVersion: 'Unknown',
db7af09b 51 signup: {
ff2c1fe8 52 allowed: false,
d9eaee39
JM
53 allowedForCurrentIP: false,
54 requiresEmailVerification: false
6a84aafd
C
55 },
56 transcoding: {
09209296
C
57 enabledResolutions: [],
58 hls: {
59 enabled: false
60 }
01de67b9
C
61 },
62 avatar: {
63 file: {
64 size: { max: 0 },
65 extensions: []
66 }
67 },
68 video: {
6de36768
C
69 image: {
70 size: { max: 0 },
71 extensions: []
72 },
01de67b9
C
73 file: {
74 extensions: []
75 }
1869c875 76 },
40e87e9e
C
77 videoCaption: {
78 file: {
79 size: { max: 0 },
80 extensions: []
81 }
82 },
1869c875 83 user: {
bee0abff
FA
84 videoQuota: -1,
85 videoQuotaDaily: -1
5d08a6a7
C
86 },
87 import: {
b2977eec 88 videos: {
5d08a6a7
C
89 http: {
90 enabled: false
a84b8fa5
C
91 },
92 torrent: {
93 enabled: false
5d08a6a7
C
94 }
95 }
9b4b15f9
AB
96 },
97 trending: {
98 videos: {
99 intervalDays: 0
100 }
7ccddd7b
JM
101 },
102 autoBlacklist: {
103 videos: {
104 ofUsers: {
105 enabled: false
106 }
107 }
db7af09b
C
108 }
109 }
8cd7faaa
C
110 private videoCategories: Array<VideoConstant<number>> = []
111 private videoLicences: Array<VideoConstant<number>> = []
9d3ef9fe 112 private videoLanguages: Array<VideoConstant<string>> = []
8cd7faaa 113 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
830b4faf 114 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
db7af09b 115
7ce44a74
C
116 constructor (
117 private http: HttpClient,
118 @Inject(LOCALE_ID) private localeId: string
119 ) {
7ce44a74 120 this.loadServerLocale()
74b7c6d4 121 this.loadConfigLocally()
36f9424f 122 }
db7af09b
C
123
124 loadConfig () {
125 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
db400f44
C
126 .pipe(tap(this.saveConfigLocally))
127 .subscribe(data => {
128 this.config = data
00b5556c 129
db400f44
C
130 this.configLoaded.next(true)
131 })
db7af09b
C
132 }
133
134 loadVideoCategories () {
830b4faf 135 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
136 }
137
138 loadVideoLicences () {
830b4faf 139 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
140 }
141
142 loadVideoLanguages () {
830b4faf 143 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
144 }
145
fd45e8f4 146 loadVideoPrivacies () {
830b4faf
C
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 )
fd45e8f4
C
157 }
158
db7af09b
C
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
fd45e8f4
C
175 getVideoPrivacies () {
176 return this.videoPrivacies
177 }
178
830b4faf
C
179 getVideoPlaylistPrivacies () {
180 return this.videoPlaylistPrivacies
181 }
182
183 private loadAttributeEnum (
184 baseUrl: string,
fd45e8f4 185 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
8cd7faaa 186 hashToPopulate: VideoConstant<string | number>[],
3580fc00
C
187 notifier: ReplaySubject<boolean>,
188 sort = false
fd45e8f4 189 ) {
7ce44a74
C
190 this.localeObservable
191 .pipe(
192 switchMap(translations => {
830b4faf 193 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
c199c427 194 .pipe(map(data => ({ data, translations })))
7ce44a74
C
195 })
196 )
197 .subscribe(({ data, translations }) => {
198 Object.keys(data)
199 .forEach(dataKey => {
200 const label = data[ dataKey ]
201
202 hashToPopulate.push({
8cd7faaa 203 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
7ce44a74
C
204 label: peertubeTranslate(label, translations)
205 })
206 })
207
ad774752 208 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74
C
209
210 notifier.next(true)
211 })
212 }
213
214 private loadServerLocale () {
74b7c6d4 215 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
7ce44a74
C
216
217 // Default locale, nothing to translate
74b7c6d4 218 if (isDefaultLocale(completeLocale)) {
4bda2e47 219 this.localeObservable = of({}).pipe(shareReplay())
74b7c6d4
C
220 return
221 }
7ce44a74
C
222
223 this.localeObservable = this.http
74b7c6d4 224 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
4bda2e47 225 .pipe(shareReplay())
db7af09b 226 }
36f9424f
C
227
228 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 229 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
230 }
231
232 private loadConfigLocally () {
0bd78bf3 233 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
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 }
db7af09b 244}