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