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