Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and retrieve display name #193

Merged
merged 15 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading