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