]>
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' |
63c4db6d | 6 | import { environment } from '../../../environments/environment' |
db7af09b C |
7 | |
8 | @Injectable() | |
9 | export class ServerService { | |
63c4db6d C |
10 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
11 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
db7af09b | 12 | |
baeefe22 C |
13 | videoPrivaciesLoaded = new ReplaySubject<boolean>(1) |
14 | videoCategoriesLoaded = new ReplaySubject<boolean>(1) | |
15 | videoLicencesLoaded = new ReplaySubject<boolean>(1) | |
16 | videoLanguagesLoaded = new ReplaySubject<boolean>(1) | |
17 | ||
db7af09b | 18 | private config: ServerConfig = { |
915c5bbe | 19 | serverVersion: 'Unknown', |
db7af09b C |
20 | signup: { |
21 | allowed: false | |
6a84aafd C |
22 | }, |
23 | transcoding: { | |
24 | enabledResolutions: [] | |
01de67b9 C |
25 | }, |
26 | avatar: { | |
27 | file: { | |
28 | size: { max: 0 }, | |
29 | extensions: [] | |
30 | } | |
31 | }, | |
32 | video: { | |
33 | file: { | |
34 | extensions: [] | |
35 | } | |
db7af09b C |
36 | } |
37 | } | |
38 | private videoCategories: Array<{ id: number, label: string }> = [] | |
39 | private videoLicences: Array<{ id: number, label: string }> = [] | |
40 | private videoLanguages: Array<{ id: number, label: string }> = [] | |
fd45e8f4 | 41 | private videoPrivacies: Array<{ id: number, label: string }> = [] |
db7af09b C |
42 | |
43 | constructor (private http: HttpClient) {} | |
44 | ||
45 | loadConfig () { | |
46 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
47 | .subscribe(data => this.config = data) | |
48 | } | |
49 | ||
50 | loadVideoCategories () { | |
baeefe22 | 51 | return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded) |
db7af09b C |
52 | } |
53 | ||
54 | loadVideoLicences () { | |
baeefe22 | 55 | return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded) |
db7af09b C |
56 | } |
57 | ||
58 | loadVideoLanguages () { | |
baeefe22 | 59 | return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded) |
db7af09b C |
60 | } |
61 | ||
fd45e8f4 | 62 | loadVideoPrivacies () { |
baeefe22 | 63 | return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded) |
fd45e8f4 C |
64 | } |
65 | ||
db7af09b C |
66 | getConfig () { |
67 | return this.config | |
68 | } | |
69 | ||
70 | getVideoCategories () { | |
71 | return this.videoCategories | |
72 | } | |
73 | ||
74 | getVideoLicences () { | |
75 | return this.videoLicences | |
76 | } | |
77 | ||
78 | getVideoLanguages () { | |
79 | return this.videoLanguages | |
80 | } | |
81 | ||
fd45e8f4 C |
82 | getVideoPrivacies () { |
83 | return this.videoPrivacies | |
84 | } | |
85 | ||
86 | private loadVideoAttributeEnum ( | |
87 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
baeefe22 C |
88 | hashToPopulate: { id: number, label: string }[], |
89 | notifier: ReplaySubject<boolean> | |
fd45e8f4 | 90 | ) { |
db7af09b | 91 | return this.http.get(ServerService.BASE_VIDEO_URL + attributeName) |
baeefe22 C |
92 | .subscribe(data => { |
93 | Object.keys(data) | |
94 | .forEach(dataKey => { | |
95 | hashToPopulate.push({ | |
96 | id: parseInt(dataKey, 10), | |
97 | label: data[dataKey] | |
98 | }) | |
db7af09b | 99 | }) |
cadb46d8 C |
100 | |
101 | notifier.next(true) | |
baeefe22 | 102 | }) |
db7af09b C |
103 | } |
104 | } |