-
Notifications
You must be signed in to change notification settings - Fork 2
/
httphandler.go
105 lines (96 loc) · 3.1 KB
/
httphandler.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
// Package httphandler contains an API handler for notification.Service.
package httphandler
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/httperror"
)
// Notification is an API handler for notification.Service.
// It returns errors compatible with httperror package.
type Notification struct {
Notification notification.Service
}
func (h Notification) ListNotifications(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodGet {
return httperror.Method{Allowed: []string{http.MethodGet}}
}
var opt notification.ListOptions // TODO: Automate this conversion process.
opt.Namespace = req.URL.Query().Get("Namespace")
opt.All, _ = strconv.ParseBool(req.URL.Query().Get("All"))
notifs, err := h.Notification.ListNotifications(req.Context(), opt)
return httperror.JSONResponse{V: struct {
Notifs []notification.Notification
Error errorJSON
}{notifs, errorJSON{err}}}
}
func (h Notification) StreamNotifications(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodGet {
return httperror.Method{Allowed: []string{http.MethodGet}}
}
fl, ok := w.(http.Flusher)
if !ok {
return fmt.Errorf("http.ResponseWriter %T is not a http.Flusher", w)
}
ch := make(chan []notification.Notification, 8)
err := h.Notification.StreamNotifications(req.Context(), ch)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
for {
select {
case <-req.Context().Done():
return req.Context().Err()
case notifs := <-ch:
err := enc.Encode(notifs)
if err != nil {
return err
}
fl.Flush()
}
}
}
func (h Notification) CountNotifications(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodGet {
return httperror.Method{Allowed: []string{http.MethodGet}}
}
c, err := h.Notification.CountNotifications(req.Context())
return httperror.JSONResponse{V: struct {
Count uint64
Error errorJSON
}{c, errorJSON{err}}}
}
func (h Notification) MarkThreadRead(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodPost {
return httperror.Method{Allowed: []string{http.MethodPost}}
}
q := req.URL.Query() // TODO: Automate this conversion process.
namespace := q.Get("Namespace")
threadType := q.Get("ThreadType")
threadID, err := strconv.ParseUint(q.Get("ThreadID"), 10, 64)
if err != nil {
return httperror.BadRequest{Err: fmt.Errorf("parsing ThreadID query parameter: %v", err)}
}
err = h.Notification.MarkThreadRead(req.Context(), namespace, threadType, threadID)
return err
}
// errorJSON marshals an error value into JSON.
//
// A nil Err value is encoded as the null JSON value, otherwise
// the string returned by the Error method is encoded as a JSON string.
type errorJSON struct {
Err error
}
// MarshalJSON implements the json.Marshaler interface.
func (e errorJSON) MarshalJSON() ([]byte, error) {
if e.Err == nil {
return []byte("null"), nil
}
return json.Marshal(e.Err.Error())
}