mirror of
https://github.com/go-gitea/gitea
synced 2025-01-31 03:54:28 +00:00
Some small refactors (#33144)
This commit is contained in:
parent
e5f3c16587
commit
d3083d2198
@ -26,14 +26,14 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
|
|||||||
return c.PosterID, c.Poster == nil && c.PosterID > 0
|
return c.PosterID, c.Poster == nil && c.PosterID > 0
|
||||||
})
|
})
|
||||||
|
|
||||||
posterMaps, err := getPostersByIDs(ctx, posterIDs)
|
posterMaps, err := user_model.GetUsersMapByIDs(ctx, posterIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, comment := range comments {
|
for _, comment := range comments {
|
||||||
if comment.Poster == nil {
|
if comment.Poster == nil {
|
||||||
comment.Poster = getPoster(comment.PosterID, posterMaps)
|
comment.Poster = user_model.GetPossibleUserFromMap(comment.PosterID, posterMaps)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -41,7 +41,7 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
|
|||||||
|
|
||||||
func (comments CommentList) getLabelIDs() []int64 {
|
func (comments CommentList) getLabelIDs() []int64 {
|
||||||
return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
|
return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
|
||||||
return comment.LabelID, comment.LabelID > 0
|
return comment.LabelID, comment.LabelID > 0 && comment.Label == nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +51,9 @@ func (comments CommentList) loadLabels(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
labelIDs := comments.getLabelIDs()
|
labelIDs := comments.getLabelIDs()
|
||||||
|
if len(labelIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
commentLabels := make(map[int64]*Label, len(labelIDs))
|
commentLabels := make(map[int64]*Label, len(labelIDs))
|
||||||
left := len(labelIDs)
|
left := len(labelIDs)
|
||||||
for left > 0 {
|
for left > 0 {
|
||||||
@ -118,8 +121,8 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
|
|||||||
milestoneIDs = milestoneIDs[limit:]
|
milestoneIDs = milestoneIDs[limit:]
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, issue := range comments {
|
for _, comment := range comments {
|
||||||
issue.Milestone = milestoneMaps[issue.MilestoneID]
|
comment.Milestone = milestoneMaps[comment.MilestoneID]
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -175,6 +178,9 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assigneeIDs := comments.getAssigneeIDs()
|
assigneeIDs := comments.getAssigneeIDs()
|
||||||
|
if len(assigneeIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
assignees := make(map[int64]*user_model.User, len(assigneeIDs))
|
assignees := make(map[int64]*user_model.User, len(assigneeIDs))
|
||||||
left := len(assigneeIDs)
|
left := len(assigneeIDs)
|
||||||
for left > 0 {
|
for left > 0 {
|
||||||
@ -301,6 +307,9 @@ func (comments CommentList) loadDependentIssues(ctx context.Context) error {
|
|||||||
|
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
issueIDs := comments.getDependentIssueIDs()
|
issueIDs := comments.getDependentIssueIDs()
|
||||||
|
if len(issueIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
issues := make(map[int64]*Issue, len(issueIDs))
|
issues := make(map[int64]*Issue, len(issueIDs))
|
||||||
left := len(issueIDs)
|
left := len(issueIDs)
|
||||||
for left > 0 {
|
for left > 0 {
|
||||||
@ -427,6 +436,9 @@ func (comments CommentList) loadReviews(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reviewIDs := comments.getReviewIDs()
|
reviewIDs := comments.getReviewIDs()
|
||||||
|
if len(reviewIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
reviews := make(map[int64]*Review, len(reviewIDs))
|
reviews := make(map[int64]*Review, len(reviewIDs))
|
||||||
if err := db.GetEngine(ctx).In("id", reviewIDs).Find(&reviews); err != nil {
|
if err := db.GetEngine(ctx).In("id", reviewIDs).Find(&reviews); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -238,6 +238,9 @@ func (issue *Issue) loadCommentsByType(ctx context.Context, tp CommentType) (err
|
|||||||
IssueID: issue.ID,
|
IssueID: issue.ID,
|
||||||
Type: tp,
|
Type: tp,
|
||||||
})
|
})
|
||||||
|
for _, comment := range issue.Comments {
|
||||||
|
comment.Issue = issue
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,53 +81,19 @@ func (issues IssueList) LoadPosters(ctx context.Context) error {
|
|||||||
return issue.PosterID, issue.Poster == nil && issue.PosterID > 0
|
return issue.PosterID, issue.Poster == nil && issue.PosterID > 0
|
||||||
})
|
})
|
||||||
|
|
||||||
posterMaps, err := getPostersByIDs(ctx, posterIDs)
|
posterMaps, err := user_model.GetUsersMapByIDs(ctx, posterIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, issue := range issues {
|
for _, issue := range issues {
|
||||||
if issue.Poster == nil {
|
if issue.Poster == nil {
|
||||||
issue.Poster = getPoster(issue.PosterID, posterMaps)
|
issue.Poster = user_model.GetPossibleUserFromMap(issue.PosterID, posterMaps)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPostersByIDs(ctx context.Context, posterIDs []int64) (map[int64]*user_model.User, error) {
|
|
||||||
posterMaps := make(map[int64]*user_model.User, len(posterIDs))
|
|
||||||
left := len(posterIDs)
|
|
||||||
for left > 0 {
|
|
||||||
limit := db.DefaultMaxInSize
|
|
||||||
if left < limit {
|
|
||||||
limit = left
|
|
||||||
}
|
|
||||||
err := db.GetEngine(ctx).
|
|
||||||
In("id", posterIDs[:limit]).
|
|
||||||
Find(&posterMaps)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
left -= limit
|
|
||||||
posterIDs = posterIDs[limit:]
|
|
||||||
}
|
|
||||||
return posterMaps, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPoster(posterID int64, posterMaps map[int64]*user_model.User) *user_model.User {
|
|
||||||
if posterID == user_model.ActionsUserID {
|
|
||||||
return user_model.NewActionsUser()
|
|
||||||
}
|
|
||||||
if posterID <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
poster, ok := posterMaps[posterID]
|
|
||||||
if !ok {
|
|
||||||
return user_model.NewGhostUser()
|
|
||||||
}
|
|
||||||
return poster
|
|
||||||
}
|
|
||||||
|
|
||||||
func (issues IssueList) getIssueIDs() []int64 {
|
func (issues IssueList) getIssueIDs() []int64 {
|
||||||
ids := make([]int64, 0, len(issues))
|
ids := make([]int64, 0, len(issues))
|
||||||
for _, issue := range issues {
|
for _, issue := range issues {
|
||||||
|
47
models/user/user_list.go
Normal file
47
models/user/user_list.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
|
||||||
|
userMaps := make(map[int64]*User, len(userIDs))
|
||||||
|
left := len(userIDs)
|
||||||
|
for left > 0 {
|
||||||
|
limit := db.DefaultMaxInSize
|
||||||
|
if left < limit {
|
||||||
|
limit = left
|
||||||
|
}
|
||||||
|
err := db.GetEngine(ctx).
|
||||||
|
In("id", userIDs[:limit]).
|
||||||
|
Find(&userMaps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
left -= limit
|
||||||
|
userIDs = userIDs[limit:]
|
||||||
|
}
|
||||||
|
return userMaps, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPossibleUserFromMap(userID int64, usererMaps map[int64]*User) *User {
|
||||||
|
switch userID {
|
||||||
|
case GhostUserID:
|
||||||
|
return NewGhostUser()
|
||||||
|
case ActionsUserID:
|
||||||
|
return NewActionsUser()
|
||||||
|
case 0:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
user, ok := usererMaps[userID]
|
||||||
|
if !ok {
|
||||||
|
return NewGhostUser()
|
||||||
|
}
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
}
|
@ -40,16 +40,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// roleDescriptor returns the role descriptor for a comment in/with the given repo, poster and issue
|
// roleDescriptor returns the role descriptor for a comment in/with the given repo, poster and issue
|
||||||
func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) {
|
func roleDescriptor(ctx stdCtx.Context, repo *repo_model.Repository, poster *user_model.User, permsCache map[int64]access_model.Permission, issue *issues_model.Issue, hasOriginalAuthor bool) (issues_model.RoleDescriptor, error) {
|
||||||
roleDescriptor := issues_model.RoleDescriptor{}
|
roleDescriptor := issues_model.RoleDescriptor{}
|
||||||
|
|
||||||
if hasOriginalAuthor {
|
if hasOriginalAuthor {
|
||||||
return roleDescriptor, nil
|
return roleDescriptor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
perm, err := access_model.GetUserRepoPermission(ctx, repo, poster)
|
var perm access_model.Permission
|
||||||
if err != nil {
|
var err error
|
||||||
return roleDescriptor, err
|
if permsCache != nil {
|
||||||
|
var ok bool
|
||||||
|
perm, ok = permsCache[poster.ID]
|
||||||
|
if !ok {
|
||||||
|
perm, err = access_model.GetUserRepoPermission(ctx, repo, poster)
|
||||||
|
if err != nil {
|
||||||
|
return roleDescriptor, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
permsCache[poster.ID] = perm
|
||||||
|
} else {
|
||||||
|
perm, err = access_model.GetUserRepoPermission(ctx, repo, poster)
|
||||||
|
if err != nil {
|
||||||
|
return roleDescriptor, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the poster is the actual poster of the issue, enable Poster role.
|
// If the poster is the actual poster of the issue, enable Poster role.
|
||||||
@ -576,6 +590,8 @@ func prepareIssueViewCommentsAndSidebarParticipants(ctx *context.Context, issue
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
permCache := make(map[int64]access_model.Permission)
|
||||||
|
|
||||||
for _, comment = range issue.Comments {
|
for _, comment = range issue.Comments {
|
||||||
comment.Issue = issue
|
comment.Issue = issue
|
||||||
|
|
||||||
@ -593,7 +609,7 @@ func prepareIssueViewCommentsAndSidebarParticipants(ctx *context.Context, issue
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
comment.ShowRole, err = roleDescriptor(ctx, issue.Repo, comment.Poster, issue, comment.HasOriginalAuthor())
|
comment.ShowRole, err = roleDescriptor(ctx, issue.Repo, comment.Poster, permCache, issue, comment.HasOriginalAuthor())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("roleDescriptor", err)
|
ctx.ServerError("roleDescriptor", err)
|
||||||
return
|
return
|
||||||
@ -691,7 +707,7 @@ func prepareIssueViewCommentsAndSidebarParticipants(ctx *context.Context, issue
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c.ShowRole, err = roleDescriptor(ctx, issue.Repo, c.Poster, issue, c.HasOriginalAuthor())
|
c.ShowRole, err = roleDescriptor(ctx, issue.Repo, c.Poster, permCache, issue, c.HasOriginalAuthor())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("roleDescriptor", err)
|
ctx.ServerError("roleDescriptor", err)
|
||||||
return
|
return
|
||||||
@ -940,7 +956,7 @@ func prepareIssueViewContent(ctx *context.Context, issue *issues_model.Issue) {
|
|||||||
ctx.ServerError("RenderString", err)
|
ctx.ServerError("RenderString", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if issue.ShowRole, err = roleDescriptor(ctx, issue.Repo, issue.Poster, issue, issue.HasOriginalAuthor()); err != nil {
|
if issue.ShowRole, err = roleDescriptor(ctx, issue.Repo, issue.Poster, nil, issue, issue.HasOriginalAuthor()); err != nil {
|
||||||
ctx.ServerError("roleDescriptor", err)
|
ctx.ServerError("roleDescriptor", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user