eplace/src/rooms/index.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2026-05-16 11:14:39 +02:00
import { authedAPIRequest } from "../utils/auth";
2026-05-16 13:38:37 +02:00
import { subscribe } from "../utils/streams";
import { resetValues } from "./canvas/utils";
2026-05-15 11:08:23 +02:00
// FIXME: This file should handle the rooms API
// Functions may include:
// - fetchRoomConfig (get the configuration of a room)
// - setCurrentRoomConfig (set the current room configuration and update the DOM accordingly)
// - getCurrentRoomConfig (get the current room configuration)
// - joinRoom (join a room by its slug)
// - listRooms (list all the rooms available)
// - createRoom (create a room)
// - updateRoom (update a room's configuration)
// - deleteRoom (delete a room)
2026-05-15 22:59:15 +02:00
2026-05-16 16:15:40 +02:00
let roomConfig = null;
function setCurrentRoomConfig(cfg) {
roomConfig = cfg;
}
2026-05-16 18:51:01 +02:00
async function getCurrentRoomConfig(room) {
2026-05-16 16:25:05 +02:00
if (!roomConfig) {
2026-05-16 18:18:56 +02:00
await fetchRoomConfig(room);
2026-05-16 16:25:05 +02:00
}
2026-05-16 16:15:40 +02:00
return roomConfig;
}
async function joinRoom(room) {
2026-05-16 13:38:37 +02:00
// socket.on('connection', (sockett) => {
// sockett.join(room);
// broadcast to all connected clients in the room
// socket.to('some room').emit('hello', 'world');
// broadcast to all connected clients except those in the room
// socket.except('some room').emit('hello', 'world');
// });
resetValues();
subscribe(room);
}
// async function leaveRoom(room) {
2026-05-16 13:38:37 +02:00
// socket.on('connection', (socket) => {
// socket.leave(room);
// })
// }
2026-05-16 13:38:37 +02:00
async function listRooms() {
const response = await authedAPIRequest("/rooms/", { method: "GET" });
2026-05-15 22:59:15 +02:00
2026-05-16 16:25:05 +02:00
if (!response || !response.ok) {
console.error(
2026-05-16 17:18:47 +02:00
"Could not retrieve rooms list: " + response && response.statusText
2026-05-16 16:25:05 +02:00
? response.statusText
: "null",
);
2026-05-16 13:38:37 +02:00
console.debug(await response.text());
return;
}
const res = await response.json();
console.debug("Retrieved rooms list");
console.debug(res);
// Update HTML
// const roomNameElt = document.getElementById("room-name");
// roomNameElt.innerText = res.metadata.name;
}
// async function createRoom() {
2026-05-15 22:59:15 +02:00
// }
// async function updateRoom() {
// }
// async function deleteRoom() {
// }
2026-05-16 11:14:39 +02:00
async function fetchRoomConfig(room) {
if (!room) {
console.error("Cannot fetch an undefined room");
return;
}
const response = await authedAPIRequest("/rooms/" + room + "/config", {
method: "GET",
});
2026-05-16 11:14:39 +02:00
2026-05-16 16:25:05 +02:00
if (!response || !response.ok) {
console.error(
2026-05-16 17:18:47 +02:00
"Could not retrieve room config" + response && response.statusText
2026-05-16 16:25:05 +02:00
? response.statusText
: "null",
);
2026-05-16 11:14:39 +02:00
console.debug(await response.text());
return;
}
const res = await response.json();
console.debug(`Retrieved config for room ${room}:`);
console.debug(res);
2026-05-16 11:14:39 +02:00
setCurrentRoomConfig(res);
2026-05-16 11:14:39 +02:00
// Update HTML
2026-05-16 19:02:11 +02:00
document.getElementById("room-name").innerText = res.metadata.name;
2026-05-16 20:15:03 +02:00
document.getElementById("room-description").innerText =
res.metadata.description;
2026-05-16 19:02:11 +02:00
document.getElementById("room-description").style.display = "block";
2026-05-16 11:14:39 +02:00
}
export {
fetchRoomConfig,
setCurrentRoomConfig,
getCurrentRoomConfig,
joinRoom,
2026-05-16 13:38:37 +02:00
listRooms,
// createRoom,
// updateRoom,
// deleteRoom
};