Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: add fallback icons for notifications#17013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
defelmnq merged 11 commits intomainfromnotif-inbox/internal-522
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
add tests and improve fallback logic
  • Loading branch information
@defelmnq
defelmnq committedMar 27, 2025
commit0469e574df9822841fae439098cda07c59008722
32 changes: 17 additions & 15 deletionscoderd/inboxnotifications.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,6 @@ const (
var fallbackIcons = map[uuid.UUID]string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

alternative data structure:

fallbackIcons = map[NotificationIcon][]uuid.UUID

not a must, just a preference

// workspace related notifications
notifications.TemplateWorkspaceCreated: FallbackIconWorkspace,
notifications.TemplateWorkspaceCreated: FallbackIconWorkspace,
notifications.TemplateWorkspaceManuallyUpdated: FallbackIconWorkspace,
notifications.TemplateWorkspaceDeleted: FallbackIconWorkspace,
notifications.TemplateWorkspaceAutobuildFailed: FallbackIconWorkspace,
Expand All@@ -63,27 +62,30 @@ var fallbackIcons = map[uuid.UUID]string{
notifications.TemplateTemplateDeleted: FallbackIconTemplate,
notifications.TemplateTemplateDeprecated: FallbackIconTemplate,
notifications.TemplateWorkspaceBuildsFailedReport: FallbackIconTemplate,
}

// other related notifications
notifications.TemplateTestNotification: FallbackIconOther,
func SetInboxNotificationIcon(notif *codersdk.InboxNotification) {
if notif.Icon != "" {
return
}

fallbackIcon, ok := fallbackIcons[notif.TemplateID]
if !ok {
fallbackIcon = FallbackIconOther
}

notif.Icon = fallbackIcon
}

// convertInboxNotificationResponse works as a util function to transform a database.InboxNotification to codersdk.InboxNotification
func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, notif database.InboxNotification) codersdk.InboxNotification {
return codersdk.InboxNotification{
convertedNotif := codersdk.InboxNotification{
ID: notif.ID,
UserID: notif.UserID,
TemplateID: notif.TemplateID,
Targets: notif.Targets,
Title: notif.Title,
Content: notif.Content,
Icon: func() string {
if notif.Icon != "" {
return notif.Icon
}

return fallbackIcons[notif.TemplateID]
}(),
Actions: func() []codersdk.InboxNotificationAction {
var actionsList []codersdk.InboxNotificationAction
err := json.Unmarshal([]byte(notif.Actions), &actionsList)
Expand All@@ -100,6 +102,9 @@ func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, n
}(),
CreatedAt: notif.CreatedAt,
}

SetInboxNotificationIcon(&convertedNotif)
return convertedNotif
}

// watchInboxNotifications watches for new inbox notifications and sends them to the client.
Expand DownExpand Up@@ -191,10 +196,7 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
}
}

// convert icon to fallback if required
if payload.InboxNotification.Icon == "" {
payload.InboxNotification.Icon = fallbackIcons[payload.InboxNotification.TemplateID]
}
SetInboxNotificationIcon(&payload.InboxNotification)

// keep a safe guard in case of latency to push notifications through websocket
select {
Expand Down
61 changes: 61 additions & 0 deletionscoderd/inboxnotifications_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
"net/http"
"runtime"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand DownExpand Up@@ -866,3 +867,63 @@
require.Len(t, notifs.Notifications, 25)
})
}

func TestInboxNotifications_SetInboxNotificationIcon(t *testing.T) {

Check failure on line 871 in coderd/inboxnotifications_test.go

View workflow job for this annotation

GitHub Actions/ lint

Function TestInboxNotifications_SetInboxNotificationIcon missing the call to method parallel (paralleltest)
tests := []struct {
name string
icon string
templateID uuid.UUID
expectedIcon string
}{
{"WorkspaceCreated", "", notifications.TemplateWorkspaceCreated, coderd.FallbackIconWorkspace},
{"WorkspaceManuallyUpdated", "", notifications.TemplateWorkspaceManuallyUpdated, coderd.FallbackIconWorkspace},
{"WorkspaceDeleted", "", notifications.TemplateWorkspaceDeleted, coderd.FallbackIconWorkspace},
{"WorkspaceAutobuildFailed", "", notifications.TemplateWorkspaceAutobuildFailed, coderd.FallbackIconWorkspace},
{"WorkspaceDormant", "", notifications.TemplateWorkspaceDormant, coderd.FallbackIconWorkspace},
{"WorkspaceAutoUpdated", "", notifications.TemplateWorkspaceAutoUpdated, coderd.FallbackIconWorkspace},
{"WorkspaceMarkedForDeletion", "", notifications.TemplateWorkspaceMarkedForDeletion, coderd.FallbackIconWorkspace},
{"WorkspaceManualBuildFailed", "", notifications.TemplateWorkspaceManualBuildFailed, coderd.FallbackIconWorkspace},
{"WorkspaceOutOfMemory", "", notifications.TemplateWorkspaceOutOfMemory, coderd.FallbackIconWorkspace},
{"WorkspaceOutOfDisk", "", notifications.TemplateWorkspaceOutOfDisk, coderd.FallbackIconWorkspace},

// Account-related events
{"UserAccountCreated", "", notifications.TemplateUserAccountCreated, coderd.FallbackIconAccount},
{"UserAccountDeleted", "", notifications.TemplateUserAccountDeleted, coderd.FallbackIconAccount},
{"UserAccountSuspended", "", notifications.TemplateUserAccountSuspended, coderd.FallbackIconAccount},
{"UserAccountActivated", "", notifications.TemplateUserAccountActivated, coderd.FallbackIconAccount},
{"YourAccountSuspended", "", notifications.TemplateYourAccountSuspended, coderd.FallbackIconAccount},
{"YourAccountActivated", "", notifications.TemplateYourAccountActivated, coderd.FallbackIconAccount},
{"UserRequestedOneTimePasscode", "", notifications.TemplateUserRequestedOneTimePasscode, coderd.FallbackIconAccount},

// Template-related events
{"TemplateDeleted", "", notifications.TemplateTemplateDeleted, coderd.FallbackIconTemplate},
{"TemplateDeprecated", "", notifications.TemplateTemplateDeprecated, coderd.FallbackIconTemplate},
{"WorkspaceBuildsFailedReport", "", notifications.TemplateWorkspaceBuildsFailedReport, coderd.FallbackIconTemplate},

// Notification-related events
{"TestNotification", "", notifications.TemplateTestNotification, coderd.FallbackIconOther},

// Testing out that we dont erase existing icon
{"TestExistingIcon", "https://cdn.coder.com/icon_notif.png", notifications.TemplateTemplateDeleted, "https://cdn.coder.com/icon_notif.png"},

// Unknown template
{"UnknownTemplate", "", uuid.New(), coderd.FallbackIconOther},
}

for _, tt := range tests {
tt := tt

notif := codersdk.InboxNotification{
ID: uuid.New(),
UserID: uuid.New(),
TemplateID: tt.templateID,
Title: "notification title",
Content: "notification content",
Icon: tt.icon,
CreatedAt: time.Now(),
}

coderd.SetInboxNotificationIcon(&notif)
require.Equal(t, tt.expectedIcon, notif.Icon)
}
}
Loading

[8]ページ先頭

©2009-2026 Movatter.jp