From 0bfc1652e3179f7053ebfda93ed6850a3c3530af Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 7 Apr 2026 05:58:02 +0100 Subject: [PATCH] appvie/notify: add DeleteRepo event to unindex repos when deleted. this changeset also includes a tweak to the NewRepo event, to fix a bug with the document IDs of new repos (it was always zero). Signed-off-by: oppiliappan --- appview/db/repos.go | 8 +++++++- appview/indexer/notifier.go | 11 ++++++++++- appview/notify/db/db.go | 3 +++ appview/notify/logging/notifier.go | 5 +++++ appview/notify/merged_notifier.go | 4 ++++ appview/notify/notifier.go | 4 +++- appview/repo/repo.go | 1 + appview/state/search.go | 14 +++++++++++++- 8 files changed, 46 insertions(+), 4 deletions(-) diff --git a/appview/db/repos.go b/appview/db/repos.go index b9a3c0cc..4c8dfc7b 100644 --- a/appview/db/repos.go +++ b/appview/db/repos.go @@ -417,7 +417,7 @@ func AddRepo(tx *sql.Tx, repo *models.Repo) error { if repo.RepoDid != "" { repoDid = &repo.RepoDid } - _, err := tx.Exec( + result, err := tx.Exec( `insert into repos (did, name, knot, rkey, at_uri, description, website, topics, source, repo_did) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, @@ -427,6 +427,12 @@ func AddRepo(tx *sql.Tx, repo *models.Repo) error { return fmt.Errorf("failed to insert repo: %w", err) } + id, err := result.LastInsertId() + if err != nil { + return fmt.Errorf("failed to get last insert id: %w", err) + } + repo.Id = id + for _, dl := range repo.Labels { if err := SubscribeLabel(tx, &models.RepoLabel{ RepoAt: repo.RepoAt(), diff --git a/appview/indexer/notifier.go b/appview/indexer/notifier.go index 5e415e1b..b6c81fb2 100644 --- a/appview/indexer/notifier.go +++ b/appview/indexer/notifier.go @@ -105,7 +105,7 @@ func (ix *Indexer) NewPullState(ctx context.Context, actor syntax.DID, pull *mod } func (ix *Indexer) NewRepo(ctx context.Context, repo *models.Repo) { - l := log.FromContext(ctx).With("notifier", "indexer", "repo", repo) + l := log.FromContext(ctx).With("notifier", "indexer", "repo", repo.RepoIdentifier(), "owner", repo.Did, "name", repo.Name) l.Debug("indexing new repo") err := ix.Repos.Index(ctx, *repo) if err != nil { @@ -113,6 +113,15 @@ func (ix *Indexer) NewRepo(ctx context.Context, repo *models.Repo) { } } +func (ix *Indexer) DeleteRepo(ctx context.Context, repo *models.Repo) { + l := log.FromContext(ctx).With("notifier", "indexer", "repo", repo) + l.Debug("deleting repo from index") + err := ix.Repos.Delete(ctx, repo.Id) + if err != nil { + l.Error("failed to index a repo", "err", err) + } +} + func (ix *Indexer) NewStar(ctx context.Context, star *models.Star) { l := log.FromContext(ctx).With("notifier", "indexer", "star", star) diff --git a/appview/notify/db/db.go b/appview/notify/db/db.go index 4079a47f..8a01d276 100644 --- a/appview/notify/db/db.go +++ b/appview/notify/db/db.go @@ -36,6 +36,9 @@ var _ notify.Notifier = &databaseNotifier{} func (n *databaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) { // no-op for now } +func (n *databaseNotifier) DeleteRepo(ctx context.Context, repo *models.Repo) { + // no-op for now +} func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) { l := log.FromContext(ctx) diff --git a/appview/notify/logging/notifier.go b/appview/notify/logging/notifier.go index 5f16c37c..726531ac 100644 --- a/appview/notify/logging/notifier.go +++ b/appview/notify/logging/notifier.go @@ -26,6 +26,11 @@ func (l *loggingNotifier) NewRepo(ctx context.Context, repo *models.Repo) { l.inner.NewRepo(ctx, repo) } +func (l *loggingNotifier) DeleteRepo(ctx context.Context, repo *models.Repo) { + ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "DeleteRepo")) + l.inner.DeleteRepo(ctx, repo) +} + func (l *loggingNotifier) NewStar(ctx context.Context, star *models.Star) { ctx = tlog.IntoContext(ctx, tlog.SubLogger(l.logger, "NewStar")) l.inner.NewStar(ctx, star) diff --git a/appview/notify/merged_notifier.go b/appview/notify/merged_notifier.go index 9f97d5a3..fb16ec6a 100644 --- a/appview/notify/merged_notifier.go +++ b/appview/notify/merged_notifier.go @@ -34,6 +34,10 @@ func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { m.fanout(func(n Notifier) { n.NewRepo(ctx, repo) }) } +func (m *mergedNotifier) DeleteRepo(ctx context.Context, repo *models.Repo) { + m.fanout(func(n Notifier) { n.DeleteRepo(ctx, repo) }) +} + func (m *mergedNotifier) NewStar(ctx context.Context, star *models.Star) { m.fanout(func(n Notifier) { n.NewStar(ctx, star) }) } diff --git a/appview/notify/notifier.go b/appview/notify/notifier.go index 3d7a790d..1f65d015 100644 --- a/appview/notify/notifier.go +++ b/appview/notify/notifier.go @@ -9,6 +9,7 @@ import ( type Notifier interface { NewRepo(ctx context.Context, repo *models.Repo) + DeleteRepo(ctx context.Context, repo *models.Repo) NewStar(ctx context.Context, star *models.Star) DeleteStar(ctx context.Context, star *models.Star) @@ -44,7 +45,8 @@ type BaseNotifier struct{} var _ Notifier = &BaseNotifier{} -func (m *BaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) {} +func (m *BaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) {} +func (m *BaseNotifier) DeleteRepo(ctx context.Context, repo *models.Repo) {} func (m *BaseNotifier) NewStar(ctx context.Context, star *models.Star) {} func (m *BaseNotifier) DeleteStar(ctx context.Context, star *models.Star) {} diff --git a/appview/repo/repo.go b/appview/repo/repo.go index c576c34f..345f1dc8 100644 --- a/appview/repo/repo.go +++ b/appview/repo/repo.go @@ -940,6 +940,7 @@ func (rp *Repo) DeleteRepo(w http.ResponseWriter, r *http.Request) { return } + rp.notifier.DeleteRepo(r.Context(), f) rp.pages.HxRedirect(w, fmt.Sprintf("/%s", f.Did)) } diff --git a/appview/state/search.go b/appview/state/search.go index f486a506..90fc8c23 100644 --- a/appview/state/search.go +++ b/appview/state/search.go @@ -47,8 +47,9 @@ func (s *State) Search(w http.ResponseWriter, r *http.Request) { var resultCount int var searchDuration time.Duration var docCount int64 + method := "bleve" - if searchOpts.HasSearchFilters() || sortField != "" { + if searchOpts.HasSearchFilters() || sortParam != "" { res, err := s.indexer.Repos.Search(r.Context(), searchOpts) if err != nil { l.Error("failed to search repos", "err", err) @@ -87,6 +88,7 @@ func (s *State) Search(w http.ResponseWriter, r *http.Request) { docCount = int64(dc) } else { + method = "db" repos, err = db.GetReposPaginated( s.db, page, @@ -110,6 +112,16 @@ func (s *State) Search(w http.ResponseWriter, r *http.Request) { docCount = int64(rc) } + l.Info( + "RepoSearch", + "method", method, + "resultCount", resultCount, + "docCount", docCount, + "time", searchDuration, + "filterQuery", query.String(), + "sortParam", sortParam, + ) + err = s.pages.SearchRepos(w, pages.SearchReposParams{ LoggedInUser: s.oauth.GetMultiAccountUser(r), Repos: repos, -- 2.51.2