-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
53 lines (46 loc) · 1.53 KB
/
notifications.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
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
)
func AddNotification(w http.ResponseWriter, r *http.Request, notificationMessage, notificationType string) {
// create map for hx-trigger header value
hxTriggerVal := make(map[string]interface{})
// get current header value
currentHxTriggerVal := w.Header().Get("Hx-Trigger")
if strings.HasPrefix(currentHxTriggerVal, "{") {
// convert current header value to map
err := json.Unmarshal([]byte(currentHxTriggerVal), &hxTriggerVal)
if err != nil {
log.Printf("Error unmarshalling hx-trigger header: %s, header value: %s", err, currentHxTriggerVal)
}
} else if currentHxTriggerVal != "" {
// convert current header value to map
hxTriggerVal = map[string]interface{}{
currentHxTriggerVal: true,
}
}
// check if notifications key exists
notifications, ok := hxTriggerVal["notifications"].([]interface{})
// if not, create it
if !ok {
notifications = []interface{}{}
}
// append new notification
notifications = append(notifications, map[string]string{
"message": notificationMessage,
"type": notificationType,
})
// add notifications to hxTriggerVal
hxTriggerVal["notifications"] = notifications
// convert hxTriggerVal to json
hxTriggerValJson, err := json.Marshal(hxTriggerVal)
if err != nil {
log.Printf("Error marshalling HxTriggerVal: %s, value: %s", err, hxTriggerVal)
}
// add hxTriggerValJson as header value
w.Header().Set("Hx-Trigger", string(hxTriggerValJson))
log.Printf("New Notification added: type: %s", notificationType)
}