fix: update models

This commit is contained in:
Ante Brähler
2024-11-19 22:14:02 +00:00
parent 45781aaff0
commit b2aa6ef2da
7 changed files with 34 additions and 22 deletions

View File

@@ -1,7 +1,10 @@
class Channel {
constructor(name, url) {
static nextId = 1;
constructor(name, url, avatar) {
this.id = Channel.nextId++;
this.name = name;
this.url = url;
this.avatar = avatar;
}
}

View File

@@ -0,0 +1,9 @@
class ChatMessage {
constructor(userId, message, timestamp) {
this.userId = userId;
this.message = message;
this.timestamp = timestamp;
}
}
module.exports = ChatMessage;

View File

@@ -30,9 +30,9 @@ const connectedUsers = {}
io.on('connection', socket => {
socket.on('new-user', name => {
connectedUsers[socket.id] = name
socket.broadcast.emit('user-connected', name)
socket.on('new-user', userId => {
connectedUsers[socket.id] = userId
socket.broadcast.emit('user-connected', userId)
})
socket.on('disconnect', () => {

View File

@@ -12,19 +12,19 @@ class ChannelService {
return this.channels;
}
addChannel(name, url) {
addChannel(name, url, avatar) {
const existing = this.channels.some(channel => channel.url === url);
if (existing) {
throw new Error('Channel already exists');
}
const newChannel = new Channel(name, url);
const newChannel = new Channel(name, url, avatar);
this.channels.push(newChannel);
return newChannel;
}
setCurrentChannel(name) {
const nextChannel = this.channels.find(channel => channel.name === name);
setCurrentChannel(id) {
const nextChannel = this.channels.find(channel => channel.id === id);
if (!nextChannel) {
throw new Error('Channel does not exist');
}

View File

@@ -1,5 +1,3 @@
const ChatMessage = require('../models/ChatMessage');
// At the moment, this service is not used! It is only a placeholder for future development for a persistent chat.
class ChatService {
@@ -7,10 +5,10 @@ class ChatService {
this.messages = [];
}
addMessage(user, message) {
const newMessage = new ChatMessage(user, message);
this.messages.push(newMessage);
return newMessage;
addMessage(userId, message, timestamp) {
const newChatMessage = new ChatMessage(userId, message, timestamp);
this.messages.push(newChatMessage);
return newChatMessage;
}
getMessages() {

View File

@@ -2,9 +2,9 @@ const ChannelService = require('../services/ChannelService');
module.exports = (io, socket) => {
socket.on('add-channel', ({ name, url }) => {
socket.on('add-channel', ({ name, url, avatar }) => {
try {
const newChannel = ChannelService.addChannel(name, url);
const newChannel = ChannelService.addChannel(name, url, avatar);
io.emit('channel-added', newChannel); // Broadcast to all clients
} catch (err) {
socket.emit('error', { message: err.message });
@@ -12,10 +12,10 @@ module.exports = (io, socket) => {
});
socket.on('set-current-channel', (name) => {
socket.on('set-current-channel', (id) => {
try {
const currentChannel = ChannelService.setCurrentChannel(name);
io.emit('channel-selected', currentChannel); // Broadcast to all clients
ChannelService.setCurrentChannel(id);
io.emit('channel-selected', id); // Broadcast to all clients
} catch (err) {
socket.emit('error', { message: err.message });
}

View File

@@ -1,8 +1,10 @@
const ChatService = require('../services/ChatService');
const ChatMessage = require('../models/ChatMessage');
module.exports = (io, socket) => {
socket.on('send-message', ({ user, message }) => {
ChatService.addMessage(user, message);
socket.broadcast.emit('chat-message', { message: message, name: user }) // Broadcast to all clients except sender
socket.on('send-message', ({ userId, message, timestamp }) => {
const chatMessage = ChatService.addMessage(userId, message, timestamp);
socket.broadcast.emit('chat-message', chatMessage) // Broadcast to all clients except sender
});
};