-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathinboxnotification.go
More file actions
136 lines (115 loc) · 4.04 KB
/
inboxnotification.go
File metadata and controls
136 lines (115 loc) · 4.04 KB
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
132
133
134
135
136
package codersdk
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
)
const (
InboxNotificationFallbackIconWorkspace = "DEFAULT_ICON_WORKSPACE"
InboxNotificationFallbackIconAccount = "DEFAULT_ICON_ACCOUNT"
InboxNotificationFallbackIconTemplate = "DEFAULT_ICON_TEMPLATE"
InboxNotificationFallbackIconOther = "DEFAULT_ICON_OTHER"
)
type InboxNotification struct {
ID uuid.UUID `json:"id" format:"uuid"`
UserID uuid.UUID `json:"user_id" format:"uuid"`
TemplateID uuid.UUID `json:"template_id" format:"uuid"`
Targets []uuid.UUID `json:"targets" format:"uuid"`
Title string `json:"title"`
Content string `json:"content"`
Icon string `json:"icon"`
Actions []InboxNotificationAction `json:"actions"`
ReadAt *time.Time `json:"read_at"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
}
type InboxNotificationAction struct {
Label string `json:"label"`
URL string `json:"url"`
}
type GetInboxNotificationResponse struct {
Notification InboxNotification `json:"notification"`
UnreadCount int `json:"unread_count"`
}
type ListInboxNotificationsRequest struct {
Targets string `json:"targets,omitempty"`
Templates string `json:"templates,omitempty"`
ReadStatus string `json:"read_status,omitempty"`
StartingBefore string `json:"starting_before,omitempty"`
}
type ListInboxNotificationsResponse struct {
Notifications []InboxNotification `json:"notifications"`
UnreadCount int `json:"unread_count"`
}
func ListInboxNotificationsRequestToQueryParams(req ListInboxNotificationsRequest) []RequestOption {
var opts []RequestOption
if req.Targets != "" {
opts = append(opts, WithQueryParam("targets", req.Targets))
}
if req.Templates != "" {
opts = append(opts, WithQueryParam("templates", req.Templates))
}
if req.ReadStatus != "" {
opts = append(opts, WithQueryParam("read_status", req.ReadStatus))
}
if req.StartingBefore != "" {
opts = append(opts, WithQueryParam("starting_before", req.StartingBefore))
}
return opts
}
func (c *Client) ListInboxNotifications(ctx context.Context, req ListInboxNotificationsRequest) (ListInboxNotificationsResponse, error) {
res, err := c.Request(
ctx, http.MethodGet,
"/api/v2/notifications/inbox",
nil, ListInboxNotificationsRequestToQueryParams(req)...,
)
if err != nil {
return ListInboxNotificationsResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ListInboxNotificationsResponse{}, ReadBodyAsError(res)
}
var listInboxNotificationsResponse ListInboxNotificationsResponse
return listInboxNotificationsResponse, json.NewDecoder(res.Body).Decode(&listInboxNotificationsResponse)
}
type UpdateInboxNotificationReadStatusRequest struct {
IsRead bool `json:"is_read"`
}
type UpdateInboxNotificationReadStatusResponse struct {
Notification InboxNotification `json:"notification"`
UnreadCount int `json:"unread_count"`
}
func (c *Client) UpdateInboxNotificationReadStatus(ctx context.Context, notifID string, req UpdateInboxNotificationReadStatusRequest) (UpdateInboxNotificationReadStatusResponse, error) {
res, err := c.Request(
ctx, http.MethodPut,
fmt.Sprintf("/api/v2/notifications/inbox/%v/read-status", notifID),
req,
)
if err != nil {
return UpdateInboxNotificationReadStatusResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return UpdateInboxNotificationReadStatusResponse{}, ReadBodyAsError(res)
}
var resp UpdateInboxNotificationReadStatusResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}
func (c *Client) MarkAllInboxNotificationsAsRead(ctx context.Context) error {
res, err := c.Request(
ctx, http.MethodPut,
"/api/v2/notifications/inbox/mark-all-as-read",
nil,
)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return ReadBodyAsError(res)
}
return nil
}