From f76b922d528239d998f84ef94c55f6d273e1733e Mon Sep 17 00:00:00 2001 From: AlexanderWangY Date: Sat, 31 Aug 2024 01:27:45 +0800 Subject: [PATCH 1/2] test --- server/.gitignore | 4 +--- server/src/utilities/firebaseInit.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/server/.gitignore b/server/.gitignore index 34bf73406..caf44477b 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -133,11 +133,9 @@ dist build # Private Key JSON -./firebase-secrets.json +firebase-secrets.json # Other .env build/ -# Firebase Secrets -.firebase-secrets.json \ No newline at end of file diff --git a/server/src/utilities/firebaseInit.ts b/server/src/utilities/firebaseInit.ts index beeb3f717..79bf6bee0 100644 --- a/server/src/utilities/firebaseInit.ts +++ b/server/src/utilities/firebaseInit.ts @@ -1,5 +1,5 @@ const admin = require('firebase-admin'); -const serviceAccount = require("../../.firebase-secrets.json"); +const serviceAccount = require("../../firebase-secrets.json"); export const adminApp = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), From c3f68e10d48dfa8bdb49bf370f8927425a027060 Mon Sep 17 00:00:00 2001 From: AlexanderWangY Date: Sat, 31 Aug 2024 01:37:35 +0800 Subject: [PATCH 2/2] observer fix with limit and smal key error fix --- .../app/screens/settings/SettingsScreen.tsx | 2 +- server/src/index.ts | 108 ++++++++++-------- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/client/app/screens/settings/SettingsScreen.tsx b/client/app/screens/settings/SettingsScreen.tsx index 5fd8664af..4db3a2632 100644 --- a/client/app/screens/settings/SettingsScreen.tsx +++ b/client/app/screens/settings/SettingsScreen.tsx @@ -46,7 +46,7 @@ const SettingsScreen: React.FC = () => { Settings {Sections.map(({ header, items }) => ( - + {header} diff --git a/server/src/index.ts b/server/src/index.ts index d52c35ff3..330eb337f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,19 +1,23 @@ -import express from 'express'; -import 'dotenv/config'; -import 'geofire-common'; -import { Message } from './types/Message'; -import { createMessage } from './actions/createMessage'; -import { createUser } from './actions/createConnectedUser'; -import { toggleUserConnectionStatus, updateUserLocation, updateUserDisplayName } from './actions/updateConnectedUser'; -import { deleteConnectedUserByUID } from './actions/deleteConnectedUser'; -import { findNearbyUsers, getConnectedUser } from './actions/getConnectedUsers'; -import {geohashForLocation} from 'geofire-common'; -import { ConnectedUser } from './types/User'; -import { getAuth } from 'firebase-admin/auth'; +import express from "express"; +import "dotenv/config"; +import "geofire-common"; +import { Message } from "./types/Message"; +import { createMessage } from "./actions/createMessage"; +import { createUser } from "./actions/createConnectedUser"; +import { + toggleUserConnectionStatus, + updateUserLocation, + updateUserDisplayName, +} from "./actions/updateConnectedUser"; +import { deleteConnectedUserByUID } from "./actions/deleteConnectedUser"; +import { findNearbyUsers, getConnectedUser } from "./actions/getConnectedUsers"; +import { geohashForLocation } from "geofire-common"; +import { ConnectedUser } from "./types/User"; +import { getAuth } from "firebase-admin/auth"; import Mailgun from "mailgun.js"; -import { messagesCollection } from './utilities/firebaseInit'; -import { calculateDistanceInMeters } from './actions/calculateDistance'; -import { scheduleCron } from './actions/deleter'; +import { messagesCollection } from "./utilities/firebaseInit"; +import { calculateDistanceInMeters } from "./actions/calculateDistance"; +import { scheduleCron } from "./actions/deleter"; const { createServer } = require("http"); const { Server } = require("socket.io"); @@ -71,30 +75,36 @@ io.on("connection", async (socket: any) => { await createUser(defaultConnectedUser); await toggleUserConnectionStatus(socket.id); - const observer = messagesCollection.where("lastUpdated", ">", Date.now()).onSnapshot((querySnapshot) => { - querySnapshot.docChanges().forEach((change) => { - - if (change.type === "added"){ - console.log("New message: ", change.doc.data()); - - const messageLat = change.doc.data().location.lat; - const messageLon = change.doc.data().location.lon; - - const userLat = defaultConnectedUser.location.lat; - const userLon = defaultConnectedUser.location.lon; - - const distance = calculateDistanceInMeters(messageLat, messageLon, userLat, userLon); - - if (distance < 300) { - console.log("Message is within 300m of user"); - socket.emit("message", change.doc.data()); - } else { - console.log("Message is not within 300m of user"); + const observer = messagesCollection + .order('lastUpdated', "desc") + .limit(0) + .onSnapshot((querySnapshot) => { + querySnapshot.docChanges().forEach((change) => { + if (change.type === "added") { + console.log("New message: ", change.doc.data()); + + const messageLat = change.doc.data().location.lat; + const messageLon = change.doc.data().location.lon; + + const userLat = defaultConnectedUser.location.lat; + const userLon = defaultConnectedUser.location.lon; + + const distance = calculateDistanceInMeters( + messageLat, + messageLon, + userLat, + userLon + ); + + if (distance < 300) { + console.log("Message is within 300m of user"); + socket.emit("message", change.doc.data()); + } else { + console.log("Message is not within 300m of user"); + } } - } - + }); }); - }); socket.on("disconnect", () => { console.log(`[WS] User <${socket.id}> exited.`); @@ -134,8 +144,8 @@ io.on("connection", async (socket: any) => { } catch (error) { console.error("[WS] Error calling updateLocation:", error.message); } - }) -}) + }); +}); socketServer.listen(socket_port, () => { console.log(`[WS] Listening for new connections on port ${socket_port}.`); }); @@ -159,7 +169,6 @@ app.get("/users", async (req, res) => { const userIds = await findNearbyUsers(lat, lon, radius); res.json(userIds); - } else if (req.query.userId) { query = "?userId"; const userId = req.query.userId; @@ -169,7 +178,7 @@ app.get("/users", async (req, res) => { if (user) { res.json(user); } else { - // getConnectedUserDisplayName() will return false is an error is thrown, and print it to console. + // getConnectedUserDisplayName() will return false is an error is thrown, and print it to console. throw Error("getConnectedUser() failed."); } } @@ -221,7 +230,6 @@ app.put("/users", async (req, res) => { const success = await toggleUserConnectionStatus(userId); if (!success) throw Error(" toggleUserConnectionStatus() failed."); - } else if (req.query.userId && req.query.lat && req.query.lon) { query = "?userId&lat&lon"; const userId = req.query.userId; @@ -233,20 +241,19 @@ app.put("/users", async (req, res) => { const success = await updateUserLocation(userId, lat, lon); if (!success) throw Error(" toggleUserConnectionStatus() failed."); - } else if (req.query.userId && req.query.displayName) { query = "?userId&displayName"; const userId = req.query.userId; if (typeof userId != "string") throw Error(" [userId] is not a string."); const displayName = req.query.displayName; - if (typeof displayName != "string") throw Error(" [displayName] is not a string."); - + if (typeof displayName != "string") + throw Error(" [displayName] is not a string."); + const success = await updateUserDisplayName(userId, displayName); if (!success) throw Error("updateDisplayName() failed."); } console.log(`[EXP] Request returned successfully.`); res.json(`Operation was handled successfully.`); - } catch (error) { console.error( `[EXP] Error returning request :\n`, @@ -292,9 +299,12 @@ app.post("/verify", async (req, res) => { from: "Mailgun Sandbox ", to: email, subject: "Verify your email for echologator", - template: "app email verification" + template: "app email verification", }; - const verifyEmailResponse = await mg.messages.create("sandboxf8629624c26849cf8546cd0bc01ee862.mailgun.org", data) + const verifyEmailResponse = await mg.messages.create( + "sandboxf8629624c26849cf8546cd0bc01ee862.mailgun.org", + data + ); console.log(`[EXP] Request returned successfully.`); res.json(verifyEmailResponse); } @@ -334,11 +344,9 @@ app.listen(express_port, () => { ); }); - //Remove the comments if you want to use the deleter !!!!!! //scheduleCron(); // Begin searching and collecting Garbage (old messages) - // Some old API routes are commented out for now due to breaking type changes. // REST functions