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