Skip to content

feat: Create a user API to synchronously return the user ID and simul… - #2251

Closed
bddiudiu wants to merge 40 commits into
QuantumNous:mainfrom
bddiudiu:feat_create_user
Closed

feat: Create a user API to synchronously return the user ID and simul…#2251
bddiudiu wants to merge 40 commits into
QuantumNous:mainfrom
bddiudiu:feat_create_user

Conversation

@bddiudiu

@bddiudiu bddiudiu commented Nov 20, 2025

Copy link
Copy Markdown
Contributor

Create a user API to synchronously return the user ID and simultaneously generate a default token according to the configuration.

Summary by CodeRabbit

  • New Features

    • Automatic token generation now occurs when a user is created (or registered) if enabled.
    • API response for user creation now includes the created user's ID.
  • Bug Fixes

    • User group from input is correctly assigned during user creation.
  • Chores

    • Continuous integration updated to use a new Docker image repository name.

✏️ Tip: You can customize this high-level summary in your review settings.

…taneously generate a default token according to the configuration.

(cherry picked from commit 90ea83a)
@coderabbitai

coderabbitai Bot commented Nov 20, 2025

Copy link
Copy Markdown
Contributor

Caution

Review failed

The head commit changed during the review from 8837132 to f8e4278.

Walkthrough

CreateUser now assigns input Group to the created user, returns the created userId in JSON, and — when constant.GenerateDefaultToken is true — generates and inserts a default token after inserting the user. Register keeps its existing token-creation behavior. The Docker workflow file was updated to replace the Docker image repository name with a new one.

Changes

Cohort / File(s) Change Summary
User controller: token & response updates
controller/user.go
In CreateUser: assign Group from input to the sanitized user, include userId in the JSON response, and conditionally generate & insert a default token after user insertion when constant.GenerateDefaultToken is true (key generation, token fields: UserId, Name, Key, timestamps, quotas, Group, optional auto-group assignment, and error handling). Register behavior for token creation remains.
CI workflow: image repository rename
.github/workflows/docker-image-arm64.yml
Replaced Docker image repository references calciumion/new-apibddiudiu/new-api across metadata extraction, single-arch build tags, and manifest creation/push steps; no structural changes to workflow logic.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller as controller/user.go
    participant DB as Database

    Client->>Controller: CreateUser / Register request (may include Group)
    Controller->>DB: Insert user record
    activate DB
    DB-->>Controller: Insert result (userId)
    deactivate DB

    alt constant.GenerateDefaultToken == true
        Controller->>Controller: Generate token key & build token payload (UserId, Name, Key, timestamps, quotas, Group)
        Controller->>DB: Insert default token
        activate DB
        DB-->>Controller: Token insert result
        deactivate DB
        alt Token insert failed
            Controller-->>Client: Error response (token error)
        else Token insert succeeded
            Controller-->>Client: Success response (includes userId)
        end
    else
        Controller-->>Client: Success response (includes userId)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Focus review areas:
    • controller/user.go: verify Group assignment, token key generation, token payload fields (quotas, timestamps, group), and error-handling/rollback semantics.
    • Ensure JSON response format change (added userId) is compatible with clients.
    • .github/workflows/docker-image-arm64.yml: confirm all image name references were changed consistently and tags/manifests still point to intended registry.

Possibly related PRs

  • feat: matrix ci #2008 — modifies the same workflow file and restructures the multi-arch build; likely related to CI changes.
  • feat: matrix ci #2009 — updates workflow image naming; overlaps with repository/name substitutions here.
  • ignore ghcr #2022 — alters manifest/tag behavior in the same workflow; relevant to manifest creation steps.

Poem

🐰 I dug a key beneath the log,

tucked Group in gently like a frog,
a user born, a token spun,
userid shining in the sun,
hop, hop — the job is done.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately reflects the main changes: adding functionality to return user ID and generate default tokens in the CreateUser API.

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
controller/user.go (1)

226-233: Now let me search for the CreateUser function to understand the inconsistency mentioned in the review:

Now I need to search for the CreateUser function to complete the verification. Let me look for it:

Based on my verification of the GORM behavior and examination of the Insert() method, I can now provide a rewritten review comment:

Remove the redundant database query—GORM populates the ID after Create().

GORM's Create() method automatically backfills primary key values on the model struct, making the query at lines 226-233 unnecessary. Notably, after calling Create(), you can access the inserted ID directly via the object's primary key field. This is demonstrated in the Insert() method itself at line 415 where user.Id is used immediately after DB.Create(user) without a separate query.

The code currently performs an additional database lookup to fetch the user by username, but this extra query can be eliminated by directly using cleanUser.Id after cleanUser.Insert(inviterId) succeeds, consistent with GORM's intended usage pattern.

♻️ Duplicate comments (1)
controller/user.go (1)

234-267: Duplicate code with same issues as CreateUser token generation.

This token generation logic is nearly identical to lines 871-904 in CreateUser, with the same issues:

  • Contradictory quota settings (RemainQuota: 500000 with UnlimitedQuota: true)
  • Hard-coded values and Chinese text
  • No transaction wrapping user+token creation
  • Partial failure risk

Please apply the same refactoring recommended in the comment for lines 871-904: extract this into a shared helper function and wrap both operations in a database transaction to ensure atomicity.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f6a5d9e and 82ca930.

📒 Files selected for processing (1)
  • controller/user.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
controller/user.go (5)
constant/env.go (1)
  • GenerateDefaultToken (14-14)
common/utils.go (2)
  • GenerateKey (256-259)
  • GetTimestamp (266-268)
common/sys_log.go (1)
  • SysLog (11-14)
model/token.go (1)
  • Token (14-31)
setting/auto_group.go (1)
  • DefaultUseAutoGroup (11-11)
🔇 Additional comments (1)
controller/user.go (1)

909-909: LGTM! Useful API improvement.

Adding the userId field to the response is a valuable enhancement that allows API consumers to immediately reference the newly created user without additional queries.

Comment thread controller/user.go
Comment on lines +871 to +904
// 生成默认令牌
if constant.GenerateDefaultToken {
key, err := common.GenerateKey()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "生成默认令牌失败",
})
common.SysLog("failed to generate token key: " + err.Error())
return
}
// 生成默认令牌
token := model.Token{
UserId: cleanUser.Id, // 使用插入后的用户ID
Name: cleanUser.Username + "的初始令牌",
Key: key,
CreatedTime: common.GetTimestamp(),
AccessedTime: common.GetTimestamp(),
ExpiredTime: -1, // 永不过期
RemainQuota: 500000, // 示例额度
UnlimitedQuota: true,
ModelLimitsEnabled: false,
}
if setting.DefaultUseAutoGroup {
token.Group = "auto"
}
if err := token.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "创建默认令牌失败",
})
return
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical inconsistency and multiple concerns with token generation logic.

Several issues identified:

  1. Inconsistency in ID retrieval: This code uses cleanUser.Id directly after Insert() (line 884), but the Register function (lines 226-233) queries the database to retrieve the inserted user's ID. This inconsistency suggests uncertainty about whether GORM populates the ID field after insertion.

  2. Contradictory quota settings: Setting both RemainQuota: 500000 and UnlimitedQuota: true is contradictory. If the quota is unlimited, why set a remain quota?

  3. Hard-coded values: The quota value (500000) and Chinese text ("的初始令牌") are hard-coded, reducing configurability and creating i18n issues.

  4. Code duplication: This token generation logic is nearly identical to lines 235-267 in the Register function, violating DRY principles.

  5. Partial failure without rollback: If token creation fails after user creation, the user exists without a token. Without a transaction wrapping both operations, this creates an inconsistent state.

Recommended actions:

  1. Verify whether cleanUser.Id is populated after Insert(). If not, fetch it from the database as done in Register.
  2. Remove the contradictory RemainQuota setting when UnlimitedQuota is true, or make quota configuration explicit.
  3. Extract token generation into a shared helper function to eliminate duplication.
  4. Use a database transaction to ensure atomicity of user+token creation.
  5. Make the token name configurable or at least support i18n.
// Example: Extract to helper function
func createDefaultToken(userId int, username string) error {
    if !constant.GenerateDefaultToken {
        return nil
    }
    
    key, err := common.GenerateKey()
    if err != nil {
        return fmt.Errorf("failed to generate token key: %w", err)
    }
    
    token := model.Token{
        UserId:             userId,
        Name:               fmt.Sprintf("%s - Initial Token", username), // or use i18n
        Key:                key,
        CreatedTime:        common.GetTimestamp(),
        AccessedTime:       common.GetTimestamp(),
        ExpiredTime:        -1,
        UnlimitedQuota:     true,
        ModelLimitsEnabled: false,
    }
    
    if setting.DefaultUseAutoGroup {
        token.Group = "auto"
    }
    
    return token.Insert()
}

Then wrap both operations in a transaction:

err := model.DB.Transaction(func(tx *gorm.DB) error {
    if err := cleanUser.Insert(0); err != nil {
        return err
    }
    return createDefaultToken(cleanUser.Id, cleanUser.Username)
})
if err != nil {
    common.ApiError(c, err)
    return
}

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 47020e4 and 18c9976.

📒 Files selected for processing (1)
  • .github/workflows/docker-image-arm64.yml (3 hunks)
🔇 Additional comments (1)
.github/workflows/docker-image-arm64.yml (1)

1-117: Misalignment between PR objectives and changes.

The PR objectives describe a feature to "Create a user API to synchronously return the user ID and generate a default token," but this file contains only Docker infrastructure changes (repository name substitution). The actual user API feature implementation (referenced in the AI summary as occurring in controller/user.go) is not provided for review.

Clarify whether:

  1. These Docker workflow changes are the only changes in this PR (which would not align with the stated objectives)
  2. Additional files (controller/user.go and related) should be included in this review
  3. The PR objectives need to be updated

with:
images: |
calciumion/new-api
bddiudiu/new-api

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Verify Docker image repository migration is coordinated across infrastructure and deployments.

The repository name has been changed from calciumion/new-api to bddiudiu/new-api across the workflow. While the changes are consistent within this file, ensure that:

  1. This repository migration is intentional and coordinated with your deployment infrastructure
  2. Access credentials (secrets) are properly configured for the new Docker Hub account (bddiudiu)
  3. Existing deployments pulling from calciumion/new-api have a migration plan
  4. No other CI/CD pipelines or manifests reference the old image name

Run the following script to verify there are no remaining references to the old repository name:

[/request_verification]

Additionally, verify that the DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets are correctly configured for the new Docker Hub account.

Also applies to: 76-77, 107-109, 114-116


🏁 Script executed:

#!/bin/bash
# Description: Search for any remaining references to the old Docker image repository

echo "Searching for references to old repository (calciumion/new-api)..."
rg -n "calciumion/new-api" --type yaml --type yml

echo ""
echo "Verifying all instances in workflow are updated..."
rg -n "bddiudiu/new-api" --type yaml --type yml

Length of output: 174


[/run_scripts]


🏁 Script executed:

#!/bin/bash
# Description: Search for Docker image repository references with corrected syntax

echo "Searching for old repository references (calciumion/new-api)..."
rg -n "calciumion/new-api" --type yaml

echo ""
echo "Verifying new repository references (bddiudiu/new-api)..."
rg -n "bddiudiu/new-api" --type yaml

echo ""
echo "Also checking .yml files directly..."
find .github -name "*.yml" -o -name "*.yaml" | xargs grep -n "calciumion/new-api\|bddiudiu/new-api" 2>/dev/null || echo "No matches found in .github directory"

Length of output: 1871


Incomplete Docker repository migration across workflows—multiple files still reference old repository.

Verification found the migration is inconsistent: docker-image-arm64.yml was updated to use bddiudiu/new-api, but other critical files still reference calciumion/new-api:

  • docker-image-alpha.yml: 8 references to calciumion/new-api (lines 69, 79-80, 121-123, 128-130)
  • docker-compose.yml: 1 reference to calciumion/new-api (line 19)

Update these files to complete the repository migration consistently, or clarify if the alpha workflow and compose file should intentionally use different registries.

🤖 Prompt for AI Agents
In .github/workflows/docker-image-arm64.yml around line 66 and also in
.github/workflows/docker-image-alpha.yml (lines 69, 79-80, 121-123, 128-130) and
docker-compose.yml (line 19), fix the incomplete migration by replacing
occurrences of "calciumion/new-api" with "bddiudiu/new-api" so all workflows and
compose file reference the same repository; if the alpha workflow or compose
file are intentionally meant to point to a different registry, add a clear
comment explaining that intent instead of leaving mixed references.

dependabot Bot and others added 24 commits November 24, 2025 09:58
Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.42.0 to 0.45.0.
- [Commits](golang/crypto@v0.42.0...v0.45.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-version: 0.45.0
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
…Handler for token tracking"

This reverts commit 83c4924.
…Usage to use context in multiple channels"

This reverts commit f88e14b.
@bddiudiu bddiudiu closed this Nov 24, 2025
@bddiudiu
bddiudiu deleted the feat_create_user branch November 24, 2025 02:01
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.

6 participants