1 import { getServerCommit } from '@server/helpers/version'
2 import { CONFIG, isEmailEnabled } from '@server/initializers/config'
3 import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '@server/initializers/constants'
4 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '@server/lib/signup'
5 import { ActorCustomPageModel } from '@server/models/account/actor-custom-page'
6 import { PluginModel } from '@server/models/server/plugin'
7 import { HTMLServerConfig, RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig } from '@shared/models'
8 import { Hooks } from './plugins/hooks'
9 import { PluginManager } from './plugins/plugin-manager'
10 import { getThemeOrDefault } from './plugins/theme-utils'
11 import { VideoTranscodingProfilesManager } from './transcoding/default-transcoding-profiles'
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)
20 class ServerConfigManager {
22 private static instance: ServerConfigManager
24 private serverCommit: string
26 private homepageEnabled = false
28 private constructor () {}
31 const instanceHomepage = await ActorCustomPageModel.loadInstanceHomepage()
33 this.updateHomepageState(instanceHomepage?.content)
36 updateHomepageState (content: string) {
37 this.homepageEnabled = !!content
40 async getHTMLServerConfig (): Promise<HTMLServerConfig> {
41 if (this.serverCommit === undefined) this.serverCommit = await getServerCommit()
43 const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
49 displayAuthorAvatar: CONFIG.CLIENT.VIDEOS.MINIATURE.DISPLAY_AUTHOR_AVATAR,
50 preferAuthorDisplayName: CONFIG.CLIENT.VIDEOS.MINIATURE.PREFER_AUTHOR_DISPLAY_NAME
53 maxChunkSize: CONFIG.CLIENT.VIDEOS.RESUMABLE_UPLOAD.MAX_CHUNK_SIZE
58 redirectOnSingleExternalAuth: CONFIG.CLIENT.MENU.LOGIN.REDIRECT_ON_SINGLE_EXTERNAL_AUTH
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
72 enabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED
75 enabled: CONFIG.DEFAULTS.P2P.EMBED.ENABLED
83 allowed: CONFIG.WEBADMIN.CONFIGURATION.EDITION.ALLOWED
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,
95 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
96 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
101 users: CONFIG.SEARCH.REMOTE_URI.USERS,
102 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
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
112 registered: this.getRegisteredPlugins(),
113 registeredExternalAuths: this.getExternalAuthsPlugins(),
114 registeredIdAndPassAuths: this.getIdAndPassAuthPlugins()
117 registered: this.getRegisteredThemes(),
118 default: defaultTheme
121 enabled: isEmailEnabled()
124 enabled: CONFIG.CONTACT_FORM.ENABLED
126 serverVersion: PEERTUBE_VERSION,
127 serverCommit: this.serverCommit,
130 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.REMOTE_RUNNERS.ENABLED
133 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.HLS.ENABLED
136 enabled: CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.WEBTORRENT.ENABLED
138 enabledResolutions: this.getEnabledResolutions('vod'),
139 profile: CONFIG.TRANSCODING.PROFILE,
140 availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('vod')
143 enabled: CONFIG.LIVE.ENABLED,
145 allowReplay: CONFIG.LIVE.ALLOW_REPLAY,
147 enabled: CONFIG.LIVE.LATENCY_SETTING.ENABLED
150 maxDuration: CONFIG.LIVE.MAX_DURATION,
151 maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES,
152 maxUserLives: CONFIG.LIVE.MAX_USER_LIVES,
155 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
157 enabled: CONFIG.LIVE.TRANSCODING.ENABLED && CONFIG.LIVE.TRANSCODING.REMOTE_RUNNERS.ENABLED
159 enabledResolutions: this.getEnabledResolutions('live'),
160 profile: CONFIG.LIVE.TRANSCODING.PROFILE,
161 availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('live')
165 port: CONFIG.LIVE.RTMP.PORT
169 enabled: CONFIG.VIDEO_STUDIO.ENABLED,
171 enabled: CONFIG.VIDEO_STUDIO.REMOTE_RUNNERS.ENABLED
177 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
180 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
183 videoChannelSynchronization: {
184 enabled: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED
190 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
197 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
199 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
205 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
207 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
212 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
214 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
218 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
224 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
226 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
230 videoQuota: CONFIG.USER.VIDEO_QUOTA,
231 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
234 maxPerUser: CONFIG.VIDEO_CHANNELS.MAX_PER_USER
238 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS,
240 enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED,
241 default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT
246 enabled: CONFIG.TRACKER.ENABLED
252 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
258 enabled: CONFIG.BROADCAST_MESSAGE.ENABLED,
259 message: CONFIG.BROADCAST_MESSAGE.MESSAGE,
260 level: CONFIG.BROADCAST_MESSAGE.LEVEL,
261 dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE
265 enabled: this.homepageEnabled
270 async getServerConfig (ip?: string): Promise<ServerConfig> {
271 const { allowed } = await Hooks.wrapPromiseFun(
276 signupMode: CONFIG.SIGNUP.REQUIRES_APPROVAL
277 ? 'request-registration'
278 : 'direct-registration'
281 CONFIG.SIGNUP.REQUIRES_APPROVAL
282 ? 'filter:api.user.request-signup.allowed.result'
283 : 'filter:api.user.signup.allowed.result'
286 const allowedForCurrentIP = isSignupAllowedForCurrentIP(ip)
291 minimumAge: CONFIG.SIGNUP.MINIMUM_AGE,
292 requiresApproval: CONFIG.SIGNUP.REQUIRES_APPROVAL,
293 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
296 const htmlConfig = await this.getHTMLServerConfig()
298 return { ...htmlConfig, signup }
301 getRegisteredThemes () {
302 return PluginManager.Instance.getRegisteredThemes()
304 npmName: PluginModel.buildNpmName(t.name, t.type),
307 description: t.description,
309 clientScripts: t.clientScripts
313 getRegisteredPlugins () {
314 return PluginManager.Instance.getRegisteredPlugins()
316 npmName: PluginModel.buildNpmName(p.name, p.type),
319 description: p.description,
320 clientScripts: p.clientScripts
324 getEnabledResolutions (type: 'vod' | 'live') {
325 const transcoding = type === 'vod'
327 : CONFIG.LIVE.TRANSCODING
329 return Object.keys(transcoding.RESOLUTIONS)
330 .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true)
331 .map(r => parseInt(r, 10))
334 private getIdAndPassAuthPlugins () {
335 const result: RegisteredIdAndPassAuthConfig[] = []
337 for (const p of PluginManager.Instance.getIdAndPassAuths()) {
338 for (const auth of p.idAndPassAuths) {
343 authName: auth.authName,
344 weight: auth.getWeight()
352 private getExternalAuthsPlugins () {
353 const result: RegisteredExternalAuthConfig[] = []
355 for (const p of PluginManager.Instance.getExternalAuths()) {
356 for (const auth of p.externalAuths) {
361 authName: auth.authName,
362 authDisplayName: auth.authDisplayName()
370 static get Instance () {
371 return this.instance || (this.instance = new this())
375 // ---------------------------------------------------------------------------