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