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