Message dialog and move individual message

This commit is contained in:
2025-08-25 12:47:07 +02:00
parent 2b1bf5040f
commit 5c76c35d81
11 changed files with 983 additions and 4 deletions

View File

@@ -51,4 +51,30 @@ export const getMessages = async (req: Request, res: Response) => {
const messages = await MessageService.getMessages(channelId);
res.json({ messages });
}
export const moveMessage = async (req: Request, res: Response) => {
const { messageId } = req.params;
const { targetChannelId } = req.body;
if (!messageId || !targetChannelId) {
return res.status(400).json({ error: 'Message ID and target channel ID are required' });
}
try {
const result = await MessageService.moveMessage(messageId, targetChannelId);
logger.info(`Message ${messageId} moved to channel ${targetChannelId}`);
res.json({
message: 'Message moved successfully',
messageId: parseInt(messageId),
targetChannelId: parseInt(targetChannelId)
});
} catch (error: any) {
if (error.message === 'Message not found') {
return res.status(404).json({ error: 'Message not found' });
}
logger.critical(`Failed to move message ${messageId}:`, error);
res.status(500).json({ error: 'Failed to move message' });
}
}

View File

@@ -14,6 +14,9 @@ export const attachEvents = (ws: WebSocket) => {
events.on('message-deleted', (id) => {
ws.send(JSON.stringify({ type: 'message-deleted', data: {id }}));
});
events.on('message-moved', (messageId, sourceChannelId, targetChannelId) => {
ws.send(JSON.stringify({ type: 'message-moved', data: {messageId, sourceChannelId, targetChannelId }}));
});
events.on('channel-created', (channel) => {
ws.send(JSON.stringify({ type: 'channel-created', data: {channel }}));
});

View File

@@ -6,6 +6,7 @@ export const router = Router({mergeParams: true});
router.post('/', authenticate, MessageController.createMessage);
router.put('/:messageId', authenticate, MessageController.updateMessage);
router.put('/:messageId/move', authenticate, MessageController.moveMessage);
router.delete('/:messageId', authenticate, MessageController.deleteMessage);
router.get('/', authenticate, MessageController.getMessages);

View File

@@ -80,4 +80,28 @@ export const getMessage = async (id: string) => {
`);
const row = query.get({ id: id });
return row;
}
export const moveMessage = async (messageId: string, targetChannelId: string) => {
// Get current message to emit proper events
const currentMessage = await getMessage(messageId);
if (!currentMessage) {
throw new Error('Message not found');
}
const query = db.prepare(`UPDATE messages SET channelId = $targetChannelId WHERE id = $messageId`);
const result = query.run({ messageId: messageId, targetChannelId: targetChannelId });
if (result.changes === 0) {
throw new Error('Message not found or not updated');
}
// Update FTS table if enabled
if (FTS5Enabled) {
// FTS table doesn't need channelId update, just content remains searchable
// No additional FTS changes needed since content hasn't changed
}
events.emit('message-moved', messageId, (currentMessage as any).channelId, targetChannelId);
return result;
}