From 88146c6def332415291ea3a0ea6a63ade39f2625 Mon Sep 17 00:00:00 2001 From: willhuff0 Date: Tue, 19 Nov 2024 17:46:59 -0500 Subject: [PATCH] api docs --- api.txt | 67 +++++++++++++++++++ server/src/index.ts | 4 ++ server/src/socket_server/firebase_methods.ts | 4 -- .../socket_server/methods/get_nearby_users.ts | 11 ++- .../methods/notify_update_profile.ts | 2 +- server/src/socket_server/socket_server.ts | 8 +-- server/src/types/user_profile.ts | 12 +--- 7 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 api.txt diff --git a/api.txt b/api.txt new file mode 100644 index 00000000..6a47ade2 --- /dev/null +++ b/api.txt @@ -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" + + +=== \ No newline at end of file diff --git a/server/src/index.ts b/server/src/index.ts index 46149f66..a497176f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -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(); diff --git a/server/src/socket_server/firebase_methods.ts b/server/src/socket_server/firebase_methods.ts index 7463fd3c..279b41d8 100644 --- a/server/src/socket_server/firebase_methods.ts +++ b/server/src/socket_server/firebase_methods.ts @@ -16,10 +16,6 @@ export const ensureUserAuthorized = async (token: any): Promise<[uid: string | u } export const getUserProfile = async (uid: string): Promise => { - return { - displayName: "Test Display Name", - profilePicture: 12, - } const userProfileSnapshot = await firebase.firestore().collection('users').doc(uid).get(); return userProfileSnapshot.data(); } \ No newline at end of file diff --git a/server/src/socket_server/methods/get_nearby_users.ts b/server/src/socket_server/methods/get_nearby_users.ts index 2f4ee66f..11b93aa7 100644 --- a/server/src/socket_server/methods/get_nearby_users.ts +++ b/server/src/socket_server/methods/get_nearby_users.ts @@ -1,18 +1,15 @@ -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): Generator { 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); @@ -20,5 +17,5 @@ export const getNearbyUsers = (ctx: ConnectionContext, callback: (nearbyUserUids const others = filterOutSenderAndConvertToProfiles(ctx, usersInView); // Response to the sender (also convert generator to array) - callback([...others]); + callback(Object.assign([...others])); } \ No newline at end of file diff --git a/server/src/socket_server/methods/notify_update_profile.ts b/server/src/socket_server/methods/notify_update_profile.ts index 0fb7199f..7a99e65c 100644 --- a/server/src/socket_server/methods/notify_update_profile.ts +++ b/server/src/socket_server/methods/notify_update_profile.ts @@ -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); } diff --git a/server/src/socket_server/socket_server.ts b/server/src/socket_server/socket_server.ts index fce22993..16faecda 100644 --- a/server/src/socket_server/socket_server.ts +++ b/server/src/socket_server/socket_server.ts @@ -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"; @@ -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)); // }); diff --git a/server/src/types/user_profile.ts b/server/src/types/user_profile.ts index a30a8d5c..99330df0 100644 --- a/server/src/types/user_profile.ts +++ b/server/src/types/user_profile.ts @@ -1,14 +1,4 @@ export interface UserProfile { displayName: string, profilePicture: number, -} - -// [ -// { -// uid: "uid", -// profile: { -// displayName: "Name", -// profilePicture: 2, -// } -// } -// ] \ No newline at end of file +} \ No newline at end of file