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