-
Notifications
You must be signed in to change notification settings - Fork 36
/
flashes.go
110 lines (84 loc) · 2.53 KB
/
flashes.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package errors
import (
"encoding/gob"
"fmt"
"html/template"
"net/http"
"github.com/gorilla/sessions"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/web/i18n"
)
type FlashHelper struct {
store sessions.Store
locHelper *i18n.Helper
}
func NewFlashHelper(s sessions.Store, loc *i18n.Helper) *FlashHelper {
gob.Register(FlashMessage{})
return &FlashHelper{
store: s,
locHelper: loc,
}
}
const flashSession = "go-ssb-room-flash-messages"
type FlashKind uint
const (
_ FlashKind = iota
// FlashError signals that a problem occured
FlashError
// FlashNotification represents a normal message (like "xyz added/updated successfull")
FlashNotification
)
type FlashMessage struct {
Kind FlashKind
Message template.HTML
}
// TODO: rethink error return - maybe panic() / maybe render package?
// AddMessage expects a i18n label, translates it and adds it as a FlashNotification
func (fh FlashHelper) AddMessage(rw http.ResponseWriter, req *http.Request, label string) {
session, err := fh.store.Get(req, flashSession)
if err != nil {
panic(fmt.Errorf("flashHelper: failed to get session: %w", err))
}
ih := fh.locHelper.FromRequest(req)
session.AddFlash(FlashMessage{
Kind: FlashNotification,
Message: ih.LocalizeSimple(label),
})
if err := session.Save(req, rw); err != nil {
panic(fmt.Errorf("flashHelper: failed to save session: %w", err))
}
}
// AddError adds a FlashError and translates the passed err using localizeError()
func (fh FlashHelper) AddError(rw http.ResponseWriter, req *http.Request, err error) {
session, getErr := fh.store.Get(req, flashSession)
if getErr != nil {
panic(fmt.Errorf("flashHelper: failed to get session: %w", err))
}
ih := fh.locHelper.FromRequest(req)
_, msg := localizeError(ih, err)
session.AddFlash(FlashMessage{
Kind: FlashError,
Message: msg,
})
if err := session.Save(req, rw); err != nil {
panic(fmt.Errorf("flashHelper: failed to save session: %w", err))
}
}
// GetAll returns all the FlashMessages, emptys and updates the store
func (fh FlashHelper) GetAll(rw http.ResponseWriter, req *http.Request) ([]FlashMessage, error) {
session, err := fh.store.Get(req, flashSession)
if err != nil {
return nil, err
}
opaqueFlashes := session.Flashes()
flashes := make([]FlashMessage, len(opaqueFlashes))
for i, of := range opaqueFlashes {
f, ok := of.(FlashMessage)
if !ok {
return nil, fmt.Errorf("GetFlashes: failed to unpack flash: %T", of)
}
flashes[i].Kind = f.Kind
flashes[i].Message = f.Message
}
err = session.Save(req, rw)
return flashes, err
}