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