diff --git a/src/data/messages.ts b/src/data/messages.ts index 56b8c25..cf517db 100644 --- a/src/data/messages.ts +++ b/src/data/messages.ts @@ -4,7 +4,10 @@ import { MessageI } from 'discreetly-interfaces'; const prisma = new PrismaClient(); -function updateRoom(roomId: string, message: MessageI, epoch: number): Promise { +function addMessageToRoom(roomId: string, message: MessageI): Promise { + if (!message.epoch) { + throw new Error('Epoch not provided'); + } return prisma.rooms.update({ where: { roomId: roomId @@ -12,7 +15,7 @@ function updateRoom(roomId: string, message: MessageI, epoch: number): Promise { if (room) { - updateRoom(roomId, message) + // Todo This should check that there is no duplicate messageId with in this room and epoch, if there is, we need to return an error and reconstruct the secret from both messages, and ban the user + addMessageToRoom(roomId, message) .then((roomToUpdate) => { console.log(roomToUpdate); + return true; }) .catch((error) => { console.error(`Error updating room: ${error}`); + return false; }); } else { console.log('Room not found'); + return false; } }) .catch((error) => { console.error(`Error getting room: ${error}`); + return false; }); + return false; } diff --git a/src/websockets/index.ts b/src/websockets/index.ts index 5bc01d9..bd07ce4 100644 --- a/src/websockets/index.ts +++ b/src/websockets/index.ts @@ -13,7 +13,7 @@ export function websocketSetup(io: SocketIOServer) { socket.on('validateMessage', (msg: MessageI) => { pp({ 'VALIDATING MESSAGE ID': String(msg.roomId).slice(0, 11), 'MSG:': msg.message }); - let valid: boolean; + let validProof: boolean; getRoomByID(String(msg.roomId)) .then((room: RoomI) => { if (!room) { @@ -22,17 +22,17 @@ export function websocketSetup(io: SocketIOServer) { } verifyProof(msg, room) .then((v) => { - valid = v; - createMessage(String(msg.roomId), msg); + validProof = v; + const validMessage: boolean = createMessage(String(msg.roomId), msg); + if (!validProof || !validMessage) { + pp('INVALID MESSAGE', 'warn'); + return; + } io.emit('messageBroadcast', msg); }) .catch((err) => { err; }); - if (!valid) { - pp('INVALID MESSAGE', 'warn'); - return; - } }) .catch((err) => pp(err, 'error')); });