adding chat features, admin features and bug fixes
This commit is contained in:
@@ -40,6 +40,24 @@ class BookStackController {
|
||||
res.status(500).json({ success: false, message: 'BookStack-Suche fehlgeschlagen' });
|
||||
}
|
||||
}
|
||||
async getPage(req: Request, res: Response): Promise<void> {
|
||||
if (!environment.bookstack.url) {
|
||||
res.status(200).json({ success: true, data: null, configured: false });
|
||||
return;
|
||||
}
|
||||
const id = parseInt(String(req.params.id), 10);
|
||||
if (isNaN(id) || id <= 0) {
|
||||
res.status(400).json({ success: false, message: 'Ungültige Seiten-ID' });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const page = await bookstackService.getPageById(id);
|
||||
res.status(200).json({ success: true, data: page, configured: true });
|
||||
} catch (error) {
|
||||
logger.error('BookStackController.getPage error', { error });
|
||||
res.status(500).json({ success: false, message: 'BookStack-Seite konnte nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new BookStackController();
|
||||
|
||||
14
backend/src/controllers/config.controller.ts
Normal file
14
backend/src/controllers/config.controller.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Request, Response } from 'express';
|
||||
import environment from '../config/environment';
|
||||
|
||||
class ConfigController {
|
||||
async getExternalLinks(_req: Request, res: Response): Promise<void> {
|
||||
const links: Record<string, string> = {};
|
||||
if (environment.nextcloudUrl) links.nextcloud = environment.nextcloudUrl;
|
||||
if (environment.bookstack.url) links.bookstack = environment.bookstack.url;
|
||||
if (environment.vikunja.url) links.vikunja = environment.vikunja.url;
|
||||
res.status(200).json({ success: true, data: links });
|
||||
}
|
||||
}
|
||||
|
||||
export default new ConfigController();
|
||||
@@ -80,6 +80,91 @@ class NextcloudController {
|
||||
res.status(500).json({ success: false, message: 'Nextcloud-Trennung fehlgeschlagen' });
|
||||
}
|
||||
}
|
||||
|
||||
async getRooms(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(200).json({ success: true, data: { connected: false, rooms: [] } });
|
||||
return;
|
||||
}
|
||||
const rooms = await nextcloudService.getAllConversations(credentials.loginName, credentials.appPassword);
|
||||
res.status(200).json({ success: true, data: { connected: true, rooms, loginName: credentials.loginName } });
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'NEXTCLOUD_AUTH_INVALID') {
|
||||
await userService.clearNextcloudCredentials(req.user!.id);
|
||||
res.status(200).json({ success: true, data: { connected: false, rooms: [] } });
|
||||
return;
|
||||
}
|
||||
logger.error('getRooms error', { error });
|
||||
res.status(500).json({ success: false, message: 'Nextcloud-Räume konnten nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async getMessages(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const token = req.params.token as string;
|
||||
if (!token) {
|
||||
res.status(400).json({ success: false, message: 'Room token fehlt' });
|
||||
return;
|
||||
}
|
||||
const messages = await nextcloudService.getMessages(token, credentials.loginName, credentials.appPassword);
|
||||
res.status(200).json({ success: true, data: messages });
|
||||
} catch (error) {
|
||||
logger.error('getMessages error', { error });
|
||||
res.status(500).json({ success: false, message: 'Nachrichten konnten nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async sendMessage(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const token = req.params.token as string;
|
||||
const { message } = req.body;
|
||||
if (!token || !message || typeof message !== 'string' || message.trim().length === 0) {
|
||||
res.status(400).json({ success: false, message: 'Token und Nachricht erforderlich' });
|
||||
return;
|
||||
}
|
||||
if (message.length > 32000) {
|
||||
res.status(400).json({ success: false, message: 'Nachricht zu lang' });
|
||||
return;
|
||||
}
|
||||
await nextcloudService.sendMessage(token, message.trim(), credentials.loginName, credentials.appPassword);
|
||||
res.status(200).json({ success: true, data: null });
|
||||
} catch (error) {
|
||||
logger.error('sendMessage error', { error });
|
||||
res.status(500).json({ success: false, message: 'Nachricht konnte nicht gesendet werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async markRoomAsRead(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const token = req.params.token as string;
|
||||
if (!token) {
|
||||
res.status(400).json({ success: false, message: 'Room token fehlt' });
|
||||
return;
|
||||
}
|
||||
await nextcloudService.markAsRead(token, credentials.loginName, credentials.appPassword);
|
||||
res.status(200).json({ success: true, data: null });
|
||||
} catch (error) {
|
||||
logger.error('markRoomAsRead error', { error });
|
||||
res.status(500).json({ success: false, message: 'Raum konnte nicht als gelesen markiert werden' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new NextcloudController();
|
||||
|
||||
192
backend/src/controllers/serviceMonitor.controller.ts
Normal file
192
backend/src/controllers/serviceMonitor.controller.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import serviceMonitorService from '../services/serviceMonitor.service';
|
||||
import notificationService from '../services/notification.service';
|
||||
import pool from '../config/database';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
const createServiceSchema = z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
url: z.string().url().max(500),
|
||||
});
|
||||
|
||||
const updateServiceSchema = z.object({
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
url: z.string().url().max(500).optional(),
|
||||
is_active: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const broadcastSchema = z.object({
|
||||
titel: z.string().min(1).max(200),
|
||||
nachricht: z.string().min(1).max(2000),
|
||||
schwere: z.enum(['info', 'warnung', 'fehler']).default('info'),
|
||||
targetGroup: z.string().optional(),
|
||||
});
|
||||
|
||||
class ServiceMonitorController {
|
||||
async getAll(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const services = await serviceMonitorService.getAllServices();
|
||||
res.json({ success: true, data: services });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get services', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get services' });
|
||||
}
|
||||
}
|
||||
|
||||
async create(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { name, url } = createServiceSchema.parse(req.body);
|
||||
const service = await serviceMonitorService.createService(name, url);
|
||||
res.status(201).json({ success: true, data: service });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to create service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to create service' });
|
||||
}
|
||||
}
|
||||
|
||||
async update(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const data = updateServiceSchema.parse(req.body);
|
||||
const service = await serviceMonitorService.updateService(req.params.id as string, data);
|
||||
res.json({ success: true, data: service });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to update service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to update service' });
|
||||
}
|
||||
}
|
||||
|
||||
async delete(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const deleted = await serviceMonitorService.deleteService(req.params.id as string);
|
||||
if (!deleted) {
|
||||
res.status(404).json({ success: false, message: 'Service not found or is internal' });
|
||||
return;
|
||||
}
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
logger.error('Failed to delete service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to delete service' });
|
||||
}
|
||||
}
|
||||
|
||||
async pingAll(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const results = await serviceMonitorService.pingAll();
|
||||
res.json({ success: true, data: results });
|
||||
} catch (error) {
|
||||
logger.error('Failed to ping services', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to ping services' });
|
||||
}
|
||||
}
|
||||
|
||||
async getStatusSummary(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const summary = await serviceMonitorService.getStatusSummary();
|
||||
res.json({ success: true, data: summary });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get status summary', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get status summary' });
|
||||
}
|
||||
}
|
||||
|
||||
async getSystemHealth(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
let dbStatus = false;
|
||||
let dbSize = '0';
|
||||
|
||||
try {
|
||||
await pool.query('SELECT 1');
|
||||
dbStatus = true;
|
||||
const sizeResult = await pool.query('SELECT pg_database_size(current_database()) as size');
|
||||
dbSize = sizeResult.rows[0].size;
|
||||
} catch {
|
||||
// DB is down
|
||||
}
|
||||
|
||||
const mem = process.memoryUsage();
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
nodeVersion: process.version,
|
||||
uptime: process.uptime(),
|
||||
memoryUsage: {
|
||||
heapUsed: mem.heapUsed,
|
||||
heapTotal: mem.heapTotal,
|
||||
rss: mem.rss,
|
||||
external: mem.external,
|
||||
},
|
||||
dbStatus,
|
||||
dbSize,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to get system health', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get system health' });
|
||||
}
|
||||
}
|
||||
|
||||
async getUsers(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`SELECT id, email, name, role, authentik_groups as groups, is_active, last_login_at
|
||||
FROM users ORDER BY name`
|
||||
);
|
||||
res.json({ success: true, data: result.rows });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get users', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get users' });
|
||||
}
|
||||
}
|
||||
|
||||
async broadcastNotification(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { titel, nachricht, schwere, targetGroup } = broadcastSchema.parse(req.body);
|
||||
|
||||
let users;
|
||||
if (targetGroup) {
|
||||
const result = await pool.query(
|
||||
`SELECT id FROM users WHERE is_active = TRUE AND $1 = ANY(authentik_groups)`,
|
||||
[targetGroup]
|
||||
);
|
||||
users = result.rows;
|
||||
} else {
|
||||
const result = await pool.query(
|
||||
`SELECT id FROM users WHERE is_active = TRUE`
|
||||
);
|
||||
users = result.rows;
|
||||
}
|
||||
|
||||
let sent = 0;
|
||||
for (const user of users) {
|
||||
await notificationService.createNotification({
|
||||
user_id: user.id,
|
||||
typ: 'broadcast',
|
||||
titel,
|
||||
nachricht,
|
||||
schwere,
|
||||
});
|
||||
sent++;
|
||||
}
|
||||
|
||||
res.json({ success: true, data: { sent } });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to broadcast notification', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to broadcast notification' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ServiceMonitorController();
|
||||
Reference in New Issue
Block a user