From 5555e5cdf17c6e8c24b9735f60182892a9c620ba Mon Sep 17 00:00:00 2001 From: Phoenix <71522316+h1divp@users.noreply.github.com> Date: Sun, 8 Sep 2024 10:34:09 +0900 Subject: [PATCH] Backend hotfixes (#249) * added similar file name in case of future accident * Added better logging for observer. Fixed invalid order attribute. Added new default env parameters for message retrival limiting. * fixed whitespacing * fixed wording * fixed observer limit --- server/.gitignore | 1 + server/src/index.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/server/.gitignore b/server/.gitignore index caf44477b..8a8fd0977 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -134,6 +134,7 @@ build # Private Key JSON firebase-secrets.json +firebase-secret.json # Other .env diff --git a/server/src/index.ts b/server/src/index.ts index 330eb337f..354b90f85 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -23,6 +23,7 @@ const { createServer } = require("http"); const { Server } = require("socket.io"); const socket_port = process.env.socket_port; const express_port = process.env.express_port; +const message_outreach_radius = Number(process.env.message_outreach_radius); const app = express(); // Middleware @@ -76,8 +77,8 @@ io.on("connection", async (socket: any) => { await toggleUserConnectionStatus(socket.id); const observer = messagesCollection - .order('lastUpdated', "desc") - .limit(0) + .orderBy('lastUpdated', "desc") + .limit(1) .onSnapshot((querySnapshot) => { querySnapshot.docChanges().forEach((change) => { if (change.type === "added") { @@ -96,20 +97,19 @@ io.on("connection", async (socket: any) => { userLon ); - if (distance < 300) { - console.log("Message is within 300m of user"); + if (distance < message_outreach_radius) { + console.log(`Message is within ${message_outreach_radius} meters of the user ${socket.id}.`); socket.emit("message", change.doc.data()); } else { - console.log("Message is not within 300m of user"); + console.log(`Message is not within ${message_outreach_radius} meters of the user ${socket.id}.`); } } }); - }); + }); socket.on("disconnect", () => { console.log(`[WS] User <${socket.id}> exited.`); deleteConnectedUserByUID(socket.id); - observer(); }); socket.on("ping", (ack) => { // The (ack) parameter stands for "acknowledgement." This function sends a message back to the originating socket. @@ -122,7 +122,7 @@ io.on("connection", async (socket: any) => { try { const messageCreated = await createMessage(message); if (!messageCreated) throw new Error("createMessage() failed."); - if (ack) ack("message recieved"); + if (ack) ack("message recieved"); } catch (error) { console.error("[WS] Error sending message:", error.message); }