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