]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
db7af09b 1import { HttpClient } from '@angular/common/http'
baeefe22
C
2import { Injectable } from '@angular/core'
3import 'rxjs/add/operator/do'
4import { ReplaySubject } from 'rxjs/ReplaySubject'
db7af09b 5import { ServerConfig } from '../../../../../shared'
09cababd 6import { About } from '../../../../../shared/models/server/about.model'
63c4db6d 7import { environment } from '../../../environments/environment'
db7af09b
C
8
9@Injectable()
10export class ServerService {
63c4db6d
C
11 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
12 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
36f9424f 13 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
db7af09b 14
00b5556c 15 configLoaded = new ReplaySubject<boolean>(1)
baeefe22
C
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
db7af09b 21 private config: ServerConfig = {
36f9424f 22 instance: {
00b5556c 23 name: 'PeerTube',
2e3a0215 24 shortDescription: '',
901637bb 25 defaultClientRoute: '',
00b5556c
C
26 customizations: {
27 javascript: '',
28 css: ''
29 }
36f9424f 30 },
915c5bbe 31 serverVersion: 'Unknown',
db7af09b
C
32 signup: {
33 allowed: false
6a84aafd
C
34 },
35 transcoding: {
36 enabledResolutions: []
01de67b9
C
37 },
38 avatar: {
39 file: {
40 size: { max: 0 },
41 extensions: []
42 }
43 },
44 video: {
6de36768
C
45 image: {
46 size: { max: 0 },
47 extensions: []
48 },
01de67b9
C
49 file: {
50 extensions: []
51 }
db7af09b
C
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 }> = []
fd45e8f4 57 private videoPrivacies: Array<{ id: number, label: string }> = []
db7af09b 58
36f9424f
C
59 constructor (private http: HttpClient) {
60 this.loadConfigLocally()
61 }
db7af09b
C
62
63 loadConfig () {
64 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
36f9424f 65 .do(this.saveConfigLocally)
00b5556c
C
66 .subscribe(data => {
67 this.config = data
68
69 this.configLoaded.next(true)
70 })
db7af09b
C
71 }
72
73 loadVideoCategories () {
3580fc00 74 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
75 }
76
77 loadVideoLicences () {
baeefe22 78 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
79 }
80
81 loadVideoLanguages () {
3580fc00 82 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
83 }
84
fd45e8f4 85 loadVideoPrivacies () {
baeefe22 86 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
fd45e8f4
C
87 }
88
db7af09b
C
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
fd45e8f4
C
105 getVideoPrivacies () {
106 return this.videoPrivacies
107 }
108
36f9424f
C
109 getAbout () {
110 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
111 }
112
fd45e8f4
C
113 private loadVideoAttributeEnum (
114 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
baeefe22 115 hashToPopulate: { id: number, label: string }[],
3580fc00
C
116 notifier: ReplaySubject<boolean>,
117 sort = false
fd45e8f4 118 ) {
db7af09b 119 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
baeefe22
C
120 .subscribe(data => {
121 Object.keys(data)
122 .forEach(dataKey => {
123 hashToPopulate.push({
124 id: parseInt(dataKey, 10),
125 label: data[dataKey]
126 })
db7af09b 127 })
cadb46d8 128
3580fc00
C
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
cadb46d8 137 notifier.next(true)
baeefe22 138 })
db7af09b 139 }
36f9424f
C
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 }
db7af09b 157}