|
| 1 | +import express from 'express'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { parseReq } from '@/services/juxt-web/routes/routeUtils'; |
| 4 | +import { WebAutomodLogListView } from '@/services/juxt-web/views/web/admin/automodLogListView'; |
| 5 | +import { automodRuleMode, automodRuleType } from '@/models/automodRules'; |
| 6 | +import { WebAutomodRuleListView } from '@/services/juxt-web/views/web/admin/automodRuleListView'; |
| 7 | +import { WebAutomodRuleCreateView } from '@/services/juxt-web/views/web/admin/automodRuleCreateView'; |
| 8 | +import { onOffSchema } from '@/services/juxt-web/routes/admin/admin'; |
| 9 | +import type { AutomodRuleListViewProps } from '@/services/juxt-web/views/web/admin/automodRuleListView'; |
| 10 | +import type { AutomodLogListViewProps } from '@/services/juxt-web/views/web/admin/automodLogListView'; |
| 11 | + |
| 12 | +export const adminAutomodRouter = express.Router(); |
| 13 | + |
| 14 | +adminAutomodRouter.get('/automod', async function (req, res) { |
| 15 | + if (!res.locals.moderator) { |
| 16 | + return res.redirect('/titles/show'); |
| 17 | + } |
| 18 | + |
| 19 | + const { query } = parseReq(req, { |
| 20 | + query: z.object({ |
| 21 | + action: z.enum(['blocked', 'logged']).optional(), |
| 22 | + page: z.coerce.number().default(0) |
| 23 | + }) |
| 24 | + }); |
| 25 | + |
| 26 | + const limit = 100; |
| 27 | + const offset = query.page * limit; |
| 28 | + const { data: logPage } = await req.api.admin.automodLogs.list({ |
| 29 | + action: query.action, |
| 30 | + offset, |
| 31 | + limit |
| 32 | + }); |
| 33 | + const hasNextPage = offset + limit < logPage.total; |
| 34 | + |
| 35 | + const props: AutomodLogListViewProps = { |
| 36 | + items: logPage.items, |
| 37 | + total: logPage.total, |
| 38 | + page: query.page, |
| 39 | + hasNextPage |
| 40 | + }; |
| 41 | + |
| 42 | + return res.jsxForDirectory({ |
| 43 | + web: <WebAutomodLogListView {...props} /> |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +adminAutomodRouter.get('/automod/rules', async function (req, res) { |
| 48 | + if (!res.locals.moderator) { |
| 49 | + return res.redirect('/titles/show'); |
| 50 | + } |
| 51 | + |
| 52 | + const { query } = parseReq(req, { |
| 53 | + query: z.object({ |
| 54 | + page: z.coerce.number().default(0) |
| 55 | + }) |
| 56 | + }); |
| 57 | + |
| 58 | + const limit = 50; |
| 59 | + const offset = query.page * limit; |
| 60 | + const { data: rulePage } = await req.api.admin.automodRules.list({ |
| 61 | + offset, |
| 62 | + limit |
| 63 | + }); |
| 64 | + const hasNextPage = offset + limit < rulePage.total; |
| 65 | + |
| 66 | + const props: AutomodRuleListViewProps = { |
| 67 | + items: rulePage.items, |
| 68 | + total: rulePage.total, |
| 69 | + page: query.page, |
| 70 | + hasNextPage, |
| 71 | + canEdit: res.locals.developer |
| 72 | + }; |
| 73 | + |
| 74 | + return res.jsxForDirectory({ |
| 75 | + web: <WebAutomodRuleListView {...props} /> |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +adminAutomodRouter.get('/automod/rules/create', async function (req, res) { |
| 80 | + if (!res.locals.developer) { |
| 81 | + return res.redirect('/titles/show'); |
| 82 | + } |
| 83 | + |
| 84 | + return res.jsxForDirectory({ |
| 85 | + web: <WebAutomodRuleCreateView /> |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +adminAutomodRouter.post('/automod/rules/create', async function (req, res) { |
| 90 | + const { body } = parseReq(req, { |
| 91 | + body: z.object({ |
| 92 | + title: z.string().min(1), |
| 93 | + type: z.enum(automodRuleType), |
| 94 | + mode: z.enum(automodRuleMode) |
| 95 | + }) |
| 96 | + }); |
| 97 | + |
| 98 | + await req.api.admin.automodRules.create({ |
| 99 | + mode: body.mode, |
| 100 | + type: body.type, |
| 101 | + title: body.title |
| 102 | + }); |
| 103 | + |
| 104 | + res.redirect('/admin/automod/rules'); |
| 105 | +}); |
| 106 | + |
| 107 | +adminAutomodRouter.post('/automod/rules/:id/update', async function (req, res) { |
| 108 | + const { body, params } = parseReq(req, { |
| 109 | + params: z.object({ |
| 110 | + id: z.string() |
| 111 | + }), |
| 112 | + body: z.object({ |
| 113 | + title: z.string().min(1), |
| 114 | + description: z.string(), |
| 115 | + type: z.enum(automodRuleType), |
| 116 | + mode: z.enum(automodRuleMode), |
| 117 | + enabled: onOffSchema(), |
| 118 | + keywordSettingsKeywords: z.string().default('') |
| 119 | + }) |
| 120 | + }); |
| 121 | + |
| 122 | + const keywords = body.keywordSettingsKeywords.split('\n').map(v => v.trim()).filter(v => v.length > 0); |
| 123 | + |
| 124 | + await req.api.admin.automodRules.update({ |
| 125 | + id: params.id, |
| 126 | + mode: body.mode, |
| 127 | + type: body.type, |
| 128 | + title: body.title, |
| 129 | + description: body.description, |
| 130 | + enabled: !!body.enabled, |
| 131 | + settings: { |
| 132 | + keyword: body.type === 'keyword' |
| 133 | + ? { |
| 134 | + keywords: keywords |
| 135 | + } |
| 136 | + : undefined |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + res.redirect('/admin/automod/rules'); |
| 141 | +}); |
| 142 | + |
| 143 | +adminAutomodRouter.post('/automod/rules/:id/delete', async function (req, res) { |
| 144 | + const { params } = parseReq(req, { |
| 145 | + params: z.object({ |
| 146 | + id: z.string() |
| 147 | + }) |
| 148 | + }); |
| 149 | + |
| 150 | + await req.api.admin.automodRules.delete({ |
| 151 | + id: params.id |
| 152 | + }); |
| 153 | + |
| 154 | + res.redirect('/admin/automod/rules'); |
| 155 | +}); |
0 commit comments