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