-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Manage User Badges in the interface #29798 #31262
Open
HenriquerPimentel
wants to merge
9
commits into
go-gitea:main
Choose a base branch
from
HenriquerPimentel:Badge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b86c31
Implemented Badge Management in administration panel (#29798)
HenriquerPimentel f68e44d
Implemented User Badge Management Interface (#29798)
HenriquerPimentel d681313
Fix linting recommendations
HenriquerPimentel 10463ec
Merge branch 'main' into Badge
techknowlogick 52bc49d
fix lints
techknowlogick 401d257
Merge branch 'main' into Badge
techknowlogick d55e25f
update join
techknowlogick d9efbf3
close session immediately
techknowlogick ae9c0a6
Merge branch 'main' into Badge
techknowlogick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,12 @@ package user | |
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
|
||
"xorm.io/builder" | ||
"xorm.io/xorm" | ||
) | ||
|
||
// Badge represents a user badge | ||
|
@@ -42,13 +46,27 @@ func GetUserBadges(ctx context.Context, u *User) ([]*Badge, int64, error) { | |
return badges, count, err | ||
} | ||
|
||
// GetBadgeUsers returns the users that have a specific badge. | ||
func GetBadgeUsers(ctx context.Context, b *Badge) ([]*User, int64, error) { | ||
sess := db.GetEngine(ctx). | ||
Select("`user`.*"). | ||
Join("INNER", "user_badge", "`user_badge`.user_id=user.id"). | ||
Join("INNER", "badge", "`user_badge`.badge_id=badge.id"). | ||
Where("badge.slug=?", b.Slug) | ||
users := make([]*User, 0, 8) | ||
count, err := sess.FindAndCount(&users) | ||
return users, count, err | ||
} | ||
|
||
// CreateBadge creates a new badge. | ||
func CreateBadge(ctx context.Context, badge *Badge) error { | ||
// this will fail if the badge already exists due to the UNIQUE constraint | ||
_, err := db.GetEngine(ctx).Insert(badge) | ||
|
||
return err | ||
} | ||
|
||
// GetBadge returns a badge | ||
// GetBadge returns a specific badge | ||
func GetBadge(ctx context.Context, slug string) (*Badge, error) { | ||
badge := new(Badge) | ||
has, err := db.GetEngine(ctx).Where("slug=?", slug).Get(badge) | ||
|
@@ -60,7 +78,7 @@ func GetBadge(ctx context.Context, slug string) (*Badge, error) { | |
|
||
// UpdateBadge updates a badge based on its slug. | ||
func UpdateBadge(ctx context.Context, badge *Badge) error { | ||
_, err := db.GetEngine(ctx).Where("slug=?", badge.Slug).Update(badge) | ||
_, err := db.GetEngine(ctx).Where("slug=?", badge.Slug).Cols("description", "image_url").Update(badge) | ||
return err | ||
} | ||
|
||
|
@@ -84,7 +102,7 @@ func AddUserBadges(ctx context.Context, u *User, badges []*Badge) error { | |
if err != nil { | ||
return err | ||
} else if !has { | ||
return fmt.Errorf("badge with slug %s doesn't exist", badge.Slug) | ||
return ErrBadgeNotExist{Slug: badge.Slug} | ||
} | ||
if err := db.Insert(ctx, &UserBadge{ | ||
BadgeID: badge.ID, | ||
|
@@ -102,13 +120,18 @@ func RemoveUserBadge(ctx context.Context, u *User, badge *Badge) error { | |
return RemoveUserBadges(ctx, u, []*Badge{badge}) | ||
} | ||
|
||
// RemoveUserBadges removes badges from a user. | ||
// RemoveUserBadges removes specific badges from a user. | ||
func RemoveUserBadges(ctx context.Context, u *User, badges []*Badge) error { | ||
return db.WithTx(ctx, func(ctx context.Context) error { | ||
for _, badge := range badges { | ||
subQuery := builder. | ||
Select("id"). | ||
From("badge"). | ||
Where(builder.Eq{"slug": badge.Slug}) | ||
|
||
if _, err := db.GetEngine(ctx). | ||
Join("INNER", "badge", "badge.id = `user_badge`.badge_id"). | ||
Where("`user_badge`.user_id=? AND `badge`.slug=?", u.ID, badge.Slug). | ||
Where("`user_badge`.user_id=?", u.ID). | ||
And(builder.In("badge_id", subQuery)). | ||
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. Why use subquery instead of delete with join? |
||
Delete(&UserBadge{}); err != nil { | ||
return err | ||
} | ||
|
@@ -122,3 +145,91 @@ func RemoveAllUserBadges(ctx context.Context, u *User) error { | |
_, err := db.GetEngine(ctx).Where("user_id=?", u.ID).Delete(&UserBadge{}) | ||
return err | ||
} | ||
|
||
// SearchBadgeOptions represents the options when fdin badges | ||
type SearchBadgeOptions struct { | ||
db.ListOptions | ||
|
||
Keyword string | ||
Slug string | ||
ID int64 | ||
OrderBy db.SearchOrderBy | ||
Actor *User // The user doing the search | ||
|
||
ExtraParamStrings map[string]string | ||
} | ||
|
||
func (opts *SearchBadgeOptions) ToConds() builder.Cond { | ||
cond := builder.NewCond() | ||
|
||
if opts.Keyword != "" { | ||
cond = cond.And(builder.Like{"badge.slug", opts.Keyword}) | ||
} | ||
|
||
return cond | ||
} | ||
|
||
func (opts *SearchBadgeOptions) ToOrders() string { | ||
orderBy := "badge.slug" | ||
return orderBy | ||
} | ||
|
||
func (opts *SearchBadgeOptions) ToJoins() []db.JoinFunc { | ||
return []db.JoinFunc{ | ||
func(e db.Engine) error { | ||
e.Join("INNER", "badge", "`user_badge`.badge_id=badge.id") | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func SearchBadges(ctx context.Context, opts *SearchBadgeOptions) (badges []*Badge, _ int64, _ error) { | ||
sessCount := opts.toSearchQueryBase(ctx) | ||
count, err := sessCount.Count(new(Badge)) | ||
techknowlogick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, 0, fmt.Errorf("count: %w", err) | ||
} | ||
sessCount.Close() | ||
|
||
if len(opts.OrderBy) == 0 { | ||
opts.OrderBy = db.SearchOrderByID | ||
} | ||
|
||
sessQuery := opts.toSearchQueryBase(ctx).OrderBy(opts.OrderBy.String()) | ||
defer sessQuery.Close() | ||
if opts.Page != 0 { | ||
sessQuery = db.SetSessionPagination(sessQuery, opts) | ||
} | ||
|
||
// the sql may contain JOIN, so we must only select Badge related columns | ||
sessQuery = sessQuery.Select("`badge`.*") | ||
badges = make([]*Badge, 0, opts.PageSize) | ||
return badges, count, sessQuery.Find(&badges) | ||
} | ||
|
||
func (opts *SearchBadgeOptions) toSearchQueryBase(ctx context.Context) *xorm.Session { | ||
var cond builder.Cond | ||
cond = builder.Neq{"id": -1} | ||
techknowlogick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if len(opts.Keyword) > 0 { | ||
lowerKeyword := strings.ToLower(opts.Keyword) | ||
keywordCond := builder.Or( | ||
builder.Like{"slug", lowerKeyword}, | ||
builder.Like{"description", lowerKeyword}, | ||
builder.Like{"id", lowerKeyword}, | ||
) | ||
cond = cond.And(keywordCond) | ||
} | ||
|
||
if opts.ID > 0 { | ||
cond = cond.And(builder.Eq{"id": opts.ID}) | ||
} | ||
|
||
if len(opts.Slug) > 0 { | ||
cond = cond.And(builder.Eq{"slug": opts.Slug}) | ||
} | ||
|
||
e := db.GetEngine(ctx) | ||
|
||
return e.Where(cond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think pagination is necessary here because you don't know how many users will have the badges which maybe a big number.