]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/server-config-manager.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / server-config-manager.ts
CommitLineData
c795e196 1import { getServerCommit } from '@server/helpers/version'
2539932e
C
2import { CONFIG, isEmailEnabled } from '@server/initializers/config'
3import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '@server/initializers/constants'
10363c74 4import { isSignupAllowed, isSignupAllowedForCurrentIP } from '@server/lib/signup'
2539932e 5import { ActorCustomPageModel } from '@server/models/account/actor-custom-page'
fb3c9e2b 6import { PluginModel } from '@server/models/server/plugin'
2539932e
C
7import { HTMLServerConfig, RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig } from '@shared/models'
8import { Hooks } from './plugins/hooks'
9import { PluginManager } from './plugins/plugin-manager'
10import { getThemeOrDefault } from './plugins/theme-utils'
c729caf6 11import { VideoTranscodingProfilesManager } from './transcoding/default-transcoding-profiles'
2539932e
C
12
13/**
14 *
15 * Used to send the server config to clients (using REST/API or plugins API)
16 * We need a singleton class to manage config state depending on external events (to build menu entries etc)
17 *
18 */
19
20class ServerConfigManager {
21
22 private static instance: ServerConfigManager
23
24 private serverCommit: string
25
26 private homepageEnabled = false
27
28 private constructor () {}
29
30 async init () {
31 const instanceHomepage = await ActorCustomPageModel.loadInstanceHomepage()
32
33 this.updateHomepageState(instanceHomepage?.content)
34 }
35
36 updateHomepageState (content: string) {
37 this.homepageEnabled = !!content
38 }
39
40 async getHTMLServerConfig (): Promise<HTMLServerConfig> {
41 if (this.serverCommit === undefined) this.serverCommit = await getServerCommit()
42
43 const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
44
45 return {
7399a79f 46 client: {
47 videos: {
48 miniature: {
2c102aac 49 displayAuthorAvatar: CONFIG.CLIENT.VIDEOS.MINIATURE.DISPLAY_AUTHOR_AVATAR,
80dea8f4 50 preferAuthorDisplayName: CONFIG.CLIENT.VIDEOS.MINIATURE.PREFER_AUTHOR_DISPLAY_NAME
60233e90 51 },
52 resumableUpload: {
53 maxChunkSize: CONFIG.CLIENT.VIDEOS.RESUMABLE_UPLOAD.MAX_CHUNK_SIZE
7399a79f 54 }
0bc53e20
C
55 },
56 menu: {
57 login: {
58 redirectOnSingleExternalAuth: CONFIG.CLIENT.MENU.LOGIN.REDIRECT_ON_SINGLE_EXTERNAL_AUTH
59 }
7399a79f 60 }
61 },
cf0c8ee5 62
3cf68b86
C
63 defaults: {
64 publish: {
65 downloadEnabled: CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
66 commentsEnabled: CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
67 privacy: CONFIG.DEFAULTS.PUBLISH.PRIVACY,
68 licence: CONFIG.DEFAULTS.PUBLISH.LICENCE
a9bfa85d
C
69 },
70 p2p: {
b65de1be
C
71 webapp: {
72 enabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED
73 },
74 embed: {
75 enabled: CONFIG.DEFAULTS.P2P.EMBED.ENABLED
76 }
3cf68b86
C
77 }
78 },
79
cf0c8ee5
C
80 webadmin: {
81 configuration: {
82 edition: {
83 allowed: CONFIG.WEBADMIN.CONFIGURATION.EDITION.ALLOWED
84 }
85 }
86 },
87
2539932e
C
88 instance: {
89 name: CONFIG.INSTANCE.NAME,
90 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
91 isNSFW: CONFIG.INSTANCE.IS_NSFW,
92 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
93 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
94 customizations: {
95 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
96 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
97 }
98 },
99 search: {
100 remoteUri: {
101 users: CONFIG.SEARCH.REMOTE_URI.USERS,
102 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
103 },
104 searchIndex: {
105 enabled: CONFIG.SEARCH.SEARCH_INDEX.ENABLED,
106 url: CONFIG.SEARCH.SEARCH_INDEX.URL,
107 disableLocalSearch: CONFIG.SEARCH.SEARCH_INDEX.DISABLE_LOCAL_SEARCH,
108 isDefaultSearch: CONFIG.SEARCH.SEARCH_INDEX.IS_DEFAULT_SEARCH
109 }
110 },
111 plugin: {
112 registered: this.getRegisteredPlugins(),
113 registeredExternalAuths: this.getExternalAuthsPlugins(),
114 registeredIdAndPassAuths: this.getIdAndPassAuthPlugins()
115 },
116 theme: {
117 registered: this.getRegisteredThemes(),
118 default: defaultTheme
119 },
120 email: {
121 enabled: isEmailEnabled()
122 },
123 contactForm: {
124 enabled: CONFIG.CONTACT_FORM.ENABLED
125 },
126 serverVersion: PEERTUBE_VERSION,
127 serverCommit: this.serverCommit,
128 transcoding: {
0c9668f7
C
129 remoteRunners: {
130 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.REMOTE_RUNNERS.ENABLED
131 },
2539932e 132 hls: {
0c9668f7 133 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.HLS.ENABLED
2539932e
C
134 },
135 webtorrent: {
0c9668f7 136 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.WEBTORRENT.ENABLED
2539932e
C
137 },
138 enabledResolutions: this.getEnabledResolutions('vod'),
139 profile: CONFIG.TRANSCODING.PROFILE,
140 availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('vod')
141 },
142 live: {
143 enabled: CONFIG.LIVE.ENABLED,
144
145 allowReplay: CONFIG.LIVE.ALLOW_REPLAY,
f443a746
C
146 latencySetting: {
147 enabled: CONFIG.LIVE.LATENCY_SETTING.ENABLED
148 },
149
2539932e
C
150 maxDuration: CONFIG.LIVE.MAX_DURATION,
151 maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES,
152 maxUserLives: CONFIG.LIVE.MAX_USER_LIVES,
153
154 transcoding: {
155 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
0c9668f7
C
156 remoteRunners: {
157 enabled: CONFIG.LIVE.TRANSCODING.ENABLED && CONFIG.LIVE.TRANSCODING.REMOTE_RUNNERS.ENABLED
158 },
2539932e
C
159 enabledResolutions: this.getEnabledResolutions('live'),
160 profile: CONFIG.LIVE.TRANSCODING.PROFILE,
161 availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('live')
162 },
163
164 rtmp: {
165 port: CONFIG.LIVE.RTMP.PORT
166 }
167 },
92e66e04 168 videoStudio: {
5e47f6ab
C
169 enabled: CONFIG.VIDEO_STUDIO.ENABLED,
170 remoteRunners: {
171 enabled: CONFIG.VIDEO_STUDIO.REMOTE_RUNNERS.ENABLED
172 }
c729caf6 173 },
2539932e
C
174 import: {
175 videos: {
176 http: {
177 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
178 },
179 torrent: {
180 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
181 }
2a491182
F
182 },
183 videoChannelSynchronization: {
184 enabled: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED
2539932e
C
185 }
186 },
187 autoBlacklist: {
188 videos: {
189 ofUsers: {
190 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
191 }
192 }
193 },
194 avatar: {
195 file: {
196 size: {
197 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
198 },
199 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
200 }
201 },
202 banner: {
203 file: {
204 size: {
205 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
206 },
207 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
208 }
209 },
210 video: {
211 image: {
212 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
213 size: {
214 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
215 }
216 },
217 file: {
218 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
219 }
220 },
221 videoCaption: {
222 file: {
223 size: {
224 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
225 },
226 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
227 }
228 },
229 user: {
230 videoQuota: CONFIG.USER.VIDEO_QUOTA,
231 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
232 },
754b6f5f
FC
233 videoChannels: {
234 maxPerUser: CONFIG.VIDEO_CHANNELS.MAX_PER_USER
235 },
2539932e
C
236 trending: {
237 videos: {
238 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS,
239 algorithms: {
240 enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED,
241 default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT
242 }
243 }
244 },
245 tracker: {
246 enabled: CONFIG.TRACKER.ENABLED
247 },
248
249 followings: {
250 instance: {
251 autoFollowIndex: {
252 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
253 }
254 }
255 },
256
257 broadcastMessage: {
258 enabled: CONFIG.BROADCAST_MESSAGE.ENABLED,
259 message: CONFIG.BROADCAST_MESSAGE.MESSAGE,
260 level: CONFIG.BROADCAST_MESSAGE.LEVEL,
261 dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE
262 },
263
264 homepage: {
265 enabled: this.homepageEnabled
266 }
267 }
268 }
269
270 async getServerConfig (ip?: string): Promise<ServerConfig> {
271 const { allowed } = await Hooks.wrapPromiseFun(
272 isSignupAllowed,
e364e31e 273
2539932e 274 {
e364e31e
C
275 ip,
276 signupMode: CONFIG.SIGNUP.REQUIRES_APPROVAL
277 ? 'request-registration'
278 : 'direct-registration'
2539932e 279 },
e364e31e
C
280
281 CONFIG.SIGNUP.REQUIRES_APPROVAL
282 ? 'filter:api.user.request-signup.allowed.result'
283 : 'filter:api.user.signup.allowed.result'
2539932e
C
284 )
285
286 const allowedForCurrentIP = isSignupAllowedForCurrentIP(ip)
287
288 const signup = {
289 allowed,
290 allowedForCurrentIP,
1f256e7d 291 minimumAge: CONFIG.SIGNUP.MINIMUM_AGE,
e364e31e 292 requiresApproval: CONFIG.SIGNUP.REQUIRES_APPROVAL,
2539932e
C
293 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
294 }
295
296 const htmlConfig = await this.getHTMLServerConfig()
297
298 return { ...htmlConfig, signup }
299 }
300
301 getRegisteredThemes () {
302 return PluginManager.Instance.getRegisteredThemes()
303 .map(t => ({
fb3c9e2b 304 npmName: PluginModel.buildNpmName(t.name, t.type),
2539932e
C
305 name: t.name,
306 version: t.version,
307 description: t.description,
308 css: t.css,
309 clientScripts: t.clientScripts
310 }))
311 }
312
313 getRegisteredPlugins () {
314 return PluginManager.Instance.getRegisteredPlugins()
315 .map(p => ({
fb3c9e2b 316 npmName: PluginModel.buildNpmName(p.name, p.type),
2539932e
C
317 name: p.name,
318 version: p.version,
319 description: p.description,
320 clientScripts: p.clientScripts
321 }))
322 }
323
324 getEnabledResolutions (type: 'vod' | 'live') {
325 const transcoding = type === 'vod'
326 ? CONFIG.TRANSCODING
327 : CONFIG.LIVE.TRANSCODING
328
329 return Object.keys(transcoding.RESOLUTIONS)
330 .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true)
331 .map(r => parseInt(r, 10))
332 }
333
334 private getIdAndPassAuthPlugins () {
335 const result: RegisteredIdAndPassAuthConfig[] = []
336
337 for (const p of PluginManager.Instance.getIdAndPassAuths()) {
338 for (const auth of p.idAndPassAuths) {
339 result.push({
340 npmName: p.npmName,
341 name: p.name,
342 version: p.version,
343 authName: auth.authName,
344 weight: auth.getWeight()
345 })
346 }
347 }
348
349 return result
350 }
351
352 private getExternalAuthsPlugins () {
353 const result: RegisteredExternalAuthConfig[] = []
354
355 for (const p of PluginManager.Instance.getExternalAuths()) {
356 for (const auth of p.externalAuths) {
357 result.push({
358 npmName: p.npmName,
359 name: p.name,
360 version: p.version,
361 authName: auth.authName,
362 authDisplayName: auth.authDisplayName()
363 })
364 }
365 }
366
367 return result
368 }
369
370 static get Instance () {
371 return this.instance || (this.instance = new this())
372 }
373}
374
375// ---------------------------------------------------------------------------
376
377export {
378 ServerConfigManager
379}