Skip to content

Commit 994e9dd

Browse files
authored
Merge branch 'main' into rtli
2 parents 094a9fa + 8b5c081 commit 994e9dd

32 files changed

+318
-1100
lines changed

modules/context/context.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
107107
return ctx
108108
}
109109

110+
func NewTemplateContextForWeb(ctx *Context) TemplateContext {
111+
tmplCtx := NewTemplateContext(ctx)
112+
tmplCtx["Locale"] = ctx.Base.Locale
113+
tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx)
114+
return tmplCtx
115+
}
116+
117+
func NewWebContext(base *Base, render Render, session session.Store) *Context {
118+
ctx := &Context{
119+
Base: base,
120+
Render: render,
121+
Session: session,
122+
123+
Cache: mc.GetCache(),
124+
Link: setting.AppSubURL + strings.TrimSuffix(base.Req.URL.EscapedPath(), "/"),
125+
Repo: &Repository{PullRequest: &PullRequest{}},
126+
Org: &Organization{},
127+
}
128+
ctx.TemplateContext = NewTemplateContextForWeb(ctx)
129+
ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
130+
return ctx
131+
}
132+
110133
// Contexter initializes a classic context for a request.
111134
func Contexter() func(next http.Handler) http.Handler {
112135
rnd := templates.HTMLRenderer()
@@ -127,21 +150,8 @@ func Contexter() func(next http.Handler) http.Handler {
127150
return func(next http.Handler) http.Handler {
128151
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
129152
base, baseCleanUp := NewBaseContext(resp, req)
130-
ctx := &Context{
131-
Base: base,
132-
Cache: mc.GetCache(),
133-
Link: setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/"),
134-
Render: rnd,
135-
Session: session.GetSession(req),
136-
Repo: &Repository{PullRequest: &PullRequest{}},
137-
Org: &Organization{},
138-
}
139153
defer baseCleanUp()
140-
141-
// TODO: "install.go" also shares the same logic, which should be refactored to a general function
142-
ctx.TemplateContext = NewTemplateContext(ctx)
143-
ctx.TemplateContext["Locale"] = ctx.Locale
144-
ctx.TemplateContext["AvatarUtils"] = templates.NewAvatarUtils(ctx)
154+
ctx := NewWebContext(base, rnd, session.GetSession(req))
145155

146156
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
147157
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
@@ -172,8 +182,7 @@ func Contexter() func(next http.Handler) http.Handler {
172182
}
173183
}
174184

175-
// prepare an empty Flash message for current request
176-
ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
185+
// if there are new messages in the ctx.Flash, write them into cookie
177186
ctx.Resp.Before(func(resp ResponseWriter) {
178187
if val := ctx.Flash.Encode(); val != "" {
179188
middleware.SetSiteCookie(ctx.Resp, CookieNameFlash, val, 0)

modules/context/package.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,10 @@ func PackageContexter() func(next http.Handler) http.Handler {
154154
return func(next http.Handler) http.Handler {
155155
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
156156
base, baseCleanUp := NewBaseContext(resp, req)
157-
ctx := &Context{
158-
Base: base,
159-
Render: renderer, // it is still needed when rendering 500 page in a package handler
160-
}
161157
defer baseCleanUp()
162158

159+
// it is still needed when rendering 500 page in a package handler
160+
ctx := NewWebContext(base, renderer, nil)
163161
ctx.Base.AppendContextValue(WebContextKey, ctx)
164162
next.ServeHTTP(ctx.Resp, ctx.Req)
165163
})

modules/test/context_tests.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.Resp
4545
resp := httptest.NewRecorder()
4646
req := mockRequest(t, reqPath)
4747
base, baseCleanUp := context.NewBaseContext(resp, req)
48+
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
4849
base.Data = middleware.GetContextData(req.Context())
4950
base.Locale = &translation.MockLocale{}
50-
ctx := &context.Context{
51-
Base: base,
52-
Render: &mockRender{},
53-
Flash: &middleware.Flash{Values: url.Values{}},
54-
}
55-
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
51+
52+
ctx := context.NewWebContext(base, &MockRender{}, nil)
53+
ctx.Flash = &middleware.Flash{Values: url.Values{}}
5654

5755
chiCtx := chi.NewRouteContext()
5856
ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx)
@@ -148,13 +146,13 @@ func LoadGitRepo(t *testing.T, ctx *context.Context) {
148146
assert.NoError(t, err)
149147
}
150148

151-
type mockRender struct{}
149+
type MockRender struct{}
152150

153-
func (tr *mockRender) TemplateLookup(tmpl string, _ gocontext.Context) (templates.TemplateExecutor, error) {
151+
func (tr *MockRender) TemplateLookup(tmpl string, _ gocontext.Context) (templates.TemplateExecutor, error) {
154152
return nil, nil
155153
}
156154

157-
func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ any, _ gocontext.Context) error {
155+
func (tr *MockRender) HTML(w io.Writer, status int, _ string, _ any, _ gocontext.Context) error {
158156
if resp, ok := w.(http.ResponseWriter); ok {
159157
resp.WriteHeader(status)
160158
}

options/locale/locale_en-US.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,6 @@ overview = Overview
598598
following = Following
599599
follow = Follow
600600
unfollow = Unfollow
601-
heatmap.loading = Loading Heatmap…
602601
user_bio = Biography
603602
disabled_public_activity = This user has disabled the public visibility of the activity.
604603
email_visibility.limited = Your email address is visible to all authenticated users

0 commit comments

Comments
 (0)