mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(coderd): add new dispatch logic for coder inbox (#16764)
This PR is [resolving the dispatch part of Coder Inbocx](https://github.com/coder/internal/issues/403). Since the DB layer has been merged - we now want to insert notifications into Coder Inbox in parallel of the other delivery target. To do so, we push two messages instead of one using the `Enqueue` method.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"text/template"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/notifications/types"
|
||||
markdown "github.com/coder/coder/v2/coderd/render"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func NewInboxHandler(log slog.Logger, store InboxStore) *InboxHandler {
|
||||
return &InboxHandler{log: log, store: store}
|
||||
}
|
||||
|
||||
func (s *InboxHandler) Dispatcher(payload types.MessagePayload, titleTmpl, bodyTmpl string, _ template.FuncMap) (DeliveryFunc, error) {
|
||||
subject, err := markdown.PlaintextFromMarkdown(titleTmpl)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("render subject: %w", err)
|
||||
}
|
||||
|
||||
htmlBody, err := markdown.PlaintextFromMarkdown(bodyTmpl)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("render html body: %w", err)
|
||||
}
|
||||
|
||||
return s.dispatch(payload, subject, htmlBody), 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
|
||||
_, 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)
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package dispatch_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/notifications/dispatch"
|
||||
"github.com/coder/coder/v2/coderd/notifications/types"
|
||||
)
|
||||
|
||||
func TestInbox(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
tests := []struct {
|
||||
name string
|
||||
msgID uuid.UUID
|
||||
payload types.MessagePayload
|
||||
expectedErr string
|
||||
expectedRetry bool
|
||||
}{
|
||||
{
|
||||
name: "OK",
|
||||
msgID: uuid.New(),
|
||||
payload: types.MessagePayload{
|
||||
NotificationName: "test",
|
||||
NotificationTemplateID: notifications.TemplateWorkspaceDeleted.String(),
|
||||
UserID: "valid",
|
||||
Actions: []types.TemplateAction{
|
||||
{
|
||||
Label: "View my workspace",
|
||||
URL: "https://coder.com/workspaces/1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "InvalidUserID",
|
||||
payload: types.MessagePayload{
|
||||
NotificationName: "test",
|
||||
NotificationTemplateID: notifications.TemplateWorkspaceDeleted.String(),
|
||||
UserID: "invalid",
|
||||
Actions: []types.TemplateAction{},
|
||||
},
|
||||
expectedErr: "parse user ID",
|
||||
expectedRetry: false,
|
||||
},
|
||||
{
|
||||
name: "InvalidTemplateID",
|
||||
payload: types.MessagePayload{
|
||||
NotificationName: "test",
|
||||
NotificationTemplateID: "invalid",
|
||||
UserID: "valid",
|
||||
Actions: []types.TemplateAction{},
|
||||
},
|
||||
expectedErr: "parse template ID",
|
||||
expectedRetry: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
|
||||
if tc.payload.UserID == "valid" {
|
||||
user := dbgen.User(t, db, database.User{})
|
||||
tc.payload.UserID = user.ID.String()
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
handler := dispatch.NewInboxHandler(logger.Named("smtp"), db)
|
||||
dispatcherFunc, err := handler.Dispatcher(tc.payload, "", "", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
retryable, err := dispatcherFunc(ctx, tc.msgID)
|
||||
|
||||
if tc.expectedErr != "" {
|
||||
require.ErrorContains(t, err, tc.expectedErr)
|
||||
require.Equal(t, tc.expectedRetry, retryable)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.False(t, retryable)
|
||||
uid := uuid.MustParse(tc.payload.UserID)
|
||||
notifs, err := db.GetInboxNotificationsByUserID(ctx, database.GetInboxNotificationsByUserIDParams{
|
||||
UserID: uid,
|
||||
ReadStatus: database.InboxNotificationReadStatusAll,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, notifs, 1)
|
||||
require.Equal(t, tc.msgID, notifs[0].ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user