- 52 JS files (backend + public/js): header with Copyright (C) 2026 Sea Network Technology Studio Author: CangLan <admin@sea-studio.top> + AGPLv3 notice - idempotent (skips if header present), all syntax-checked
66 lines
3.6 KiB
JavaScript
66 lines
3.6 KiB
JavaScript
/*
|
|
* MC Report System
|
|
* Copyright (C) 2026 Sea Network Technology Studio
|
|
* Author: CangLan <admin@sea-studio.top>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
const express = require('express');
|
|
const { query, getRow } = require('../db');
|
|
const { authenticate, requireRole } = require('../middleware/auth');
|
|
const { sendWebhook } = require('../webhook');
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
|
res.json(await query('SELECT * FROM notification_configs ORDER BY id'));
|
|
});
|
|
|
|
router.post('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
|
const { name, type, webhook_url, events } = req.body;
|
|
if (!name||!type) return res.status(400).json({ error: '名称和类型为必填' });
|
|
if (type==='webhook' && !webhook_url) return res.status(400).json({ error: 'Webhook地址必填' });
|
|
const r = await query('INSERT INTO notification_configs(name, type, webhook_url, events) VALUES (?,?,?,?)', [name, type, webhook_url||'', events||'all']);
|
|
await query("INSERT INTO audit_logs(user_id, username, action, entity_type, entity_id, details) VALUES (?,?,?,?,?,?)", [req.user.id, req.user.username, 'create_notification', 'notification_config', r.insertId, `创建通知: ${name}`]);
|
|
res.status(201).json({ id: r.insertId, message: '创建成功' });
|
|
});
|
|
|
|
router.put('/:id', authenticate, requireRole('owner','admin'), async (req, res) => {
|
|
const cfg = await getRow('SELECT * FROM notification_configs WHERE id = ?', [req.params.id]);
|
|
if (!cfg) return res.status(404).json({ error: '配置不存在' });
|
|
const fields = {};
|
|
if (req.body.name) fields.name = req.body.name;
|
|
if (req.body.webhook_url !== undefined) fields.webhook_url = req.body.webhook_url;
|
|
if (req.body.type) fields.type = req.body.type;
|
|
if (req.body.active !== undefined) fields.active = req.body.active?1:0;
|
|
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
|
|
const sets = Object.keys(fields).map(k=>`${k}=?`).join(',');
|
|
await query(`UPDATE notification_configs SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]);
|
|
res.json({ message: '更新成功' });
|
|
});
|
|
|
|
router.delete('/:id', authenticate, requireRole('owner','admin'), async (req, res) => {
|
|
await query('DELETE FROM notification_configs WHERE id = ?', [req.params.id]);
|
|
res.json({ message: '已删除' });
|
|
});
|
|
|
|
router.post('/:id/test', authenticate, requireRole('owner','admin'), async (req, res) => {
|
|
const cfg = await getRow('SELECT * FROM notification_configs WHERE id = ?', [req.params.id]);
|
|
if (!cfg||!cfg.webhook_url) return res.status(404).json({ error: '配置不存在' });
|
|
sendWebhook('ticket_created', { ticket_id:'TEST', ticket_title:'测试通知', ticket_type:'测试', reporter_game_name:'TestPlayer', reporter_game_uid:'TEST001', ticket_reason:'这是一条测试消息' }).then(() => res.json({ message:'测试消息已发送' })).catch(e => res.status(500).json({ error:e.message }));
|
|
});
|
|
|
|
module.exports = router;
|