From abd5dd6cf4a4d97866784ed41cba58da3b603ffd Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Mon, 29 Sep 2025 16:54:31 +0100 Subject: [PATCH] appview/notifications: code cleanup for notifier Change-Id: zsrpqrknyuruvozqzkyzuolwmmstluxt GetRepo is a specialization for GetRepos which is to be used when the filters are primary keys. Signed-off-by: oppiliappan --- appview/db/notifications.go | 12 ------ appview/notifications/notifications.go | 4 +- appview/notify/db/db.go | 56 ++++---------------------- 3 files changed, 11 insertions(+), 61 deletions(-) diff --git a/appview/db/notifications.go b/appview/db/notifications.go index f65dcc00..68e3b825 100644 --- a/appview/db/notifications.go +++ b/appview/db/notifications.go @@ -248,18 +248,6 @@ func GetNotifications(e Execer, filters ...filter) ([]*models.Notification, erro return GetNotificationsPaginated(e, pagination.FirstPage(), filters...) } -// GetNotifications retrieves notifications for a user with pagination (legacy method for backward compatibility) -func (d *DB) GetNotifications(ctx context.Context, userDID string, limit, offset int) ([]*models.Notification, error) { - page := pagination.Page{Limit: limit, Offset: offset} - return GetNotificationsPaginated(d.DB, page, FilterEq("recipient_did", userDID)) -} - -// GetNotificationsWithEntities retrieves notifications with entities for a user with pagination -func (d *DB) GetNotificationsWithEntities(ctx context.Context, userDID string, limit, offset int) ([]*models.NotificationWithEntity, error) { - page := pagination.Page{Limit: limit, Offset: offset} - return GetNotificationsWithEntities(d.DB, page, FilterEq("recipient_did", userDID)) -} - func (d *DB) GetUnreadNotificationCount(ctx context.Context, userDID string) (int, error) { recipientFilter := FilterEq("recipient_did", userDID) readFilter := FilterEq("read", 0) diff --git a/appview/notifications/notifications.go b/appview/notifications/notifications.go index 8ba1c8c7..de7c5b22 100644 --- a/appview/notifications/notifications.go +++ b/appview/notifications/notifications.go @@ -10,6 +10,7 @@ import ( "tangled.org/core/appview/middleware" "tangled.org/core/appview/oauth" "tangled.org/core/appview/pages" + "tangled.org/core/appview/pagination" ) type Notifications struct { @@ -61,7 +62,8 @@ func (n *Notifications) notificationsPage(w http.ResponseWriter, r *http.Request } } - notifications, err := n.db.GetNotificationsWithEntities(r.Context(), userDid, limit+1, offset) + page := pagination.Page{Limit: limit + 1, Offset: offset} + notifications, err := db.GetNotificationsWithEntities(n.db, page, db.FilterEq("recipient_did", userDid)) if err != nil { log.Println("failed to get notifications:", err) n.pages.Error500(w) diff --git a/appview/notify/db/db.go b/appview/notify/db/db.go index ef75550e..900428ea 100644 --- a/appview/notify/db/db.go +++ b/appview/notify/db/db.go @@ -30,16 +30,11 @@ func (n *databaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) { func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) { var err error - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(star.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(star.RepoAt))) if err != nil { log.Printf("NewStar: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewStar: no repo found for %s", star.RepoAt) - return - } - repo := repos[0] // don't notify yourself if repo.Did == star.StarredByDid { @@ -76,16 +71,11 @@ func (n *databaseNotifier) DeleteStar(ctx context.Context, star *models.Star) { } func (n *databaseNotifier) NewIssue(ctx context.Context, issue *models.Issue) { - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(issue.RepoAt))) if err != nil { log.Printf("NewIssue: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewIssue: no repo found for %s", issue.RepoAt) - return - } - repo := repos[0] if repo.Did == issue.Did { return @@ -129,16 +119,11 @@ func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *models. } issue := issues[0] - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(issue.RepoAt))) if err != nil { log.Printf("NewIssueComment: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewIssueComment: no repo found for %s", issue.RepoAt) - return - } - repo := repos[0] recipients := make(map[string]bool) @@ -211,16 +196,11 @@ func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *models.Foll } func (n *databaseNotifier) NewPull(ctx context.Context, pull *models.Pull) { - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) if err != nil { log.Printf("NewPull: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewPull: no repo found for %s", pull.RepoAt) - return - } - repo := repos[0] if repo.Did == pull.OwnerDid { return @@ -266,16 +246,11 @@ func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *models.P } pull := pulls[0] - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", comment.RepoAt)) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", comment.RepoAt)) if err != nil { log.Printf("NewPullComment: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewPullComment: no repo found for %s", comment.RepoAt) - return - } - repo := repos[0] recipients := make(map[string]bool) @@ -335,16 +310,11 @@ func (n *databaseNotifier) NewString(ctx context.Context, string *models.String) func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *models.Issue) { // Get repo details - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(issue.RepoAt))) if err != nil { log.Printf("NewIssueClosed: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewIssueClosed: no repo found for %s", issue.RepoAt) - return - } - repo := repos[0] // Don't notify yourself if repo.Did == issue.Did { @@ -380,16 +350,11 @@ func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *models.Iss func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { // Get repo details - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) if err != nil { log.Printf("NewPullMerged: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewPullMerged: no repo found for %s", pull.RepoAt) - return - } - repo := repos[0] // Don't notify yourself if repo.Did == pull.OwnerDid { @@ -425,16 +390,11 @@ func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { // Get repo details - repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) + repo, err := db.GetRepo(n.db, db.FilterEq("at_uri", string(pull.RepoAt))) if err != nil { log.Printf("NewPullClosed: failed to get repos: %v", err) return } - if len(repos) == 0 { - log.Printf("NewPullClosed: no repo found for %s", pull.RepoAt) - return - } - repo := repos[0] // Don't notify yourself if repo.Did == pull.OwnerDid { -- 2.43.0