Skip to content

Add user badges#36752

Draft
bircni wants to merge 9 commits intogo-gitea:mainfrom
bircni:feature/retry-badges
Draft

Add user badges#36752
bircni wants to merge 9 commits intogo-gitea:mainfrom
bircni:feature/retry-badges

Conversation

@bircni
Copy link
Contributor

@bircni bircni commented Feb 25, 2026

trying to reenable #31262

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 25, 2026
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/migrations labels Feb 25, 2026
@bircni
Copy link
Contributor Author

bircni commented Feb 25, 2026

Addressed the open review threads with the following changes:

  1. Removed services/user/badge.go wrappers (UpdateBadge, DeleteBadge, GetBadgeUsers) and switched admin badge handlers to direct model calls.
  2. Removed redundant transaction wrapping by dropping the service wrappers and calling user_model.UpdateBadge / user_model.DeleteBadge directly.
  3. Fixed badge edit form semantics: removed invalid disabled usage on parent container, removed non-local, and made slug display readonly.
  4. Removed unnecessary id/for attributes in templates/admin/badge/edit.tmpl where framework defaults are sufficient.
  5. Removed no-op delete class from the badge delete modal.
  6. Moved the add-user form to the top of templates/admin/badge/users.tmpl and removed unused id attributes.
  7. Re-aligned and cleaned nested <div> structure in templates/admin/badge/view.tmpl for readability.
  8. Fixed control flow in DeleteBadgeUser: now returns immediately after flash + redirect on IsErrUserNotExist.
  9. Added dedicated AdminEditBadgeForm and updated route binding for badge edit, so edit validation no longer depends on slug input.

Validation run:

  • make fmt
  • make lint-go (0 issues)

@bircni bircni marked this pull request as ready for review February 25, 2026 18:28
@bircni bircni force-pushed the feature/retry-badges branch from 56aeab8 to 5cbe732 Compare February 26, 2026 18:30
@lunny
Copy link
Member

lunny commented Feb 26, 2026

It's better to have a user name search when inputing like other places.
image

@lunny
Copy link
Member

lunny commented Feb 26, 2026

image Align problem.

@bircni
Copy link
Contributor Author

bircni commented Feb 26, 2026

fixed

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 26, 2026
@lunny lunny added this to the 1.26.0 milestone Feb 26, 2026
Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

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

Need more improvements

@GiteaBot GiteaBot removed the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Feb 27, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Re-enables and wires up admin-side badge management and display by adding UI pages/routes for creating/editing/viewing badges, plus model/validation/migration support to enforce better data integrity.

Changes:

  • Add admin badge management routes, handlers, and templates (list/new/view/edit/users) and expose the nav entry.
  • Introduce badge slug validation + i18n strings, and update profile badge rendering to support image-less “chip” badges.
  • Add model logic/tests for badges, plus a migration to enforce a unique (user_id, badge_id) constraint.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
web_src/css/user.css Adjust badge layout to flex-wrap and add styling for text “chip” badges.
templates/shared/user/profile_big_avatar.tmpl Render badges as images when available, otherwise as slug chips.
templates/admin/navbar.tmpl Add “Badges” entry and keep Identity & Access menu expanded on badge pages.
templates/admin/badge/view.tmpl New admin badge detail page (badge info + users preview).
templates/admin/badge/users.tmpl New admin page to add/remove users for a badge.
templates/admin/badge/new.tmpl New admin create-badge form.
templates/admin/badge/list.tmpl New admin badge listing with search + sorting.
templates/admin/badge/edit.tmpl New admin edit/delete badge UI.
services/forms/admin.go Add admin badge create/edit form structs + validation hooks.
routers/web/web.go Register admin /badges routes.
routers/web/admin/badges.go Implement admin badge CRUD + user assignment endpoints and badge search rendering.
options/locale/locale_en-US.json Add i18n strings for badge management + slug validation error.
modules/web/middleware/binding.go Map new badge-slug validation error classification to a translated message.
modules/validation/helpers_test.go Add tests for badge slug validation.
modules/validation/helpers.go Add IsValidBadgeSlug and patterns.
modules/validation/binding.go Add BadgeSlug binding rule and error classification.
models/user/badge_test.go Add model-level tests for badges, users, and searching.
models/user/badge.go Add badge search, badge-user listing, uniqueness index, and improved error semantics.
models/migrations/v1_26/v326_test.go Add migration test for deduping and unique index creation.
models/migrations/v1_26/v326.go Add migration to dedupe user_badge and add compound unique index.
models/migrations/migrations.go Register migration #326.
models/fixtures/badge.yml Add badge fixture data for tests.
Comments suppressed due to low confidence (1)

models/user/badge.go:158

  • AddUserBadges inserts into user_badge without checking whether the user already has the badge. With the new unique constraint (unique_user_badge), attempting to assign the same badge twice will return a raw DB unique-violation error (db.Insert doesn't wrap it), which currently bubbles up to handlers as a 500. Consider detecting duplicates (pre-check user_badge existence or wrap unique-violation into util.ErrAlreadyExist) so callers like the admin UI can show a user-friendly message instead of a server error.
		for _, badge := range badges {
			// hydrate badge and check if it exists
			has, err := db.GetEngine(ctx).Where("slug=?", badge.Slug).Get(badge)
			if err != nil {
				return err
			} else if !has {
				return util.NewNotExistErrorf("badge does not exist [slug: %s]", badge.Slug)
			}
			if err := db.Insert(ctx, &UserBadge{
				BadgeID: badge.ID,
				UserID:  u.ID,
			}); err != nil {
				return err

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@bircni
Copy link
Contributor Author

bircni commented Mar 8, 2026

The more I think about it the more I think images are not useful here and maybe a tab like bade could be enough

@bircni bircni marked this pull request as draft March 8, 2026 20:40
@bircni
Copy link
Contributor Author

bircni commented Mar 8, 2026

Cannot reproduce the errors on my machine :-(

@bircni bircni force-pushed the feature/retry-badges branch from da63623 to 525b0bb Compare March 8, 2026 21:01
@bircni bircni marked this pull request as ready for review March 9, 2026 18:59
@silverwind
Copy link
Member

This comment was written by Claude on behalf of @silverwind

Review

  1. IsValidSlug is dead codemodules/validation/helpers.go adds IsValidSlug() that wraps IsValidBadgeSlug(), but nothing calls it. The binding rule calls IsValidBadgeSlug directly. Remove it.

  2. DeleteBadge uses an unnecessary subquery — The user_badge cleanup does WHERE badge_id = (SELECT id FROM badge WHERE slug = ?), but the caller (routers/web/admin/badges.go:DeleteBadge) calls GetBadge first which returns a fully populated Badge with ID. Should just use Where("badge_id = ?", badge.ID).

  3. prepareBadgeInfo loads users on edit pages — It queries GetBadgeUsers every time, but it's called from EditBadge/EditBadgePost where the template (edit.tmpl) never uses Users or UsersTotal. Unnecessary DB queries on every edit page load.

  4. Edit form doesn't use form-fetch-actionnew.tmpl uses class="ui form form-fetch-action" and its handler uses ctx.JSONError/ctx.JSONRedirect. But edit.tmpl uses a plain class="ui form" with ctx.HTML/ctx.Redirect. These should be consistent — especially since the project is deprecating RenderWithErr in favor of form-fetch-action (Deprecate RenderWithErr #36769).

  5. SearchBadgeOptions.Actor is a dead field — It's set in Badges() handler (Actor: ctx.Doer) but never read in ToConds() or ToOrders(). Remove it or use it.

@bircni
Copy link
Contributor Author

bircni commented Mar 12, 2026

Fixed

@silverwind silverwind changed the title Feature/retry badges Add user badges Mar 13, 2026
@silverwind
Copy link
Member

PR title updated to be more accurate.

@bircni
Copy link
Contributor Author

bircni commented Mar 16, 2026

Updated to main and resolved conflicts

@bircni bircni marked this pull request as draft March 16, 2026 20:44
@bircni bircni closed this Mar 18, 2026
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Mar 18, 2026

I think this PR's code is good enough.

I don't need badges and don't know what the origin requirement is.

So the maintainers from the origin PR can help to review: @techknowlogick

@bircni bircni reopened this Mar 18, 2026
@bircni
Copy link
Contributor Author

bircni commented Mar 18, 2026

reopened - if there is still need for it sure but I want to focus on other stuff to improve currently

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

Labels

lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/frontend modifies/go Pull requests that update Go code modifies/migrations modifies/templates This PR modifies the template files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants