From 2539932e16129992a2c0889b4ff527c265a8e2c7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 27 May 2021 15:59:55 +0200 Subject: Instance homepage support (#4007) * Prepare homepage parsers * Add ability to update instance hompage * Add ability to set homepage as landing page * Add homepage preview in admin * Dynamically update left menu for homepage * Inject home content in homepage * Add videos list and channel miniature custom markup * Remove unused elements in markup service --- server/controllers/api/config.ts | 4 ++-- server/controllers/api/custom-page.ts | 42 +++++++++++++++++++++++++++++++++ server/controllers/api/index.ts | 2 ++ server/controllers/api/videos/import.ts | 4 ++-- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 server/controllers/api/custom-page.ts (limited to 'server/controllers/api') 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 @@ +import { ServerConfigManager } from '@server/lib/server-config-manager' import * as express from 'express' import { remove, writeJSON } from 'fs-extra' import { snakeCase } from 'lodash' import validator from 'validator' -import { getServerConfig } from '@server/lib/config' import { UserRight } from '../../../shared' import { About } from '../../../shared/models/server/about.model' import { CustomConfig } from '../../../shared/models/server/custom-config.model' @@ -43,7 +43,7 @@ configRouter.delete('/custom', ) async function getConfig (req: express.Request, res: express.Response) { - const json = await getServerConfig(req.ip) + const json = await ServerConfigManager.Instance.getServerConfig(req.ip) return res.json(json) } 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 @@ +import * as express from 'express' +import { ServerConfigManager } from '@server/lib/server-config-manager' +import { ActorCustomPageModel } from '@server/models/account/actor-custom-page' +import { HttpStatusCode } from '@shared/core-utils' +import { UserRight } from '@shared/models' +import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' + +const customPageRouter = express.Router() + +customPageRouter.get('/homepage/instance', + asyncMiddleware(getInstanceHomepage) +) + +customPageRouter.put('/homepage/instance', + authenticate, + ensureUserHasRight(UserRight.MANAGE_INSTANCE_CUSTOM_PAGE), + asyncMiddleware(updateInstanceHomepage) +) + +// --------------------------------------------------------------------------- + +export { + customPageRouter +} + +// --------------------------------------------------------------------------- + +async function getInstanceHomepage (req: express.Request, res: express.Response) { + const page = await ActorCustomPageModel.loadInstanceHomepage() + if (!page) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + + return res.json(page.toFormattedJSON()) +} + +async function updateInstanceHomepage (req: express.Request, res: express.Response) { + const content = req.body.content + + await ActorCustomPageModel.updateInstanceHomepage(content) + ServerConfigManager.Instance.updateHomepageState(content) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} 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' import { accountsRouter } from './accounts' import { bulkRouter } from './bulk' import { configRouter } from './config' +import { customPageRouter } from './custom-page' import { jobsRouter } from './jobs' import { oauthClientsRouter } from './oauth-clients' import { overviewsRouter } from './overviews' @@ -47,6 +48,7 @@ apiRouter.use('/jobs', jobsRouter) apiRouter.use('/search', searchRouter) apiRouter.use('/overviews', overviewsRouter) apiRouter.use('/plugins', pluginRouter) +apiRouter.use('/custom-pages', customPageRouter) apiRouter.use('/ping', pong) apiRouter.use('/*', badRequest) 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' import * as magnetUtil from 'magnet-uri' import * as parseTorrent from 'parse-torrent' import { join } from 'path' -import { getEnabledResolutions } from '@server/lib/config' +import { ServerConfigManager } from '@server/lib/server-config-manager' import { setVideoTags } from '@server/lib/video' import { FilteredModelAttributes } from '@server/types' import { @@ -134,7 +134,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) const targetUrl = body.targetUrl const user = res.locals.oauth.token.User - const youtubeDL = new YoutubeDL(targetUrl, getEnabledResolutions('vod')) + const youtubeDL = new YoutubeDL(targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod')) // Get video infos let youtubeDLInfo: YoutubeDLInfo -- cgit v1.2.3