2025-03-10 17:27:25 +01:00

34 lines
1.2 KiB
JavaScript

const database = require("../db");
const stringutils = require("../stringutils");
const chatController = {
add: async (req, res) => {
try {
const { pubkey } = req.body;
if (!pubkey) {
return res.status(400).json({ error: "Missing publicKey" });
}
const hexKey = stringutils.pemToHex(pubkey)
const roomid = await database.createRoom(req.session.publicKey, hexKey);
return res.status(201).json({ message: "Room created successfully.",
roomid: roomid,
peer: hexKey
});
} catch (error) {
console.error("Error creating the room:", error);
return res.status(500).json({ error: "Failed to create the room" });
}
},
rooms: async (req, res) => {
try {
const rooms = await database.getRooms(req.session.publicKey);
return res.status(200).json({ rooms: rooms });
} catch (error) {
console.error("Error fetching rooms:", error);
return res.status(500).json({ error: "Error fetching rooms" });
}
}
}
module.exports = chatController;