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