From 2b7b6b8684c5cf365908025f4f1d49bec1b2c6d6 Mon Sep 17 00:00:00 2001 From: Guillem George Date: Sat, 16 May 2026 11:14:39 +0200 Subject: [PATCH] no moulinette ? --- src/pages/index.js | 12 ++++++++++-- src/rooms/index.js | 20 ++++++++++++++++++++ src/utils/auth.js | 14 ++++++-------- src/utils/redirect.js | 2 +- src/utils/streams.js | 18 +++++++++++++----- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/pages/index.js b/src/pages/index.js index f09dade..c839e35 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -1,6 +1,7 @@ -import { initSocket } from "../utils/streams"; +import { initSocket, subscribe } from "../utils/streams"; import { calculateLayout } from "./utils"; import { authenticate } from "../utils/auth"; +import { fetchRoomConfig } from "../rooms"; // Initialize the layout calculateLayout(); @@ -11,6 +12,13 @@ async () => { return; } + let room = window.location.pathname.split("/")[1]; + + if (room === "") { + room = "epi-place"; + } + initSocket(); - // subscribe() + subscribe(room, "pixel-update"); + fetchRoomConfig(room); }; diff --git a/src/rooms/index.js b/src/rooms/index.js index f70fe6e..00afe91 100644 --- a/src/rooms/index.js +++ b/src/rooms/index.js @@ -1,3 +1,4 @@ +import { authedAPIRequest } from "../utils/auth"; // FIXME: This file should handle the rooms API // Functions may include: // - fetchRoomConfig (get the configuration of a room) @@ -24,3 +25,22 @@ // // leave the room // socket.leave('some room'); // }); +// + +async function fetchRoomConfig(room) { + const response = await authedAPIRequest("/rooms/" + room + "/config"); + + if (!response.ok) { + console.error("Could not retrieve room config: " + response.statusText); + console.debug(await response.text()); + return; + } + + const obj = await response.json(); + + console.debug(`Retrieved config for room ${room}: ${obj}`); + + // Update HTML +} + +export { fetchRoomConfig }; diff --git a/src/utils/auth.js b/src/utils/auth.js index 015db2a..f0f1005 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -134,10 +134,10 @@ async function authedAPIRequest(endpoint, options) { return null; } - if (!options.method) { - console.error("Invalid parameter: options (missing method)"); - return null; - } + // if (!options.method) { + // console.error("Invalid parameter: options (missing method)"); + // return null; + // } if (!options.headers) { options.headers = {}; @@ -146,9 +146,9 @@ async function authedAPIRequest(endpoint, options) { options.headers.Authorization = "Bearer " + localStorage.getItem("token"); const full_endpoint = import.meta.env.VITE_URL + "/api" + endpoint; - let response; - response = await fetch(full_endpoint, options); + const response = await fetch(full_endpoint, options); + if (response.status === 401) { const response_err = await response.text(); @@ -156,8 +156,6 @@ async function authedAPIRequest(endpoint, options) { if (await refreshToken(null)) { return await authedAPIRequest(endpoint, options); } - - return null; } localStorage.clear(); diff --git a/src/utils/redirect.js b/src/utils/redirect.js index 9c0a6d3..1be53f8 100644 --- a/src/utils/redirect.js +++ b/src/utils/redirect.js @@ -21,7 +21,7 @@ function createLink() { function redirectToLoginPage() { const redirectUrl = createLink(); - window.location.href = redirectUrl; + window.location.href = redirectUrl.href; } export { createLink, redirectToLoginPage }; diff --git a/src/utils/streams.js b/src/utils/streams.js index 938f0dd..b269f45 100644 --- a/src/utils/streams.js +++ b/src/utils/streams.js @@ -42,18 +42,26 @@ async function initSocket() { return; } -// TODO -async function subscribe(room) { +async function subscribe(room, channel) { if (!room) { room = "epi-place"; } - // console.warn("Skipping room susbscription (not implemented)") + if (!channel) { + channel = "message"; + } + + let path = "rooms.canvas.getStream"; + + if (channel.includes("chat") || channel.includes("message")) { + path = "rooms.getChat"; + } + const msg = { id: uuid, method: "subscription", params: { - path: "rooms.canvas.getStream" | "rooms.getChat", + path: path, input: { json: { roomSlug: room, @@ -72,4 +80,4 @@ async function subscribe(room) { // } -export { initSocket, socket }; +export { socket, initSocket, subscribe };