Skip to content

Commit

Permalink
api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuff0 committed Nov 19, 2024
1 parent 0204589 commit 88146c6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 27 deletions.
67 changes: 67 additions & 0 deletions api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Websocket API Protocol

=== Objects ===

"""
ObjectName {
requiredProp: type
optionalProp: type?
}
"""

Location {
lat: float,
lon: float,
geohash: string? (generated if not provided)
}

Message {
author: string,
content: {
text?: string,
attachment?: string,
},
location: Location,
replyTo?: string,
reactions: {
[key: string]: int,
}
}

UserProfile {
displayName: string,
profilePicture: int,
}

===

=== Client -> Server Methods ===

"""
methodName(
arguments / inputs...
) ack -> ackResponse
"""

// Must be called at least once before calling any other methods
updateLocation(
location: Location
ack: func?
) ack -> "success"

sendMessage(
message: Message
ack: func?
) ack -> "success"

getNearbyUsers(
callback: func(nearbyUserUids: { [uid: string]: UserProfile }) -> _,
) callback -> map of nearby user uids to user profiles

// Call after the client has already updated their profile document in firestore
notifyUpdateProfile(
ack: func?
) ack -> "success"


===
4 changes: 4 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { initializeApp } from "firebase-admin/app";
import { startSocketServer } from "./socket_server/socket_server"

// Must be called from google environment (Cloud Run, App Engine, and Cloud Functions)
initializeApp();

startSocketServer();
4 changes: 0 additions & 4 deletions server/src/socket_server/firebase_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ 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();
}
11 changes: 4 additions & 7 deletions server/src/socket_server/methods/get_nearby_users.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { ActiveUser } from "../../types";
import { ActiveUser, UserProfile } from "../../types";
import { getActiveUsersInView } from "../regions";
import { ConnectionContext } from "../socket_server";

const filterOutSenderAndConvertToProfiles = function* (ctx: ConnectionContext, activeUsers: Generator<ActiveUser, any, any>): Generator<any, any, any> {
for (const activeUser of activeUsers) {
if (activeUser.uid === ctx.user.uid) continue;
yield {
uid: activeUser.uid,
profile: activeUser.profile,
};
yield { uid: activeUser.profile }
}
}

export const getNearbyUsers = (ctx: ConnectionContext, callback: (nearbyUserUids: string[]) => void): void => {
export const getNearbyUsers = (ctx: ConnectionContext, callback: (nearbyUserUids: { [uid: string]: UserProfile }) => void): void => {
// Get all users in view
const usersInView = getActiveUsersInView(ctx.user.location);

// Filter out user who send this request and convert ActiveUser objects to uids
const others = filterOutSenderAndConvertToProfiles(ctx, usersInView);

// Response to the sender (also convert generator to array)
callback([...others]);
callback(Object.assign([...others]));
}
2 changes: 1 addition & 1 deletion server/src/socket_server/methods/notify_update_profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const notifyUpdateProfile = async (ctx: ConnectionContext, ack: any): Pro

// forward notification to all nearby users
const usersInView = getActiveUsersInView(ctx.user.location);
for(const userInView of usersInView) {
for (const userInView of usersInView) {
if (userInView.uid === ctx.user.uid) continue; // Skip sender
userInView.socket.emit('notifyUpdateProfile', messageToNearbyOthers);
}
Expand Down
8 changes: 4 additions & 4 deletions server/src/socket_server/socket_server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from "http"
import io from "socket.io";

import { ActiveUser } from "../types";
import { ActiveUser, UserProfile } from "../types";
import * as methods from "./methods";
import { initRegions, removeActiveUser } from "./regions";
import { ensureUserAuthorized, getUserProfile } from "./firebase_methods";
Expand Down Expand Up @@ -81,11 +81,11 @@ export const startSocketServer = () => {

// === METHODS ===

socket.on("ping", (_: any, ack: any) => methods.ping(ctx, ack));
socket.on("ping", (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", (_: any, callback: (nearbyUserUids: string[]) => void) => methods.getNearbyUsers(ctx, callback));
socket.on("notifyUpdateProfile", (_: any, ack: any) => methods.notifyUpdateProfile(ctx, ack));
socket.on("getNearbyUsers", (callback: (nearbyUserUids: { [uid: string]: UserProfile }) => void) => methods.getNearbyUsers(ctx, callback));
socket.on("notifyUpdateProfile", (ack: any) => methods.notifyUpdateProfile(ctx, ack));

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

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

0 comments on commit 88146c6

Please sign in to comment.