Files
SerpentRace/SerpentRace_Backend/dist/Application/Chat/commands/SendMessageCommandHandler.js
T

74 lines
3.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendMessageCommandHandler = void 0;
const Logger_1 = require("../../Services/Logger");
const uuid_1 = require("uuid");
class SendMessageCommandHandler {
constructor(chatRepository) {
this.chatRepository = chatRepository;
}
async execute(command) {
try {
// Validate message is non-empty string
if (typeof command.message !== 'string' || !command.message.trim()) {
throw new Error('Message must be a non-empty string');
}
const chat = await this.chatRepository.findById(command.chatId);
if (!chat) {
throw new Error('Chat not found');
}
// Check if user is member of this chat
if (!chat.users.includes(command.userId)) {
throw new Error('User is not a member of this chat');
}
// Create message
const message = {
id: (0, uuid_1.v4)(),
date: new Date(),
userid: command.userId,
text: command.message.trim()
};
// Manage message history (keep last 10 per user, up to 2 weeks)
let updatedMessages = [...chat.messages, message];
updatedMessages = this.pruneMessages(updatedMessages);
// Update chat
await this.chatRepository.update(command.chatId, {
messages: updatedMessages,
lastActivity: new Date()
});
(0, Logger_1.logAuth)('Message sent successfully', command.userId, {
chatId: command.chatId,
messageLength: command.message.length,
totalMessages: updatedMessages.length
});
return message;
}
catch (error) {
(0, Logger_1.logError)('SendMessageCommandHandler error', error);
return null;
}
}
pruneMessages(messages) {
const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000);
// Remove messages older than 2 weeks
let prunedMessages = messages.filter(msg => new Date(msg.date) > twoWeeksAgo);
// Group by user and keep last 10 messages per user
const messagesByUser = new Map();
prunedMessages.forEach(msg => {
if (!messagesByUser.has(msg.userid)) {
messagesByUser.set(msg.userid, []);
}
messagesByUser.get(msg.userid).push(msg);
});
// Keep only last 10 messages per user
const finalMessages = [];
messagesByUser.forEach((userMessages, userId) => {
const last10 = userMessages.slice(-10);
finalMessages.push(...last10);
});
// Sort by date
return finalMessages.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
}
}
exports.SendMessageCommandHandler = SendMessageCommandHandler;
//# sourceMappingURL=SendMessageCommandHandler.js.map