]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Upgrade to rxjs 6
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5 import { ReplaySubject } from 'rxjs'
6 import { ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { environment } from '../../../environments/environment'
9 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
10
11 @Injectable()
12 export class ServerService {
13 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
14 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
15 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
16
17 configLoaded = new ReplaySubject<boolean>(1)
18 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
19 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
20 videoLicencesLoaded = new ReplaySubject<boolean>(1)
21 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
22
23 private config: ServerConfig = {
24 instance: {
25 name: 'PeerTube',
26 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
27 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
28 defaultClientRoute: '',
29 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
30 customizations: {
31 javascript: '',
32 css: ''
33 }
34 },
35 serverVersion: 'Unknown',
36 signup: {
37 allowed: false
38 },
39 transcoding: {
40 enabledResolutions: []
41 },
42 avatar: {
43 file: {
44 size: { max: 0 },
45 extensions: []
46 }
47 },
48 video: {
49 image: {
50 size: { max: 0 },
51 extensions: []
52 },
53 file: {
54 extensions: []
55 }
56 },
57 user: {
58 videoQuota: -1
59 }
60 }
61 private videoCategories: Array<VideoConstant<number>> = []
62 private videoLicences: Array<VideoConstant<number>> = []
63 private videoLanguages: Array<VideoConstant<string>> = []
64 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
65
66 constructor (private http: HttpClient) {
67 this.loadConfigLocally()
68 }
69
70 loadConfig () {
71 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
72 .pipe(tap(this.saveConfigLocally))
73 .subscribe(data => {
74 this.config = data
75
76 this.configLoaded.next(true)
77 })
78 }
79
80 loadVideoCategories () {
81 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
82 }
83
84 loadVideoLicences () {
85 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
86 }
87
88 loadVideoLanguages () {
89 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
90 }
91
92 loadVideoPrivacies () {
93 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
94 }
95
96 getConfig () {
97 return this.config
98 }
99
100 getVideoCategories () {
101 return this.videoCategories
102 }
103
104 getVideoLicences () {
105 return this.videoLicences
106 }
107
108 getVideoLanguages () {
109 return this.videoLanguages
110 }
111
112 getVideoPrivacies () {
113 return this.videoPrivacies
114 }
115
116 getAbout () {
117 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
118 }
119
120 private loadVideoAttributeEnum (
121 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
122 hashToPopulate: VideoConstant<number | string>[],
123 notifier: ReplaySubject<boolean>,
124 sort = false
125 ) {
126 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
127 .subscribe(data => {
128 Object.keys(data)
129 .forEach(dataKey => {
130 hashToPopulate.push({
131 id: dataKey,
132 label: data[dataKey]
133 })
134 })
135
136 if (sort === true) {
137 hashToPopulate.sort((a, b) => {
138 if (a.label < b.label) return -1
139 if (a.label === b.label) return 0
140 return 1
141 })
142 }
143
144 notifier.next(true)
145 })
146 }
147
148 private saveConfigLocally (config: ServerConfig) {
149 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
150 }
151
152 private loadConfigLocally () {
153 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
154
155 if (configString) {
156 try {
157 const parsed = JSON.parse(configString)
158 Object.assign(this.config, parsed)
159 } catch (err) {
160 console.error('Cannot parse config saved in local storage.', err)
161 }
162 }
163 }
164 }