Skip to content

refactor(user): enhance user and group handling in modify_user - #271

Merged
x0sina merged 1 commit into
devfrom
pref/user-modify
Feb 15, 2026
Merged

x0sina merged 1 commit into
devfrom
pref/user-modify

Conversation

@x0sina

@x0sina x0sina commented Feb 15, 2026 •

Copy link
Copy Markdown
Collaborator
  • Updated modify_user to accept pre-validated groups for improved efficiency.
  • Refactored get_groups_by_ids to handle empty group IDs and preserve input order.
  • Introduced _inbounds_from_loaded_groups to optimize inbound retrieval from loaded groups in user serialization.
  • Adjusted user serialization logic to utilize loaded inbounds when available.

Summary by CodeRabbit

  • Chores
    • Optimized database query performance through implementation of batched loading operations for user and group relationships, reducing unnecessary database overhead and improving system efficiency.
    • Refactored internal group relationship handling during user modification to streamline operations and reduce database calls.
    • Enhanced data loading strategies with selective eager-loading techniques for improved overall system performance.

…rialization functions

- Updated `modify_user` to accept pre-validated groups for improved efficiency.
- Refactored `get_groups_by_ids` to handle empty group IDs and preserve input order.
- Introduced `_inbounds_from_loaded_groups` to optimize inbound retrieval from loaded groups in user serialization.
- Adjusted user serialization logic to utilize loaded inbounds when available.
@x0sina x0sina added the Backend label Feb 15, 2026
@coderabbitai

coderabbitai Bot commented Feb 15, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The changes optimize group and user data loading patterns by introducing eager-loading with batched queries, eliminating redundant database calls. User modification now accepts optional group data, and inbounds derivation is shifted from lazy database lookups to extraction from pre-loaded group relationships.

Changes

Cohort / File(s) Summary
Database Query Optimization
app/db/crud/group.py
Added selectinload eager-loading for Group.users and Group.inbounds with conditional flags. Replaced per-item attribute loading with single batched query and explicit mapping to preserve input order.
User Modification Flow
app/db/crud/user.py, app/operation/user.py
Extended modify_user signature to accept optional groups parameter. Added validated_groups variable in _modify_user to capture validation results. Updated remove_user validation to exclude subscription URL.
Inbounds Derivation
app/node/user.py
Introduced _inbounds_from_loaded_groups helper to extract inbounds from non-disabled groups. Updated serialize_user and serialize_users_for_node to prefer pre-loaded group data over database lookups with fallback support.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 Batched queries hop with glee,
No more visits, one and three,
Groups pre-loaded, swift and bright,
Inbounds flow from loaded light, ✨
Database dreams now optimized!

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: refactoring user and group handling, specifically enhancing the modify_user function with optimizations for group handling and eager-loading.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into dev

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pref/user-modify

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
app/db/crud/user.py (1)

776-777: groups or ... is falsy-fragile for empty lists.

If groups is [] (e.g., caller intentionally wants to clear all groups), the or falls through to get_groups_by_ids, ignoring the caller's intent. Currently safe because _modify_user only passes validated groups when group_ids is non-empty, but this is a subtle trap for future callers.

Suggested safer alternative
     if modify.group_ids:
-        db_user.groups = groups or await get_groups_by_ids(db, modify.group_ids, load_users=False, load_inbounds=True)
+        db_user.groups = groups if groups is not None else await get_groups_by_ids(db, modify.group_ids, load_users=False, load_inbounds=True)

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@x0sina

x0sina commented Feb 15, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Feb 15, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@x0sina
x0sina merged commit 7a8b4b3 into dev Feb 15, 2026
14 checks passed
@x0sina
x0sina deleted the pref/user-modify branch February 16, 2026 14:26
@coderabbitai coderabbitai Bot mentioned this pull request Jul 20, 2026
6 of 11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant