- Notifications
You must be signed in to change notification settings - Fork906
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 fromall commits
f9662a8
f2c57f4
033d78a
a633e8e
0469e57
d22f0b5
4c4527c
783b04b
3ac9d92
0b2496e
dc38924
File 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,9 +29,51 @@ const ( | ||
notificationFormatPlaintext = "plaintext" | ||
) | ||
var fallbackIcons = map[uuid.UUID]string{ | ||
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: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceManuallyUpdated: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceDeleted: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceAutobuildFailed: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceDormant: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceAutoUpdated: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceMarkedForDeletion: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceManualBuildFailed: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceOutOfMemory: codersdk.FallbackIconWorkspace, | ||
notifications.TemplateWorkspaceOutOfDisk: codersdk.FallbackIconWorkspace, | ||
// account related notifications | ||
notifications.TemplateUserAccountCreated: codersdk.FallbackIconAccount, | ||
notifications.TemplateUserAccountDeleted: codersdk.FallbackIconAccount, | ||
notifications.TemplateUserAccountSuspended: codersdk.FallbackIconAccount, | ||
notifications.TemplateUserAccountActivated: codersdk.FallbackIconAccount, | ||
notifications.TemplateYourAccountSuspended: codersdk.FallbackIconAccount, | ||
notifications.TemplateYourAccountActivated: codersdk.FallbackIconAccount, | ||
notifications.TemplateUserRequestedOneTimePasscode: codersdk.FallbackIconAccount, | ||
// template related notifications | ||
notifications.TemplateTemplateDeleted: codersdk.FallbackIconTemplate, | ||
notifications.TemplateTemplateDeprecated: codersdk.FallbackIconTemplate, | ||
notifications.TemplateWorkspaceBuildsFailedReport: codersdk.FallbackIconTemplate, | ||
} | ||
func ensureNotificationIcon(notif codersdk.InboxNotification) codersdk.InboxNotification { | ||
if notif.Icon != "" { | ||
return notif | ||
} | ||
fallbackIcon, ok := fallbackIcons[notif.TemplateID] | ||
if !ok { | ||
fallbackIcon = codersdk.FallbackIconOther | ||
} | ||
notif.Icon = fallbackIcon | ||
return notif | ||
} | ||
// 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, | ||
@@ -54,6 +97,8 @@ func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, n | ||
}(), | ||
CreatedAt: notif.CreatedAt, | ||
} | ||
return ensureNotificationIcon(convertedNotif) | ||
} | ||
// watchInboxNotifications watches for new inbox notifications and sends them to the client. | ||
@@ -147,7 +192,7 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request) | ||
// keep a safe guard in case of latency to push notifications through websocket | ||
select { | ||
case notificationCh <-ensureNotificationIcon(payload.InboxNotification): | ||
default: | ||
api.Logger.Error(ctx, "failed to push consumed notification into websocket handler, check latency") | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package coderd | ||
import ( | ||
"testing" | ||
"time" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/coderd/notifications" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
func TestInboxNotifications_ensureNotificationIcon(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
icon string | ||
templateID uuid.UUID | ||
expectedIcon string | ||
}{ | ||
{"WorkspaceCreated", "", notifications.TemplateWorkspaceCreated, codersdk.FallbackIconWorkspace}, | ||
{"UserAccountCreated", "", notifications.TemplateUserAccountCreated, codersdk.FallbackIconAccount}, | ||
{"TemplateDeleted", "", notifications.TemplateTemplateDeleted, codersdk.FallbackIconTemplate}, | ||
{"TestNotification", "", notifications.TemplateTestNotification, codersdk.FallbackIconOther}, | ||
{"TestExistingIcon", "https://cdn.coder.com/icon_notif.png", notifications.TemplateTemplateDeleted, "https://cdn.coder.com/icon_notif.png"}, | ||
{"UnknownTemplate", "", uuid.New(), codersdk.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(), | ||
} | ||
notif = ensureNotificationIcon(notif) | ||
require.Equal(t, tt.expectedIcon, notif.Icon) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.