]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Little i18n refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
CommitLineData
7ce44a74 1import { map, share, 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'
74b7c6d4
C
5import { Observable, ReplaySubject, of } from 'rxjs'
6import { getCompleteLocale, ServerConfig } from '../../../../../shared'
09cababd 7import { About } from '../../../../../shared/models/server/about.model'
63c4db6d 8import { environment } from '../../../environments/environment'
9d3ef9fe 9import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
74b7c6d4
C
10import { isDefaultLocale } from '../../../../../shared/models/i18n'
11import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-utils'
db7af09b
C
12
13@Injectable()
14export 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)
144 .forEach(dataKey => {
145 const label = data[ dataKey ]
146
147 hashToPopulate.push({
148 id: dataKey,
149 label: peertubeTranslate(label, translations)
150 })
151 })
152
153 if (sort === true) {
154 hashToPopulate.sort((a, b) => {
155 if (a.label < b.label) return -1
156 if (a.label === b.label) return 0
157 return 1
158 })
159 }
160
161 notifier.next(true)
162 })
163 }
164
165 private loadServerLocale () {
74b7c6d4 166 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
7ce44a74
C
167
168 // Default locale, nothing to translate
74b7c6d4
C
169 if (isDefaultLocale(completeLocale)) {
170 this.localeObservable = of({}).pipe(share())
171 return
172 }
7ce44a74
C
173
174 this.localeObservable = this.http
74b7c6d4 175 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
7ce44a74 176 .pipe(share())
db7af09b 177 }
36f9424f
C
178
179 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 180 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
181 }
182
183 private loadConfigLocally () {
0bd78bf3 184 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
185
186 if (configString) {
187 try {
188 const parsed = JSON.parse(configString)
189 Object.assign(this.config, parsed)
190 } catch (err) {
191 console.error('Cannot parse config saved in local storage.', err)
192 }
193 }
194 }
db7af09b 195}