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