Add ability to update thumbnail and preview on client
[oweals/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       image: {
39         size: { max: 0 },
40         extensions: []
41       },
42       file: {
43         extensions: []
44       }
45     }
46   }
47   private videoCategories: Array<{ id: number, label: string }> = []
48   private videoLicences: Array<{ id: number, label: string }> = []
49   private videoLanguages: Array<{ id: number, label: string }> = []
50   private videoPrivacies: Array<{ id: number, label: string }> = []
51
52   constructor (private http: HttpClient) {
53     this.loadConfigLocally()
54   }
55
56   loadConfig () {
57     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
58       .do(this.saveConfigLocally)
59       .subscribe(data => this.config = data)
60   }
61
62   loadVideoCategories () {
63     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
64   }
65
66   loadVideoLicences () {
67     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
68   }
69
70   loadVideoLanguages () {
71     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
72   }
73
74   loadVideoPrivacies () {
75     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
76   }
77
78   getConfig () {
79     return this.config
80   }
81
82   getVideoCategories () {
83     return this.videoCategories
84   }
85
86   getVideoLicences () {
87     return this.videoLicences
88   }
89
90   getVideoLanguages () {
91     return this.videoLanguages
92   }
93
94   getVideoPrivacies () {
95     return this.videoPrivacies
96   }
97
98   getAbout () {
99     return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
100   }
101
102   private loadVideoAttributeEnum (
103     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
104     hashToPopulate: { id: number, label: string }[],
105     notifier: ReplaySubject<boolean>
106   ) {
107     return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
108        .subscribe(data => {
109          Object.keys(data)
110                .forEach(dataKey => {
111                  hashToPopulate.push({
112                    id: parseInt(dataKey, 10),
113                    label: data[dataKey]
114                  })
115                })
116
117          notifier.next(true)
118        })
119   }
120
121   private saveConfigLocally (config: ServerConfig) {
122     localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
123   }
124
125   private loadConfigLocally () {
126     const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
127
128     if (configString) {
129       try {
130         const parsed = JSON.parse(configString)
131         Object.assign(this.config, parsed)
132       } catch (err) {
133         console.error('Cannot parse config saved in local storage.', err)
134       }
135     }
136   }
137 }