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