diff options
Diffstat (limited to 'server')
21 files changed, 648 insertions, 296 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 5ce7adc35..c9b5c8047 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import { ServerConfigManager } from '@server/lib/server-config-manager' | ||
1 | import * as express from 'express' | 2 | import * as express from 'express' |
2 | import { remove, writeJSON } from 'fs-extra' | 3 | import { remove, writeJSON } from 'fs-extra' |
3 | import { snakeCase } from 'lodash' | 4 | import { snakeCase } from 'lodash' |
4 | import validator from 'validator' | 5 | import validator from 'validator' |
5 | import { getServerConfig } from '@server/lib/config' | ||
6 | import { UserRight } from '../../../shared' | 6 | import { UserRight } from '../../../shared' |
7 | import { About } from '../../../shared/models/server/about.model' | 7 | import { About } from '../../../shared/models/server/about.model' |
8 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' | 8 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' |
@@ -43,7 +43,7 @@ configRouter.delete('/custom', | |||
43 | ) | 43 | ) |
44 | 44 | ||
45 | async function getConfig (req: express.Request, res: express.Response) { | 45 | async function getConfig (req: express.Request, res: express.Response) { |
46 | const json = await getServerConfig(req.ip) | 46 | const json = await ServerConfigManager.Instance.getServerConfig(req.ip) |
47 | 47 | ||
48 | return res.json(json) | 48 | return res.json(json) |
49 | } | 49 | } |
diff --git a/server/controllers/api/custom-page.ts b/server/controllers/api/custom-page.ts new file mode 100644 index 000000000..3c47f7b9a --- /dev/null +++ b/server/controllers/api/custom-page.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import * as express from 'express' | ||
2 | import { ServerConfigManager } from '@server/lib/server-config-manager' | ||
3 | import { ActorCustomPageModel } from '@server/models/account/actor-custom-page' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | import { UserRight } from '@shared/models' | ||
6 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' | ||
7 | |||
8 | const customPageRouter = express.Router() | ||
9 | |||
10 | customPageRouter.get('/homepage/instance', | ||
11 | asyncMiddleware(getInstanceHomepage) | ||
12 | ) | ||
13 | |||
14 | customPageRouter.put('/homepage/instance', | ||
15 | authenticate, | ||
16 | ensureUserHasRight(UserRight.MANAGE_INSTANCE_CUSTOM_PAGE), | ||
17 | asyncMiddleware(updateInstanceHomepage) | ||
18 | ) | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | export { | ||
23 | customPageRouter | ||
24 | } | ||
25 | |||
26 | // --------------------------------------------------------------------------- | ||
27 | |||
28 | async function getInstanceHomepage (req: express.Request, res: express.Response) { | ||
29 | const page = await ActorCustomPageModel.loadInstanceHomepage() | ||
30 | if (!page) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | ||
31 | |||
32 | return res.json(page.toFormattedJSON()) | ||
33 | } | ||
34 | |||
35 | async function updateInstanceHomepage (req: express.Request, res: express.Response) { | ||
36 | const content = req.body.content | ||
37 | |||
38 | await ActorCustomPageModel.updateInstanceHomepage(content) | ||
39 | ServerConfigManager.Instance.updateHomepageState(content) | ||
40 | |||
41 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
42 | } | ||
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts index 7ade1df3a..28378654a 100644 --- a/server/controllers/api/index.ts +++ b/server/controllers/api/index.ts | |||
@@ -8,6 +8,7 @@ import { abuseRouter } from './abuse' | |||
8 | import { accountsRouter } from './accounts' | 8 | import { accountsRouter } from './accounts' |
9 | import { bulkRouter } from './bulk' | 9 | import { bulkRouter } from './bulk' |
10 | import { configRouter } from './config' | 10 | import { configRouter } from './config' |
11 | import { customPageRouter } from './custom-page' | ||
11 | import { jobsRouter } from './jobs' | 12 | import { jobsRouter } from './jobs' |
12 | import { oauthClientsRouter } from './oauth-clients' | 13 | import { oauthClientsRouter } from './oauth-clients' |
13 | import { overviewsRouter } from './overviews' | 14 | import { overviewsRouter } from './overviews' |
@@ -47,6 +48,7 @@ apiRouter.use('/jobs', jobsRouter) | |||
47 | apiRouter.use('/search', searchRouter) | 48 | apiRouter.use('/search', searchRouter) |
48 | apiRouter.use('/overviews', overviewsRouter) | 49 | apiRouter.use('/overviews', overviewsRouter) |
49 | apiRouter.use('/plugins', pluginRouter) | 50 | apiRouter.use('/plugins', pluginRouter) |
51 | apiRouter.use('/custom-pages', customPageRouter) | ||
50 | apiRouter.use('/ping', pong) | 52 | apiRouter.use('/ping', pong) |
51 | apiRouter.use('/*', badRequest) | 53 | apiRouter.use('/*', badRequest) |
52 | 54 | ||
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index ee63c7b77..0d5d7a962 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts | |||
@@ -3,7 +3,7 @@ import { move, readFile } from 'fs-extra' | |||
3 | import * as magnetUtil from 'magnet-uri' | 3 | import * as magnetUtil from 'magnet-uri' |
4 | import * as parseTorrent from 'parse-torrent' | 4 | import * as parseTorrent from 'parse-torrent' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { getEnabledResolutions } from '@server/lib/config' | 6 | import { ServerConfigManager } from '@server/lib/server-config-manager' |
7 | import { setVideoTags } from '@server/lib/video' | 7 | import { setVideoTags } from '@server/lib/video' |
8 | import { FilteredModelAttributes } from '@server/types' | 8 | import { FilteredModelAttributes } from '@server/types' |
9 | import { | 9 | import { |
@@ -134,7 +134,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) | |||
134 | const targetUrl = body.targetUrl | 134 | const targetUrl = body.targetUrl |
135 | const user = res.locals.oauth.token.User | 135 | const user = res.locals.oauth.token.User |
136 | 136 | ||
137 | const youtubeDL = new YoutubeDL(targetUrl, getEnabledResolutions('vod')) | 137 | const youtubeDL = new YoutubeDL(targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod')) |
138 | 138 | ||
139 | // Get video infos | 139 | // Get video infos |
140 | let youtubeDLInfo: YoutubeDLInfo | 140 | let youtubeDLInfo: YoutubeDLInfo |
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 8a747ec52..3870ebfe9 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -2,7 +2,7 @@ import * as cors from 'cors' | |||
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { serveIndexHTML } from '@server/lib/client-html' | 4 | import { serveIndexHTML } from '@server/lib/client-html' |
5 | import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/lib/config' | 5 | import { ServerConfigManager } from '@server/lib/server-config-manager' |
6 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | 6 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
7 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo/nodeinfo.model' | 7 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo/nodeinfo.model' |
8 | import { root } from '../helpers/core-utils' | 8 | import { root } from '../helpers/core-utils' |
@@ -203,10 +203,10 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { | |||
203 | } | 203 | } |
204 | }, | 204 | }, |
205 | plugin: { | 205 | plugin: { |
206 | registered: getRegisteredPlugins() | 206 | registered: ServerConfigManager.Instance.getRegisteredPlugins() |
207 | }, | 207 | }, |
208 | theme: { | 208 | theme: { |
209 | registered: getRegisteredThemes(), | 209 | registered: ServerConfigManager.Instance.getRegisteredThemes(), |
210 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | 210 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) |
211 | }, | 211 | }, |
212 | email: { | 212 | email: { |
@@ -222,13 +222,13 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { | |||
222 | webtorrent: { | 222 | webtorrent: { |
223 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | 223 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED |
224 | }, | 224 | }, |
225 | enabledResolutions: getEnabledResolutions('vod') | 225 | enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('vod') |
226 | }, | 226 | }, |
227 | live: { | 227 | live: { |
228 | enabled: CONFIG.LIVE.ENABLED, | 228 | enabled: CONFIG.LIVE.ENABLED, |
229 | transcoding: { | 229 | transcoding: { |
230 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | 230 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, |
231 | enabledResolutions: getEnabledResolutions('live') | 231 | enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('live') |
232 | } | 232 | } |
233 | }, | 233 | }, |
234 | import: { | 234 | import: { |
diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts index 2126bb752..41e57d857 100644 --- a/server/helpers/markdown.ts +++ b/server/helpers/markdown.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' | 1 | import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' |
2 | |||
3 | const sanitizeOptions = getSanitizeOptions() | ||
2 | 4 | ||
3 | const sanitizeHtml = require('sanitize-html') | 5 | const sanitizeHtml = require('sanitize-html') |
4 | const markdownItEmoji = require('markdown-it-emoji/light') | 6 | const markdownItEmoji = require('markdown-it-emoji/light') |
@@ -18,7 +20,7 @@ const toSafeHtml = text => { | |||
18 | const html = markdownIt.render(textWithLineFeed) | 20 | const html = markdownIt.render(textWithLineFeed) |
19 | 21 | ||
20 | // Convert to safe Html | 22 | // Convert to safe Html |
21 | return sanitizeHtml(html, SANITIZE_OPTIONS) | 23 | return sanitizeHtml(html, sanitizeOptions) |
22 | } | 24 | } |
23 | 25 | ||
24 | const mdToPlainText = text => { | 26 | const mdToPlainText = text => { |
@@ -28,7 +30,7 @@ const mdToPlainText = text => { | |||
28 | const html = markdownIt.render(text) | 30 | const html = markdownIt.render(text) |
29 | 31 | ||
30 | // Convert to safe Html | 32 | // Convert to safe Html |
31 | const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS) | 33 | const safeHtml = sanitizeHtml(html, sanitizeOptions) |
32 | 34 | ||
33 | return safeHtml.replace(/<[^>]+>/g, '') | 35 | return safeHtml.replace(/<[^>]+>/g, '') |
34 | .replace(/\n$/, '') | 36 | .replace(/\n$/, '') |
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 4cf7dcf0a..919f9ea6e 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts | |||
@@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' | |||
24 | 24 | ||
25 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
26 | 26 | ||
27 | const LAST_MIGRATION_VERSION = 645 | 27 | const LAST_MIGRATION_VERSION = 650 |
28 | 28 | ||
29 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
30 | 30 | ||
diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 75a13ec8b..38e7a76d0 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts | |||
@@ -44,6 +44,7 @@ import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-pla | |||
44 | import { VideoTagModel } from '../models/video/video-tag' | 44 | import { VideoTagModel } from '../models/video/video-tag' |
45 | import { VideoViewModel } from '../models/video/video-view' | 45 | import { VideoViewModel } from '../models/video/video-view' |
46 | import { CONFIG } from './config' | 46 | import { CONFIG } from './config' |
47 | import { ActorCustomPageModel } from '@server/models/account/actor-custom-page' | ||
47 | 48 | ||
48 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string | 49 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string |
49 | 50 | ||
@@ -141,7 +142,8 @@ async function initDatabaseModels (silent: boolean) { | |||
141 | ThumbnailModel, | 142 | ThumbnailModel, |
142 | TrackerModel, | 143 | TrackerModel, |
143 | VideoTrackerModel, | 144 | VideoTrackerModel, |
144 | PluginModel | 145 | PluginModel, |
146 | ActorCustomPageModel | ||
145 | ]) | 147 | ]) |
146 | 148 | ||
147 | // Check extensions exist in the database | 149 | // Check extensions exist in the database |
diff --git a/server/initializers/migrations/0650-actor-custom-pages.ts b/server/initializers/migrations/0650-actor-custom-pages.ts new file mode 100644 index 000000000..1338327e8 --- /dev/null +++ b/server/initializers/migrations/0650-actor-custom-pages.ts | |||
@@ -0,0 +1,33 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | async function up (utils: { | ||
4 | transaction: Sequelize.Transaction | ||
5 | queryInterface: Sequelize.QueryInterface | ||
6 | sequelize: Sequelize.Sequelize | ||
7 | db: any | ||
8 | }): Promise<void> { | ||
9 | { | ||
10 | const query = ` | ||
11 | CREATE TABLE IF NOT EXISTS "actorCustomPage" ( | ||
12 | "id" serial, | ||
13 | "content" TEXT, | ||
14 | "type" varchar(255) NOT NULL, | ||
15 | "actorId" integer NOT NULL REFERENCES "actor" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
16 | "createdAt" timestamp WITH time zone NOT NULL, | ||
17 | "updatedAt" timestamp WITH time zone NOT NULL, | ||
18 | PRIMARY KEY ("id") | ||
19 | ); | ||
20 | ` | ||
21 | |||
22 | await utils.sequelize.query(query) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | function down (options) { | ||
27 | throw new Error('Not implemented.') | ||
28 | } | ||
29 | |||
30 | export { | ||
31 | up, | ||
32 | down | ||
33 | } | ||
diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts index 85fdc8754..4b2968e8b 100644 --- a/server/lib/client-html.ts +++ b/server/lib/client-html.ts | |||
@@ -26,7 +26,7 @@ import { VideoChannelModel } from '../models/video/video-channel' | |||
26 | import { getActivityStreamDuration } from '../models/video/video-format-utils' | 26 | import { getActivityStreamDuration } from '../models/video/video-format-utils' |
27 | import { VideoPlaylistModel } from '../models/video/video-playlist' | 27 | import { VideoPlaylistModel } from '../models/video/video-playlist' |
28 | import { MAccountActor, MChannelActor } from '../types/models' | 28 | import { MAccountActor, MChannelActor } from '../types/models' |
29 | import { getHTMLServerConfig } from './config' | 29 | import { ServerConfigManager } from './server-config-manager' |
30 | 30 | ||
31 | type Tags = { | 31 | type Tags = { |
32 | ogType: string | 32 | ogType: string |
@@ -211,7 +211,7 @@ class ClientHtml { | |||
211 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | 211 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
212 | 212 | ||
213 | const buffer = await readFile(path) | 213 | const buffer = await readFile(path) |
214 | const serverConfig = await getHTMLServerConfig() | 214 | const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() |
215 | 215 | ||
216 | let html = buffer.toString() | 216 | let html = buffer.toString() |
217 | html = await ClientHtml.addAsyncPluginCSS(html) | 217 | html = await ClientHtml.addAsyncPluginCSS(html) |
@@ -280,7 +280,7 @@ class ClientHtml { | |||
280 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | 280 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
281 | 281 | ||
282 | const buffer = await readFile(path) | 282 | const buffer = await readFile(path) |
283 | const serverConfig = await getHTMLServerConfig() | 283 | const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() |
284 | 284 | ||
285 | let html = buffer.toString() | 285 | let html = buffer.toString() |
286 | 286 | ||
diff --git a/server/lib/config.ts b/server/lib/config.ts deleted file mode 100644 index 18d49f05a..000000000 --- a/server/lib/config.ts +++ /dev/null | |||
@@ -1,274 +0,0 @@ | |||
1 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '@server/helpers/signup' | ||
2 | import { getServerCommit } from '@server/helpers/utils' | ||
3 | import { CONFIG, isEmailEnabled } from '@server/initializers/config' | ||
4 | import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '@server/initializers/constants' | ||
5 | import { HTMLServerConfig, RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig } from '@shared/models' | ||
6 | import { Hooks } from './plugins/hooks' | ||
7 | import { PluginManager } from './plugins/plugin-manager' | ||
8 | import { getThemeOrDefault } from './plugins/theme-utils' | ||
9 | import { VideoTranscodingProfilesManager } from './transcoding/video-transcoding-profiles' | ||
10 | |||
11 | async function getServerConfig (ip?: string): Promise<ServerConfig> { | ||
12 | const { allowed } = await Hooks.wrapPromiseFun( | ||
13 | isSignupAllowed, | ||
14 | { | ||
15 | ip | ||
16 | }, | ||
17 | 'filter:api.user.signup.allowed.result' | ||
18 | ) | ||
19 | |||
20 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(ip) | ||
21 | |||
22 | const signup = { | ||
23 | allowed, | ||
24 | allowedForCurrentIP, | ||
25 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | ||
26 | } | ||
27 | |||
28 | const htmlConfig = await getHTMLServerConfig() | ||
29 | |||
30 | return { ...htmlConfig, signup } | ||
31 | } | ||
32 | |||
33 | // Config injected in HTML | ||
34 | let serverCommit: string | ||
35 | async function getHTMLServerConfig (): Promise<HTMLServerConfig> { | ||
36 | if (serverCommit === undefined) serverCommit = await getServerCommit() | ||
37 | |||
38 | const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | ||
39 | |||
40 | return { | ||
41 | instance: { | ||
42 | name: CONFIG.INSTANCE.NAME, | ||
43 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | ||
44 | isNSFW: CONFIG.INSTANCE.IS_NSFW, | ||
45 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
46 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | ||
47 | customizations: { | ||
48 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | ||
49 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | ||
50 | } | ||
51 | }, | ||
52 | search: { | ||
53 | remoteUri: { | ||
54 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | ||
55 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | ||
56 | }, | ||
57 | searchIndex: { | ||
58 | enabled: CONFIG.SEARCH.SEARCH_INDEX.ENABLED, | ||
59 | url: CONFIG.SEARCH.SEARCH_INDEX.URL, | ||
60 | disableLocalSearch: CONFIG.SEARCH.SEARCH_INDEX.DISABLE_LOCAL_SEARCH, | ||
61 | isDefaultSearch: CONFIG.SEARCH.SEARCH_INDEX.IS_DEFAULT_SEARCH | ||
62 | } | ||
63 | }, | ||
64 | plugin: { | ||
65 | registered: getRegisteredPlugins(), | ||
66 | registeredExternalAuths: getExternalAuthsPlugins(), | ||
67 | registeredIdAndPassAuths: getIdAndPassAuthPlugins() | ||
68 | }, | ||
69 | theme: { | ||
70 | registered: getRegisteredThemes(), | ||
71 | default: defaultTheme | ||
72 | }, | ||
73 | email: { | ||
74 | enabled: isEmailEnabled() | ||
75 | }, | ||
76 | contactForm: { | ||
77 | enabled: CONFIG.CONTACT_FORM.ENABLED | ||
78 | }, | ||
79 | serverVersion: PEERTUBE_VERSION, | ||
80 | serverCommit, | ||
81 | transcoding: { | ||
82 | hls: { | ||
83 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | ||
84 | }, | ||
85 | webtorrent: { | ||
86 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | ||
87 | }, | ||
88 | enabledResolutions: getEnabledResolutions('vod'), | ||
89 | profile: CONFIG.TRANSCODING.PROFILE, | ||
90 | availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('vod') | ||
91 | }, | ||
92 | live: { | ||
93 | enabled: CONFIG.LIVE.ENABLED, | ||
94 | |||
95 | allowReplay: CONFIG.LIVE.ALLOW_REPLAY, | ||
96 | maxDuration: CONFIG.LIVE.MAX_DURATION, | ||
97 | maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES, | ||
98 | maxUserLives: CONFIG.LIVE.MAX_USER_LIVES, | ||
99 | |||
100 | transcoding: { | ||
101 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | ||
102 | enabledResolutions: getEnabledResolutions('live'), | ||
103 | profile: CONFIG.LIVE.TRANSCODING.PROFILE, | ||
104 | availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('live') | ||
105 | }, | ||
106 | |||
107 | rtmp: { | ||
108 | port: CONFIG.LIVE.RTMP.PORT | ||
109 | } | ||
110 | }, | ||
111 | import: { | ||
112 | videos: { | ||
113 | http: { | ||
114 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | ||
115 | }, | ||
116 | torrent: { | ||
117 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | ||
118 | } | ||
119 | } | ||
120 | }, | ||
121 | autoBlacklist: { | ||
122 | videos: { | ||
123 | ofUsers: { | ||
124 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | ||
125 | } | ||
126 | } | ||
127 | }, | ||
128 | avatar: { | ||
129 | file: { | ||
130 | size: { | ||
131 | max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max | ||
132 | }, | ||
133 | extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME | ||
134 | } | ||
135 | }, | ||
136 | banner: { | ||
137 | file: { | ||
138 | size: { | ||
139 | max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max | ||
140 | }, | ||
141 | extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME | ||
142 | } | ||
143 | }, | ||
144 | video: { | ||
145 | image: { | ||
146 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | ||
147 | size: { | ||
148 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | ||
149 | } | ||
150 | }, | ||
151 | file: { | ||
152 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | ||
153 | } | ||
154 | }, | ||
155 | videoCaption: { | ||
156 | file: { | ||
157 | size: { | ||
158 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | ||
159 | }, | ||
160 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | ||
161 | } | ||
162 | }, | ||
163 | user: { | ||
164 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | ||
165 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | ||
166 | }, | ||
167 | trending: { | ||
168 | videos: { | ||
169 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS, | ||
170 | algorithms: { | ||
171 | enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED, | ||
172 | default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT | ||
173 | } | ||
174 | } | ||
175 | }, | ||
176 | tracker: { | ||
177 | enabled: CONFIG.TRACKER.ENABLED | ||
178 | }, | ||
179 | |||
180 | followings: { | ||
181 | instance: { | ||
182 | autoFollowIndex: { | ||
183 | indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL | ||
184 | } | ||
185 | } | ||
186 | }, | ||
187 | |||
188 | broadcastMessage: { | ||
189 | enabled: CONFIG.BROADCAST_MESSAGE.ENABLED, | ||
190 | message: CONFIG.BROADCAST_MESSAGE.MESSAGE, | ||
191 | level: CONFIG.BROADCAST_MESSAGE.LEVEL, | ||
192 | dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | |||
197 | function getRegisteredThemes () { | ||
198 | return PluginManager.Instance.getRegisteredThemes() | ||
199 | .map(t => ({ | ||
200 | name: t.name, | ||
201 | version: t.version, | ||
202 | description: t.description, | ||
203 | css: t.css, | ||
204 | clientScripts: t.clientScripts | ||
205 | })) | ||
206 | } | ||
207 | |||
208 | function getRegisteredPlugins () { | ||
209 | return PluginManager.Instance.getRegisteredPlugins() | ||
210 | .map(p => ({ | ||
211 | name: p.name, | ||
212 | version: p.version, | ||
213 | description: p.description, | ||
214 | clientScripts: p.clientScripts | ||
215 | })) | ||
216 | } | ||
217 | |||
218 | function getEnabledResolutions (type: 'vod' | 'live') { | ||
219 | const transcoding = type === 'vod' | ||
220 | ? CONFIG.TRANSCODING | ||
221 | : CONFIG.LIVE.TRANSCODING | ||
222 | |||
223 | return Object.keys(transcoding.RESOLUTIONS) | ||
224 | .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true) | ||
225 | .map(r => parseInt(r, 10)) | ||
226 | } | ||
227 | |||
228 | // --------------------------------------------------------------------------- | ||
229 | |||
230 | export { | ||
231 | getServerConfig, | ||
232 | getRegisteredThemes, | ||
233 | getEnabledResolutions, | ||
234 | getRegisteredPlugins, | ||
235 | getHTMLServerConfig | ||
236 | } | ||
237 | |||
238 | // --------------------------------------------------------------------------- | ||
239 | |||
240 | function getIdAndPassAuthPlugins () { | ||
241 | const result: RegisteredIdAndPassAuthConfig[] = [] | ||
242 | |||
243 | for (const p of PluginManager.Instance.getIdAndPassAuths()) { | ||
244 | for (const auth of p.idAndPassAuths) { | ||
245 | result.push({ | ||
246 | npmName: p.npmName, | ||
247 | name: p.name, | ||
248 | version: p.version, | ||
249 | authName: auth.authName, | ||
250 | weight: auth.getWeight() | ||
251 | }) | ||
252 | } | ||
253 | } | ||
254 | |||
255 | return result | ||
256 | } | ||
257 | |||
258 | function getExternalAuthsPlugins () { | ||
259 | const result: RegisteredExternalAuthConfig[] = [] | ||
260 | |||
261 | for (const p of PluginManager.Instance.getExternalAuths()) { | ||
262 | for (const auth of p.externalAuths) { | ||
263 | result.push({ | ||
264 | npmName: p.npmName, | ||
265 | name: p.name, | ||
266 | version: p.version, | ||
267 | authName: auth.authName, | ||
268 | authDisplayName: auth.authDisplayName() | ||
269 | }) | ||
270 | } | ||
271 | } | ||
272 | |||
273 | return result | ||
274 | } | ||
diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index 3067ce214..d71053e87 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts | |||
@@ -2,8 +2,10 @@ import * as Bull from 'bull' | |||
2 | import { move, remove, stat } from 'fs-extra' | 2 | import { move, remove, stat } from 'fs-extra' |
3 | import { extname } from 'path' | 3 | import { extname } from 'path' |
4 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | 4 | import { retryTransactionWrapper } from '@server/helpers/database-utils' |
5 | import { YoutubeDL } from '@server/helpers/youtube-dl' | ||
5 | import { isPostImportVideoAccepted } from '@server/lib/moderation' | 6 | import { isPostImportVideoAccepted } from '@server/lib/moderation' |
6 | import { Hooks } from '@server/lib/plugins/hooks' | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
8 | import { ServerConfigManager } from '@server/lib/server-config-manager' | ||
7 | import { isAbleToUploadVideo } from '@server/lib/user' | 9 | import { isAbleToUploadVideo } from '@server/lib/user' |
8 | import { addOptimizeOrMergeAudioJob } from '@server/lib/video' | 10 | import { addOptimizeOrMergeAudioJob } from '@server/lib/video' |
9 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' | 11 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' |
@@ -33,8 +35,6 @@ import { MThumbnail } from '../../../types/models/video/thumbnail' | |||
33 | import { federateVideoIfNeeded } from '../../activitypub/videos' | 35 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
34 | import { Notifier } from '../../notifier' | 36 | import { Notifier } from '../../notifier' |
35 | import { generateVideoMiniature } from '../../thumbnail' | 37 | import { generateVideoMiniature } from '../../thumbnail' |
36 | import { YoutubeDL } from '@server/helpers/youtube-dl' | ||
37 | import { getEnabledResolutions } from '@server/lib/config' | ||
38 | 38 | ||
39 | async function processVideoImport (job: Bull.Job) { | 39 | async function processVideoImport (job: Bull.Job) { |
40 | const payload = job.data as VideoImportPayload | 40 | const payload = job.data as VideoImportPayload |
@@ -76,7 +76,7 @@ async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutub | |||
76 | videoImportId: videoImport.id | 76 | videoImportId: videoImport.id |
77 | } | 77 | } |
78 | 78 | ||
79 | const youtubeDL = new YoutubeDL(videoImport.targetUrl, getEnabledResolutions('vod')) | 79 | const youtubeDL = new YoutubeDL(videoImport.targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod')) |
80 | 80 | ||
81 | return processFile( | 81 | return processFile( |
82 | () => youtubeDL.downloadYoutubeDLVideo(payload.fileExt, VIDEO_IMPORT_TIMEOUT), | 82 | () => youtubeDL.downloadYoutubeDLVideo(payload.fileExt, VIDEO_IMPORT_TIMEOUT), |
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts index cb1cd4d9a..8487672ba 100644 --- a/server/lib/plugins/plugin-helpers-builder.ts +++ b/server/lib/plugins/plugin-helpers-builder.ts | |||
@@ -15,7 +15,7 @@ import { MPlugin } from '@server/types/models' | |||
15 | import { PeerTubeHelpers } from '@server/types/plugins' | 15 | import { PeerTubeHelpers } from '@server/types/plugins' |
16 | import { VideoBlacklistCreate } from '@shared/models' | 16 | import { VideoBlacklistCreate } from '@shared/models' |
17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' | 17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' |
18 | import { getServerConfig } from '../config' | 18 | import { ServerConfigManager } from '../server-config-manager' |
19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' | 19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' |
20 | import { UserModel } from '@server/models/user/user' | 20 | import { UserModel } from '@server/models/user/user' |
21 | 21 | ||
@@ -147,7 +147,7 @@ function buildConfigHelpers () { | |||
147 | }, | 147 | }, |
148 | 148 | ||
149 | getServerConfig () { | 149 | getServerConfig () { |
150 | return getServerConfig() | 150 | return ServerConfigManager.Instance.getServerConfig() |
151 | } | 151 | } |
152 | } | 152 | } |
153 | } | 153 | } |
diff --git a/server/lib/server-config-manager.ts b/server/lib/server-config-manager.ts new file mode 100644 index 000000000..1aff6f446 --- /dev/null +++ b/server/lib/server-config-manager.ts | |||
@@ -0,0 +1,303 @@ | |||
1 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '@server/helpers/signup' | ||
2 | import { getServerCommit } from '@server/helpers/utils' | ||
3 | import { CONFIG, isEmailEnabled } from '@server/initializers/config' | ||
4 | import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '@server/initializers/constants' | ||
5 | import { ActorCustomPageModel } from '@server/models/account/actor-custom-page' | ||
6 | import { HTMLServerConfig, RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig } from '@shared/models' | ||
7 | import { Hooks } from './plugins/hooks' | ||
8 | import { PluginManager } from './plugins/plugin-manager' | ||
9 | import { getThemeOrDefault } from './plugins/theme-utils' | ||
10 | import { VideoTranscodingProfilesManager } from './transcoding/video-transcoding-profiles' | ||
11 | |||
12 | /** | ||
13 | * | ||
14 | * Used to send the server config to clients (using REST/API or plugins API) | ||
15 | * We need a singleton class to manage config state depending on external events (to build menu entries etc) | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | class ServerConfigManager { | ||
20 | |||
21 | private static instance: ServerConfigManager | ||
22 | |||
23 | private serverCommit: string | ||
24 | |||
25 | private homepageEnabled = false | ||
26 | |||
27 | private constructor () {} | ||
28 | |||
29 | async init () { | ||
30 | const instanceHomepage = await ActorCustomPageModel.loadInstanceHomepage() | ||
31 | |||
32 | this.updateHomepageState(instanceHomepage?.content) | ||
33 | } | ||
34 | |||
35 | updateHomepageState (content: string) { | ||
36 | this.homepageEnabled = !!content | ||
37 | } | ||
38 | |||
39 | async getHTMLServerConfig (): Promise<HTMLServerConfig> { | ||
40 | if (this.serverCommit === undefined) this.serverCommit = await getServerCommit() | ||
41 | |||
42 | const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | ||
43 | |||
44 | return { | ||
45 | instance: { | ||
46 | name: CONFIG.INSTANCE.NAME, | ||
47 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | ||
48 | isNSFW: CONFIG.INSTANCE.IS_NSFW, | ||
49 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
50 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | ||
51 | customizations: { | ||
52 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | ||
53 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | ||
54 | } | ||
55 | }, | ||
56 | search: { | ||
57 | remoteUri: { | ||
58 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | ||
59 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | ||
60 | }, | ||
61 | searchIndex: { | ||
62 | enabled: CONFIG.SEARCH.SEARCH_INDEX.ENABLED, | ||
63 | url: CONFIG.SEARCH.SEARCH_INDEX.URL, | ||
64 | disableLocalSearch: CONFIG.SEARCH.SEARCH_INDEX.DISABLE_LOCAL_SEARCH, | ||
65 | isDefaultSearch: CONFIG.SEARCH.SEARCH_INDEX.IS_DEFAULT_SEARCH | ||
66 | } | ||
67 | }, | ||
68 | plugin: { | ||
69 | registered: this.getRegisteredPlugins(), | ||
70 | registeredExternalAuths: this.getExternalAuthsPlugins(), | ||
71 | registeredIdAndPassAuths: this.getIdAndPassAuthPlugins() | ||
72 | }, | ||
73 | theme: { | ||
74 | registered: this.getRegisteredThemes(), | ||
75 | default: defaultTheme | ||
76 | }, | ||
77 | email: { | ||
78 | enabled: isEmailEnabled() | ||
79 | }, | ||
80 | contactForm: { | ||
81 | enabled: CONFIG.CONTACT_FORM.ENABLED | ||
82 | }, | ||
83 | serverVersion: PEERTUBE_VERSION, | ||
84 | serverCommit: this.serverCommit, | ||
85 | transcoding: { | ||
86 | hls: { | ||
87 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | ||
88 | }, | ||
89 | webtorrent: { | ||
90 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | ||
91 | }, | ||
92 | enabledResolutions: this.getEnabledResolutions('vod'), | ||
93 | profile: CONFIG.TRANSCODING.PROFILE, | ||
94 | availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('vod') | ||
95 | }, | ||
96 | live: { | ||
97 | enabled: CONFIG.LIVE.ENABLED, | ||
98 | |||
99 | allowReplay: CONFIG.LIVE.ALLOW_REPLAY, | ||
100 | maxDuration: CONFIG.LIVE.MAX_DURATION, | ||
101 | maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES, | ||
102 | maxUserLives: CONFIG.LIVE.MAX_USER_LIVES, | ||
103 | |||
104 | transcoding: { | ||
105 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | ||
106 | enabledResolutions: this.getEnabledResolutions('live'), | ||
107 | profile: CONFIG.LIVE.TRANSCODING.PROFILE, | ||
108 | availableProfiles: VideoTranscodingProfilesManager.Instance.getAvailableProfiles('live') | ||
109 | }, | ||
110 | |||
111 | rtmp: { | ||
112 | port: CONFIG.LIVE.RTMP.PORT | ||
113 | } | ||
114 | }, | ||
115 | import: { | ||
116 | videos: { | ||
117 | http: { | ||
118 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | ||
119 | }, | ||
120 | torrent: { | ||
121 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | ||
122 | } | ||
123 | } | ||
124 | }, | ||
125 | autoBlacklist: { | ||
126 | videos: { | ||
127 | ofUsers: { | ||
128 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | ||
129 | } | ||
130 | } | ||
131 | }, | ||
132 | avatar: { | ||
133 | file: { | ||
134 | size: { | ||
135 | max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max | ||
136 | }, | ||
137 | extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME | ||
138 | } | ||
139 | }, | ||
140 | banner: { | ||
141 | file: { | ||
142 | size: { | ||
143 | max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max | ||
144 | }, | ||
145 | extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME | ||
146 | } | ||
147 | }, | ||
148 | video: { | ||
149 | image: { | ||
150 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | ||
151 | size: { | ||
152 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | ||
153 | } | ||
154 | }, | ||
155 | file: { | ||
156 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | ||
157 | } | ||
158 | }, | ||
159 | videoCaption: { | ||
160 | file: { | ||
161 | size: { | ||
162 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | ||
163 | }, | ||
164 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | ||
165 | } | ||
166 | }, | ||
167 | user: { | ||
168 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | ||
169 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | ||
170 | }, | ||
171 | trending: { | ||
172 | videos: { | ||
173 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS, | ||
174 | algorithms: { | ||
175 | enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED, | ||
176 | default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT | ||
177 | } | ||
178 | } | ||
179 | }, | ||
180 | tracker: { | ||
181 | enabled: CONFIG.TRACKER.ENABLED | ||
182 | }, | ||
183 | |||
184 | followings: { | ||
185 | instance: { | ||
186 | autoFollowIndex: { | ||
187 | indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL | ||
188 | } | ||
189 | } | ||
190 | }, | ||
191 | |||
192 | broadcastMessage: { | ||
193 | enabled: CONFIG.BROADCAST_MESSAGE.ENABLED, | ||
194 | message: CONFIG.BROADCAST_MESSAGE.MESSAGE, | ||
195 | level: CONFIG.BROADCAST_MESSAGE.LEVEL, | ||
196 | dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE | ||
197 | }, | ||
198 | |||
199 | homepage: { | ||
200 | enabled: this.homepageEnabled | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | async getServerConfig (ip?: string): Promise<ServerConfig> { | ||
206 | const { allowed } = await Hooks.wrapPromiseFun( | ||
207 | isSignupAllowed, | ||
208 | { | ||
209 | ip | ||
210 | }, | ||
211 | 'filter:api.user.signup.allowed.result' | ||
212 | ) | ||
213 | |||
214 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(ip) | ||
215 | |||
216 | const signup = { | ||
217 | allowed, | ||
218 | allowedForCurrentIP, | ||
219 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | ||
220 | } | ||
221 | |||
222 | const htmlConfig = await this.getHTMLServerConfig() | ||
223 | |||
224 | return { ...htmlConfig, signup } | ||
225 | } | ||
226 | |||
227 | getRegisteredThemes () { | ||
228 | return PluginManager.Instance.getRegisteredThemes() | ||
229 | .map(t => ({ | ||
230 | name: t.name, | ||
231 | version: t.version, | ||
232 | description: t.description, | ||
233 | css: t.css, | ||
234 | clientScripts: t.clientScripts | ||
235 | })) | ||
236 | } | ||
237 | |||
238 | getRegisteredPlugins () { | ||
239 | return PluginManager.Instance.getRegisteredPlugins() | ||
240 | .map(p => ({ | ||
241 | name: p.name, | ||
242 | version: p.version, | ||
243 | description: p.description, | ||
244 | clientScripts: p.clientScripts | ||
245 | })) | ||
246 | } | ||
247 | |||
248 | getEnabledResolutions (type: 'vod' | 'live') { | ||
249 | const transcoding = type === 'vod' | ||
250 | ? CONFIG.TRANSCODING | ||
251 | : CONFIG.LIVE.TRANSCODING | ||
252 | |||
253 | return Object.keys(transcoding.RESOLUTIONS) | ||
254 | .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true) | ||
255 | .map(r => parseInt(r, 10)) | ||
256 | } | ||
257 | |||
258 | private getIdAndPassAuthPlugins () { | ||
259 | const result: RegisteredIdAndPassAuthConfig[] = [] | ||
260 | |||
261 | for (const p of PluginManager.Instance.getIdAndPassAuths()) { | ||
262 | for (const auth of p.idAndPassAuths) { | ||
263 | result.push({ | ||
264 | npmName: p.npmName, | ||
265 | name: p.name, | ||
266 | version: p.version, | ||
267 | authName: auth.authName, | ||
268 | weight: auth.getWeight() | ||
269 | }) | ||
270 | } | ||
271 | } | ||
272 | |||
273 | return result | ||
274 | } | ||
275 | |||
276 | private getExternalAuthsPlugins () { | ||
277 | const result: RegisteredExternalAuthConfig[] = [] | ||
278 | |||
279 | for (const p of PluginManager.Instance.getExternalAuths()) { | ||
280 | for (const auth of p.externalAuths) { | ||
281 | result.push({ | ||
282 | npmName: p.npmName, | ||
283 | name: p.name, | ||
284 | version: p.version, | ||
285 | authName: auth.authName, | ||
286 | authDisplayName: auth.authDisplayName() | ||
287 | }) | ||
288 | } | ||
289 | } | ||
290 | |||
291 | return result | ||
292 | } | ||
293 | |||
294 | static get Instance () { | ||
295 | return this.instance || (this.instance = new this()) | ||
296 | } | ||
297 | } | ||
298 | |||
299 | // --------------------------------------------------------------------------- | ||
300 | |||
301 | export { | ||
302 | ServerConfigManager | ||
303 | } | ||
diff --git a/server/models/account/actor-custom-page.ts b/server/models/account/actor-custom-page.ts new file mode 100644 index 000000000..893023181 --- /dev/null +++ b/server/models/account/actor-custom-page.ts | |||
@@ -0,0 +1,69 @@ | |||
1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { CustomPage } from '@shared/models' | ||
3 | import { ActorModel } from '../actor/actor' | ||
4 | import { getServerActor } from '../application/application' | ||
5 | |||
6 | @Table({ | ||
7 | tableName: 'actorCustomPage', | ||
8 | indexes: [ | ||
9 | { | ||
10 | fields: [ 'actorId', 'type' ], | ||
11 | unique: true | ||
12 | } | ||
13 | ] | ||
14 | }) | ||
15 | export class ActorCustomPageModel extends Model { | ||
16 | |||
17 | @AllowNull(true) | ||
18 | @Column(DataType.TEXT) | ||
19 | content: string | ||
20 | |||
21 | @AllowNull(false) | ||
22 | @Column | ||
23 | type: 'homepage' | ||
24 | |||
25 | @CreatedAt | ||
26 | createdAt: Date | ||
27 | |||
28 | @UpdatedAt | ||
29 | updatedAt: Date | ||
30 | |||
31 | @ForeignKey(() => ActorModel) | ||
32 | @Column | ||
33 | actorId: number | ||
34 | |||
35 | @BelongsTo(() => ActorModel, { | ||
36 | foreignKey: { | ||
37 | name: 'actorId', | ||
38 | allowNull: false | ||
39 | }, | ||
40 | onDelete: 'cascade' | ||
41 | }) | ||
42 | Actor: ActorModel | ||
43 | |||
44 | static async updateInstanceHomepage (content: string) { | ||
45 | const serverActor = await getServerActor() | ||
46 | |||
47 | return ActorCustomPageModel.upsert({ | ||
48 | content, | ||
49 | actorId: serverActor.id, | ||
50 | type: 'homepage' | ||
51 | }) | ||
52 | } | ||
53 | |||
54 | static async loadInstanceHomepage () { | ||
55 | const serverActor = await getServerActor() | ||
56 | |||
57 | return ActorCustomPageModel.findOne({ | ||
58 | where: { | ||
59 | actorId: serverActor.id | ||
60 | } | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | toFormattedJSON (): CustomPage { | ||
65 | return { | ||
66 | content: this.content | ||
67 | } | ||
68 | } | ||
69 | } | ||
diff --git a/server/tests/api/check-params/custom-pages.ts b/server/tests/api/check-params/custom-pages.ts new file mode 100644 index 000000000..74ca3384c --- /dev/null +++ b/server/tests/api/check-params/custom-pages.ts | |||
@@ -0,0 +1,81 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import 'mocha' | ||
4 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
5 | import { | ||
6 | cleanupTests, | ||
7 | createUser, | ||
8 | flushAndRunServer, | ||
9 | ServerInfo, | ||
10 | setAccessTokensToServers, | ||
11 | userLogin | ||
12 | } from '../../../../shared/extra-utils' | ||
13 | import { makeGetRequest, makePutBodyRequest } from '../../../../shared/extra-utils/requests/requests' | ||
14 | |||
15 | describe('Test custom pages validators', function () { | ||
16 | const path = '/api/v1/custom-pages/homepage/instance' | ||
17 | |||
18 | let server: ServerInfo | ||
19 | let userAccessToken: string | ||
20 | |||
21 | // --------------------------------------------------------------- | ||
22 | |||
23 | before(async function () { | ||
24 | this.timeout(120000) | ||
25 | |||
26 | server = await flushAndRunServer(1) | ||
27 | await setAccessTokensToServers([ server ]) | ||
28 | |||
29 | const user = { username: 'user1', password: 'password' } | ||
30 | await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) | ||
31 | |||
32 | userAccessToken = await userLogin(server, user) | ||
33 | }) | ||
34 | |||
35 | describe('When updating instance homepage', function () { | ||
36 | |||
37 | it('Should fail with an unauthenticated user', async function () { | ||
38 | await makePutBodyRequest({ | ||
39 | url: server.url, | ||
40 | path, | ||
41 | fields: { content: 'super content' }, | ||
42 | statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 | ||
43 | }) | ||
44 | }) | ||
45 | |||
46 | it('Should fail with a non admin user', async function () { | ||
47 | await makePutBodyRequest({ | ||
48 | url: server.url, | ||
49 | path, | ||
50 | token: userAccessToken, | ||
51 | fields: { content: 'super content' }, | ||
52 | statusCodeExpected: HttpStatusCode.FORBIDDEN_403 | ||
53 | }) | ||
54 | }) | ||
55 | |||
56 | it('Should succeed with the correct params', async function () { | ||
57 | await makePutBodyRequest({ | ||
58 | url: server.url, | ||
59 | path, | ||
60 | token: server.accessToken, | ||
61 | fields: { content: 'super content' }, | ||
62 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | ||
63 | }) | ||
64 | }) | ||
65 | }) | ||
66 | |||
67 | describe('When getting instance homapage', function () { | ||
68 | |||
69 | it('Should succeed with the correct params', async function () { | ||
70 | await makeGetRequest({ | ||
71 | url: server.url, | ||
72 | path, | ||
73 | statusCodeExpected: HttpStatusCode.OK_200 | ||
74 | }) | ||
75 | }) | ||
76 | }) | ||
77 | |||
78 | after(async function () { | ||
79 | await cleanupTests([ server ]) | ||
80 | }) | ||
81 | }) | ||
diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 143515838..ce2335e42 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts | |||
@@ -3,6 +3,7 @@ import './accounts' | |||
3 | import './blocklist' | 3 | import './blocklist' |
4 | import './bulk' | 4 | import './bulk' |
5 | import './config' | 5 | import './config' |
6 | import './custom-pages' | ||
6 | import './contact-form' | 7 | import './contact-form' |
7 | import './debug' | 8 | import './debug' |
8 | import './follows' | 9 | import './follows' |
diff --git a/server/tests/api/server/homepage.ts b/server/tests/api/server/homepage.ts new file mode 100644 index 000000000..e8ba89ca6 --- /dev/null +++ b/server/tests/api/server/homepage.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import 'mocha' | ||
4 | import * as chai from 'chai' | ||
5 | import { HttpStatusCode } from '@shared/core-utils' | ||
6 | import { CustomPage, ServerConfig } from '@shared/models' | ||
7 | import { | ||
8 | cleanupTests, | ||
9 | flushAndRunServer, | ||
10 | getConfig, | ||
11 | getInstanceHomepage, | ||
12 | killallServers, | ||
13 | reRunServer, | ||
14 | ServerInfo, | ||
15 | setAccessTokensToServers, | ||
16 | updateInstanceHomepage | ||
17 | } from '../../../../shared/extra-utils/index' | ||
18 | |||
19 | const expect = chai.expect | ||
20 | |||
21 | async function getHomepageState (server: ServerInfo) { | ||
22 | const res = await getConfig(server.url) | ||
23 | |||
24 | const config = res.body as ServerConfig | ||
25 | return config.homepage.enabled | ||
26 | } | ||
27 | |||
28 | describe('Test instance homepage actions', function () { | ||
29 | let server: ServerInfo | ||
30 | |||
31 | before(async function () { | ||
32 | this.timeout(30000) | ||
33 | |||
34 | server = await flushAndRunServer(1) | ||
35 | await setAccessTokensToServers([ server ]) | ||
36 | }) | ||
37 | |||
38 | it('Should not have a homepage', async function () { | ||
39 | const state = await getHomepageState(server) | ||
40 | expect(state).to.be.false | ||
41 | |||
42 | await getInstanceHomepage(server.url, HttpStatusCode.NOT_FOUND_404) | ||
43 | }) | ||
44 | |||
45 | it('Should set a homepage', async function () { | ||
46 | await updateInstanceHomepage(server.url, server.accessToken, '<picsou-magazine></picsou-magazine>') | ||
47 | |||
48 | const res = await getInstanceHomepage(server.url) | ||
49 | const page: CustomPage = res.body | ||
50 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') | ||
51 | |||
52 | const state = await getHomepageState(server) | ||
53 | expect(state).to.be.true | ||
54 | }) | ||
55 | |||
56 | it('Should have the same homepage after a restart', async function () { | ||
57 | this.timeout(30000) | ||
58 | |||
59 | killallServers([ server ]) | ||
60 | |||
61 | await reRunServer(server) | ||
62 | |||
63 | const res = await getInstanceHomepage(server.url) | ||
64 | const page: CustomPage = res.body | ||
65 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') | ||
66 | |||
67 | const state = await getHomepageState(server) | ||
68 | expect(state).to.be.true | ||
69 | }) | ||
70 | |||
71 | it('Should empty the homepage', async function () { | ||
72 | await updateInstanceHomepage(server.url, server.accessToken, '') | ||
73 | |||
74 | const res = await getInstanceHomepage(server.url) | ||
75 | const page: CustomPage = res.body | ||
76 | expect(page.content).to.be.empty | ||
77 | |||
78 | const state = await getHomepageState(server) | ||
79 | expect(state).to.be.false | ||
80 | }) | ||
81 | |||
82 | after(async function () { | ||
83 | await cleanupTests([ server ]) | ||
84 | }) | ||
85 | }) | ||
diff --git a/server/tests/api/server/index.ts b/server/tests/api/server/index.ts index be743973a..56e6eb5da 100644 --- a/server/tests/api/server/index.ts +++ b/server/tests/api/server/index.ts | |||
@@ -5,6 +5,7 @@ import './email' | |||
5 | import './follow-constraints' | 5 | import './follow-constraints' |
6 | import './follows' | 6 | import './follows' |
7 | import './follows-moderation' | 7 | import './follows-moderation' |
8 | import './homepage' | ||
8 | import './handle-down' | 9 | import './handle-down' |
9 | import './jobs' | 10 | import './jobs' |
10 | import './logs' | 11 | import './logs' |
diff --git a/server/types/models/account/actor-custom-page.ts b/server/types/models/account/actor-custom-page.ts new file mode 100644 index 000000000..2cb8aa7e4 --- /dev/null +++ b/server/types/models/account/actor-custom-page.ts | |||
@@ -0,0 +1,4 @@ | |||
1 | |||
2 | import { ActorCustomPageModel } from '../../../models/account/actor-custom-page' | ||
3 | |||
4 | export type MActorCustomPage = Omit<ActorCustomPageModel, 'Actor'> | ||
diff --git a/server/types/models/account/index.ts b/server/types/models/account/index.ts index dab2eea7e..9679c01e4 100644 --- a/server/types/models/account/index.ts +++ b/server/types/models/account/index.ts | |||
@@ -1,2 +1,3 @@ | |||
1 | export * from './account' | 1 | export * from './account' |
2 | export * from './actor-custom-page' | ||
2 | export * from './account-blocklist' | 3 | export * from './account-blocklist' |