Skip to content

Commit

Permalink
Update and retrieve display name (#193)
Browse files Browse the repository at this point in the history
* Rename file to avoid confusion

* Add function to get display name

* Add function to update display name

* Add socket hooks to activate based on display name related actions

* Update dependencies in lockfile

* Move all functions related to getting user info into one file

* Make some socket calls REST API calls to avoid overloading traffic

* Send JSON response on success

* Fix error fetching connected user info

* Re-added mailgun import

* variable name updates and other minor fixes

* Changed getConnectedUserDisplayName() to getConnectedUser(), and return all ConnectedUser document information. Updated /users?userId GET endpoint to match this change.

* Minor comment change

---------

Co-authored-by: h1divp <[email protected]>
  • Loading branch information
aaditkamat and h1divp authored Mar 20, 2024
1 parent 0c14ef7 commit 93b848b
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 20 deletions.
90 changes: 90 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions server/src/actions/getConnectedUsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { distanceBetween, geohashForLocation, geohashQueryBounds } from 'geofire-common'
import { connectedUsersCollection } from '../utilities/firebaseInit'

export const getConnectedUser = async (socketID: string) => {
try {
const user = await connectedUsersCollection.doc(socketID).get();
return user.data();
} catch (error) {
console.error("getConnectedUser failed:", error.message);
return false;
}
}

export const findNearbyUsers = async (centerLat: number, centerLon: number, radius: number) => {
// Return an array of nearby userIds (which are also socket ids) given a center latitude and longitude.
// Latitude and longitude values use degrees with the same bounds as GeoPoints. Radius values use meters.
Expand Down
10 changes: 10 additions & 0 deletions server/src/actions/updateConnectedUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export const updateUserLocation = async (socketID: string, lat: number, lon: num
return false
}
}

export const updateUserDisplayName = async (socketID: string, displayName: string) => {
try {
await connectedUsersCollection.doc(socketID).update({ displayName: displayName })
return true
} catch (error) {
console.error(error.message)
return false
}
}
60 changes: 40 additions & 20 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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,
} from "./actions/updateConnectedUser";
import { deleteConnectedUserByUID } from "./actions/deleteConnectedUser";
import { geohashForLocation } from "geofire-common";
import { findNearbyUsers } from "./actions/getConnectedUsers";
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";

const { createServer } = require("http");
Expand Down Expand Up @@ -143,9 +140,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 @@ -168,8 +164,20 @@ app.get("/users", async (req, res) => {
const radius = Number(req.query.radius);

const userIds = await findNearbyUsers(lat, lon, radius);
console.log(userIds);
res.json(userIds);

} else if (req.query.userId) {
query = "?userId";
const userId = req.query.userId;
if (typeof userId != "string") throw Error(" [userId] is not a string.");

const user = await getConnectedUser(userId);
if (user) {
res.json(user);
} else {
// getConnectedUserDisplayName() will return false is an error is thrown, and print it to console.
throw Error("getConnectedUser() failed.");
}
}
} catch (error) {
console.error(
Expand Down Expand Up @@ -219,6 +227,7 @@ 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 @@ -230,9 +239,20 @@ 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.");

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 /user${query}> was handled successfully.`);
res.json(`Operation <PUT /users${query}> was handled successfully.`);

} catch (error) {
console.error(
`[EXP] Error returning request <PUT /users${query}>:\n`,
Expand Down

0 comments on commit 93b848b

Please sign in to comment.