Make web context initialize correctly for different cases (#26726)
The web context (modules/context.Context) is quite complex, it's difficult for the callers to initialize correctly. This PR introduces a `NewWebContext` function, to make sure the web context have the same behavior for different cases.
This commit is contained in:
parent
ee9e83b230
commit
412e5c0946
11 changed files with 50 additions and 54 deletions
|
@ -60,17 +60,9 @@ func Contexter() func(next http.Handler) http.Handler {
|
|||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
||||
base, baseCleanUp := context.NewBaseContext(resp, req)
|
||||
ctx := &context.Context{
|
||||
Base: base,
|
||||
Flash: &middleware.Flash{},
|
||||
Render: rnd,
|
||||
Session: session.GetSession(req),
|
||||
}
|
||||
defer baseCleanUp()
|
||||
|
||||
ctx.TemplateContext = context.NewTemplateContext(ctx)
|
||||
ctx.TemplateContext["Locale"] = ctx.Locale
|
||||
|
||||
ctx := context.NewWebContext(base, rnd, session.GetSession(req))
|
||||
ctx.AppendContextValue(context.WebContextKey, ctx)
|
||||
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
|
||||
ctx.Data.MergeFrom(middleware.ContextData{
|
||||
|
|
|
@ -191,7 +191,7 @@ func List(ctx *context.Context) {
|
|||
ctx.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ctx.Data["Actors"] = repo.MakeSelfOnTop(ctx, actors)
|
||||
ctx.Data["Actors"] = repo.MakeSelfOnTop(ctx.Doer, actors)
|
||||
|
||||
ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx)
|
||||
|
||||
|
|
|
@ -7,16 +7,15 @@ import (
|
|||
"sort"
|
||||
|
||||
"code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
)
|
||||
|
||||
func MakeSelfOnTop(ctx *context.Context, users []*user.User) []*user.User {
|
||||
if ctx.Doer != nil {
|
||||
func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
|
||||
if doer != nil {
|
||||
sort.Slice(users, func(i, j int) bool {
|
||||
if users[i].ID == users[j].ID {
|
||||
return false
|
||||
}
|
||||
return users[i].ID == ctx.Doer.ID // if users[i] is self, put it before others, so less=true
|
||||
return users[i].ID == doer.ID // if users[i] is self, put it before others, so less=true
|
||||
})
|
||||
}
|
||||
return users
|
||||
|
|
|
@ -7,21 +7,20 @@ import (
|
|||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMakeSelfOnTop(t *testing.T) {
|
||||
users := MakeSelfOnTop(&context.Context{}, []*user.User{{ID: 2}, {ID: 1}})
|
||||
users := MakeSelfOnTop(nil, []*user.User{{ID: 2}, {ID: 1}})
|
||||
assert.Len(t, users, 2)
|
||||
assert.EqualValues(t, 2, users[0].ID)
|
||||
|
||||
users = MakeSelfOnTop(&context.Context{Doer: &user.User{ID: 1}}, []*user.User{{ID: 2}, {ID: 1}})
|
||||
users = MakeSelfOnTop(&user.User{ID: 1}, []*user.User{{ID: 2}, {ID: 1}})
|
||||
assert.Len(t, users, 2)
|
||||
assert.EqualValues(t, 1, users[0].ID)
|
||||
|
||||
users = MakeSelfOnTop(&context.Context{Doer: &user.User{ID: 2}}, []*user.User{{ID: 2}, {ID: 1}})
|
||||
users = MakeSelfOnTop(&user.User{ID: 2}, []*user.User{{ID: 2}, {ID: 1}})
|
||||
assert.Len(t, users, 2)
|
||||
assert.EqualValues(t, 2, users[0].ID)
|
||||
}
|
||||
|
|
|
@ -331,7 +331,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
|
|||
ctx.ServerError("GetRepoAssignees", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
||||
|
||||
handleTeamMentions(ctx)
|
||||
if ctx.Written() {
|
||||
|
@ -535,7 +535,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *repo_model.R
|
|||
ctx.ServerError("GetRepoAssignees", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
||||
|
||||
handleTeamMentions(ctx)
|
||||
}
|
||||
|
@ -3625,7 +3625,7 @@ func issuePosters(ctx *context.Context, isPullList bool) {
|
|||
}
|
||||
}
|
||||
|
||||
posters = MakeSelfOnTop(ctx, posters)
|
||||
posters = MakeSelfOnTop(ctx.Doer, posters)
|
||||
|
||||
resp := &userSearchResponse{}
|
||||
resp.Results = make([]*userSearchInfo, len(posters))
|
||||
|
|
|
@ -956,7 +956,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
|
|||
ctx.ServerError("GetRepoAssignees", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
||||
|
||||
handleTeamMentions(ctx)
|
||||
if ctx.Written() {
|
||||
|
|
|
@ -349,7 +349,7 @@ func NewRelease(ctx *context.Context) {
|
|||
ctx.ServerError("GetRepoAssignees", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
||||
|
||||
upload.AddUploadContext(ctx, "release")
|
||||
|
||||
|
@ -538,7 +538,7 @@ func EditRelease(ctx *context.Context) {
|
|||
ctx.ServerError("GetRepoAssignees", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
|
||||
ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
||||
|
||||
ctx.HTML(http.StatusOK, tplReleaseNew)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue