Skip to content

Commit 646d418

Browse files
committed
feat(api): provide flag if user has v1 specific notifications
1 parent af85db8 commit 646d418

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

backend/pkg/api/data_access/notifications.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,3 +2112,20 @@ func (d *DataAccessService) QueueTestPushNotification(ctx context.Context, userI
21122112
func (d *DataAccessService) QueueTestWebhookNotification(ctx context.Context, userId uint64, webhookUrl string, isDiscordWebhook bool) error {
21132113
return notification.SendTestWebhookNotification(ctx, types.UserId(userId), webhookUrl, isDiscordWebhook)
21142114
}
2115+
2116+
// hasUserV1NotificationSubscriptions checks if a user has any v1 notification subscriptions.
2117+
// Since some notification events are indistinguishable from v1 and v2, only validator-related that are not v2 are checked.
2118+
func (d *DataAccessService) hasUserV1NotificationSubscriptions(ctx context.Context, userId uint64) (bool, error) {
2119+
now := time.Now()
2120+
ds := goqu.Dialect("postgres").
2121+
Select(goqu.COUNT(goqu.I("id"))).
2122+
From(goqu.T("users_subscriptions")).
2123+
Where(
2124+
goqu.I("user_id").Eq(userId),
2125+
goqu.I("event_name").Like("validator%"),
2126+
goqu.I("event_filter").NotLike("vdb:%"),
2127+
)
2128+
count, err := runQuery[int](ctx, d.userReader, ds)
2129+
log.Info("time it took", time.Since(now))
2130+
return count > 0, err
2131+
}

backend/pkg/api/data_access/user.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,33 @@ func (d *DataAccessService) GetUserIdByResetHash(ctx context.Context, hash strin
289289
}
290290

291291
func (d *DataAccessService) GetUserInfo(ctx context.Context, userId uint64) (*t.UserInfo, error) {
292-
return db.GetUserInfo(ctx, userId, d.userReader)
292+
wg := errgroup.Group{}
293+
294+
var userInfo *t.UserInfo
295+
wg.Go(func() error {
296+
var err error
297+
userInfo, err = db.GetUserInfo(ctx, userId, d.userReader)
298+
if err != nil {
299+
err = fmt.Errorf("error retrieving user info: %w", err)
300+
}
301+
return err
302+
})
303+
304+
var hasV1Notifications bool
305+
wg.Go(func() error {
306+
var err error
307+
hasV1Notifications, err = d.hasUserV1NotificationSubscriptions(ctx, userId)
308+
if err != nil {
309+
return fmt.Errorf("error retrieving if user has v1 notifications: %w", err)
310+
}
311+
return nil
312+
})
313+
err := wg.Wait()
314+
if userInfo != nil {
315+
userInfo.HasV1Notifications = hasV1Notifications
316+
}
317+
318+
return userInfo, err
293319
}
294320

295321
func (d *DataAccessService) GetProductSummary(ctx context.Context) (*t.ProductSummary, error) {

backend/pkg/api/types/user.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const UserGroupAdmin = "ADMIN"
66
const UserGroupDev = "DEV"
77

88
type UserInfo struct {
9-
Id uint64 `json:"id"`
10-
UserGroup string `json:"-"`
11-
Email string `json:"email"`
12-
ApiKeys []string `json:"api_keys"`
13-
ApiPerks ApiPerks `json:"api_perks"`
14-
PremiumPerks PremiumPerks `json:"premium_perks"`
15-
Subscriptions []UserSubscription `json:"subscriptions"`
9+
Id uint64 `json:"id"`
10+
UserGroup string `json:"-"`
11+
Email string `json:"email"`
12+
ApiKeys []string `json:"api_keys"`
13+
ApiPerks ApiPerks `json:"api_perks"`
14+
PremiumPerks PremiumPerks `json:"premium_perks"`
15+
Subscriptions []UserSubscription `json:"subscriptions"`
16+
HasV1Notifications bool `json:"has_v1_notifications"`
1617
}
1718

1819
type UserSubscription struct {

frontend/types/api/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface UserInfo {
1414
api_perks: ApiPerks;
1515
premium_perks: PremiumPerks;
1616
subscriptions: UserSubscription[];
17+
has_v1_notifications: boolean;
1718
}
1819
export interface UserSubscription {
1920
product_id: string;

0 commit comments

Comments
 (0)