/
api_notification.go
131 lines (101 loc) · 3.29 KB
/
api_notification.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/twinj/uuid"
)
type NotificationRequest struct {
Content string `json:"content"`
Plan *NotificationPlan `json:"-"`
}
type NotifyPersonResponse struct {
Username string `json:"username"`
UUID string `json:"uuid"`
Content string `json:"content"`
Message string `json:"message"`
Error string `json:"error"`
}
// Notifies a Person by looking up their NotificationPlan and sending it to the notification engine.
func NotifyPerson(w http.ResponseWriter, r *http.Request) {
var res NotifyPersonResponse
var req NotificationRequest
vars := mux.Vars(r)
username := vars["person"]
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1024*20))
// If something went wrong, return an error in the JSON response
if err != nil {
res.Error = err.Error()
json.NewEncoder(w).Encode(res)
return
}
err = r.Body.Close()
if err != nil {
res.Error = err.Error()
json.NewEncoder(w).Encode(res)
return
}
err = json.Unmarshal(body, &req)
req.Plan, err = c.GetNotificationPlan(username)
if err != nil {
// res.Error = err.Error()
// errjson, _ := json.Marshal(res)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// Assign a UUID to this notification. The UUID is used to track notifications-in-progress (NIP) and to stop
// them when requested.
uuid.SwitchFormat(uuid.CleanHyphen)
req.Plan.ID = uuid.NewV4()
// Send our NotificationRequest to the notification engine
planChan <- &req
res = NotifyPersonResponse{
Message: "Notification initiated",
Content: req.Content,
UUID: req.Plan.ID.String(),
Username: req.Plan.Username,
}
json.NewEncoder(w).Encode(res)
}
// Stop a notification-in-progress (NIP) by sending the UUID to the notification engine
func StopNotification(w http.ResponseWriter, r *http.Request) {
var res NotifyPersonResponse
vars := mux.Vars(r)
id := vars["uuid"]
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if _, exists := NIP.Stoppers[id]; !exists {
res = NotifyPersonResponse{
Error: "No active notifications for this UUID",
UUID: id,
}
errjson, _ := json.Marshal(res)
http.Error(w, string(errjson), http.StatusNotFound)
return
}
// Attempt to stop the notification by sending the UUID to the notification engine
stopChan <- id
// TO DO: make sure that this is a valid UUID and obtain
// confirmation of deletion
res = NotifyPersonResponse{
Message: "Attempting to terminate notification",
UUID: id,
}
json.NewEncoder(w).Encode(res)
}
// A simple GET-able endpoint to stop notifications when a link is clicked in an email client
func StopNotificationClick(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["uuid"]
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
if _, exists := NIP.Stoppers[id]; !exists {
http.Error(w, fmt.Sprint("UUID not found."), http.StatusNotFound)
return
}
// Attempt to stop the notification by sending the UUID to the notification engine
stopChan <- id
fmt.Fprintln(w, "<html><body><b>Thank you!</b><br><br>Chicken Little has received your acknowledgement and you will no longer be notified with this message.</body></html>")
}