Skip to content

Commit a094305

Browse files
authored
fix: don't filter to just unread notifications when squashing/upserting (#578)
1 parent afe4c0d commit a094305

6 files changed

Lines changed: 23 additions & 28 deletions

File tree

api/internal/db/queries/notifications.sql.go

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/internal/db/queries/querier.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/internal/db/sql/notifications.sql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,21 @@ WHERE notifications.user_id = $1 AND notifications.viewed = $4 AND notifications
8484
ORDER BY notifications.created_at DESC
8585
LIMIT $3;
8686

87-
-- name: FindUnreadLikeNotificationForPost :one
87+
-- name: FindLikeNotificationForPost :one
8888
SELECT *
8989
FROM notifications
9090
WHERE user_id = $1
9191
AND notification_type = 'like'
92-
AND viewed = FALSE
9392
AND post_id = $2
9493
AND comment_id IS NULL
9594
ORDER BY created_at DESC
9695
LIMIT 1;
9796

98-
-- name: FindUnreadLikeNotificationForComment :one
97+
-- name: FindLikeNotificationForComment :one
9998
SELECT *
10099
FROM notifications
101100
WHERE user_id = $1
102101
AND notification_type = 'like'
103-
AND viewed = FALSE
104102
AND post_id = $2
105103
AND comment_id = $3
106104
ORDER BY created_at DESC

api/internal/notification/service.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,12 @@ func (s *Service) AddLikeNotification(ctx context.Context, currentUserId int, po
212212
return err
213213
}
214214

215-
existingLikeNotification, err := s.notificationRepository.FindUnreadLikeNotification(ctx, recipientId, postId, commentId)
215+
existingLikeNotification, err := s.notificationRepository.FindLikeNotification(ctx, recipientId, postId, commentId)
216216
if err != nil {
217217
return err
218218
}
219219

220-
// TODO: add a unique constraint on the notifications table (e.g. (user_id, post_id, notification_type) with a
221-
// partial index WHERE NOT viewed)
220+
// TODO: add a unique constraint on the notifications table (e.g. (user_id, post_id, notification_type))
222221
if existingLikeNotification == nil {
223222
message, err := s.buildLikedMessage(ctx, []int{currentUser.UserID}, commentId != nil)
224223
if err != nil {
@@ -272,7 +271,7 @@ func (s *Service) RemoveLikeNotification(ctx context.Context, currentUserId int,
272271
recipientId = comment.UserID
273272
}
274273

275-
existingLikeNotification, err := s.notificationRepository.FindUnreadLikeNotification(ctx, recipientId, postId, commentId)
274+
existingLikeNotification, err := s.notificationRepository.FindLikeNotification(ctx, recipientId, postId, commentId)
276275
if err != nil || existingLikeNotification == nil {
277276
return err
278277
}

api/internal/notification/service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestGetUserUnreadNotificationsCount(t *testing.T) {
162162
assert.Equal(t, 1, count)
163163
}
164164

165-
func TestFindUnreadLikeNotification_PostNotification(t *testing.T) {
165+
func TestFindLikeNotification_PostNotification(t *testing.T) {
166166
env := setupNotificationService(t)
167167
user := testutil.CreateTestUser(t, env.userRepository, "testUser")
168168

@@ -173,13 +173,13 @@ func TestFindUnreadLikeNotification_PostNotification(t *testing.T) {
173173
_, err = env.notificationRepository.InsertNotification(t.Context(), user.UserID, &post.PostID, nil, nil, "@user liked your post.", models.NotificationTypeLike, nil)
174174
require.NoError(t, err)
175175

176-
result, err := env.notificationRepository.FindUnreadLikeNotification(t.Context(), user.UserID, post.PostID, nil)
176+
result, err := env.notificationRepository.FindLikeNotification(t.Context(), user.UserID, post.PostID, nil)
177177
require.NoError(t, err)
178178
require.NotNil(t, result)
179179
assert.Equal(t, "@user liked your post.", result.Message)
180180
}
181181

182-
func TestFindUnreadLikeNotification_CommentNotification(t *testing.T) {
182+
func TestFindLikeNotification_CommentNotification(t *testing.T) {
183183
env := setupNotificationService(t)
184184
user := testutil.CreateTestUser(t, env.userRepository, "testUser")
185185

@@ -193,7 +193,7 @@ func TestFindUnreadLikeNotification_CommentNotification(t *testing.T) {
193193
_, err = env.notificationRepository.InsertNotification(t.Context(), user.UserID, &post.PostID, &comment.CommentID, nil, "@user liked your comment.", models.NotificationTypeLike, nil)
194194
require.NoError(t, err)
195195

196-
result, err := env.notificationRepository.FindUnreadLikeNotification(t.Context(), user.UserID, post.PostID, &comment.CommentID)
196+
result, err := env.notificationRepository.FindLikeNotification(t.Context(), user.UserID, post.PostID, &comment.CommentID)
197197
require.NoError(t, err)
198198
require.NotNil(t, result)
199199
assert.Equal(t, "@user liked your comment.", result.Message)

api/internal/notification/store.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,18 @@ func (r NotificationStore) GetUnreadNotificationsForUserIdWithTimeOffset(ctx con
153153
return result, nil
154154
}
155155

156-
// FindUnreadLikeNotification finds an unread like notification for a user
157-
func (r NotificationStore) FindUnreadLikeNotification(ctx context.Context, userId int, postId int, commentId *int) (*models.Notification, error) {
156+
// FindLikeNotification finds the most recent like notification for a user on a post or comment
157+
func (r NotificationStore) FindLikeNotification(ctx context.Context, userId int, postId int, commentId *int) (*models.Notification, error) {
158158
var notification queries.Notification
159159
var err error
160160

161161
if commentId == nil {
162-
notification, err = r.querier.FindUnreadLikeNotificationForPost(ctx, queries.FindUnreadLikeNotificationForPostParams{
162+
notification, err = r.querier.FindLikeNotificationForPost(ctx, queries.FindLikeNotificationForPostParams{
163163
UserID: userId,
164164
PostID: &postId,
165165
})
166166
} else {
167-
notification, err = r.querier.FindUnreadLikeNotificationForComment(ctx, queries.FindUnreadLikeNotificationForCommentParams{
167+
notification, err = r.querier.FindLikeNotificationForComment(ctx, queries.FindLikeNotificationForCommentParams{
168168
UserID: userId,
169169
PostID: &postId,
170170
CommentID: commentId,

0 commit comments

Comments
 (0)