Skip to content

Commit

Permalink
Fix multi-user argument adding updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sofvanh committed Dec 4, 2024
1 parent e7415b3 commit 4ce5e9b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backend/src/websocket/argumentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions backend/src/websocket/updateHandler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/hooks/useGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 4ce5e9b

Please sign in to comment.