]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Add notifications in the client
[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'
09cababd 7import { About } from '../../../../../shared/models/server/about.model'
63c4db6d 8import { environment } from '../../../environments/environment'
8cd7faaa 9import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
3dfa8494
C
10import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
11import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
ad774752 12import { sortBy } from '@app/shared/misc/utils'
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/'
7ce44a74 18 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
36f9424f 19 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
db7af09b 20
00b5556c 21 configLoaded = new ReplaySubject<boolean>(1)
baeefe22
C
22 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
23 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
24 videoLicencesLoaded = new ReplaySubject<boolean>(1)
25 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
7ce44a74 26 localeObservable: Observable<any>
baeefe22 27
db7af09b 28 private config: ServerConfig = {
36f9424f 29 instance: {
00b5556c 30 name: 'PeerTube',
63ac2857
C
31 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
32 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
901637bb 33 defaultClientRoute: '',
0883b324 34 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
00b5556c
C
35 customizations: {
36 javascript: '',
37 css: ''
38 }
36f9424f 39 },
3b3b1820
C
40 email: {
41 enabled: false
42 },
915c5bbe 43 serverVersion: 'Unknown',
db7af09b 44 signup: {
ff2c1fe8 45 allowed: false,
d9eaee39
JM
46 allowedForCurrentIP: false,
47 requiresEmailVerification: false
6a84aafd
C
48 },
49 transcoding: {
50 enabledResolutions: []
01de67b9
C
51 },
52 avatar: {
53 file: {
54 size: { max: 0 },
55 extensions: []
56 }
57 },
58 video: {
6de36768
C
59 image: {
60 size: { max: 0 },
61 extensions: []
62 },
01de67b9
C
63 file: {
64 extensions: []
65 }
1869c875 66 },
40e87e9e
C
67 videoCaption: {
68 file: {
69 size: { max: 0 },
70 extensions: []
71 }
72 },
1869c875 73 user: {
bee0abff
FA
74 videoQuota: -1,
75 videoQuotaDaily: -1
5d08a6a7
C
76 },
77 import: {
b2977eec 78 videos: {
5d08a6a7
C
79 http: {
80 enabled: false
a84b8fa5
C
81 },
82 torrent: {
83 enabled: false
5d08a6a7
C
84 }
85 }
db7af09b
C
86 }
87 }
8cd7faaa
C
88 private videoCategories: Array<VideoConstant<number>> = []
89 private videoLicences: Array<VideoConstant<number>> = []
9d3ef9fe 90 private videoLanguages: Array<VideoConstant<string>> = []
8cd7faaa 91 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
db7af09b 92
7ce44a74
C
93 constructor (
94 private http: HttpClient,
95 @Inject(LOCALE_ID) private localeId: string
96 ) {
7ce44a74 97 this.loadServerLocale()
74b7c6d4 98 this.loadConfigLocally()
36f9424f 99 }
db7af09b
C
100
101 loadConfig () {
102 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
db400f44
C
103 .pipe(tap(this.saveConfigLocally))
104 .subscribe(data => {
105 this.config = data
00b5556c 106
db400f44
C
107 this.configLoaded.next(true)
108 })
db7af09b
C
109 }
110
111 loadVideoCategories () {
3580fc00 112 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
113 }
114
115 loadVideoLicences () {
baeefe22 116 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
117 }
118
119 loadVideoLanguages () {
3580fc00 120 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
121 }
122
fd45e8f4 123 loadVideoPrivacies () {
baeefe22 124 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
fd45e8f4
C
125 }
126
db7af09b
C
127 getConfig () {
128 return this.config
129 }
130
131 getVideoCategories () {
132 return this.videoCategories
133 }
134
135 getVideoLicences () {
136 return this.videoLicences
137 }
138
139 getVideoLanguages () {
140 return this.videoLanguages
141 }
142
fd45e8f4
C
143 getVideoPrivacies () {
144 return this.videoPrivacies
145 }
146
36f9424f
C
147 getAbout () {
148 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
149 }
150
fd45e8f4
C
151 private loadVideoAttributeEnum (
152 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
8cd7faaa 153 hashToPopulate: VideoConstant<string | number>[],
3580fc00
C
154 notifier: ReplaySubject<boolean>,
155 sort = false
fd45e8f4 156 ) {
7ce44a74
C
157 this.localeObservable
158 .pipe(
159 switchMap(translations => {
c199c427
C
160 return this.http.get<{ [id: string]: string }>(ServerService.BASE_VIDEO_URL + attributeName)
161 .pipe(map(data => ({ data, translations })))
7ce44a74
C
162 })
163 )
164 .subscribe(({ data, translations }) => {
165 Object.keys(data)
166 .forEach(dataKey => {
167 const label = data[ dataKey ]
168
169 hashToPopulate.push({
8cd7faaa 170 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
7ce44a74
C
171 label: peertubeTranslate(label, translations)
172 })
173 })
174
ad774752 175 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74
C
176
177 notifier.next(true)
178 })
179 }
180
181 private loadServerLocale () {
74b7c6d4 182 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
7ce44a74
C
183
184 // Default locale, nothing to translate
74b7c6d4 185 if (isDefaultLocale(completeLocale)) {
4bda2e47 186 this.localeObservable = of({}).pipe(shareReplay())
74b7c6d4
C
187 return
188 }
7ce44a74
C
189
190 this.localeObservable = this.http
74b7c6d4 191 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
4bda2e47 192 .pipe(shareReplay())
db7af09b 193 }
36f9424f
C
194
195 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 196 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
197 }
198
199 private loadConfigLocally () {
0bd78bf3 200 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
201
202 if (configString) {
203 try {
204 const parsed = JSON.parse(configString)
205 Object.assign(this.config, parsed)
206 } catch (err) {
207 console.error('Cannot parse config saved in local storage.', err)
208 }
209 }
210 }
db7af09b 211}