aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/custom-page.ts
blob: d1c672f3fc0a62f6a637caeaafe5dfbc0717dd82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import express from 'express'
import { ServerConfigManager } from '@server/lib/server-config-manager'
import { ActorCustomPageModel } from '@server/models/account/actor-custom-page'
import { HttpStatusCode, 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.fail({
      status: HttpStatusCode.NOT_FOUND_404,
      message: 'Instance homepage could not be found'
    })
  }

  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.status(HttpStatusCode.NO_CONTENT_204).end()
}