Skip to content

Commit

Permalink
Observer fix to limit retrieve messages (#231)
Browse files Browse the repository at this point in the history
* test

* observer fix with limit and smal key error fix
  • Loading branch information
AlexanderWangY authored Aug 30, 2024
1 parent 7cb29ba commit aa34d69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
2 changes: 1 addition & 1 deletion client/app/screens/settings/SettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const SettingsScreen: React.FC = () => {
<Text style={styles.headerText}>Settings</Text>
</View>
{Sections.map(({ header, items }) => (
<View style={styles.section}>
<View style={styles.section} key={header}>
<View style={styles.sectionHeader}>
<Text style={styles.sectionHeaderText}>{header}</Text>
</View>
Expand Down
4 changes: 1 addition & 3 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ dist
build

# Private Key JSON
./firebase-secrets.json
firebase-secrets.json

# Other
.env
build/

# Firebase Secrets
.firebase-secrets.json
108 changes: 58 additions & 50 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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.`);
Expand Down Expand Up @@ -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}.`);
});
Expand All @@ -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;
Expand All @@ -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.");
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 <PUT /users${query}> returned successfully.`);
res.json(`Operation <PUT /users${query}> was handled successfully.`);

} catch (error) {
console.error(
`[EXP] Error returning request <PUT /users${query}>:\n`,
Expand Down Expand Up @@ -292,9 +299,12 @@ app.post("/verify", async (req, res) => {
from: "Mailgun Sandbox <[email protected]>",
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 <POST /verify${query}>returned successfully.`);
res.json(verifyEmailResponse);
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/src/utilities/firebaseInit.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down

0 comments on commit aa34d69

Please sign in to comment.