]> source.dussan.org Git - gitea.git/commitdiff
Fix "Flash" message usage (#25895)
authorwxiaoguang <wxiaoguang@gmail.com>
Sat, 15 Jul 2023 08:52:03 +0000 (16:52 +0800)
committerGitHub <noreply@github.com>
Sat, 15 Jul 2023 08:52:03 +0000 (11:52 +0300)
Resolve https://github.com/go-gitea/gitea/pull/25820/files#r1264309059

modules/web/middleware/flash.go
routers/web/repo/pull.go

index fa29ddeffc79d14e5ce2098e35bcabbed77e1a50..41f3aac27c5f936aef9ea26f7c0902e4ffda3f72 100644 (file)
@@ -5,17 +5,6 @@ package middleware
 
 import "net/url"
 
-// flashes enumerates all the flash types
-const (
-       SuccessFlash = "SuccessMsg"
-       ErrorFlash   = "ErrorMsg"
-       WarnFlash    = "WarningMsg"
-       InfoFlash    = "InfoMsg"
-)
-
-// FlashNow FIXME:
-var FlashNow bool
-
 // Flash represents a one time data transfer between two requests.
 type Flash struct {
        DataStore ContextDataStore
@@ -27,15 +16,12 @@ func (f *Flash) set(name, msg string, current ...bool) {
        if f.Values == nil {
                f.Values = make(map[string][]string)
        }
-       isShow := false
-       if (len(current) == 0 && FlashNow) ||
-               (len(current) > 0 && current[0]) {
-               isShow = true
-       }
-
-       if isShow {
+       showInCurrentPage := len(current) > 0 && current[0]
+       if showInCurrentPage {
+               // assign it to the context data, then the template can use ".Flash.XxxMsg" to render the message
                f.DataStore.GetData()["Flash"] = f
        } else {
+               // the message map will be saved into the cookie and be shown in next response (a new page response which decodes the cookie)
                f.Set(name, msg)
        }
 }
index 505e1424cd119a7b0aa90a54052e65bd985d618e..c5f260adfa745bf89cc14d90cb6ebd1a216d52af 100644 (file)
@@ -175,10 +175,8 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
        } else if len(orgs) > 0 {
                ctx.Data["ContextUser"] = orgs[0]
        } else {
-               msg := ctx.Tr("repo.fork_no_valid_owners")
-               ctx.Data["Flash"] = ctx.Flash
-               ctx.Flash.Error(msg)
                ctx.Data["CanForkRepo"] = false
+               ctx.Flash.Error(ctx.Tr("repo.fork_no_valid_owners"), true)
                return nil
        }
 
@@ -194,8 +192,7 @@ func Fork(ctx *context.Context) {
        } else {
                maxCreationLimit := ctx.Doer.MaxCreationLimit()
                msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit)
-               ctx.Data["Flash"] = ctx.Flash
-               ctx.Flash.Error(msg)
+               ctx.Flash.Error(msg, true)
        }
 
        getForkRepository(ctx)