]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
add loop setting for playlists, and use sessionStorage
[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'
88a7f93f 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-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 },
7cd4d2ba
C
45 plugin: {
46 registered: []
47 },
48 theme: {
49 registered: [],
50 default: 'default'
51 },
3b3b1820
C
52 email: {
53 enabled: false
54 },
3866f1a0
C
55 contactForm: {
56 enabled: false
57 },
915c5bbe 58 serverVersion: 'Unknown',
db7af09b 59 signup: {
ff2c1fe8 60 allowed: false,
d9eaee39
JM
61 allowedForCurrentIP: false,
62 requiresEmailVerification: false
6a84aafd
C
63 },
64 transcoding: {
09209296
C
65 enabledResolutions: [],
66 hls: {
67 enabled: false
5a71acd2
C
68 },
69 webtorrent: {
70 enabled: true
09209296 71 }
01de67b9
C
72 },
73 avatar: {
74 file: {
75 size: { max: 0 },
76 extensions: []
77 }
78 },
79 video: {
6de36768
C
80 image: {
81 size: { max: 0 },
82 extensions: []
83 },
01de67b9
C
84 file: {
85 extensions: []
86 }
1869c875 87 },
40e87e9e
C
88 videoCaption: {
89 file: {
90 size: { max: 0 },
91 extensions: []
92 }
93 },
1869c875 94 user: {
bee0abff
FA
95 videoQuota: -1,
96 videoQuotaDaily: -1
5d08a6a7
C
97 },
98 import: {
b2977eec 99 videos: {
5d08a6a7
C
100 http: {
101 enabled: false
a84b8fa5
C
102 },
103 torrent: {
104 enabled: false
5d08a6a7
C
105 }
106 }
9b4b15f9
AB
107 },
108 trending: {
109 videos: {
110 intervalDays: 0
111 }
7ccddd7b
JM
112 },
113 autoBlacklist: {
114 videos: {
115 ofUsers: {
116 enabled: false
117 }
118 }
31b6ddf8
C
119 },
120 tracker: {
121 enabled: true
db7af09b
C
122 }
123 }
8cd7faaa
C
124 private videoCategories: Array<VideoConstant<number>> = []
125 private videoLicences: Array<VideoConstant<number>> = []
9d3ef9fe 126 private videoLanguages: Array<VideoConstant<string>> = []
8cd7faaa 127 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
830b4faf 128 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
db7af09b 129
7ce44a74
C
130 constructor (
131 private http: HttpClient,
132 @Inject(LOCALE_ID) private localeId: string
133 ) {
7ce44a74 134 this.loadServerLocale()
74b7c6d4 135 this.loadConfigLocally()
36f9424f 136 }
db7af09b
C
137
138 loadConfig () {
139 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
db400f44
C
140 .pipe(tap(this.saveConfigLocally))
141 .subscribe(data => {
142 this.config = data
00b5556c 143
db400f44
C
144 this.configLoaded.next(true)
145 })
db7af09b
C
146 }
147
148 loadVideoCategories () {
830b4faf 149 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
150 }
151
152 loadVideoLicences () {
830b4faf 153 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
154 }
155
156 loadVideoLanguages () {
830b4faf 157 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
158 }
159
fd45e8f4 160 loadVideoPrivacies () {
830b4faf
C
161 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
162 }
163
164 loadVideoPlaylistPrivacies () {
165 return this.loadAttributeEnum(
166 ServerService.BASE_VIDEO_PLAYLIST_URL,
167 'privacies',
168 this.videoPlaylistPrivacies,
169 this.videoPlaylistPrivaciesLoaded
170 )
fd45e8f4
C
171 }
172
db7af09b 173 getConfig () {
12bec528 174 return cloneDeep(this.config)
db7af09b
C
175 }
176
177 getVideoCategories () {
12bec528 178 return cloneDeep(this.videoCategories)
db7af09b
C
179 }
180
181 getVideoLicences () {
12bec528 182 return cloneDeep(this.videoLicences)
db7af09b
C
183 }
184
185 getVideoLanguages () {
12bec528 186 return cloneDeep(this.videoLanguages)
db7af09b
C
187 }
188
fd45e8f4 189 getVideoPrivacies () {
12bec528 190 return cloneDeep(this.videoPrivacies)
fd45e8f4
C
191 }
192
830b4faf 193 getVideoPlaylistPrivacies () {
12bec528 194 return cloneDeep(this.videoPlaylistPrivacies)
830b4faf
C
195 }
196
197 private loadAttributeEnum (
198 baseUrl: string,
fd45e8f4 199 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
8cd7faaa 200 hashToPopulate: VideoConstant<string | number>[],
3580fc00
C
201 notifier: ReplaySubject<boolean>,
202 sort = false
fd45e8f4 203 ) {
7ce44a74
C
204 this.localeObservable
205 .pipe(
206 switchMap(translations => {
830b4faf 207 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
c199c427 208 .pipe(map(data => ({ data, translations })))
7ce44a74
C
209 })
210 )
211 .subscribe(({ data, translations }) => {
212 Object.keys(data)
213 .forEach(dataKey => {
214 const label = data[ dataKey ]
215
216 hashToPopulate.push({
8cd7faaa 217 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
7ce44a74
C
218 label: peertubeTranslate(label, translations)
219 })
220 })
221
ad774752 222 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74
C
223
224 notifier.next(true)
225 })
226 }
227
228 private loadServerLocale () {
74b7c6d4 229 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
7ce44a74
C
230
231 // Default locale, nothing to translate
74b7c6d4 232 if (isDefaultLocale(completeLocale)) {
4bda2e47 233 this.localeObservable = of({}).pipe(shareReplay())
74b7c6d4
C
234 return
235 }
7ce44a74
C
236
237 this.localeObservable = this.http
74b7c6d4 238 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
4bda2e47 239 .pipe(shareReplay())
db7af09b 240 }
36f9424f
C
241
242 private saveConfigLocally (config: ServerConfig) {
0bd78bf3 243 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
36f9424f
C
244 }
245
246 private loadConfigLocally () {
0bd78bf3 247 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
36f9424f
C
248
249 if (configString) {
250 try {
251 const parsed = JSON.parse(configString)
252 Object.assign(this.config, parsed)
253 } catch (err) {
254 console.error('Cannot parse config saved in local storage.', err)
255 }
256 }
257 }
db7af09b 258}