summaryrefslogtreecommitdiffstats
path: root/modules/web/middleware/flash.go
blob: 88da2049a41cf47a9341b56dedd12ff660e6af1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package middleware

import (
	"fmt"
	"html/template"
	"net/url"
)

// Flash represents a one time data transfer between two requests.
type Flash struct {
	DataStore ContextDataStore
	url.Values
	ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
}

func (f *Flash) set(name, msg string, current ...bool) {
	if f.Values == nil {
		f.Values = make(map[string][]string)
	}
	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)
	}
}

func flashMsgStringOrHTML(msg any) string {
	switch v := msg.(type) {
	case string:
		return v
	case template.HTML:
		return string(v)
	}
	panic(fmt.Sprintf("unknown type: %T", msg))
}

// Error sets error message
func (f *Flash) Error(msg any, current ...bool) {
	f.ErrorMsg = flashMsgStringOrHTML(msg)
	f.set("error", f.ErrorMsg, current...)
}

// Warning sets warning message
func (f *Flash) Warning(msg any, current ...bool) {
	f.WarningMsg = flashMsgStringOrHTML(msg)
	f.set("warning", f.WarningMsg, current...)
}

// Info sets info message
func (f *Flash) Info(msg any, current ...bool) {
	f.InfoMsg = flashMsgStringOrHTML(msg)
	f.set("info", f.InfoMsg, current...)
}

// Success sets success message
func (f *Flash) Success(msg any, current ...bool) {
	f.SuccessMsg = flashMsgStringOrHTML(msg)
	f.set("success", f.SuccessMsg, current...)
}