Repository navigation
fix: resolve debate opponents from room state #812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,10 +55,6 @@ func SubmitTranscripts( | |
| role string, | ||
| email string, | ||
| transcripts map[string]string, | ||
| opponentRole string, | ||
| opponentID string, | ||
| opponentEmail string, | ||
| opponentTranscripts map[string]string, | ||
| ) (map[string]interface{}, error) { | ||
| roomID = strings.ToLower(strings.TrimSpace(roomID)) | ||
| if roomID == "" { | ||
|
|
@@ -142,26 +138,47 @@ func SubmitTranscripts( | |
| }, nil | ||
| } | ||
| } else { | ||
| if len(transcripts) > 0 { | ||
| if err := upsertTranscript(ctx, transcriptCollection, roomID, role, email, transcripts); err != nil { | ||
| return nil, err | ||
| } | ||
| if role != "for" && role != "against" { | ||
| return nil, fmt.Errorf("invalid debate role %q", role) | ||
|
Comment on lines
+141
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate the submitted side against the side assigned to the caller. The non-team path accepts any Before the upsert, reject any submission whose
|
||
| } | ||
|
|
||
| if opponentRole != "" && len(opponentTranscripts) > 0 { | ||
| resolvedEmail := opponentEmail | ||
| if resolvedEmail == "" && opponentID != "" { | ||
| if lookup, lookupErr := getUserEmailByID(ctx, opponentID); lookupErr == nil { | ||
| resolvedEmail = lookup | ||
| } | ||
| } | ||
| if resolvedEmail == "" && opponentID != "" { | ||
| resolvedEmail = opponentID | ||
| // The opponent is resolved from the room, never from the request. Only the | ||
| // authenticated participant may submit their own side of the debate. | ||
| participants, err := loadRoomParticipants(ctx, roomID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| caller, err := resolveRoomCaller(ctx, participants, email) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := validateSubmittedRole(caller.Role, role); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := validateTranscriptPhases(role, transcripts); err != nil { | ||
| return nil, err | ||
| } | ||
| opponent, err := resolveRoomOpponent(participants, caller) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opponentEmail := opponent.Email | ||
| if opponentEmail == "" { | ||
| opponentEmail, err = getUserEmailByID(ctx, opponent.ID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to resolve opponent identity: %w", err) | ||
| } | ||
| if resolvedEmail != "" { | ||
| if err := upsertTranscript(ctx, transcriptCollection, roomID, opponentRole, resolvedEmail, opponentTranscripts); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| if opponentEmail == "" || strings.EqualFold(opponentEmail, email) { | ||
| return nil, errors.New("opponent must be a different room participant") | ||
| } | ||
| if err := verifyExistingSubmissions(ctx, transcriptCollection, roomID, role, email, opponentEmail); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(transcripts) > 0 { | ||
| if err := upsertTranscript(ctx, transcriptCollection, roomID, role, email, transcripts); err != nil { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -566,6 +583,31 @@ func saveTeammateTranscripts( | |
| return nil | ||
| } | ||
|
|
||
| func validateSubmittedRole(assignedRole, submittedRole string) error { | ||
| assignedRole = strings.ToLower(strings.TrimSpace(assignedRole)) | ||
| if assignedRole == "" { | ||
| return nil | ||
| } | ||
| if submittedRole != assignedRole { | ||
| return fmt.Errorf("role %q does not match your assigned side %q", submittedRole, assignedRole) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func validateTranscriptPhases(role string, transcripts map[string]string) error { | ||
| allowed, ok := roleAllowedPhases[role] | ||
| if !ok { | ||
| return fmt.Errorf("invalid debate role %q", role) | ||
| } | ||
| for phase := range transcripts { | ||
| if allowed[phase] || phase == "topic" { | ||
| continue | ||
| } | ||
| return fmt.Errorf("phase %q is not allowed for role %q", phase, role) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| var roleAllowedPhases = map[string]map[string]bool{ | ||
| "for": { | ||
| "openingFor": true, | ||
|
|
@@ -713,6 +755,102 @@ func mergeTeamTranscripts(submissions ...map[string]string) map[string]string { | |
| return merged | ||
| } | ||
|
|
||
| type roomParticipant struct { | ||
| ID string `bson:"id"` | ||
| Email string `bson:"email,omitempty"` | ||
| Role string `bson:"role,omitempty"` | ||
| } | ||
|
|
||
| func loadRoomParticipants(ctx context.Context, roomID string) ([]roomParticipant, error) { | ||
| if db.MongoDatabase == nil { | ||
| return nil, errors.New("database is not initialized") | ||
| } | ||
| var room struct { | ||
| Participants []roomParticipant `bson:"participants"` | ||
| } | ||
| if err := db.MongoDatabase.Collection("rooms").FindOne(ctx, bson.M{"_id": roomID}).Decode(&room); err != nil { | ||
| if errors.Is(err, mongo.ErrNoDocuments) { | ||
| return nil, errors.New("debate room not found") | ||
| } | ||
| return nil, fmt.Errorf("failed to load debate room: %w", err) | ||
| } | ||
| if len(room.Participants) == 0 { | ||
| return nil, errors.New("debate room has no participants") | ||
| } | ||
| return room.Participants, nil | ||
| } | ||
|
|
||
| func resolveRoomCaller(ctx context.Context, participants []roomParticipant, email string) (roomParticipant, error) { | ||
| if db.MongoDatabase == nil { | ||
| return roomParticipant{}, errors.New("database is not initialized") | ||
| } | ||
| var user models.User | ||
| if err := findUserByIdentifier(ctx, db.MongoDatabase.Collection("users"), email, &user); err != nil { | ||
| return roomParticipant{}, errors.New("authenticated user is not registered") | ||
| } | ||
| for _, participant := range participants { | ||
| if participant.ID != "" && participant.ID == user.ID.Hex() { | ||
| return participant, nil | ||
| } | ||
| if participant.Email != "" && strings.EqualFold(participant.Email, user.Email) { | ||
| return participant, nil | ||
| } | ||
| } | ||
| return roomParticipant{}, errors.New("authenticated user is not a participant of this room") | ||
| } | ||
|
|
||
| func sameRoomParticipant(a, b roomParticipant) bool { | ||
| if a.ID != "" && b.ID != "" { | ||
| return a.ID == b.ID | ||
| } | ||
| return a.Email != "" && b.Email != "" && strings.EqualFold(a.Email, b.Email) | ||
| } | ||
|
|
||
| func resolveRoomOpponent(participants []roomParticipant, caller roomParticipant) (roomParticipant, error) { | ||
| var opponent roomParticipant | ||
| found := false | ||
| for _, participant := range participants { | ||
| if sameRoomParticipant(participant, caller) { | ||
| continue | ||
| } | ||
| if found { | ||
| return roomParticipant{}, errors.New("debate room must have exactly two participants") | ||
| } | ||
| opponent = participant | ||
| found = true | ||
| } | ||
| if !found { | ||
| return roomParticipant{}, errors.New("debate room has no opponent participant") | ||
| } | ||
| return opponent, nil | ||
| } | ||
|
|
||
| func verifyExistingSubmissions(ctx context.Context, collection *mongo.Collection, roomID, role, email, opponentEmail string) error { | ||
| for _, existingRole := range []string{"for", "against"} { | ||
| var existing models.DebateTranscript | ||
| err := collection.FindOne(ctx, bson.M{"roomId": roomID, "role": existingRole}).Decode(&existing) | ||
| if errors.Is(err, mongo.ErrNoDocuments) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if existingRole == role { | ||
| if !strings.EqualFold(existing.Email, email) { | ||
| return errors.New("this side already has a submission from another participant") | ||
| } | ||
| continue | ||
| } | ||
| if strings.EqualFold(existing.Email, email) { | ||
| return errors.New("authenticated user already submitted the opposing side") | ||
| } | ||
| if !strings.EqualFold(existing.Email, opponentEmail) { | ||
| return errors.New("opposing side was submitted by a non-participant") | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getUserEmailByID(ctx context.Context, userID string) (string, error) { | ||
| if userID == "" { | ||
| return "", errors.New("empty user ID") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -722,6 +722,35 @@ func handleLiveTranscript(room *Room, conn *websocket.Conn, message Message, cli | |
| } | ||
|
|
||
| // handlePhaseChange handles phase changes | ||
| func persistRoomDebaterRoles(roomID string, room *Room) { | ||
| if db.MongoDatabase == nil { | ||
| return | ||
| } | ||
|
|
||
| participants := make([]bson.M, 0, len(room.Clients)) | ||
| for _, client := range room.Clients { | ||
| if client.IsSpectator { | ||
| continue | ||
| } | ||
| participants = append(participants, bson.M{ | ||
| "id": client.UserID, | ||
| "email": client.Email, | ||
| "displayName": client.Username, | ||
| "role": client.Role, | ||
| }) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
| if _, err := db.MongoDatabase.Collection("rooms").UpdateOne( | ||
| ctx, | ||
| bson.M{"_id": roomID}, | ||
| bson.M{"$set": bson.M{"participants": participants}}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check persisted room membership before admitting a debater to
Reject a debater connection unless the caller is already a persisted participant of the room. Before choosing the check, confirm which stored field the join flow writes, so the check reads the same source.
|
||
| ); err != nil { | ||
| log.Printf("[ws] failed to persist debater roles: room=%s err=%v", roomID, err) | ||
| } | ||
| } | ||
|
|
||
| func handlePhaseChange(room *Room, conn *websocket.Conn, message Message, roomID string) { | ||
| type muteUpdate struct { | ||
| client *Client | ||
|
|
@@ -784,6 +813,7 @@ func handlePhaseChange(room *Room, conn *websocket.Conn, message Message, roomID | |
| room.DebaterRoles[client.UserID] = client.Role | ||
| } | ||
| } | ||
| persistRoomDebaterRoles(roomID, room) | ||
| } | ||
| currentTurn := debatePhaseRole(room.Phase) | ||
| muteUpdates := make([]muteUpdate, 0, len(room.Clients)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-owners never submit their own transcript, so non-team judgment cannot start.
With the opponent fields removed, the service stores only the caller's own side. Judgment runs only after both the
forandagainstsubmissions exist. In a non-team room, only the owner submits transcripts. A non-owner only polls, and that poll sendstranscripts: {}, which stores nothing. The second side is never written, and the debate stays in "Waiting for opponent submission".Each participant must submit their own side:
logMessageHistory, submitgatherTranscriptsForRole(localRole)before starting the poll.!isRoomOwnerearly return fromsendTranscriptsToBackendso non-owners can call it.opponentRole,opponentId,opponentEmailandopponentTranscriptsfrom the request body.Do not restore client-supplied opponent transcripts. The server must keep using only the authenticated caller's side.