gitea源码

flash.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package middleware
  4. import (
  5. "fmt"
  6. "html/template"
  7. "net/http"
  8. "net/url"
  9. "code.gitea.io/gitea/modules/reqctx"
  10. )
  11. // Flash represents a one time data transfer between two requests.
  12. type Flash struct {
  13. DataStore reqctx.RequestDataStore
  14. url.Values
  15. ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
  16. }
  17. func (f *Flash) set(name, msg string, current ...bool) {
  18. if f.Values == nil {
  19. f.Values = make(map[string][]string)
  20. }
  21. showInCurrentPage := len(current) > 0 && current[0]
  22. if showInCurrentPage {
  23. // assign it to the context data, then the template can use ".Flash.XxxMsg" to render the message
  24. f.DataStore.GetData()["Flash"] = f
  25. } else {
  26. // the message map will be saved into the cookie and be shown in next response (a new page response which decodes the cookie)
  27. f.Set(name, msg)
  28. }
  29. }
  30. func flashMsgStringOrHTML(msg any) string {
  31. switch v := msg.(type) {
  32. case string:
  33. return v
  34. case template.HTML:
  35. return string(v)
  36. }
  37. panic(fmt.Sprintf("unknown type: %T", msg))
  38. }
  39. // Error sets error message
  40. func (f *Flash) Error(msg any, current ...bool) {
  41. f.ErrorMsg = flashMsgStringOrHTML(msg)
  42. f.set("error", f.ErrorMsg, current...)
  43. }
  44. // Warning sets warning message
  45. func (f *Flash) Warning(msg any, current ...bool) {
  46. f.WarningMsg = flashMsgStringOrHTML(msg)
  47. f.set("warning", f.WarningMsg, current...)
  48. }
  49. // Info sets info message
  50. func (f *Flash) Info(msg any, current ...bool) {
  51. f.InfoMsg = flashMsgStringOrHTML(msg)
  52. f.set("info", f.InfoMsg, current...)
  53. }
  54. // Success sets success message
  55. func (f *Flash) Success(msg any, current ...bool) {
  56. f.SuccessMsg = flashMsgStringOrHTML(msg)
  57. f.set("success", f.SuccessMsg, current...)
  58. }
  59. func ParseCookieFlashMessage(val string) *Flash {
  60. if vals, _ := url.ParseQuery(val); len(vals) > 0 {
  61. return &Flash{
  62. Values: vals,
  63. ErrorMsg: vals.Get("error"),
  64. SuccessMsg: vals.Get("success"),
  65. InfoMsg: vals.Get("info"),
  66. WarningMsg: vals.Get("warning"),
  67. }
  68. }
  69. return nil
  70. }
  71. func GetSiteCookieFlashMessage(dataStore reqctx.RequestDataStore, req *http.Request, cookieName string) (string, *Flash) {
  72. // Get the last flash message from cookie
  73. lastFlashCookie := GetSiteCookie(req, cookieName)
  74. lastFlashMsg := ParseCookieFlashMessage(lastFlashCookie)
  75. if lastFlashMsg != nil {
  76. lastFlashMsg.DataStore = dataStore
  77. return lastFlashCookie, lastFlashMsg
  78. }
  79. return lastFlashCookie, nil
  80. }