feat: notification enhancement
This commit is contained in:
@@ -317,7 +317,9 @@ func (r *NotificationRepo) Create(ctx context.Context, userID string, actorID *s
|
||||
|
||||
// ListByUser returns a recipient's notifications, newest first, with the
|
||||
// actor's display fields joined in for rendering (nil for system
|
||||
// notifications or since-deleted actors).
|
||||
// notifications or since-deleted actors). Kinds the user has turned off for
|
||||
// the in-app channel are filtered at read time — the rows still persist (they
|
||||
// back like-aggregation and reappear if the pref is re-enabled).
|
||||
func (r *NotificationRepo) ListByUser(ctx context.Context, userID string, limit, offset int) ([]model.Notification, error) {
|
||||
rows, err := r.pg.Pool.Query(ctx, `
|
||||
SELECT n.id, n.user_id, n.actor_id, n.kind, n.post_id, n.actor_count,
|
||||
@@ -326,6 +328,10 @@ func (r *NotificationRepo) ListByUser(ctx context.Context, userID string, limit,
|
||||
FROM notifications n
|
||||
LEFT JOIN users u ON u.id = n.actor_id
|
||||
WHERE n.user_id = $1
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM notification_prefs np
|
||||
WHERE np.user_id = n.user_id AND np.kind = n.kind
|
||||
AND np.channel = 'in_app' AND np.enabled = FALSE)
|
||||
ORDER BY n.created_at DESC
|
||||
LIMIT $2 OFFSET $3`, userID, limit, offset,
|
||||
)
|
||||
@@ -394,32 +400,18 @@ func (r *NotificationRepo) CreateMention(ctx context.Context, userID, actorID, p
|
||||
return nil
|
||||
}
|
||||
|
||||
// LikeEmailsEnabled reports whether the user wants aggregated like emails.
|
||||
func (r *NotificationRepo) LikeEmailsEnabled(ctx context.Context, userID string) (bool, error) {
|
||||
var enabled bool
|
||||
if err := r.pg.Pool.QueryRow(ctx,
|
||||
`SELECT like_emails_enabled FROM users WHERE id = $1`, userID,
|
||||
).Scan(&enabled); err != nil {
|
||||
return false, fmt.Errorf("get like-email pref: %w", err)
|
||||
}
|
||||
return enabled, nil
|
||||
}
|
||||
|
||||
// SetLikeEmailsEnabled flips the user's like-email opt-in.
|
||||
func (r *NotificationRepo) SetLikeEmailsEnabled(ctx context.Context, userID string, enabled bool) error {
|
||||
if _, err := r.pg.Pool.Exec(ctx,
|
||||
`UPDATE users SET like_emails_enabled = $1 WHERE id = $2`, enabled, userID,
|
||||
); err != nil {
|
||||
return fmt.Errorf("set like-email pref: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountUnread returns how many of a recipient's notifications are unread.
|
||||
// CountUnread returns how many of a recipient's notifications are unread,
|
||||
// excluding kinds the user has turned off for the in-app channel (matching
|
||||
// ListByUser, so the badge never counts rows the feed won't show).
|
||||
func (r *NotificationRepo) CountUnread(ctx context.Context, userID string) (int, error) {
|
||||
var n int
|
||||
err := r.pg.Pool.QueryRow(ctx,
|
||||
`SELECT count(*) FROM notifications WHERE user_id = $1 AND read_at IS NULL`, userID,
|
||||
err := r.pg.Pool.QueryRow(ctx, `
|
||||
SELECT count(*) FROM notifications n
|
||||
WHERE n.user_id = $1 AND n.read_at IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM notification_prefs np
|
||||
WHERE np.user_id = n.user_id AND np.kind = n.kind
|
||||
AND np.channel = 'in_app' AND np.enabled = FALSE)`, userID,
|
||||
).Scan(&n)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("count unread notifications: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user