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