From 1563c7b21dd105e8c92ed39b60b7ebc00b2ac802 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 6 Jun 2022 22:05:56 +0930 Subject: [PATCH] Manage list can be free-text searched and limited by tag. --- .vscode/settings.json | 1 + db/bookmarks.go | 18 ++++++++++++- db/bookmarks_test.go | 6 ++--- db/index_test.go | 12 ++++----- version/version.go | 2 +- web/templates/manage.html | 44 +++++++++---------------------- web/templates/manage_results.html | 31 ++++++++++++++++++++++ web/templates/tags_widget.html | 6 ++--- web/web.go | 20 +++++++++++++- 9 files changed, 94 insertions(+), 46 deletions(-) create mode 100644 web/templates/manage_results.html diff --git a/.vscode/settings.json b/.vscode/settings.json index 3a609b4..75fef50 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "cSpell.words": [ "bolthold", "colly", + "htmx", "incpatch", "linkwallet", "nicetime", diff --git a/db/bookmarks.go b/db/bookmarks.go index 7805cf2..81530c8 100644 --- a/db/bookmarks.go +++ b/db/bookmarks.go @@ -110,7 +110,7 @@ func (m *BookmarkManager) LoadBookmarksByIDs(ids []uint64) []entity.Bookmark { return ret } -func (m *BookmarkManager) Search(query string) ([]entity.Bookmark, error) { +func (m *BookmarkManager) Search(query string, tags []string) ([]entity.Bookmark, error) { rets := make([]uint64, 0, 0) counts := make(map[uint64]uint8) @@ -140,9 +140,25 @@ func (m *BookmarkManager) Search(query string) ([]entity.Bookmark, error) { } } + if tags != nil && len(tags) > 0 { + rets = m.LimitToIdsWithTags(rets, tags) + } return m.LoadBookmarksByIDs(rets), nil } +func (m *BookmarkManager) LimitToIdsWithTags(ids []uint64, tags []string) []uint64 { + outIds := []uint64{} + err := m.db.store.ForEach(bolthold.Where("ID").ContainsAny(bolthold.Slice(ids)...).And("Tags").ContainsAll(bolthold.Slice(tags)...), + func(bm *entity.Bookmark) error { + outIds = append(outIds, bm.ID) + return nil + }) + if err != nil { + panic(err) + } + return outIds +} + func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error { log.Printf("Start scrape for %s", bm.URL) diff --git a/db/bookmarks_test.go b/db/bookmarks_test.go index 17e44f7..6326c28 100644 --- a/db/bookmarks_test.go +++ b/db/bookmarks_test.go @@ -73,7 +73,7 @@ func BenchmarkOneWordSearch(b *testing.B) { bmm := NewBookmarkManager(&dbh) b.ResetTimer() for i := 0; i < b.N; i++ { - bmm.Search("hello") + bmm.Search("hello", nil) } } @@ -84,7 +84,7 @@ func BenchmarkTwoWordSearch(b *testing.B) { bmm := NewBookmarkManager(&dbh) b.ResetTimer() for i := 0; i < b.N; i++ { - bmm.Search("human relate") + bmm.Search("human relate", nil) } } @@ -95,6 +95,6 @@ func BenchmarkThreeWordSearch(b *testing.B) { bmm := NewBookmarkManager(&dbh) b.ResetTimer() for i := 0; i < b.N; i++ { - bmm.Search("human wiki editor") + bmm.Search("human wiki editor", nil) } } diff --git a/db/index_test.go b/db/index_test.go index 9e797e1..602cad2 100644 --- a/db/index_test.go +++ b/db/index_test.go @@ -47,7 +47,7 @@ func TestAddRemove(t *testing.T) { t.Errorf("scrape index returned %s", err) } - searchRes, err := bmm.Search("fox") + searchRes, err := bmm.Search("fox", nil) if err != nil { t.Errorf("search returned %s", err) } @@ -62,7 +62,7 @@ func TestAddRemove(t *testing.T) { t.Errorf("scrape index returned %s", err) } - searchRes, err = bmm.Search("fox") + searchRes, err = bmm.Search("fox", nil) if err != nil { t.Errorf("search returned %s", err) } @@ -70,7 +70,7 @@ func TestAddRemove(t *testing.T) { t.Error("got result when should not") } - searchRes, err = bmm.Search("rabbit") + searchRes, err = bmm.Search("rabbit", nil) if err != nil { t.Errorf("search returned %s", err) } @@ -83,7 +83,7 @@ func TestAddRemove(t *testing.T) { t.Errorf("got error when deleting: %s", err) } - searchRes, err = bmm.Search("rabbit") + searchRes, err = bmm.Search("rabbit", nil) if err != nil { t.Errorf("search returned %s", err) } @@ -119,7 +119,7 @@ func TestTagIndexing(t *testing.T) { t.Errorf("scrape index returned %s", err) } - searchRes, err := bmm.Search("fox") + searchRes, err := bmm.Search("fox", nil) if err != nil { t.Errorf("search returned %s", err) } @@ -133,7 +133,7 @@ func TestTagIndexing(t *testing.T) { if err != nil { t.Errorf("scrape index returned %s", err) } - searchRes, err = bmm.Search("sloth") + searchRes, err = bmm.Search("sloth", nil) if err != nil { t.Errorf("search returned %s", err) } diff --git a/version/version.go b/version/version.go index af17052..41ab91d 100644 --- a/version/version.go +++ b/version/version.go @@ -8,7 +8,7 @@ import ( "golang.org/x/mod/semver" ) -const Tag = "v0.0.20" +const Tag = "v0.0.21" var versionInfo struct { Local struct { diff --git a/web/templates/manage.html b/web/templates/manage.html index a051e27..40fa456 100644 --- a/web/templates/manage.html +++ b/web/templates/manage.html @@ -1,38 +1,20 @@ -
+
Manage links
- - - - - - - - - {{ range .bookmarks }} - - - - - - - - - - {{ end }} -
 title/urltagscreatedscraped
edit - {{ .Info.Title }} -
- {{ niceURL .URL }} -
- {{ range .Tags }} - {{ . }} - {{ end }} - {{ (nicetime .TimestampCreated).HumanDuration }} ago{{ (nicetime .TimestampLastScraped).HumanDuration }} ago - scrape -
+
+
+
+ + +
+
+ {{ template "tags_widget.html" . }} +
+ {{ template "manage_results.html" . }}
\ No newline at end of file diff --git a/web/templates/manage_results.html b/web/templates/manage_results.html new file mode 100644 index 0000000..20e2b27 --- /dev/null +++ b/web/templates/manage_results.html @@ -0,0 +1,31 @@ + + + + + + + + + + {{ range .bookmarks }} + + + + + + + + + + {{ end }} +
 title/urltagscreatedscraped
edit + {{ .Info.Title }} +
+ {{ niceURL .URL }} +
+ {{ range .Tags }} + {{ . }} + {{ end }} + {{ (nicetime .TimestampCreated).HumanDuration }} ago{{ (nicetime .TimestampLastScraped).HumanDuration }} ago + scrape +
\ No newline at end of file diff --git a/web/templates/tags_widget.html b/web/templates/tags_widget.html index da7fb23..495a39f 100644 --- a/web/templates/tags_widget.html +++ b/web/templates/tags_widget.html @@ -1,4 +1,4 @@ -