]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/custom-page.ts
refactor API errors to standard error format
[github/Chocobozzz/PeerTube.git] / server / controllers / api / custom-page.ts
CommitLineData
2539932e
C
1import * as express from 'express'
2import { ServerConfigManager } from '@server/lib/server-config-manager'
3import { ActorCustomPageModel } from '@server/models/account/actor-custom-page'
4import { HttpStatusCode } from '@shared/core-utils'
5import { UserRight } from '@shared/models'
6import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
7
8const customPageRouter = express.Router()
9
10customPageRouter.get('/homepage/instance',
11 asyncMiddleware(getInstanceHomepage)
12)
13
14customPageRouter.put('/homepage/instance',
15 authenticate,
16 ensureUserHasRight(UserRight.MANAGE_INSTANCE_CUSTOM_PAGE),
17 asyncMiddleware(updateInstanceHomepage)
18)
19
20// ---------------------------------------------------------------------------
21
22export {
23 customPageRouter
24}
25
26// ---------------------------------------------------------------------------
27
28async function getInstanceHomepage (req: express.Request, res: express.Response) {
29 const page = await ActorCustomPageModel.loadInstanceHomepage()
76148b27
RK
30 if (!page) {
31 return res.fail({
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Instance homepage could not be found'
34 })
35 }
2539932e
C
36
37 return res.json(page.toFormattedJSON())
38}
39
40async function updateInstanceHomepage (req: express.Request, res: express.Response) {
41 const content = req.body.content
42
43 await ActorCustomPageModel.updateInstanceHomepage(content)
44 ServerConfigManager.Instance.updateHomepageState(content)
45
76148b27 46 return res.status(HttpStatusCode.NO_CONTENT_204).end()
2539932e 47}