]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Add the video tags restrictions to the API docs
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { map, shareReplay, switchMap, tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5 import { Observable, of, ReplaySubject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
13
14 @Injectable()
15 export class ServerService {
16 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
17 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
18 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
19 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
20 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
21 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
22
23 configLoaded = new ReplaySubject<boolean>(1)
24 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
25 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
26 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
27 videoLicencesLoaded = new ReplaySubject<boolean>(1)
28 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
29 localeObservable: Observable<any>
30
31 private config: ServerConfig = {
32 instance: {
33 name: 'PeerTube',
34 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
35 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
36 defaultClientRoute: '',
37 isNSFW: false,
38 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
39 customizations: {
40 javascript: '',
41 css: ''
42 }
43 },
44 email: {
45 enabled: false
46 },
47 contactForm: {
48 enabled: false
49 },
50 serverVersion: 'Unknown',
51 signup: {
52 allowed: false,
53 allowedForCurrentIP: false,
54 requiresEmailVerification: false
55 },
56 transcoding: {
57 enabledResolutions: [],
58 hls: {
59 enabled: false
60 }
61 },
62 avatar: {
63 file: {
64 size: { max: 0 },
65 extensions: []
66 }
67 },
68 video: {
69 image: {
70 size: { max: 0 },
71 extensions: []
72 },
73 file: {
74 extensions: []
75 }
76 },
77 videoCaption: {
78 file: {
79 size: { max: 0 },
80 extensions: []
81 }
82 },
83 user: {
84 videoQuota: -1,
85 videoQuotaDaily: -1
86 },
87 import: {
88 videos: {
89 http: {
90 enabled: false
91 },
92 torrent: {
93 enabled: false
94 }
95 }
96 },
97 trending: {
98 videos: {
99 intervalDays: 0
100 }
101 }
102 }
103 private videoCategories: Array<VideoConstant<number>> = []
104 private videoLicences: Array<VideoConstant<number>> = []
105 private videoLanguages: Array<VideoConstant<string>> = []
106 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
107 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
108
109 constructor (
110 private http: HttpClient,
111 @Inject(LOCALE_ID) private localeId: string
112 ) {
113 this.loadServerLocale()
114 this.loadConfigLocally()
115 }
116
117 loadConfig () {
118 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
119 .pipe(tap(this.saveConfigLocally))
120 .subscribe(data => {
121 this.config = data
122
123 this.configLoaded.next(true)
124 })
125 }
126
127 loadVideoCategories () {
128 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
129 }
130
131 loadVideoLicences () {
132 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
133 }
134
135 loadVideoLanguages () {
136 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
137 }
138
139 loadVideoPrivacies () {
140 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
141 }
142
143 loadVideoPlaylistPrivacies () {
144 return this.loadAttributeEnum(
145 ServerService.BASE_VIDEO_PLAYLIST_URL,
146 'privacies',
147 this.videoPlaylistPrivacies,
148 this.videoPlaylistPrivaciesLoaded
149 )
150 }
151
152 getConfig () {
153 return this.config
154 }
155
156 getVideoCategories () {
157 return this.videoCategories
158 }
159
160 getVideoLicences () {
161 return this.videoLicences
162 }
163
164 getVideoLanguages () {
165 return this.videoLanguages
166 }
167
168 getVideoPrivacies () {
169 return this.videoPrivacies
170 }
171
172 getVideoPlaylistPrivacies () {
173 return this.videoPlaylistPrivacies
174 }
175
176 private loadAttributeEnum (
177 baseUrl: string,
178 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
179 hashToPopulate: VideoConstant<string | number>[],
180 notifier: ReplaySubject<boolean>,
181 sort = false
182 ) {
183 this.localeObservable
184 .pipe(
185 switchMap(translations => {
186 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
187 .pipe(map(data => ({ data, translations })))
188 })
189 )
190 .subscribe(({ data, translations }) => {
191 Object.keys(data)
192 .forEach(dataKey => {
193 const label = data[ dataKey ]
194
195 hashToPopulate.push({
196 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
197 label: peertubeTranslate(label, translations)
198 })
199 })
200
201 if (sort === true) sortBy(hashToPopulate, 'label')
202
203 notifier.next(true)
204 })
205 }
206
207 private loadServerLocale () {
208 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
209
210 // Default locale, nothing to translate
211 if (isDefaultLocale(completeLocale)) {
212 this.localeObservable = of({}).pipe(shareReplay())
213 return
214 }
215
216 this.localeObservable = this.http
217 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
218 .pipe(shareReplay())
219 }
220
221 private saveConfigLocally (config: ServerConfig) {
222 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
223 }
224
225 private loadConfigLocally () {
226 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
227
228 if (configString) {
229 try {
230 const parsed = JSON.parse(configString)
231 Object.assign(this.config, parsed)
232 } catch (err) {
233 console.error('Cannot parse config saved in local storage.', err)
234 }
235 }
236 }
237 }