-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathinbox.go
More file actions
104 lines (90 loc) · 3.02 KB
/
Copy pathinbox.go
File metadata and controls
104 lines (90 loc) · 3.02 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
package dispatch
import (
"context"
"encoding/json"
"text/template"
"github.com/google/uuid"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/coderd/notifications/types"
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
"github.com/coder/coder/v2/codersdk"
)
type InboxStore interface {
InsertInboxNotification(ctx context.Context, arg database.InsertInboxNotificationParams) (database.InboxNotification, error)
}
// InboxHandler is responsible for dispatching notification messages to the Coder Inbox.
type InboxHandler struct {
log slog.Logger
store InboxStore
pubsub pubsub.Pubsub
}
func NewInboxHandler(log slog.Logger, store InboxStore, ps pubsub.Pubsub) *InboxHandler {
return &InboxHandler{log: log, store: store, pubsub: ps}
}
func (s *InboxHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string, _ template.FuncMap) (DeliveryFunc, error) {
return s.dispatch(payload, titleTmpl, bodyTmpl), nil
}
func (s *InboxHandler) dispatch(payload types.MessagePayload, title, body string) DeliveryFunc {
return func(ctx context.Context, msgID uuid.UUID) (bool, error) {
userID, err := uuid.Parse(payload.UserID)
if err != nil {
return false, xerrors.Errorf("parse user ID: %w", err)
}
templateID, err := uuid.Parse(payload.NotificationTemplateID)
if err != nil {
return false, xerrors.Errorf("parse template ID: %w", err)
}
actions, err := json.Marshal(payload.Actions)
if err != nil {
return false, xerrors.Errorf("marshal actions: %w", err)
}
// nolint:exhaustruct
insertedNotif, err := s.store.InsertInboxNotification(ctx, database.InsertInboxNotificationParams{
ID: msgID,
UserID: userID,
TemplateID: templateID,
Targets: payload.Targets,
Title: title,
Content: body,
Actions: actions,
CreatedAt: dbtime.Now(),
})
if err != nil {
return false, xerrors.Errorf("insert inbox notification: %w", err)
}
event := coderdpubsub.InboxNotificationEvent{
Kind: coderdpubsub.InboxNotificationEventKindNew,
InboxNotification: codersdk.InboxNotification{
ID: msgID,
UserID: userID,
TemplateID: templateID,
Targets: payload.Targets,
Title: title,
Content: body,
Actions: func() []codersdk.InboxNotificationAction {
var actions []codersdk.InboxNotificationAction
err := json.Unmarshal(insertedNotif.Actions, &actions)
if err != nil {
return actions
}
return actions
}(),
ReadAt: nil, // notification just has been inserted
CreatedAt: insertedNotif.CreatedAt,
},
}
payload, err := json.Marshal(event)
if err != nil {
return false, xerrors.Errorf("marshal event: %w", err)
}
err = s.pubsub.Publish(coderdpubsub.InboxNotificationForOwnerEventChannel(userID), payload)
if err != nil {
return false, xerrors.Errorf("publish event: %w", err)
}
return false, nil
}
}