- Notifications
You must be signed in to change notification settings - Fork1.1k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from6 commits
f9662a8f2c57f4033d78aa633e8e0469e57d22f0b54c4527c783b04b3ac9d920b2496edc38924File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,6 +16,7 @@ import ( | ||
| "github.com/coder/coder/v2/coderd/database/dbtime" | ||
| "github.com/coder/coder/v2/coderd/httpapi" | ||
| "github.com/coder/coder/v2/coderd/httpmw" | ||
| "github.com/coder/coder/v2/coderd/notifications" | ||
| "github.com/coder/coder/v2/coderd/pubsub" | ||
| markdown "github.com/coder/coder/v2/coderd/render" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| @@ -28,16 +29,63 @@ const ( | ||
| notificationFormatPlaintext = "plaintext" | ||
| ) | ||
| const ( | ||
| FallbackIconWorkspace = "DEFAULT_WORKSPACE_ICON" | ||
| FallbackIconAccount = "DEFAULT_ACCOUNT_ICON" | ||
| FallbackIconTemplate = "DEFAULT_TEMPLATE_ICON" | ||
| FallbackIconOther = "DEFAULT_OTHER_ICON" | ||
| ||
| ) | ||
| var fallbackIcons = map[uuid.UUID]string{ | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. alternative data structure:
not a must, just a preference | ||
| // workspace related notifications | ||
| notifications.TemplateWorkspaceCreated: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceManuallyUpdated: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceDeleted: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceAutobuildFailed: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceDormant: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceAutoUpdated: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceMarkedForDeletion: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceManualBuildFailed: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceOutOfMemory: FallbackIconWorkspace, | ||
| notifications.TemplateWorkspaceOutOfDisk: FallbackIconWorkspace, | ||
| // account related notifications | ||
| notifications.TemplateUserAccountCreated: FallbackIconAccount, | ||
| notifications.TemplateUserAccountDeleted: FallbackIconAccount, | ||
| notifications.TemplateUserAccountSuspended: FallbackIconAccount, | ||
| notifications.TemplateUserAccountActivated: FallbackIconAccount, | ||
| notifications.TemplateYourAccountSuspended: FallbackIconAccount, | ||
| notifications.TemplateYourAccountActivated: FallbackIconAccount, | ||
| notifications.TemplateUserRequestedOneTimePasscode: FallbackIconAccount, | ||
| // template related notifications | ||
| notifications.TemplateTemplateDeleted: FallbackIconTemplate, | ||
| notifications.TemplateTemplateDeprecated: FallbackIconTemplate, | ||
| notifications.TemplateWorkspaceBuildsFailedReport: FallbackIconTemplate, | ||
| } | ||
| func SetInboxNotificationIcon(notif *codersdk.InboxNotification) { | ||
mtojek marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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 { | ||
| convertedNotif := codersdk.InboxNotification{ | ||
| ID: notif.ID, | ||
| UserID: notif.UserID, | ||
| TemplateID: notif.TemplateID, | ||
| Targets: notif.Targets, | ||
| Title: notif.Title, | ||
| Content: notif.Content, | ||
| Actions: func() []codersdk.InboxNotificationAction { | ||
| var actionsList []codersdk.InboxNotificationAction | ||
| err := json.Unmarshal([]byte(notif.Actions), &actionsList) | ||
| @@ -54,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. | ||
| @@ -145,6 +196,8 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request) | ||
| } | ||
| } | ||
| SetInboxNotificationIcon(&payload.InboxNotification) | ||
| // keep a safe guard in case of latency to push notifications through websocket | ||
| select { | ||
| case notificationCh <- payload.InboxNotification: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,10 +7,12 @@ import ( | ||
| "net/http" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/coder/coder/v2/coderd" | ||
| "github.com/coder/coder/v2/coderd/coderdtest" | ||
| "github.com/coder/coder/v2/coderd/database" | ||
| "github.com/coder/coder/v2/coderd/database/dbgen" | ||
| @@ -135,6 +137,9 @@ func TestInboxNotification_Watch(t *testing.T) { | ||
| require.Equal(t, 1, notif.UnreadCount) | ||
| require.Equal(t, memberClient.ID, notif.Notification.UserID) | ||
| // check for the fallback icon logic | ||
| require.Equal(t, coderd.FallbackIconWorkspace, notif.Notification.Icon) | ||
| }) | ||
| t.Run("OK - change format", func(t *testing.T) { | ||
| @@ -862,3 +867,69 @@ func TestInboxNotifications_MarkAllAsRead(t *testing.T) { | ||
| require.Len(t, notifs.Notifications, 25) | ||
| }) | ||
| } | ||
| func TestInboxNotifications_SetInboxNotificationIcon(t *testing.T) { | ||
| t.Parallel() | ||
| 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 | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| 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(¬if) | ||
mtojek marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| require.Equal(t, tt.expectedIcon, notif.Icon) | ||
| }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.