feat(coderd): add inbox notifications endpoints (#16889)

This PR is part of the inbox notifications topic, and rely on previous
PRs merged - it adds :

- Endpoints to : 
  - WS : watch new inbox notifications
  - REST : list inbox notifications
  - REST : update the read status of a notification

Also, this PR acts as a follow-up PR from previous work and : 

- fix DB query issues
- fix DBMem logic to match DB
This commit is contained in:
Vincent Vielle
2025-03-18 00:02:47 +01:00
committed by GitHub
parent e85c92e7d5
commit 3ae55bbbf4
20 changed files with 2093 additions and 65 deletions

View File

@ -13,8 +13,11 @@ import (
"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"
markdown "github.com/coder/coder/v2/coderd/render"
"github.com/coder/coder/v2/codersdk"
)
type InboxStore interface {
@ -23,12 +26,13 @@ type InboxStore interface {
// InboxHandler is responsible for dispatching notification messages to the Coder Inbox.
type InboxHandler struct {
log slog.Logger
store InboxStore
log slog.Logger
store InboxStore
pubsub pubsub.Pubsub
}
func NewInboxHandler(log slog.Logger, store InboxStore) *InboxHandler {
return &InboxHandler{log: log, store: store}
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) {
@ -62,7 +66,7 @@ func (s *InboxHandler) dispatch(payload types.MessagePayload, title, body string
}
// nolint:exhaustruct
_, err = s.store.InsertInboxNotification(ctx, database.InsertInboxNotificationParams{
insertedNotif, err := s.store.InsertInboxNotification(ctx, database.InsertInboxNotificationParams{
ID: msgID,
UserID: userID,
TemplateID: templateID,
@ -76,6 +80,38 @@ func (s *InboxHandler) dispatch(payload types.MessagePayload, title, body string
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
}
}