]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Fix title and action buttons in video watch
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
CommitLineData
db7af09b 1import { HttpClient } from '@angular/common/http'
baeefe22
C
2import { Injectable } from '@angular/core'
3import 'rxjs/add/operator/do'
4import { ReplaySubject } from 'rxjs/ReplaySubject'
db7af09b 5import { ServerConfig } from '../../../../../shared'
36f9424f 6import { About } from '../../../../../shared/models/config/about.model'
63c4db6d 7import { environment } from '../../../environments/environment'
db7af09b
C
8
9@Injectable()
10export class ServerService {
63c4db6d
C
11 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
12 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
36f9424f 13 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
db7af09b 14
baeefe22
C
15 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
16 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
17 videoLicencesLoaded = new ReplaySubject<boolean>(1)
18 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
19
db7af09b 20 private config: ServerConfig = {
36f9424f
C
21 instance: {
22 name: 'PeerTube'
23 },
915c5bbe 24 serverVersion: 'Unknown',
db7af09b
C
25 signup: {
26 allowed: false
6a84aafd
C
27 },
28 transcoding: {
29 enabledResolutions: []
01de67b9
C
30 },
31 avatar: {
32 file: {
33 size: { max: 0 },
34 extensions: []
35 }
36 },
37 video: {
38 file: {
39 extensions: []
40 }
db7af09b
C
41 }
42 }
43 private videoCategories: Array<{ id: number, label: string }> = []
44 private videoLicences: Array<{ id: number, label: string }> = []
45 private videoLanguages: Array<{ id: number, label: string }> = []
fd45e8f4 46 private videoPrivacies: Array<{ id: number, label: string }> = []
db7af09b 47
36f9424f
C
48 constructor (private http: HttpClient) {
49 this.loadConfigLocally()
50 }
db7af09b
C
51
52 loadConfig () {
53 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
36f9424f
C
54 .do(this.saveConfigLocally)
55 .subscribe(data => this.config = data)
db7af09b
C
56 }
57
58 loadVideoCategories () {
baeefe22 59 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
db7af09b
C
60 }
61
62 loadVideoLicences () {
baeefe22 63 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
64 }
65
66 loadVideoLanguages () {
baeefe22 67 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
db7af09b
C
68 }
69
fd45e8f4 70 loadVideoPrivacies () {
baeefe22 71 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
fd45e8f4
C
72 }
73
db7af09b
C
74 getConfig () {
75 return this.config
76 }
77
78 getVideoCategories () {
79 return this.videoCategories
80 }
81
82 getVideoLicences () {
83 return this.videoLicences
84 }
85
86 getVideoLanguages () {
87 return this.videoLanguages
88 }
89
fd45e8f4
C
90 getVideoPrivacies () {
91 return this.videoPrivacies
92 }
93
36f9424f
C
94 getAbout () {
95 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
96 }
97
fd45e8f4
C
98 private loadVideoAttributeEnum (
99 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
baeefe22
C
100 hashToPopulate: { id: number, label: string }[],
101 notifier: ReplaySubject<boolean>
fd45e8f4 102 ) {
db7af09b 103 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
baeefe22
C
104 .subscribe(data => {
105 Object.keys(data)
106 .forEach(dataKey => {
107 hashToPopulate.push({
108 id: parseInt(dataKey, 10),
109 label: data[dataKey]
110 })
db7af09b 111 })
cadb46d8
C
112
113 notifier.next(true)
baeefe22 114 })
db7af09b 115 }
36f9424f
C
116
117 private saveConfigLocally (config: ServerConfig) {
118 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
119 }
120
121 private loadConfigLocally () {
122 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
123
124 if (configString) {
125 try {
126 const parsed = JSON.parse(configString)
127 Object.assign(this.config, parsed)
128 } catch (err) {
129 console.error('Cannot parse config saved in local storage.', err)
130 }
131 }
132 }
db7af09b 133}