Skip to content

Conversation

@rixitgithub
Copy link
Contributor

@rixitgithub rixitgithub commented Jul 2, 2025

Summary by CodeRabbit

  • New Features

    • Added a manual "Next Turn" button after the bot's response in debates, requiring user interaction to proceed.
  • Improvements

    • Updated rating terminology across the platform from "EloRating" to "Rating" for clarity and consistency.
    • Enhanced rating calculations and storage to use higher precision for improved accuracy.
    • Simplified and improved error handling in user profile and leaderboard features.
  • Removals

    • Removed text-to-speech functionality and mute controls in the debate room interface.

@coderabbitai
Copy link

coderabbitai bot commented Jul 2, 2025

Walkthrough

The changes unify and update the user rating system across backend and frontend components. Backend controllers, models, and utilities rename EloRating to Rating, introduce float64 precision for ratings, and update related logic for user creation, debate history, and leaderboard. The frontend debate room now requires manual user confirmation to advance after the bot's turn.

Changes

Files/Paths Change Summary
backend/controllers/auth.go, .../rooms.go, .../leaderboard.go, .../user.go Renamed EloRating field to Rating in user-related logic, controllers, and sample data.
backend/controllers/profile_controller.go Switched rating calculations to float64, refactored error handling, unified display name/avatars, and simplified logic.
backend/models/debate.go, .../services/rating_service.go, .../utils/debate.go Renamed fields: UserEmailEmail, EloChangeRatingChange; updated field usage accordingly.
backend/rating/glicko2.go Made Glicko2 config field public (configConfig); updated all internal references.
backend/routes/leaderboard.go Replaced leaderboard route handler logic with a controller call; removed direct DB access from route.
backend/routes/debate.go Updated DB collection access method; cleaned up imports.
backend/utils/populate.go Added explicit config loading; updated rating system initialization and DB access.
frontend/src/Pages/DebateRoom.tsx Introduced manual turn advancement after bot response; removed text-to-speech; disabled input during pending state.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DebateRoom (Frontend)
    participant Bot
    participant Backend

    User->>DebateRoom: Sends message / starts turn
    DebateRoom->>Bot: Sends user message
    Bot-->>DebateRoom: Returns bot response
    DebateRoom->>User: Displays bot response, disables input, shows "Next Turn" button
    User->>DebateRoom: Clicks "Next Turn"
    DebateRoom->>DebateRoom: Advances to next turn, enables input
Loading

Possibly related PRs

  • AOSSIE-Org/DebateAI#99: Introduces or modifies the Glicko-2 rating system and updates user rating fields, directly related to rating logic changes.
  • AOSSIE-Org/DebateAI#86: Implements initial profile, leaderboard, and authentication controllers using Elo rating fields, which are refactored in this PR.

Poem

A bunny hopped through lines of code,
Changing Elo to Rating—what a load!
Float64 now rules the score,
Debate turns wait for clicks, not before.
Leaderboards shine, bot pauses for fun,
Hooray for progress—this hop is done!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Nitpick comments (2)
backend/rating/glicko2.go (1)

52-52: Consider configuration immutability.

Making the Config field public enables external access, which supports the usage pattern in populate.go. However, consider making the configuration immutable after initialization to prevent accidental modifications.

Example approach:

+// GetConfig returns a copy of the configuration to prevent modifications
+func (g *Glicko2) GetConfig() Config {
+    return *g.Config
+}
frontend/src/Pages/DebateRoom.tsx (1)

339-340: Simplify the boolean expression.

The ternary operator is unnecessary when the result is a boolean.

Apply this diff to simplify:

-          isBotTurn:
-            prevState.userStance === phaseSequences[newPhase][0] ? false : true,
+          isBotTurn: prevState.userStance !== phaseSequences[newPhase][0],
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13dde33 and 8e8fc26.

📒 Files selected for processing (13)
  • backend/controllers/auth.go (2 hunks)
  • backend/controllers/leaderboard.go (2 hunks)
  • backend/controllers/profile_controller.go (6 hunks)
  • backend/models/debate.go (1 hunks)
  • backend/rating/glicko2.go (3 hunks)
  • backend/routes/debate.go (2 hunks)
  • backend/routes/leaderboard.go (0 hunks)
  • backend/routes/rooms.go (2 hunks)
  • backend/services/rating_service.go (4 hunks)
  • backend/utils/debate.go (1 hunks)
  • backend/utils/populate.go (1 hunks)
  • backend/utils/user.go (2 hunks)
  • frontend/src/Pages/DebateRoom.tsx (19 hunks)
💤 Files with no reviewable changes (1)
  • backend/routes/leaderboard.go
🧰 Additional context used
🧬 Code Graph Analysis (4)
backend/routes/debate.go (1)
backend/db/db.go (1)
  • MongoDatabase (17-17)
backend/services/rating_service.go (3)
backend/rating/glicko2.go (2)
  • Glicko2 (51-53)
  • Player (21-26)
backend/models/debate.go (1)
  • Debate (10-25)
backend/db/db.go (1)
  • MongoDatabase (17-17)
frontend/src/Pages/DebateRoom.tsx (3)
backend/models/debatevsbot.go (1)
  • Message (6-10)
frontend/src/components/ui/button.tsx (1)
  • Button (57-57)
frontend/src/components/ui/input.tsx (1)
  • Input (25-25)
backend/controllers/profile_controller.go (2)
backend/db/db.go (1)
  • MongoDatabase (17-17)
backend/models/user.go (1)
  • User (10-30)
🪛 Biome (1.9.4)
frontend/src/Pages/DebateRoom.tsx

[error] 340-340: Unnecessary use of boolean literals in conditional expression.

Simplify your code by directly assigning the result without using a ternary operator.
If your goal is negation, you may use the logical NOT (!) or double NOT (!!) operator for clearer and concise code.
Check for more details about NOT operator.
Unsafe fix: Remove the conditional expression with

(lint/complexity/noUselessTernary)

🔇 Additional comments (28)
backend/routes/debate.go (2)

4-4: LGTM: Import updates support the new database access pattern.

The addition of "context" and "arguehub/db" imports aligns with the direct database access pattern being implemented across the codebase.

Also applies to: 9-9


60-60: Direct Database Access Pattern Verified

The search confirmed there are no remaining db.GetCollection calls and all collection usages now use db.MongoDatabase.Collection(). The direct access pattern is consistently applied across the codebase.

backend/utils/debate.go (1)

30-30: LGTM: Consistent field renaming from EloChange to RatingChange.

All sample debate entries have been updated consistently to use the new RatingChange field name, aligning with the broader rating terminology standardization across the codebase.

Also applies to: 37-37, 44-44, 51-51, 58-58

backend/controllers/auth.go (2)

78-78: LGTM: Consistent field renaming in Google login user creation.

The field rename from EloRating to Rating maintains the same initialization value (1200) and aligns with the codebase-wide rating terminology standardization.


147-147: LGTM: Consistent field renaming in sign-up user creation.

The field rename from EloRating to Rating maintains the same initialization value (1200) and aligns with the codebase-wide rating terminology standardization.

backend/routes/rooms.go (2)

67-67: Excellent approach: Preserving database compatibility while updating field naming.

The field rename from EloRating to Rating while maintaining the BSON tag as "eloRating" is a best practice that ensures backward compatibility with existing database records while adopting the new rating terminology in the codebase.


80-80: LGTM: Assignment updated to match the new field name.

The assignment correctly uses user.Rating to match the updated struct field name while maintaining the same functionality.

backend/utils/user.go (1)

24-24: LGTM: Consistent field renaming in sample user data.

All sample users have been updated consistently to use the new Rating field name, maintaining the same rating values while aligning with the codebase-wide rating terminology standardization.

Also applies to: 32-32, 40-40

backend/models/debate.go (1)

13-13: Confirm database migration strategy for renamed field

No remaining code references to the old userEmail field were found, but this change still breaks existing documents and API clients. Please ensure:

  • A migration exists to rename or backfill the BSON field from userEmailemail in all persisted documents
  • API clients that expect userEmail are either updated or the service continues to accept/emit both names until the migration is complete
  • Any tests, fixtures or serialized schemas (e.g. GraphQL, OpenAPI, mock data) are updated to use email
backend/controllers/leaderboard.go (1)

49-49: LGTM on comment update.

The comment correctly reflects the unified rating field naming.

backend/rating/glicko2.go (2)

60-60: LGTM on constructor update.

Correctly updated to match the public field name.


66-68: LGTM on config field access updates.

All method updates correctly reference the public Config field. The logic remains unchanged.

Also applies to: 109-109, 115-115, 121-121, 126-126, 171-171

backend/services/rating_service.go (5)

10-10: LGTM on import cleanup.

Correctly removed unused imports and added the necessary database import.


22-24: LGTM on getter function addition.

The GetRatingSystem() function provides proper access to the singleton rating system instance and follows Go conventions.


66-66: LGTM on field name consistency.

Correctly updated to match the Email field name in the Debate model.


58-58: LGTM on code cleanup.

Minor whitespace cleanup improves readability.


94-94: MongoDatabase initialization verified. The MongoDatabase is properly set in backend/db/db.go:52 via MongoDatabase = client.Database(dbName), and db.ConnectMongoDB(cfg.Database.URI) is called in cmd/server/main.go before any request handlers run. No further action required.

backend/utils/populate.go (3)

23-23: LGTM on database access standardization.

Consistent with the direct database collection access pattern used across the codebase.


31-32: LGTM on rating system initialization.

Proper initialization of the rating service with configuration and use of the new getter function. The pattern ensures the rating system is properly configured before use.


39-41: LGTM on config field access.

Correctly updated to use the public Config field, consistent with the changes in the rating system.

Also applies to: 48-50

frontend/src/Pages/DebateRoom.tsx (6)

162-162: Good addition of manual turn control state.

The nextTurnPending state effectively tracks when the bot has completed its turn and is waiting for user confirmation to proceed.


268-270: Timer logic correctly updated for manual turn control.

The bot turn timer expiration now properly sets nextTurnPending instead of automatically advancing, aligning with the new manual control flow.


357-362: Clean implementation of manual turn advancement.

The handleNextTurn function properly uses the setState callback pattern to ensure state consistency when advancing turns.


424-428: Robust bot response handling.

Excellent error handling - setting nextTurnPending in both success and error cases ensures the user can always advance the debate even if the bot fails.


600-607: Well-implemented Next Turn button UI.

The button is properly positioned, conditionally rendered, and styled with an intuitive green color to indicate the user should proceed.


672-674: Consistent input disabling during pending state.

All user inputs (text input, microphone, send button) are properly disabled when nextTurnPending is true, preventing accidental inputs while waiting for turn advancement.

Also applies to: 686-688, 699-701

backend/controllers/profile_controller.go (2)

20-30: Well-implemented Elo rating calculation.

The function correctly implements the standard Elo rating formula with float64 precision for more accurate calculations. The K-factor of 32 is appropriate for a competitive system.


247-262: Clean debate logging implementation.

Using BSON maps for debate insertion is efficient and the documents properly capture all necessary fields including the float64 rating values.


// PopulateTestUsers inserts sample users into the database
func PopulateTestUsers() {
func PopulateTestUsers1() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Clarify the function rename from PopulateTestUsers to PopulateTestUsers1.

The function renaming to PopulateTestUsers1 may indicate multiple versions, incomplete migration, or testing scenarios. Please clarify if this is intentional or if there are additional related functions that need to be updated consistently.


🏁 Script executed:

#!/bin/bash
# Description: Check for other PopulateTestUsers functions and their usage
# Expected: Understand if there are multiple versions and their usage patterns

# Search for any PopulateTestUsers functions
rg "PopulateTestUsers" --type go -A 2 -B 2

# Search for function calls to understand usage
rg "PopulateTestUsers" --type go

Length of output: 1261


Remove or rename the unused PopulateTestUsers1 helper

It looks like you’ve added a second test-user population function with a “1” suffix that isn’t called anywhere, while the original PopulateTestUsers in backend/utils/populate.go is still being used in backend/cmd/server/main.go. This can be confusing and may be a leftover from testing.

• backend/utils/user.go (around line 14): func PopulateTestUsers1() { … } is never invoked
• backend/utils/populate.go: func PopulateTestUsers() { … } remains the only one used in main.go
• backend/cmd/server/main.go: still calls utils.PopulateTestUsers(), not the new variant

Please clarify your intent:

  • If PopulateTestUsers1 contains improved logic, replace the call in main.go (and remove the old version) or give it a descriptive name.
  • Otherwise, remove PopulateTestUsers1 to avoid dead code and confusion.
🤖 Prompt for AI Agents
In backend/utils/user.go at line 14, the function PopulateTestUsers1 is defined
but never called, causing dead code and potential confusion. Review the
function's logic: if it improves upon the existing PopulateTestUsers in
backend/utils/populate.go, update main.go to call PopulateTestUsers1 instead and
remove the old function; otherwise, delete PopulateTestUsers1 entirely to clean
up unused code.

Rank: i + 1,
Name: name,
Score: user.EloRating,
Score: int(user.Rating),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify database field consistency.

The code uses user.Rating (line 86) but the MongoDB query sorts by eloRating (line 51). This inconsistency suggests either:

  1. The database field is still named eloRating but the Go struct uses Rating
  2. The query needs updating to use rating

Ensure the BSON tags in the User model match the actual database field names.


🏁 Script executed:

#!/bin/bash
# Description: Check User model BSON tags and database field consistency
# Expected: Verify the actual field name used in database vs Go struct

echo "=== Checking User model rating field BSON tags ==="
ast-grep --pattern 'type User struct {
  $$$
}'

echo "=== Checking for rating field references ==="
rg -A 2 -B 2 "eloRating|rating.*bson" --type go

Length of output: 5182


Ensure consistent use of rating instead of eloRating.

Your User model now uses bson:"rating", but the code still references eloRating in multiple places. Update all of these to avoid mismatches at query and marshalling time:

• backend/routes/rooms.go
– Change the struct tag on the Rating field:
diff - Rating int `bson:"eloRating"` + Rating int `bson:"rating"`

• backend/controllers/leaderboard.go
– Update the sort key in findOptions:
diff - SetSort(bson.D{{"eloRating", -1}}) + SetSort(bson.D{{"rating", -1}})

• backend/controllers/profile_controller.go
– Change all occurrences of "eloRating" to "rating" in:
• the initial Find().SetSort(...) call
• the BSON payload in Create/Log operations ("eloRating": int(user.Rating))
• the UpdateOne calls ("$set": bson.M{"eloRating": newWinnerElo}{"rating": newWinnerElo})
• the logged document fields ("eloRating": newWinnerElo"rating": newWinnerElo)

After updating, verify your MongoDB collection’s field name (and any existing indexes) have been migrated from eloRating to rating.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In backend/controllers/leaderboard.go at line 86 and related files, replace all
references to "eloRating" with "rating" to match the updated User model's BSON
tag. Specifically, update the struct tag in backend/routes/rooms.go, change the
sort key in findOptions in backend/controllers/leaderboard.go, and modify all
occurrences in backend/controllers/profile_controller.go including
Find().SetSort calls, BSON payloads in Create/Log operations, UpdateOne calls,
and logged document fields. After these code changes, verify that the MongoDB
collection fields and indexes have been migrated from "eloRating" to "rating" to
ensure consistency and prevent query or marshalling errors.

Comment on lines +18 to +21
cfg, err := config.LoadConfig("./config/config.prod.yml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make config path environment-aware.

While explicit config loading is good, hardcoding the production config path (config.prod.yml) may not be appropriate for all environments. Consider making this configurable based on environment variables or build tags.

-cfg, err := config.LoadConfig("./config/config.prod.yml")
+configPath := os.Getenv("CONFIG_PATH")
+if configPath == "" {
+    configPath = "./config/config.dev.yml" // default for development
+}
+cfg, err := config.LoadConfig(configPath)
🤖 Prompt for AI Agents
In backend/utils/populate.go around lines 18 to 21, the config file path is
hardcoded to "./config/config.prod.yml", which is not flexible for different
environments. Modify the code to read the config file path from an environment
variable, falling back to a default if the variable is not set. This will make
the config loading environment-aware and adaptable to various deployment
scenarios.

Comment on lines 69 to 72
dbCtx,
bson.M{},
options.Find().SetSort(bson.D{{"eloRating", -1}}).SetLimit(5),
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix field name inconsistency in leaderboard query.

The query sorts by "eloRating" but based on the User model, the field is named "rating".

Apply this diff to fix the field name:

-		options.Find().SetSort(bson.D{{"eloRating", -1}}).SetLimit(5),
+		options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(5),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbCtx,
bson.M{},
options.Find().SetSort(bson.D{{"eloRating", -1}}).SetLimit(5),
)
dbCtx,
bson.M{},
options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(5),
)
🤖 Prompt for AI Agents
In backend/controllers/profile_controller.go around lines 69 to 72, the
leaderboard query sorts by the field "eloRating" which is inconsistent with the
User model where the field is named "rating". Update the sort field name in the
options.Find().SetSort call from "eloRating" to "rating" to fix this
inconsistency.

Comment on lines +242 to +243
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"eloRating": newWinnerElo}})
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"eloRating": newLoserElo}})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix field name inconsistency in rating updates.

The updates use "eloRating" but the User model field is "rating".

Apply this diff to fix the field names:

-	db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"eloRating": newWinnerElo}})
-	db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"eloRating": newLoserElo}})
+	db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"rating": newWinnerElo}})
+	db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"rating": newLoserElo}})
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"eloRating": newWinnerElo}})
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"eloRating": newLoserElo}})
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"rating": newWinnerElo}})
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"rating": newLoserElo}})
🤖 Prompt for AI Agents
In backend/controllers/profile_controller.go around lines 242 to 243, the field
name used in the UpdateOne calls is "eloRating" but it should be "rating" to
match the User model. Change the field name in both update queries from
"eloRating" to "rating" to ensure consistency and correct database updates.

Comment on lines +229 to +234
winnerID, _ := primitive.ObjectIDFromHex(req.WinnerID)
loserID, _ := primitive.ObjectIDFromHex(req.LoserID)

var winner, loser models.User
if err = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": winnerObjID}).Decode(&winner); err != nil {
if err == mongo.ErrNoDocuments {
ctx.JSON(http.StatusNotFound, gin.H{"error": "Winner not found"})
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching winner from DB"})
}
return
}
_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": winnerID}).Decode(&winner)
_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": loserID}).Decode(&loser)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for user fetching.

The function doesn't check if ObjectID parsing succeeds or if users exist. This could lead to incorrect Elo calculations with zero-valued ratings.

Apply this diff to add proper error handling:

-	winnerID, _ := primitive.ObjectIDFromHex(req.WinnerID)
-	loserID, _ := primitive.ObjectIDFromHex(req.LoserID)
+	winnerID, err := primitive.ObjectIDFromHex(req.WinnerID)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid winner ID"})
+		return
+	}
+	loserID, err := primitive.ObjectIDFromHex(req.LoserID)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loser ID"})
+		return
+	}

 	var winner, loser models.User
-	_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": winnerID}).Decode(&winner)
-	_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": loserID}).Decode(&loser)
+	if err := db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": winnerID}).Decode(&winner); err != nil {
+		ctx.JSON(http.StatusNotFound, gin.H{"error": "Winner not found"})
+		return
+	}
+	if err := db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": loserID}).Decode(&loser); err != nil {
+		ctx.JSON(http.StatusNotFound, gin.H{"error": "Loser not found"})
+		return
+	}
🤖 Prompt for AI Agents
In backend/controllers/profile_controller.go around lines 229 to 234, the code
does not check for errors when parsing ObjectIDs or fetching users from the
database, which can cause issues if invalid IDs are provided or users are not
found. Update the code to handle errors returned by ObjectIDFromHex and
FindOne(). If parsing fails or a user is not found, return an appropriate error
response or handle the error to prevent proceeding with zero-valued user data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants