17 Commits

Author SHA1 Message Date
6175c77478 Bump version 2022-06-10 11:24:29 +09:30
742f42115f Add error checking for URL validity 2022-06-10 11:23:55 +09:30
9e7914c9a1 Space cells so that tags do not go on a new row. 2022-06-10 11:14:21 +09:30
fe9d8e71f4 Bump version 2022-06-09 20:21:45 +09:30
0c3bf0701d Don't panic if we cannot fetch current version from github 2022-06-09 20:20:31 +09:30
0bc777c6f1 Bump version 2022-06-09 19:56:21 +09:30
2055bba5f0 Capture tag if URL added before tag submission, allow for tags to be comma separated. 2022-06-09 19:51:21 +09:30
a2a1f9c06d Fix some element positioning 2022-06-09 19:38:30 +09:30
79280135a1 Bump version 2022-06-07 20:00:12 +09:30
4422f3840e Fix bug where column headings were displayed incorrectly on small screens 2022-06-07 19:59:33 +09:30
ffcf7c0438 Bump version 2022-06-07 19:52:35 +09:30
a910aac946 Add reversible column sorting, with sort direction indicators 2022-06-07 19:51:56 +09:30
eed41aebbb Neaten the formatting of durations 2022-06-07 16:45:03 +09:30
037be4d7e8 Bump version 2022-06-07 16:26:39 +09:30
6cf327226d Add basic sorting to management interface 2022-06-07 16:25:45 +09:30
42fd1973b8 Clean up search code, limit bookmarks list by query and/or tags 2022-06-07 11:07:38 +09:30
1563c7b21d Manage list can be free-text searched and limited by tag. 2022-06-06 22:05:56 +09:30
12 changed files with 217 additions and 80 deletions

View File

@@ -2,6 +2,7 @@
"cSpell.words": [ "cSpell.words": [
"bolthold", "bolthold",
"colly", "colly",
"htmx",
"incpatch", "incpatch",
"linkwallet", "linkwallet",
"nicetime", "nicetime",

View File

@@ -1,9 +1,11 @@
package db package db
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
"strings"
"sync" "sync"
"time" "time"
@@ -18,6 +20,12 @@ type BookmarkManager struct {
scrapeQueue chan *entity.Bookmark scrapeQueue chan *entity.Bookmark
} }
type SearchOptions struct {
Query string
Tags []string
Sort string
}
func NewBookmarkManager(db *DB) *BookmarkManager { func NewBookmarkManager(db *DB) *BookmarkManager {
return &BookmarkManager{db: db, scrapeQueue: make(chan *entity.Bookmark)} return &BookmarkManager{db: db, scrapeQueue: make(chan *entity.Bookmark)}
} }
@@ -26,6 +34,12 @@ func NewBookmarkManager(db *DB) *BookmarkManager {
// if this bookmark already exists (based on URL match). // if this bookmark already exists (based on URL match).
// The entity.Bookmark ID field will be updated. // The entity.Bookmark ID field will be updated.
func (m *BookmarkManager) AddBookmark(bm *entity.Bookmark) error { func (m *BookmarkManager) AddBookmark(bm *entity.Bookmark) error {
if strings.Index(bm.URL, "https://") != 0 &&
strings.Index(bm.URL, "http://") != 0 {
return errors.New("URL must begin with http:// or https://")
}
existing := entity.Bookmark{} existing := entity.Bookmark{}
err := m.db.store.FindOne(&existing, bolthold.Where("URL").Eq(bm.URL)) err := m.db.store.FindOne(&existing, bolthold.Where("URL").Eq(bm.URL))
if err != bolthold.ErrNotFound { if err != bolthold.ErrNotFound {
@@ -94,28 +108,12 @@ func (m *BookmarkManager) LoadBookmarkByID(id uint64) entity.Bookmark {
return ret return ret
} }
func (m *BookmarkManager) LoadBookmarksByIDs(ids []uint64) []entity.Bookmark { func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error) {
// log.Printf("loading %v", ids)
ret := make([]entity.Bookmark, 0, 0)
s := make([]interface{}, len(ids))
for i, v := range ids {
s[i] = v
}
err := m.db.store.Find(&ret, bolthold.Where("ID").In(s...))
if err != nil {
panic(err)
}
return ret
}
func (m *BookmarkManager) Search(query string) ([]entity.Bookmark, error) {
rets := make([]uint64, 0, 0)
// first get a list of all the ids that match our query
idsMatchingQuery := make([]uint64, 0, 0)
counts := make(map[uint64]uint8) counts := make(map[uint64]uint8)
words := content.StringToSearchWords(opts.Query)
words := content.StringToSearchWords(query)
for _, word := range words { for _, word := range words {
var wi *entity.WordIndex var wi *entity.WordIndex
@@ -133,14 +131,55 @@ func (m *BookmarkManager) Search(query string) ([]entity.Bookmark, error) {
for k, v := range counts { for k, v := range counts {
if v == uint8(len(words)) { if v == uint8(len(words)) {
rets = append(rets, k) idsMatchingQuery = append(idsMatchingQuery, k)
if len(rets) > 10 { if len(idsMatchingQuery) > 10 {
break break
} }
} }
} }
return m.LoadBookmarksByIDs(rets), nil // now we can do our search
bhQuery := bolthold.Query{}
if opts.Query != "" {
bhQuery = bolthold.Query(*bhQuery.And("ID").In(bolthold.Slice(idsMatchingQuery)...))
}
if opts.Tags != nil && len(opts.Tags) > 0 {
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...))
}
reverse := false
sortOrder := opts.Sort
if sortOrder != "" && sortOrder[0] == '-' {
reverse = true
sortOrder = sortOrder[1:]
}
if sortOrder == "title" {
bhQuery.SortBy("Info.Title")
} else if sortOrder == "created" {
bhQuery.SortBy("TimestampCreated")
} else if sortOrder == "scraped" {
bhQuery.SortBy("TimestampLastScraped")
} else {
bhQuery.SortBy("ID")
}
if reverse {
bhQuery = *bhQuery.Reverse()
}
out := []entity.Bookmark{}
err := m.db.store.ForEach(&bhQuery,
func(bm *entity.Bookmark) error {
out = append(out, *bm)
return nil
})
if err != nil {
panic(err)
}
return out, nil
} }
func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error { func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {

View File

@@ -73,7 +73,7 @@ func BenchmarkOneWordSearch(b *testing.B) {
bmm := NewBookmarkManager(&dbh) bmm := NewBookmarkManager(&dbh)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
bmm.Search("hello") bmm.Search(SearchOptions{Query: "hello"})
} }
} }
@@ -84,7 +84,7 @@ func BenchmarkTwoWordSearch(b *testing.B) {
bmm := NewBookmarkManager(&dbh) bmm := NewBookmarkManager(&dbh)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
bmm.Search("human relate") bmm.Search(SearchOptions{Query: "human relate"})
} }
} }
@@ -95,6 +95,6 @@ func BenchmarkThreeWordSearch(b *testing.B) {
bmm := NewBookmarkManager(&dbh) bmm := NewBookmarkManager(&dbh)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
bmm.Search("human wiki editor") bmm.Search(SearchOptions{Query: "human wiki editor"})
} }
} }

View File

@@ -47,7 +47,7 @@ func TestAddRemove(t *testing.T) {
t.Errorf("scrape index returned %s", err) t.Errorf("scrape index returned %s", err)
} }
searchRes, err := bmm.Search("fox") searchRes, err := bmm.Search(SearchOptions{Query: "fox"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }
@@ -62,7 +62,7 @@ func TestAddRemove(t *testing.T) {
t.Errorf("scrape index returned %s", err) t.Errorf("scrape index returned %s", err)
} }
searchRes, err = bmm.Search("fox") searchRes, err = bmm.Search(SearchOptions{Query: "fox"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }
@@ -70,7 +70,7 @@ func TestAddRemove(t *testing.T) {
t.Error("got result when should not") t.Error("got result when should not")
} }
searchRes, err = bmm.Search("rabbit") searchRes, err = bmm.Search(SearchOptions{Query: "rabbit"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }
@@ -83,7 +83,7 @@ func TestAddRemove(t *testing.T) {
t.Errorf("got error when deleting: %s", err) t.Errorf("got error when deleting: %s", err)
} }
searchRes, err = bmm.Search("rabbit") searchRes, err = bmm.Search(SearchOptions{Query: "rabbit"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }
@@ -119,7 +119,7 @@ func TestTagIndexing(t *testing.T) {
t.Errorf("scrape index returned %s", err) t.Errorf("scrape index returned %s", err)
} }
searchRes, err := bmm.Search("fox") searchRes, err := bmm.Search(SearchOptions{Query: "fox"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }
@@ -133,7 +133,7 @@ func TestTagIndexing(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("scrape index returned %s", err) t.Errorf("scrape index returned %s", err)
} }
searchRes, err = bmm.Search("sloth") searchRes, err = bmm.Search(SearchOptions{Query: "sloth"})
if err != nil { if err != nil {
t.Errorf("search returned %s", err) t.Errorf("search returned %s", err)
} }

View File

@@ -8,7 +8,7 @@ import (
"golang.org/x/mod/semver" "golang.org/x/mod/semver"
) )
const Tag = "v0.0.20" const Tag = "v0.0.27"
var versionInfo struct { var versionInfo struct {
Local struct { Local struct {
@@ -55,7 +55,7 @@ func UpdateVersionInfo() {
rels, _, err := client.Repositories.ListReleases(context.Background(), "tardisx", "linkwallet", nil) rels, _, err := client.Repositories.ListReleases(context.Background(), "tardisx", "linkwallet", nil)
if err != nil { if err != nil {
panic(err) return
} }
if len(rels) == 0 { if len(rels) == 0 {
return return

View File

@@ -1,18 +1,18 @@
<div class="large-8 medium-8 cell" id="add-url-form" > <div class="large-8 medium-8 cell" id="add-url-form" >
<div> <div>
<h5 style="display:inline-block;">Add a new URL</h5> <h5 style="display:inline-block;">Add a new URL</h5>
<p style="display:inline-block;">[<a hx-get="/bulk_add" hx-target="#add-url-form" href="#">bulk add</a>]</h5> <div style="display:inline-block;">[<a hx-get="/bulk_add" hx-swap="outerHTML" hx-target="#add-url-form" href="#">bulk add</a>]</div></h5>
</div> </div>
<form onsubmit="return false"> <form onsubmit="return false">
<div class="grid-x grid-padding-x"> <div class="grid-x grid-padding-x">
<div class="large-6 cell"> <div class="medium-6 cell">
<label>URL</label> <label>URL</label>
<input type="text" name="url" value="{{ .url }}" <input type="text" name="url" value="{{ .url }}"
hx-trigger="" hx-trigger=""
/> />
</div> </div>
<div class="large-6 cell"> <div class="medium-6 cell">
{{ template "tags_widget.html" . }} {{ template "tags_widget.html" . }}
</div> </div>
</div> </div>

View File

@@ -1,14 +1,13 @@
<div class="large-8 medium-8 cell" id="add-url-form" > <div class="large-8 medium-8 cell" id="add-url-form" >
<div> <div>
<h5 style="display:inline-block;">Add bulk URLs</h5> <h5 style="display:inline-block;">Add bulk URLs</h5>
<p style="display:inline-block;">[<a hx-get="/single_add" hx-target="#add-url-form" href="#">single add</a>]</h5> <p style="display:inline-block;">[<a hx-get="/single_add" hx-swap="outerHTML" hx-target="#add-url-form" href="#">single add</a>]</h5>
</div> <form onsubmit="return false"> </div>
<form onsubmit="return false">
<div class="grid-x grid-padding-x"> <div class="grid-x grid-padding-x">
<div class="large-12 cell"> <div class="large-12 cell">
<label>Paste URL's, one per line</label> <label>Paste URL's, one per line</label>
<textarea type="text" name="urls" rows="10" <textarea type="text" name="urls" rows="10"></textarea>
></textarea>
</div> </div>
</div> </div>
<button <button

View File

@@ -1,38 +1,21 @@
<div class="grid-x grid-padding-x"> <div class="grid-x grid-padding-x" id="manage">
<div class="large-12 cell"> <div class="large-12 cell">
<h5>Manage links</h5> <h5>Manage links</h5>
<table> <form onsubmit="return false">
<tr> <div class="grid-x grid-padding-x">
<th>&nbsp;</th> <div class="large-12 cell">
<th>title/url</th> <label>Filter</label>
<th>tags</th> <input type="text" name="query" placeholder="" hx-post="/manage/results" hx-swap="outerHTML"
<th class="show-for-large">created</th> hx-trigger="keyup changed delay:500ms, tag_update" hx-target="#manage-results"
<th class="show-for-large">scraped</th> hx-indicator="#htmx-indicator-search" id="manage-search" />
</tr> </div>
{{ range .bookmarks }} </div>
<tr> {{ template "tags_widget.html" . }}
<th><a class="button" href="/edit/{{ .ID }}">edit</a></th> {{ template "manage_results.html" . }}
<td>
<a href="{{ .URL }}">{{ .Info.Title }}</a>
<br>
<a href="{{ .URL }}">{{ niceURL .URL }}</a>
</td>
<td>
{{ range .Tags }}
<span class="label primary">{{ . }}</span>
{{ end }}
</td>
<td class="show-for-large">{{ (nicetime .TimestampCreated).HumanDuration }} ago</td>
<td class="show-for-large">{{ (nicetime .TimestampLastScraped).HumanDuration }} ago</td>
<td> </form>
<a class="button" hx-swap="outerHTML" hx-post="/scrape/{{ .ID }}">scrape</button>
</td>
</tr>
{{ end }}
</table>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,31 @@
<table id="manage-results">
<tr>
<th>&nbsp;</th>
{{ template "manage_results_column_header.html" .column.title }}
<th>tags</th>
{{ template "manage_results_column_header.html" .column.created }}
{{ template "manage_results_column_header.html" .column.scraped }}
</tr>
{{ range .bookmarks }}
<tr>
<th><a class="button" href="/edit/{{ .ID }}">edit</a></th>
<td>
<a href="{{ .URL }}">{{ .Info.Title }}</a>
<br>
<a href="{{ .URL }}">{{ niceURL .URL }}</a>
</td>
<td>
{{ range .Tags }}
<span class="label primary">{{ . }}</span>
{{ end }}
</td>
<td class="show-for-large">{{ (nicetime .TimestampCreated).HumanDuration }} ago</td>
<td class="show-for-large">{{ (nicetime .TimestampLastScraped).HumanDuration }} ago</td>
<td>
<a class="button" hx-swap="outerHTML" hx-post="/scrape/{{ .ID }}">scrape</button>
</td>
</tr>
{{ end }}
</table>

View File

@@ -0,0 +1,3 @@
<th class="{{ .Class }}" hx-post="/manage/results?sort={{ .URLString }}" hx-target="#manage-results">{{ .Name }}&nbsp;{{ .TitleArrow }}
</th>

View File

@@ -1,15 +1,15 @@
<div id="label-widget"> <div id="label-widget" >
<div class="grid-x grid-padding-x"> <div class="grid-x grid-padding-x">
<div class="small-9 medium-10 large-5 cell" <div class="small-6 cell"
hx-post="/tags" hx-post="/tags"
hx-target="#label-widget" hx-target="#label-widget"
hx-trigger="change"> hx-trigger="change queue:first">
<label for="tag-entry" <label for="tag-entry"
class="">Tags</label> class="">Tags</label>
<input id="tag-entry" type="text" name="tag" placeholder="enter tags" /> <input id="tag-entry" type="text" name="tag" placeholder="enter tags" />
</div> </div>
<div class="small-12 large-6 cell"> <div class="small-6 cell" id="tags-list">
{{ range .tags }} {{ range .tags }}
<a href="#" <a href="#"
class="" class=""
@@ -20,7 +20,7 @@
<span class="label primary">{{ . }}</span> <span class="label primary">{{ . }}</span>
{{ end }} {{ end }}
<input type="hidden" name="tags_hidden" value="{{ .tags_hidden }}"> <input _="on load send tag_update to #manage-search" type="hidden" id="tags-hidden" name="tags_hidden" value="{{ .tags_hidden }}">
</div> </div>
</div> </div>
</div> </div>

View File

@@ -42,6 +42,29 @@ type Server struct {
bmm *db.BookmarkManager bmm *db.BookmarkManager
} }
type ColumnInfo struct {
Name string
Param string
Sorted string
Class string
}
func (c ColumnInfo) URLString() string {
if c.Sorted == "asc" {
return "-" + c.Param
}
return c.Param
}
func (c ColumnInfo) TitleArrow() string {
if c.Sorted == "asc" {
return "↑"
} else if c.Sorted == "desc" {
return "↓"
}
return ""
}
// Create creates a new web server instance and sets up routing. // Create creates a new web server instance and sets up routing.
func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server { func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
@@ -81,6 +104,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
}) })
r.GET("/manage", func(c *gin.Context) { r.GET("/manage", func(c *gin.Context) {
allBookmarks, _ := bmm.ListBookmarks() allBookmarks, _ := bmm.ListBookmarks()
meta := gin.H{"page": "manage", "config": config, "bookmarks": allBookmarks} meta := gin.H{"page": "manage", "config": config, "bookmarks": allBookmarks}
c.HTML(http.StatusOK, c.HTML(http.StatusOK,
@@ -88,6 +112,52 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
) )
}) })
r.POST("/manage/results", func(c *gin.Context) {
query := c.PostForm("query")
tags := []string{}
sort := c.Query("sort")
if c.PostForm("tags_hidden") != "" {
tags = strings.Split(c.PostForm("tags_hidden"), "|")
}
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags, Sort: sort})
meta := gin.H{"config": config, "bookmarks": allBookmarks}
colTitle := &ColumnInfo{Name: "Title/URL", Param: "title"}
colCreated := &ColumnInfo{Name: "Created", Param: "created", Class: "show-for-large"}
colScraped := &ColumnInfo{Name: "Scraped", Param: "scraped", Class: "show-for-large"}
if sort == "title" {
colTitle.Sorted = "asc"
}
if sort == "-title" {
colTitle.Sorted = "desc"
}
if sort == "scraped" {
colScraped.Sorted = "asc"
}
if sort == "-scraped" {
colScraped.Sorted = "desc"
}
if sort == "created" {
colCreated.Sorted = "asc"
}
if sort == "-created" {
colCreated.Sorted = "desc"
}
cols := gin.H{
"title": colTitle,
"created": colCreated,
"scraped": colScraped,
}
meta["column"] = cols
c.HTML(http.StatusOK,
"manage_results.html", meta,
)
})
r.GET("/config", func(c *gin.Context) { r.GET("/config", func(c *gin.Context) {
meta := gin.H{"page": "config", "config": config} meta := gin.H{"page": "config", "config": config}
c.HTML(http.StatusOK, c.HTML(http.StatusOK,
@@ -107,7 +177,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
r.POST("/search", func(c *gin.Context) { r.POST("/search", func(c *gin.Context) {
query := c.PostForm("query") query := c.PostForm("query")
sr, err := bmm.Search(query) sr, err := bmm.Search(db.SearchOptions{Query: query})
data := gin.H{ data := gin.H{
"results": sr, "results": sr,
"error": err, "error": err,
@@ -135,6 +205,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
"bm": bm, "bm": bm,
"error": err, "error": err,
} }
if err != nil {
data["url"] = url
data["tags"] = tags
data["tags_hidden"] = c.PostForm("tags_hidden")
}
c.HTML(http.StatusOK, "add_url_form.html", data) c.HTML(http.StatusOK, "add_url_form.html", data)
}) })
r.POST("/add_bulk", func(c *gin.Context) { r.POST("/add_bulk", func(c *gin.Context) {
@@ -332,7 +409,10 @@ func cleanupTags(tags []string) []string {
keys := make(map[string]struct{}) keys := make(map[string]struct{})
for _, k := range tags { for _, k := range tags {
if k != "" && k != "|" { if k != "" && k != "|" {
keys[strings.ToLower(k)] = struct{}{} for _, subKey := range strings.Split(k, ",") {
subKey := strings.Trim(subKey, " ")
keys[strings.ToLower(subKey)] = struct{}{}
}
} }
} }
out := []string{} out := []string{}
@@ -356,6 +436,7 @@ func niceTime(t time.Time) timeVariations {
panic(err) panic(err)
} }
ago := durafmt.Parse(time.Since(t)).LimitFirstN(1).Format(units) ago := durafmt.Parse(time.Since(t)).LimitFirstN(1).Format(units)
ago = strings.ReplaceAll(ago, " ", "")
return timeVariations{HumanDuration: ago} return timeVariations{HumanDuration: ago}
} }