]>
Commit | Line | Data |
---|---|---|
db7af09b | 1 | import { HttpClient } from '@angular/common/http' |
baeefe22 C |
2 | import { Injectable } from '@angular/core' |
3 | import 'rxjs/add/operator/do' | |
4 | import { ReplaySubject } from 'rxjs/ReplaySubject' | |
db7af09b | 5 | import { ServerConfig } from '../../../../../shared' |
36f9424f | 6 | import { About } from '../../../../../shared/models/config/about.model' |
63c4db6d | 7 | import { environment } from '../../../environments/environment' |
db7af09b C |
8 | |
9 | @Injectable() | |
10 | export 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 | |
baeefe22 C |
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 | ||
db7af09b | 20 | private config: ServerConfig = { |
36f9424f C |
21 | instance: { |
22 | name: 'PeerTube' | |
23 | }, | |
915c5bbe | 24 | serverVersion: 'Unknown', |
db7af09b C |
25 | signup: { |
26 | allowed: false | |
6a84aafd C |
27 | }, |
28 | transcoding: { | |
29 | enabledResolutions: [] | |
01de67b9 C |
30 | }, |
31 | avatar: { | |
32 | file: { | |
33 | size: { max: 0 }, | |
34 | extensions: [] | |
35 | } | |
36 | }, | |
37 | video: { | |
6de36768 C |
38 | image: { |
39 | size: { max: 0 }, | |
40 | extensions: [] | |
41 | }, | |
01de67b9 C |
42 | file: { |
43 | extensions: [] | |
44 | } | |
db7af09b C |
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 }> = [] | |
fd45e8f4 | 50 | private videoPrivacies: Array<{ id: number, label: string }> = [] |
db7af09b | 51 | |
36f9424f C |
52 | constructor (private http: HttpClient) { |
53 | this.loadConfigLocally() | |
54 | } | |
db7af09b C |
55 | |
56 | loadConfig () { | |
57 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
36f9424f C |
58 | .do(this.saveConfigLocally) |
59 | .subscribe(data => this.config = data) | |
db7af09b C |
60 | } |
61 | ||
62 | loadVideoCategories () { | |
baeefe22 | 63 | return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded) |
db7af09b C |
64 | } |
65 | ||
66 | loadVideoLicences () { | |
baeefe22 | 67 | return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded) |
db7af09b C |
68 | } |
69 | ||
70 | loadVideoLanguages () { | |
baeefe22 | 71 | return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded) |
db7af09b C |
72 | } |
73 | ||
fd45e8f4 | 74 | loadVideoPrivacies () { |
baeefe22 | 75 | return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded) |
fd45e8f4 C |
76 | } |
77 | ||
db7af09b C |
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 | ||
fd45e8f4 C |
94 | getVideoPrivacies () { |
95 | return this.videoPrivacies | |
96 | } | |
97 | ||
36f9424f C |
98 | getAbout () { |
99 | return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about') | |
100 | } | |
101 | ||
fd45e8f4 C |
102 | private loadVideoAttributeEnum ( |
103 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
baeefe22 C |
104 | hashToPopulate: { id: number, label: string }[], |
105 | notifier: ReplaySubject<boolean> | |
fd45e8f4 | 106 | ) { |
db7af09b | 107 | return this.http.get(ServerService.BASE_VIDEO_URL + attributeName) |
baeefe22 C |
108 | .subscribe(data => { |
109 | Object.keys(data) | |
110 | .forEach(dataKey => { | |
111 | hashToPopulate.push({ | |
112 | id: parseInt(dataKey, 10), | |
113 | label: data[dataKey] | |
114 | }) | |
db7af09b | 115 | }) |
cadb46d8 C |
116 | |
117 | notifier.next(true) | |
baeefe22 | 118 | }) |
db7af09b | 119 | } |
36f9424f C |
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 | } | |
db7af09b | 137 | } |