]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Implement captions/subtitles
[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 61 },
40e87e9e
C
62 videoCaption: {
63 file: {
64 size: { max: 0 },
65 extensions: []
66 }
67 },
1869c875
RK
68 user: {
69 videoQuota: -1
db7af09b
C
70 }
71 }
9d3ef9fe
C
72 private videoCategories: Array<VideoConstant<number>> = []
73 private videoLicences: Array<VideoConstant<number>> = []
74 private videoLanguages: Array<VideoConstant<string>> = []
75 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
db7af09b 76
7ce44a74
C
77 constructor (
78 private http: HttpClient,
79 @Inject(LOCALE_ID) private localeId: string
80 ) {
7ce44a74 81 this.loadServerLocale()
74b7c6d4 82 this.loadConfigLocally()
36f9424f 83 }
db7af09b
C
84
85 loadConfig () {
86 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
db400f44
C
87 .pipe(tap(this.saveConfigLocally))
88 .subscribe(data => {
89 this.config = data
00b5556c 90
db400f44
C
91 this.configLoaded.next(true)
92 })
db7af09b
C
93 }
94
95 loadVideoCategories () {
3580fc00 96 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
97 }
98
99 loadVideoLicences () {
baeefe22 100 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
101 }
102
103 loadVideoLanguages () {
3580fc00 104 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
105 }
106
fd45e8f4 107 loadVideoPrivacies () {
baeefe22 108 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
fd45e8f4
C
109 }
110
db7af09b
C
111 getConfig () {
112 return this.config
113 }
114
115 getVideoCategories () {
116 return this.videoCategories
117 }
118
119 getVideoLicences () {
120 return this.videoLicences
121 }
122
123 getVideoLanguages () {
124 return this.videoLanguages
125 }
126
fd45e8f4
C
127 getVideoPrivacies () {
128 return this.videoPrivacies
129 }
130
36f9424f
C
131 getAbout () {
132 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
133 }
134
fd45e8f4
C
135 private loadVideoAttributeEnum (
136 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
9d3ef9fe 137 hashToPopulate: VideoConstant<number | string>[],
3580fc00
C
138 notifier: ReplaySubject<boolean>,
139 sort = false
fd45e8f4 140 ) {
7ce44a74
C
141 this.localeObservable
142 .pipe(
143 switchMap(translations => {
144 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
145 .pipe(map(data => ({ data, translations })))
146 })
147 )
148 .subscribe(({ data, translations }) => {
149 Object.keys(data)
150 .forEach(dataKey => {
151 const label = data[ dataKey ]
152
153 hashToPopulate.push({
154 id: dataKey,
155 label: peertubeTranslate(label, translations)
156 })
157 })
158
159 if (sort === true) {
160 hashToPopulate.sort((a, b) => {
161 if (a.label < b.label) return -1
162 if (a.label === b.label) return 0
163 return 1
164 })
165 }
166
167 notifier.next(true)
168 })
169 }
170
171 private loadServerLocale () {
74b7c6d4 172 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
7ce44a74
C
173
174 // Default locale, nothing to translate
74b7c6d4
C
175 if (isDefaultLocale(completeLocale)) {
176 this.localeObservable = of({}).pipe(share())
177 return
178 }
7ce44a74
C
179
180 this.localeObservable = this.http
74b7c6d4 181 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
7ce44a74 182 .pipe(share())
db7af09b 183 }
36f9424f
C
184
185 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 186 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
187 }
188
189 private loadConfigLocally () {
0bd78bf3 190 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
191
192 if (configString) {
193 try {
194 const parsed = JSON.parse(configString)
195 Object.assign(this.config, parsed)
196 } catch (err) {
197 console.error('Cannot parse config saved in local storage.', err)
198 }
199 }
200 }
db7af09b 201}