-
Notifications
You must be signed in to change notification settings - Fork 943
feat(coderd): add inbox notifications endpoints #16889
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3c6512b
c8ccc60
18b694b
0e8ac4c
d72d1f2
796bcd0
75c310d
07ab7c4
cb41d1a
6ff4c7e
1ebc7f4
736a2d7
c28002e
2637d86
4d0a561
1f18868
4f01a86
cf6af1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,7 +164,7 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request) | |
case notif := <-notificationCh: | ||
unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "count unread inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to count unread inbox notifications", slog.Error(err)) | ||
return | ||
} | ||
if err := encoder.Encode(codersdk.GetInboxNotificationResponse{ | ||
|
@@ -261,7 +261,7 @@ func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request) | |
lastNotif, err := api.Database.GetInboxNotificationByID(ctx, lastNotifID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Invalid starting before.", | ||
Message: "Failed to get notification by id.", | ||
}) | ||
return | ||
} | ||
|
@@ -276,16 +276,16 @@ func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request) | |
CreatedAtOpt: startingBefore, | ||
}) | ||
if err != nil { | ||
api.Logger.Error(ctx, "get filtered inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to get filtered inbox notifications", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to get inbox notifications.", | ||
Message: "Failed to get filtered inbox notifications.", | ||
}) | ||
return | ||
} | ||
|
||
unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "count unread inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to count unread inbox notifications", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to count unread inbox notifications.", | ||
}) | ||
|
@@ -351,7 +351,7 @@ func (api *API) updateInboxNotificationReadStatus(rw http.ResponseWriter, r *htt | |
|
||
parsedNotifID, err := uuid.Parse(notifID) | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
api.Logger.Error(ctx, "failed to parse uuid", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to parse notification uuid", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
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. This is probably more of a 4xx error (bad request?) 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. Yeah you're right ✅ |
||
Message: "Failed to parse notification uuid.", | ||
}) | ||
|
@@ -372,27 +372,27 @@ func (api *API) updateInboxNotificationReadStatus(rw http.ResponseWriter, r *htt | |
}(), | ||
}) | ||
if err != nil { | ||
api.Logger.Error(ctx, "get filtered inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to update inbox notification read status", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to get inbox notifications.", | ||
Message: "Failed to update inbox notification read status.", | ||
}) | ||
return | ||
} | ||
|
||
unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "count unread inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to call count unread inbox notifications", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to count unread inbox notifications.", | ||
Message: "Failed to call count unread inbox notifications.", | ||
}) | ||
return | ||
} | ||
|
||
updatedNotification, err := api.Database.GetInboxNotificationByID(ctx, parsedNotifID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "count unread inbox notifications", slog.Error(err)) | ||
api.Logger.Error(ctx, "failed to get notification by id", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to count unread inbox notifications.", | ||
Message: "Failed to get notification by id.", | ||
}) | ||
return | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,10 @@ package coderd_test | |
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -43,6 +42,13 @@ func TestInboxNotifications_List(t *testing.T) { | |
t.Run("OK with pagination", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// I skip these tests specifically on windows as for now they are flaky - only on Windows. | ||
// For now the idea is that the runner takes too long to insert the entries, could be worth | ||
// investigating a manual Tx. | ||
if runtime.GOOS == "windows" { | ||
t.Skip("our runners are randomly taking too long to insert entries") | ||
} | ||
|
||
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{}) | ||
firstUser := coderdtest.CreateFirstUser(t, client) | ||
|
||
|
@@ -58,23 +64,18 @@ func TestInboxNotifications_List(t *testing.T) { | |
// nolint:gocritic // used only to seed database | ||
notifierCtx := dbauthz.AsNotifier(ctx) | ||
|
||
api.Database.InTx(func(tx database.Store) error { | ||
for i := range 40 { | ||
_, err = api.Database.InsertInboxNotification(notifierCtx, database.InsertInboxNotificationParams{ | ||
ID: uuid.New(), | ||
UserID: firstUser.UserID, | ||
TemplateID: notifications.TemplateWorkspaceOutOfMemory, | ||
Title: fmt.Sprintf("Notification %d", i), | ||
Actions: json.RawMessage("[]"), | ||
Content: fmt.Sprintf("Content of the notif %d", i), | ||
CreatedAt: dbtime.Now(), | ||
}) | ||
require.NoError(t, err) | ||
} | ||
return nil | ||
}, &database.TxOptions{ | ||
Isolation: sql.LevelReadCommitted, | ||
}) | ||
for i := range 40 { | ||
_, err = api.Database.InsertInboxNotification(notifierCtx, database.InsertInboxNotificationParams{ | ||
ID: uuid.New(), | ||
UserID: firstUser.UserID, | ||
TemplateID: notifications.TemplateWorkspaceOutOfMemory, | ||
Title: fmt.Sprintf("Notification %d", i), | ||
Actions: json.RawMessage("[]"), | ||
Content: fmt.Sprintf("Content of the notif %d", i), | ||
CreatedAt: dbtime.Now(), | ||
}) | ||
require.NoError(t, err) | ||
} | ||
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. This could be a good candidate for a 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. good catch - even more that the function was already there for other testing.. :') I migrated all tests to it. |
||
|
||
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{}) | ||
require.NoError(t, err) | ||
|
@@ -132,8 +133,6 @@ func TestInboxNotifications_List(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{ | ||
Templates: []uuid.UUID{notifications.TemplateWorkspaceOutOfMemory}, | ||
}) | ||
|
@@ -185,8 +184,6 @@ func TestInboxNotifications_List(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{ | ||
Targets: []uuid.UUID{filteredTarget}, | ||
}) | ||
|
@@ -244,8 +241,6 @@ func TestInboxNotifications_List(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{ | ||
Targets: []uuid.UUID{filteredTarget}, | ||
Templates: []uuid.UUID{notifications.TemplateWorkspaceOutOfDisk}, | ||
|
@@ -290,8 +285,6 @@ func TestInboxNotifications_ReadStatus(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{}) | ||
require.NoError(t, err) | ||
require.NotNil(t, notifs) | ||
|
Uh oh!
There was an error while loading. Please reload this page.