From 4ce5e9b8f66a87691cd9c941e8d2730536fe39b0 Mon Sep 17 00:00:00 2001 From: sofvanh <23138848+sofvanh@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:34:13 +0000 Subject: [PATCH] Fix multi-user argument adding updates --- backend/src/websocket/argumentHandler.ts | 5 +++-- backend/src/websocket/updateHandler.ts | 10 ++++++++++ frontend/src/hooks/useGraph.ts | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/src/websocket/argumentHandler.ts b/backend/src/websocket/argumentHandler.ts index 77b41e3..955d7dd 100644 --- a/backend/src/websocket/argumentHandler.ts +++ b/backend/src/websocket/argumentHandler.ts @@ -3,6 +3,7 @@ import { addArgument } from '../db/operations/argumentOperations'; import { getGraphDataWithUserReactions } from '../db/operations/graphOperations'; import { updateGraphEdges } from '../db/operations/edgeOperations'; import { embedText, generateTopKSimilarEdges } from '../embeddingHandler'; +import { sendNewArgumentUpdate } from './updateHandler'; export const handleAddArgument = async ( socket: Socket, @@ -32,8 +33,8 @@ export const handleAddArgument = async ( const newEdges = generateTopKSimilarEdges(graph); await updateGraphEdges(graphId, newEdges); - const updatedGraph = await getGraphDataWithUserReactions(graphId, socket.data.user.id); // TODO This is also broken (shouldn't be using author user id for updating everyone) - io.to(graphId).emit('graph update', updatedGraph); + sendNewArgumentUpdate(io, graphId, newArgument, newEdges); + // TODO Make author 'agree' with their own argument callback?.({ success: true, argument: newArgument }); } catch (error) { console.error('Error adding argument:', error); diff --git a/backend/src/websocket/updateHandler.ts b/backend/src/websocket/updateHandler.ts index d39273d..1ffbb6e 100644 --- a/backend/src/websocket/updateHandler.ts +++ b/backend/src/websocket/updateHandler.ts @@ -1,8 +1,18 @@ import { Socket } from "socket.io"; import { getArgumentScores } from "../analysis/argumentScoreHandler"; import { getReactionCountsForArgument, getUserReactionForArgument } from "../db/operations/reactionOperations"; +import { Argument, Edge } from "../.shared/types"; +export const sendNewArgumentUpdate = async ( + io: any, + graphId: string, + argument: Argument, + newEdges: Edge[] +) => { + io.to(graphId).emit('argument added', { argument, newEdges }); +} + export const sendReactionUpdate = async ( socket: Socket, io: any, diff --git a/frontend/src/hooks/useGraph.ts b/frontend/src/hooks/useGraph.ts index 9ddb54b..e3cdbe8 100644 --- a/frontend/src/hooks/useGraph.ts +++ b/frontend/src/hooks/useGraph.ts @@ -19,6 +19,12 @@ export function useGraph(graphId: string) { socket?.emit('join graph', graphId); socket?.on('graph data', setGraph); socket?.on('graph update', setGraph); + socket?.on('argument added', ({ argument, newEdges }) => { + setGraph(prevGraph => { + if (!prevGraph) return prevGraph; + return { ...prevGraph, arguments: [...prevGraph.arguments, argument], edges: newEdges }; + }); + }); socket?.on('user reaction update', ({ argumentId, userReaction }: { argumentId: string, userReaction: UserReaction }) => { setGraph(prevGraph => { if (!prevGraph) return prevGraph; @@ -51,6 +57,7 @@ export function useGraph(graphId: string) { socket?.emit('leave graph', graphId); socket?.off('graph data'); socket?.off('graph update'); + socket?.off('argument added'); socket?.off('user reaction update'); socket?.off('argument reactions update'); socket?.off('graph scores update');