]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Use instance name for page titles
[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 { About } from '../../../../../shared/models/server/about.model'
7 import { environment } from '../../../environments/environment'
8
9 @Injectable()
10 export class ServerService {
11 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
12 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
13 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
14
15 configLoaded = new ReplaySubject<boolean>(1)
16 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
17 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
18 videoLicencesLoaded = new ReplaySubject<boolean>(1)
19 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
20
21 private config: ServerConfig = {
22 instance: {
23 name: 'PeerTube',
24 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
25 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
26 defaultClientRoute: '',
27 customizations: {
28 javascript: '',
29 css: ''
30 }
31 },
32 serverVersion: 'Unknown',
33 signup: {
34 allowed: false
35 },
36 transcoding: {
37 enabledResolutions: []
38 },
39 avatar: {
40 file: {
41 size: { max: 0 },
42 extensions: []
43 }
44 },
45 video: {
46 image: {
47 size: { max: 0 },
48 extensions: []
49 },
50 file: {
51 extensions: []
52 }
53 }
54 }
55 private videoCategories: Array<{ id: number, label: string }> = []
56 private videoLicences: Array<{ id: number, label: string }> = []
57 private videoLanguages: Array<{ id: number, label: string }> = []
58 private videoPrivacies: Array<{ id: number, label: string }> = []
59
60 constructor (private http: HttpClient) {
61 this.loadConfigLocally()
62 }
63
64 loadConfig () {
65 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
66 .do(this.saveConfigLocally)
67 .subscribe(data => {
68 this.config = data
69
70 this.configLoaded.next(true)
71 })
72 }
73
74 loadVideoCategories () {
75 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
76 }
77
78 loadVideoLicences () {
79 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
80 }
81
82 loadVideoLanguages () {
83 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
84 }
85
86 loadVideoPrivacies () {
87 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
88 }
89
90 getConfig () {
91 return this.config
92 }
93
94 getVideoCategories () {
95 return this.videoCategories
96 }
97
98 getVideoLicences () {
99 return this.videoLicences
100 }
101
102 getVideoLanguages () {
103 return this.videoLanguages
104 }
105
106 getVideoPrivacies () {
107 return this.videoPrivacies
108 }
109
110 getAbout () {
111 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
112 }
113
114 private loadVideoAttributeEnum (
115 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
116 hashToPopulate: { id: number, label: string }[],
117 notifier: ReplaySubject<boolean>,
118 sort = false
119 ) {
120 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
121 .subscribe(data => {
122 Object.keys(data)
123 .forEach(dataKey => {
124 hashToPopulate.push({
125 id: parseInt(dataKey, 10),
126 label: data[dataKey]
127 })
128 })
129
130 if (sort === true) {
131 hashToPopulate.sort((a, b) => {
132 if (a.label < b.label) return -1
133 if (a.label === b.label) return 0
134 return 1
135 })
136 }
137
138 notifier.next(true)
139 })
140 }
141
142 private saveConfigLocally (config: ServerConfig) {
143 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
144 }
145
146 private loadConfigLocally () {
147 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
148
149 if (configString) {
150 try {
151 const parsed = JSON.parse(configString)
152 Object.assign(this.config, parsed)
153 } catch (err) {
154 console.error('Cannot parse config saved in local storage.', err)
155 }
156 }
157 }
158 }