]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
45f68b4340731abd2918ee0c7efaee36ca6fd5bf
[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 signup: {
20 allowed: false
21 },
22 transcoding: {
23 enabledResolutions: []
24 },
25 avatar: {
26 file: {
27 size: { max: 0 },
28 extensions: []
29 }
30 },
31 video: {
32 file: {
33 extensions: []
34 }
35 }
36 }
37 private videoCategories: Array<{ id: number, label: string }> = []
38 private videoLicences: Array<{ id: number, label: string }> = []
39 private videoLanguages: Array<{ id: number, label: string }> = []
40 private videoPrivacies: Array<{ id: number, label: string }> = []
41
42 constructor (private http: HttpClient) {}
43
44 loadConfig () {
45 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
46 .subscribe(data => this.config = data)
47 }
48
49 loadVideoCategories () {
50 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
51 }
52
53 loadVideoLicences () {
54 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
55 }
56
57 loadVideoLanguages () {
58 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
59 }
60
61 loadVideoPrivacies () {
62 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
63 }
64
65 getConfig () {
66 return this.config
67 }
68
69 getVideoCategories () {
70 return this.videoCategories
71 }
72
73 getVideoLicences () {
74 return this.videoLicences
75 }
76
77 getVideoLanguages () {
78 return this.videoLanguages
79 }
80
81 getVideoPrivacies () {
82 return this.videoPrivacies
83 }
84
85 private loadVideoAttributeEnum (
86 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
87 hashToPopulate: { id: number, label: string }[],
88 notifier: ReplaySubject<boolean>
89 ) {
90 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
91 .subscribe(data => {
92 Object.keys(data)
93 .forEach(dataKey => {
94 hashToPopulate.push({
95 id: parseInt(dataKey, 10),
96 label: data[dataKey]
97 })
98 })
99
100 notifier.next(true)
101 })
102 }
103 }