Skip to content

Commit

Permalink
Fixed bugs in server
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuff0 committed Nov 16, 2024
1 parent 3e1285a commit 0204589
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 75 deletions.
4 changes: 1 addition & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
"description": "",
"dependencies": {
"collections": "^5.1.13",
"express": "^4.21.1",
"firebase-admin": "^12.7.0",
"geofire-common": "^6.0.0",
"socket.io": "^4.8.1"
},
"devDependencies": {
"@types/express": "^5.0.0",
"@types/jest": "^29.5.11",
"@babel/preset-env": "^7.23.8",
"@babel/preset-typescript": "^7.23.3",
Expand All @@ -32,4 +30,4 @@
".js"
]
}
}
}
3 changes: 0 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { startRESTServer } from "./rest_server/rest_server"
import { startSocketServer } from "./socket_server/socket_server"

// Unsure if REST server is even necessary for MVP; auth, managing database can be done on client with https://rnfirebase.io/
//startRESTServer();
startSocketServer();
42 changes: 0 additions & 42 deletions server/src/rest_server/rest_server.ts

This file was deleted.

5 changes: 0 additions & 5 deletions server/src/rest_server/routes/users/post_create.ts

This file was deleted.

5 changes: 0 additions & 5 deletions server/src/rest_server/routes/users/users.ts

This file was deleted.

4 changes: 4 additions & 0 deletions server/src/socket_server/firebase_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const ensureUserAuthorized = async (token: any): Promise<[uid: string | u
}

export const getUserProfile = async (uid: string): Promise<firebase.firestore.DocumentData | undefined> => {
return {
displayName: "Test Display Name",
profilePicture: 12,
}
const userProfileSnapshot = await firebase.firestore().collection('users').doc(uid).get();
return userProfileSnapshot.data();
}
5 changes: 5 additions & 0 deletions server/src/socket_server/methods/send_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { ConnectionContext } from "../socket_server";

// Called when client sends a message
export const sendMessage = (ctx: ConnectionContext, message: Message, ack: any): void => {
if (message.location === undefined) {
message.location = ctx.user.location;
}

const recipients = getActiveUsersInView(message.location);
for (const recipient of recipients) {
if (recipient.uid == ctx.user.uid) continue; // skip sender
recipient.socket.emit("message", message);
}

Expand Down
32 changes: 22 additions & 10 deletions server/src/socket_server/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const viewDistanceMeters = 100;
var regions = new SortedSet(
null,
function keysEqual(a: { key: any; }, b: { key: any; }) {
return a.key === b.key;
return b.key === a.key;
},
function compareKeys(a: { key: string; }, b: { key: any; }) {
return a.key.localeCompare(b.key);
return b.key.localeCompare(a.key);
}
);

Expand All @@ -27,13 +27,18 @@ export const initRegions = () => {
export const setActiveUserRegion = (activeUser: ActiveUser, oldLocation: Location | undefined, newLocation: Location): void => {
if (oldLocation !== undefined) {
// Remove user from previous geohash
delete regions[oldLocation.geohash][activeUser.uid];
delete regions.get({ key: oldLocation.geohash, value: undefined }).value[activeUser.uid];
}
regions[newLocation.geohash][activeUser.uid] = activeUser;
if (!regions.has({ key: newLocation.geohash, value: undefined })) {
regions.push({ key: newLocation.geohash, value: {} })
}
regions.get({ key: newLocation.geohash, value: undefined }).value[activeUser.uid] = activeUser;
};

export const removeActiveUser = (activeUser: ActiveUser): void => {
delete regions[activeUser.location.geohash][activeUser.uid];
if (activeUser.location.geohash == "") return; // User never sent an updateLocation message so they are not in the regions map
//if (!regions.has({ key: activeUser.location.geohash, value: undefined })) return;
delete regions.get({ key: activeUser.location.geohash, value: undefined }).value[activeUser.uid];
}

export const getActiveUsersInView = function* (location: Location): Generator<ActiveUser, any, any> {
Expand All @@ -46,14 +51,20 @@ export const getActiveUsersInView = function* (location: Location): Generator<Ac
// For each geohash range, yield all active users contained in the regions
for (var geohashRange of geohashRanges) {
const leftBoundGeohash = geohashRange[0];
const rightBoundGeohash = geohashRange[0];
const rightBoundGeohash = geohashRange[1];

// Find bottom bound for geohashes that are actual in the map
var geohash = regions.findLeastGreaterThanOrEqual({key: leftBoundGeohash, value: undefined});
var index = regions.indexOf(geohash);
var node = regions.findLeastGreaterThanOrEqual({ key: leftBoundGeohash, value: undefined });
if (node === undefined) continue;

var index = node.index;
var geohash = node.value.key;

if (geohash > rightBoundGeohash) continue;

regions.splayIndex(index);
while (geohash <= rightBoundGeohash) {
const activeUsers = regions[index].value;
const activeUsers = Object.values<any>(regions.root.value.value);
for (const activeUser of activeUsers) {
// Run distance check to ensure users are actually in view, since geofire output is liberal
if (isWithinRadiusMeters(location, activeUser.location, viewDistanceMeters)) {
Expand All @@ -62,7 +73,8 @@ export const getActiveUsersInView = function* (location: Location): Generator<Ac
}

if (index + 1 >= regions.length) break;
geohash = regions[++index];
regions.splayIndex(++index);
geohash = regions.root.value.key;
}
}
}
11 changes: 5 additions & 6 deletions server/src/socket_server/socket_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import http from "http"
import io from "socket.io";

import { ActiveUser } from "../types";
import firebase from "firebase-admin";
import * as methods from "./methods";
import { initRegions, removeActiveUser } from "./regions";
import { ensureUserAuthorized, getUserProfile } from "./firebase_methods";
Expand All @@ -17,7 +16,7 @@ export interface ConnectionContext {
export const startSocketServer = () => {
initRegions();

const port = Number(process.env.socket_port) ?? 8082;
const port = Number(process.env.socket_port ?? "8082");

const httpServer = http.createServer();
const socketServer = new io.Server(httpServer, {
Expand All @@ -40,7 +39,7 @@ export const startSocketServer = () => {
}

console.log(`[WS] User <${uid}> authenticated.`);

//

// === Pull User Profile from Firebase ===
Expand Down Expand Up @@ -82,11 +81,11 @@ export const startSocketServer = () => {

// === METHODS ===

socket.on("ping", (ack: any) => methods.ping(ctx, ack));
socket.on("ping", (_: any, ack: any) => methods.ping(ctx, ack));
socket.on("updateLocation", (location: any, ack: any) => methods.updateLocation(ctx, location, ack))
socket.on("sendMessage", (message: any, ack: any) => methods.sendMessage(ctx, message, ack));
socket.on("getNearbyUsers", (callback: (nearbyUserUids: string[]) => void) => methods.getNearbyUsers(ctx, callback));
socket.on("notifyUpdateProfile", (ack: any) => methods.notifyUpdateProfile(ctx, ack));
socket.on("getNearbyUsers", (_: any, callback: (nearbyUserUids: string[]) => void) => methods.getNearbyUsers(ctx, callback));
socket.on("notifyUpdateProfile", (_: any, ack: any) => methods.notifyUpdateProfile(ctx, ack));

//
});
Expand Down
12 changes: 11 additions & 1 deletion server/src/types/user_profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
export interface UserProfile {
displayName: string,
profilePicture: number,
}
}

// [
// {
// uid: "uid",
// profile: {
// displayName: "Name",
// profilePicture: 2,
// }
// }
// ]

0 comments on commit 0204589

Please sign in to comment.