]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/custom-page.ts
Fix retrying update on sql serialization conflict
[github/Chocobozzz/PeerTube.git] / server / controllers / api / custom-page.ts
1 import 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, UserRight } from '@shared/models'
5 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
6
7 const customPageRouter = express.Router()
8
9 customPageRouter.get('/homepage/instance',
10 asyncMiddleware(getInstanceHomepage)
11 )
12
13 customPageRouter.put('/homepage/instance',
14 authenticate,
15 ensureUserHasRight(UserRight.MANAGE_INSTANCE_CUSTOM_PAGE),
16 asyncMiddleware(updateInstanceHomepage)
17 )
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 customPageRouter
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function getInstanceHomepage (req: express.Request, res: express.Response) {
28 const page = await ActorCustomPageModel.loadInstanceHomepage()
29 if (!page) {
30 return res.fail({
31 status: HttpStatusCode.NOT_FOUND_404,
32 message: 'Instance homepage could not be found'
33 })
34 }
35
36 return res.json(page.toFormattedJSON())
37 }
38
39 async function updateInstanceHomepage (req: express.Request, res: express.Response) {
40 const content = req.body.content
41
42 await ActorCustomPageModel.updateInstanceHomepage(content)
43 ServerConfigManager.Instance.updateHomepageState(content)
44
45 return res.status(HttpStatusCode.NO_CONTENT_204).end()
46 }