mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set a context cache to do that. i.e. Some pages may load a user from a database with the same id in different areas on the same page. But the code is hidden in two different deep logic. How should we share the user? As a result of this PR, now if both entry functions accept `context.Context` as the first parameter and we just need to refactor `GetUserByID` to reuse the user from the context cache. Then it will not be loaded twice on an HTTP request. But of course, sometimes we would like to reload an object from the database, that's why `RemoveContextData` is also exposed. The core context cache is here. It defines a new context ```go type cacheContext struct { ctx context.Context data map[any]map[any]any lock sync.RWMutex } var cacheContextKey = struct{}{} func WithCacheContext(ctx context.Context) context.Context { return context.WithValue(ctx, cacheContextKey, &cacheContext{ ctx: ctx, data: make(map[any]map[any]any), }) } ``` Then you can use the below 4 methods to read/write/del the data within the same context. ```go func GetContextData(ctx context.Context, tp, key any) any func SetContextData(ctx context.Context, tp, key, value any) func RemoveContextData(ctx context.Context, tp, key any) func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) ``` Then let's take a look at how `system.GetString` implement it. ```go func GetSetting(ctx context.Context, key string) (string, error) { return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) { return cache.GetString(genSettingCacheKey(key), func() (string, error) { res, err := GetSettingNoCache(ctx, key) if err != nil { return "", err } return res.SettingValue, nil }) }) } ``` First, it will check if context data include the setting object with the key. If not, it will query from the global cache which may be memory or a Redis cache. If not, it will get the object from the database. In the end, if the object gets from the global cache or database, it will be set into the context cache. An object stored in the context cache will only be destroyed after the context disappeared.
This commit is contained in:
@@ -39,7 +39,7 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
|
||||
}
|
||||
|
||||
// ToBranch convert a git.Commit and git.Branch to an api.Branch
|
||||
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
|
||||
func ToBranch(ctx context.Context, repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
|
||||
if bp == nil {
|
||||
var hasPerm bool
|
||||
var canPush bool
|
||||
@@ -59,7 +59,7 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git
|
||||
|
||||
return &api.Branch{
|
||||
Name: b.Name,
|
||||
Commit: ToPayloadCommit(repo, c),
|
||||
Commit: ToPayloadCommit(ctx, repo, c),
|
||||
Protected: false,
|
||||
RequiredApprovals: 0,
|
||||
EnableStatusCheck: false,
|
||||
@@ -71,7 +71,7 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git
|
||||
|
||||
branch := &api.Branch{
|
||||
Name: b.Name,
|
||||
Commit: ToPayloadCommit(repo, c),
|
||||
Commit: ToPayloadCommit(ctx, repo, c),
|
||||
Protected: true,
|
||||
RequiredApprovals: bp.RequiredApprovals,
|
||||
EnableStatusCheck: bp.EnableStatusCheck,
|
||||
@@ -169,8 +169,8 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
|
||||
}
|
||||
|
||||
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
|
||||
func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
|
||||
verif := asymkey_model.ParseCommitWithSignature(c)
|
||||
func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification {
|
||||
verif := asymkey_model.ParseCommitWithSignature(ctx, c)
|
||||
commitVerification := &api.PayloadCommitVerification{
|
||||
Verified: verif.Verified,
|
||||
Reason: verif.Reason,
|
||||
@@ -271,10 +271,10 @@ func ToDeployKey(apiLink string, key *asymkey_model.DeployKey) *api.DeployKey {
|
||||
}
|
||||
|
||||
// ToOrganization convert user_model.User to api.Organization
|
||||
func ToOrganization(org *organization.Organization) *api.Organization {
|
||||
func ToOrganization(ctx context.Context, org *organization.Organization) *api.Organization {
|
||||
return &api.Organization{
|
||||
ID: org.ID,
|
||||
AvatarURL: org.AsUser().AvatarLink(),
|
||||
AvatarURL: org.AsUser().AvatarLink(ctx),
|
||||
Name: org.Name,
|
||||
UserName: org.Name,
|
||||
FullName: org.FullName,
|
||||
@@ -287,8 +287,8 @@ func ToOrganization(org *organization.Organization) *api.Organization {
|
||||
}
|
||||
|
||||
// ToTeam convert models.Team to api.Team
|
||||
func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
|
||||
teams, err := ToTeams([]*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
|
||||
func ToTeam(ctx context.Context, team *organization.Team, loadOrg ...bool) (*api.Team, error) {
|
||||
teams, err := ToTeams(ctx, []*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
|
||||
if err != nil || len(teams) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
|
||||
}
|
||||
|
||||
// ToTeams convert models.Team list to api.Team list
|
||||
func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
|
||||
func ToTeams(ctx context.Context, teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
|
||||
if len(teams) == 0 || teams[0] == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -326,7 +326,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
apiOrg = ToOrganization(org)
|
||||
apiOrg = ToOrganization(ctx, org)
|
||||
cache[teams[i].OrgID] = apiOrg
|
||||
}
|
||||
apiTeams[i].Organization = apiOrg
|
||||
@@ -336,7 +336,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
|
||||
}
|
||||
|
||||
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
|
||||
func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
||||
func ToAnnotatedTag(ctx context.Context, repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
||||
return &api.AnnotatedTag{
|
||||
Tag: t.Name,
|
||||
SHA: t.ID.String(),
|
||||
@@ -344,7 +344,7 @@ func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api
|
||||
Message: t.Message,
|
||||
URL: util.URLJoin(repo.APIURL(), "git/tags", t.ID.String()),
|
||||
Tagger: ToCommitUser(t.Tagger),
|
||||
Verification: ToVerification(c),
|
||||
Verification: ToVerification(ctx, c),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
@@ -37,16 +38,16 @@ func ToCommitMeta(repo *repo_model.Repository, tag *git.Tag) *api.CommitMeta {
|
||||
}
|
||||
|
||||
// ToPayloadCommit convert a git.Commit to api.PayloadCommit
|
||||
func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
|
||||
func ToPayloadCommit(ctx context.Context, repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
|
||||
authorUsername := ""
|
||||
if author, err := user_model.GetUserByEmail(c.Author.Email); err == nil {
|
||||
if author, err := user_model.GetUserByEmail(ctx, c.Author.Email); err == nil {
|
||||
authorUsername = author.Name
|
||||
} else if !user_model.IsErrUserNotExist(err) {
|
||||
log.Error("GetUserByEmail: %v", err)
|
||||
}
|
||||
|
||||
committerUsername := ""
|
||||
if committer, err := user_model.GetUserByEmail(c.Committer.Email); err == nil {
|
||||
if committer, err := user_model.GetUserByEmail(ctx, c.Committer.Email); err == nil {
|
||||
committerUsername = committer.Name
|
||||
} else if !user_model.IsErrUserNotExist(err) {
|
||||
log.Error("GetUserByEmail: %v", err)
|
||||
@@ -67,12 +68,12 @@ func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCom
|
||||
UserName: committerUsername,
|
||||
},
|
||||
Timestamp: c.Author.When,
|
||||
Verification: ToVerification(c),
|
||||
Verification: ToVerification(ctx, c),
|
||||
}
|
||||
}
|
||||
|
||||
// ToCommit convert a git.Commit to api.Commit
|
||||
func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) {
|
||||
func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) {
|
||||
var apiAuthor, apiCommitter *api.User
|
||||
|
||||
// Retrieve author and committer information
|
||||
@@ -87,13 +88,13 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
|
||||
}
|
||||
|
||||
if ok {
|
||||
apiAuthor = ToUser(cacheAuthor, nil)
|
||||
apiAuthor = ToUser(ctx, cacheAuthor, nil)
|
||||
} else {
|
||||
author, err := user_model.GetUserByEmail(commit.Author.Email)
|
||||
author, err := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
||||
if err != nil && !user_model.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
apiAuthor = ToUser(author, nil)
|
||||
apiAuthor = ToUser(ctx, author, nil)
|
||||
if userCache != nil {
|
||||
userCache[commit.Author.Email] = author
|
||||
}
|
||||
@@ -109,13 +110,13 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
|
||||
}
|
||||
|
||||
if ok {
|
||||
apiCommitter = ToUser(cacheCommitter, nil)
|
||||
apiCommitter = ToUser(ctx, cacheCommitter, nil)
|
||||
} else {
|
||||
committer, err := user_model.GetUserByEmail(commit.Committer.Email)
|
||||
committer, err := user_model.GetUserByEmail(ctx, commit.Committer.Email)
|
||||
if err != nil && !user_model.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
apiCommitter = ToUser(committer, nil)
|
||||
apiCommitter = ToUser(ctx, committer, nil)
|
||||
if userCache != nil {
|
||||
userCache[commit.Committer.Email] = committer
|
||||
}
|
||||
@@ -161,7 +162,7 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
|
||||
SHA: commit.ID.String(),
|
||||
Created: commit.Committer.When,
|
||||
},
|
||||
Verification: ToVerification(commit),
|
||||
Verification: ToVerification(ctx, commit),
|
||||
},
|
||||
Author: apiAuthor,
|
||||
Committer: apiCommitter,
|
||||
|
@@ -41,7 +41,7 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
|
||||
URL: issue.APIURL(),
|
||||
HTMLURL: issue.HTMLURL(),
|
||||
Index: issue.Index,
|
||||
Poster: ToUser(issue.Poster, nil),
|
||||
Poster: ToUser(ctx, issue.Poster, nil),
|
||||
Title: issue.Title,
|
||||
Body: issue.Content,
|
||||
Attachments: ToAttachments(issue.Attachments),
|
||||
@@ -77,9 +77,9 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
|
||||
}
|
||||
if len(issue.Assignees) > 0 {
|
||||
for _, assignee := range issue.Assignees {
|
||||
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, nil))
|
||||
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(ctx, assignee, nil))
|
||||
}
|
||||
apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
||||
apiIssue.Assignee = ToUser(ctx, issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
||||
}
|
||||
if issue.IsPull {
|
||||
if err := issue.LoadPullRequest(ctx); err != nil {
|
||||
|
@@ -14,10 +14,10 @@ import (
|
||||
)
|
||||
|
||||
// ToComment converts a issues_model.Comment to the api.Comment format
|
||||
func ToComment(c *issues_model.Comment) *api.Comment {
|
||||
func ToComment(ctx context.Context, c *issues_model.Comment) *api.Comment {
|
||||
return &api.Comment{
|
||||
ID: c.ID,
|
||||
Poster: ToUser(c.Poster, nil),
|
||||
Poster: ToUser(ctx, c.Poster, nil),
|
||||
HTMLURL: c.HTMLURL(),
|
||||
IssueURL: c.IssueURL(),
|
||||
PRURL: c.PRURL(),
|
||||
@@ -69,7 +69,7 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
|
||||
comment := &api.TimelineComment{
|
||||
ID: c.ID,
|
||||
Type: c.Type.String(),
|
||||
Poster: ToUser(c.Poster, nil),
|
||||
Poster: ToUser(ctx, c.Poster, nil),
|
||||
HTMLURL: c.HTMLURL(),
|
||||
IssueURL: c.IssueURL(),
|
||||
PRURL: c.PRURL(),
|
||||
@@ -131,7 +131,7 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
|
||||
log.Error("LoadPoster: %v", err)
|
||||
return nil
|
||||
}
|
||||
comment.RefComment = ToComment(com)
|
||||
comment.RefComment = ToComment(ctx, com)
|
||||
}
|
||||
|
||||
if c.Label != nil {
|
||||
@@ -157,14 +157,14 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
|
||||
}
|
||||
|
||||
if c.Assignee != nil {
|
||||
comment.Assignee = ToUser(c.Assignee, nil)
|
||||
comment.Assignee = ToUser(ctx, c.Assignee, nil)
|
||||
}
|
||||
if c.AssigneeTeam != nil {
|
||||
comment.AssigneeTeam, _ = ToTeam(c.AssigneeTeam)
|
||||
comment.AssigneeTeam, _ = ToTeam(ctx, c.AssigneeTeam)
|
||||
}
|
||||
|
||||
if c.ResolveDoer != nil {
|
||||
comment.ResolveDoer = ToUser(c.ResolveDoer, nil)
|
||||
comment.ResolveDoer = ToUser(ctx, c.ResolveDoer, nil)
|
||||
}
|
||||
|
||||
if c.DependentIssue != nil {
|
||||
|
@@ -28,9 +28,9 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m
|
||||
|
||||
return &api.Package{
|
||||
ID: pd.Version.ID,
|
||||
Owner: ToUser(pd.Owner, doer),
|
||||
Owner: ToUser(ctx, pd.Owner, doer),
|
||||
Repository: repo,
|
||||
Creator: ToUser(pd.Creator, doer),
|
||||
Creator: ToUser(ctx, pd.Creator, doer),
|
||||
Type: string(pd.Package.Type),
|
||||
Name: pd.Package.Name,
|
||||
Version: pd.Version.Version,
|
||||
|
@@ -201,7 +201,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
|
||||
if pr.HasMerged {
|
||||
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
||||
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
||||
apiPullRequest.MergedBy = ToUser(pr.Merger, nil)
|
||||
apiPullRequest.MergedBy = ToUser(ctx, pr.Merger, nil)
|
||||
}
|
||||
|
||||
return apiPullRequest
|
||||
|
@@ -21,14 +21,14 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.
|
||||
r.Reviewer = user_model.NewGhostUser()
|
||||
}
|
||||
|
||||
apiTeam, err := ToTeam(r.ReviewerTeam)
|
||||
apiTeam, err := ToTeam(ctx, r.ReviewerTeam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &api.PullReview{
|
||||
ID: r.ID,
|
||||
Reviewer: ToUser(r.Reviewer, doer),
|
||||
Reviewer: ToUser(ctx, r.Reviewer, doer),
|
||||
ReviewerTeam: apiTeam,
|
||||
State: api.ReviewStateUnknown,
|
||||
Body: r.Content,
|
||||
@@ -93,8 +93,8 @@ func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, d
|
||||
apiComment := &api.PullReviewComment{
|
||||
ID: comment.ID,
|
||||
Body: comment.Content,
|
||||
Poster: ToUser(comment.Poster, doer),
|
||||
Resolver: ToUser(comment.ResolveDoer, doer),
|
||||
Poster: ToUser(ctx, comment.Poster, doer),
|
||||
Resolver: ToUser(ctx, comment.ResolveDoer, doer),
|
||||
ReviewID: review.ID,
|
||||
Created: comment.CreatedUnix.AsTime(),
|
||||
Updated: comment.UpdatedUnix.AsTime(),
|
||||
|
@@ -4,12 +4,14 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToRelease convert a repo_model.Release to api.Release
|
||||
func ToRelease(r *repo_model.Release) *api.Release {
|
||||
func ToRelease(ctx context.Context, r *repo_model.Release) *api.Release {
|
||||
return &api.Release{
|
||||
ID: r.ID,
|
||||
TagName: r.TagName,
|
||||
@@ -24,7 +26,7 @@ func ToRelease(r *repo_model.Release) *api.Release {
|
||||
IsPrerelease: r.IsPrerelease,
|
||||
CreatedAt: r.CreatedUnix.AsTime(),
|
||||
PublishedAt: r.CreatedUnix.AsTime(),
|
||||
Publisher: ToUser(r.Publisher, nil),
|
||||
Publisher: ToUser(ctx, r.Publisher, nil),
|
||||
Attachments: ToAttachments(r.Attachments),
|
||||
}
|
||||
}
|
||||
|
@@ -126,7 +126,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
|
||||
if err := t.LoadAttributes(ctx); err != nil {
|
||||
log.Warn("LoadAttributes of RepoTransfer: %v", err)
|
||||
} else {
|
||||
transfer = ToRepoTransfer(t)
|
||||
transfer = ToRepoTransfer(ctx, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
|
||||
|
||||
return &api.Repository{
|
||||
ID: repo.ID,
|
||||
Owner: ToUserWithAccessMode(repo.Owner, mode),
|
||||
Owner: ToUserWithAccessMode(ctx, repo.Owner, mode),
|
||||
Name: repo.Name,
|
||||
FullName: repo.FullName(),
|
||||
Description: repo.Description,
|
||||
@@ -185,7 +185,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
|
||||
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
|
||||
DefaultMergeStyle: string(defaultMergeStyle),
|
||||
DefaultAllowMaintainerEdit: defaultAllowMaintainerEdit,
|
||||
AvatarURL: repo.AvatarLink(),
|
||||
AvatarURL: repo.AvatarLink(ctx),
|
||||
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
||||
MirrorInterval: mirrorInterval,
|
||||
MirrorUpdated: mirrorUpdated,
|
||||
@@ -194,12 +194,12 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
|
||||
}
|
||||
|
||||
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
|
||||
func ToRepoTransfer(t *models.RepoTransfer) *api.RepoTransfer {
|
||||
teams, _ := ToTeams(t.Teams, false)
|
||||
func ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
|
||||
teams, _ := ToTeams(ctx, t.Teams, false)
|
||||
|
||||
return &api.RepoTransfer{
|
||||
Doer: ToUser(t.Doer, nil),
|
||||
Recipient: ToUser(t.Recipient, nil),
|
||||
Doer: ToUser(ctx, t.Doer, nil),
|
||||
Recipient: ToUser(ctx, t.Recipient, nil),
|
||||
Teams: teams,
|
||||
}
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ func ToCommitStatus(ctx context.Context, status *git_model.CommitStatus) *api.Co
|
||||
|
||||
if status.CreatorID != 0 {
|
||||
creator, _ := user_model.GetUserByID(ctx, status.CreatorID)
|
||||
apiStatus.Creator = ToUser(creator, nil)
|
||||
apiStatus.Creator = ToUser(ctx, creator, nil)
|
||||
}
|
||||
|
||||
return apiStatus
|
||||
|
@@ -4,6 +4,8 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
@@ -11,7 +13,7 @@ import (
|
||||
|
||||
// ToUser convert user_model.User to api.User
|
||||
// if doer is set, private information is added if the doer has the permission to see it
|
||||
func ToUser(user, doer *user_model.User) *api.User {
|
||||
func ToUser(ctx context.Context, user, doer *user_model.User) *api.User {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -21,36 +23,36 @@ func ToUser(user, doer *user_model.User) *api.User {
|
||||
signed = true
|
||||
authed = doer.ID == user.ID || doer.IsAdmin
|
||||
}
|
||||
return toUser(user, signed, authed)
|
||||
return toUser(ctx, user, signed, authed)
|
||||
}
|
||||
|
||||
// ToUsers convert list of user_model.User to list of api.User
|
||||
func ToUsers(doer *user_model.User, users []*user_model.User) []*api.User {
|
||||
func ToUsers(ctx context.Context, doer *user_model.User, users []*user_model.User) []*api.User {
|
||||
result := make([]*api.User, len(users))
|
||||
for i := range users {
|
||||
result[i] = ToUser(users[i], doer)
|
||||
result[i] = ToUser(ctx, users[i], doer)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ToUserWithAccessMode convert user_model.User to api.User
|
||||
// AccessMode is not none show add some more information
|
||||
func ToUserWithAccessMode(user *user_model.User, accessMode perm.AccessMode) *api.User {
|
||||
func ToUserWithAccessMode(ctx context.Context, user *user_model.User, accessMode perm.AccessMode) *api.User {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
return toUser(user, accessMode != perm.AccessModeNone, false)
|
||||
return toUser(ctx, user, accessMode != perm.AccessModeNone, false)
|
||||
}
|
||||
|
||||
// toUser convert user_model.User to api.User
|
||||
// signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself
|
||||
func toUser(user *user_model.User, signed, authed bool) *api.User {
|
||||
func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *api.User {
|
||||
result := &api.User{
|
||||
ID: user.ID,
|
||||
UserName: user.Name,
|
||||
FullName: user.FullName,
|
||||
Email: user.GetEmail(),
|
||||
AvatarURL: user.AvatarLink(),
|
||||
AvatarURL: user.AvatarLink(ctx),
|
||||
Created: user.CreatedUnix.AsTime(),
|
||||
Restricted: user.IsRestricted,
|
||||
Location: user.Location,
|
||||
@@ -97,9 +99,9 @@ func User2UserSettings(user *user_model.User) api.UserSettings {
|
||||
}
|
||||
|
||||
// ToUserAndPermission return User and its collaboration permission for a repository
|
||||
func ToUserAndPermission(user, doer *user_model.User, accessMode perm.AccessMode) api.RepoCollaboratorPermission {
|
||||
func ToUserAndPermission(ctx context.Context, user, doer *user_model.User, accessMode perm.AccessMode) api.RepoCollaboratorPermission {
|
||||
return api.RepoCollaboratorPermission{
|
||||
User: ToUser(user, doer),
|
||||
User: ToUser(ctx, user, doer),
|
||||
Permission: accessMode.String(),
|
||||
RoleName: accessMode.String(),
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ package convert
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
@@ -18,22 +19,22 @@ func TestUser_ToUser(t *testing.T) {
|
||||
|
||||
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, IsAdmin: true})
|
||||
|
||||
apiUser := toUser(user1, true, true)
|
||||
apiUser := toUser(db.DefaultContext, user1, true, true)
|
||||
assert.True(t, apiUser.IsAdmin)
|
||||
assert.Contains(t, apiUser.AvatarURL, "://")
|
||||
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2, IsAdmin: false})
|
||||
|
||||
apiUser = toUser(user2, true, true)
|
||||
apiUser = toUser(db.DefaultContext, user2, true, true)
|
||||
assert.False(t, apiUser.IsAdmin)
|
||||
|
||||
apiUser = toUser(user1, false, false)
|
||||
apiUser = toUser(db.DefaultContext, user1, false, false)
|
||||
assert.False(t, apiUser.IsAdmin)
|
||||
assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility)
|
||||
|
||||
user31 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate})
|
||||
|
||||
apiUser = toUser(user31, true, true)
|
||||
apiUser = toUser(db.DefaultContext, user31, true, true)
|
||||
assert.False(t, apiUser.IsAdmin)
|
||||
assert.EqualValues(t, api.VisibleTypePrivate.String(), apiUser.Visibility)
|
||||
}
|
||||
|
Reference in New Issue
Block a user