]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/server/server.service.ts
Add ability to update another user video
[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: {
6de36768
C
38 image: {
39 size: { max: 0 },
40 extensions: []
41 },
01de67b9
C
42 file: {
43 extensions: []
44 }
db7af09b
C
45 }
46 }
47 private videoCategories: Array<{ id: number, label: string }> = []
48 private videoLicences: Array<{ id: number, label: string }> = []
49 private videoLanguages: Array<{ id: number, label: string }> = []
fd45e8f4 50 private videoPrivacies: Array<{ id: number, label: string }> = []
db7af09b 51
36f9424f
C
52 constructor (private http: HttpClient) {
53 this.loadConfigLocally()
54 }
db7af09b
C
55
56 loadConfig () {
57 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
36f9424f
C
58 .do(this.saveConfigLocally)
59 .subscribe(data => this.config = data)
db7af09b
C
60 }
61
62 loadVideoCategories () {
3580fc00 63 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
db7af09b
C
64 }
65
66 loadVideoLicences () {
baeefe22 67 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
db7af09b
C
68 }
69
70 loadVideoLanguages () {
3580fc00 71 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
db7af09b
C
72 }
73
fd45e8f4 74 loadVideoPrivacies () {
baeefe22 75 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
fd45e8f4
C
76 }
77
db7af09b
C
78 getConfig () {
79 return this.config
80 }
81
82 getVideoCategories () {
83 return this.videoCategories
84 }
85
86 getVideoLicences () {
87 return this.videoLicences
88 }
89
90 getVideoLanguages () {
91 return this.videoLanguages
92 }
93
fd45e8f4
C
94 getVideoPrivacies () {
95 return this.videoPrivacies
96 }
97
36f9424f
C
98 getAbout () {
99 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
100 }
101
fd45e8f4
C
102 private loadVideoAttributeEnum (
103 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
baeefe22 104 hashToPopulate: { id: number, label: string }[],
3580fc00
C
105 notifier: ReplaySubject<boolean>,
106 sort = false
fd45e8f4 107 ) {
db7af09b 108 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
baeefe22
C
109 .subscribe(data => {
110 Object.keys(data)
111 .forEach(dataKey => {
112 hashToPopulate.push({
113 id: parseInt(dataKey, 10),
114 label: data[dataKey]
115 })
db7af09b 116 })
cadb46d8 117
3580fc00
C
118 if (sort === true) {
119 hashToPopulate.sort((a, b) => {
120 if (a.label < b.label) return -1
121 if (a.label === b.label) return 0
122 return 1
123 })
124 }
125
cadb46d8 126 notifier.next(true)
baeefe22 127 })
db7af09b 128 }
36f9424f
C
129
130 private saveConfigLocally (config: ServerConfig) {
131 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
132 }
133
134 private loadConfigLocally () {
135 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
136
137 if (configString) {
138 try {
139 const parsed = JSON.parse(configString)
140 Object.assign(this.config, parsed)
141 } catch (err) {
142 console.error('Cannot parse config saved in local storage.', err)
143 }
144 }
145 }
db7af09b 146}