Add functionality for managing user-owned chat rooms: Implement getOwnRooms and deleteOwnRoom methods in ChatController and ChatService, add corresponding API routes in chatRouter, and enhance MultiChatDialog for displaying and deleting owned rooms with localized messages. Update i18n files for new features.

This commit is contained in:
Torsten Schulz (local)
2026-03-04 23:22:16 +01:00
parent 2bc34acacf
commit 190cf626f9
8 changed files with 334 additions and 26 deletions

View File

@@ -14,6 +14,8 @@ class ChatController {
this.getOneToOneMessageHistory = this.getOneToOneMessageHistory.bind(this);
this.getRoomList = this.getRoomList.bind(this);
this.getRoomCreateOptions = this.getRoomCreateOptions.bind(this);
this.getOwnRooms = this.getOwnRooms.bind(this);
this.deleteOwnRoom = this.deleteOwnRoom.bind(this);
}
async getMessages(req, res) {
@@ -185,6 +187,32 @@ class ChatController {
res.status(500).json({ error: error.message });
}
}
async getOwnRooms(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const rooms = await chatService.getOwnRooms(hashedUserId);
res.status(200).json(rooms);
} catch (error) {
const status = error.message === 'user_not_found' ? 404 : 500;
res.status(status).json({ error: error.message });
}
}
async deleteOwnRoom(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const roomId = Number.parseInt(req.params.id, 10);
if (!Number.isInteger(roomId) || roomId <= 0) {
return res.status(400).json({ error: 'invalid_room_id' });
}
await chatService.deleteOwnRoom(hashedUserId, roomId);
res.status(204).send();
} catch (error) {
const status = error.message === 'room_not_found_or_not_owner' || error.message === 'user_not_found' ? 404 : 500;
res.status(status).json({ error: error.message });
}
}
}
export default ChatController;

View File

@@ -16,5 +16,7 @@ router.post('/oneToOne/sendMessage', authenticate, chatController.sendOneToOneMe
router.get('/oneToOne/messageHistory', authenticate, chatController.getOneToOneMessageHistory); // Neue Route zum Abrufen der Nachrichtengeschichte
router.get('/rooms', chatController.getRoomList);
router.get('/room-create-options', authenticate, chatController.getRoomCreateOptions);
router.get('/my-rooms', authenticate, chatController.getOwnRooms);
router.delete('/my-rooms/:id', authenticate, chatController.deleteOwnRoom);
export default router;

View File

@@ -1,5 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import amqp from 'amqplib/callback_api.js';
import User from '../models/community/user.js';
import Room from '../models/chat/room.js';
const RABBITMQ_URL = 'amqp://localhost';
const QUEUE = 'oneToOne_messages';
@@ -169,6 +171,45 @@ class ChatService {
roomTypes: interests.map((i) => ({ id: i.id, name: i.name }))
};
}
async getOwnRooms(hashedUserId) {
const user = await User.findOne({
where: { hashedId: hashedUserId },
attributes: ['id']
});
if (!user) {
throw new Error('user_not_found');
}
return Room.findAll({
where: { ownerId: user.id },
attributes: ['id', 'title', 'isPublic', 'roomTypeId', 'ownerId'],
order: [['title', 'ASC']]
});
}
async deleteOwnRoom(hashedUserId, roomId) {
const user = await User.findOne({
where: { hashedId: hashedUserId },
attributes: ['id']
});
if (!user) {
throw new Error('user_not_found');
}
const deleted = await Room.destroy({
where: {
id: roomId,
ownerId: user.id
}
});
if (!deleted) {
throw new Error('room_not_found_or_not_owner');
}
return true;
}
}
export default new ChatService();