]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
6df449018763c4d518ef40b7214ec99a1d3c6794
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { HttpClient } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/do'
4 import { ReplaySubject } from 'rxjs/ReplaySubject'
5 import { ServerConfig } from '../../../../../shared'
6 import { environment } from '../../../environments/environment'
7
8 @Injectable()
9 export class ServerService {
10 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
11 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
12
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
18 private config: ServerConfig = {
19 serverVersion: 'Unknown',
20 signup: {
21 allowed: false
22 },
23 transcoding: {
24 enabledResolutions: []
25 },
26 avatar: {
27 file: {
28 size: { max: 0 },
29 extensions: []
30 }
31 },
32 video: {
33 file: {
34 extensions: []
35 }
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 }> = []
41 private videoPrivacies: Array<{ id: number, label: string }> = []
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 () {
51 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
52 }
53
54 loadVideoLicences () {
55 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
56 }
57
58 loadVideoLanguages () {
59 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
60 }
61
62 loadVideoPrivacies () {
63 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
64 }
65
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
82 getVideoPrivacies () {
83 return this.videoPrivacies
84 }
85
86 private loadVideoAttributeEnum (
87 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
88 hashToPopulate: { id: number, label: string }[],
89 notifier: ReplaySubject<boolean>
90 ) {
91 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
92 .subscribe(data => {
93 Object.keys(data)
94 .forEach(dataKey => {
95 hashToPopulate.push({
96 id: parseInt(dataKey, 10),
97 label: data[dataKey]
98 })
99 })
100
101 notifier.next(true)
102 })
103 }
104 }