15 Commits

9 changed files with 118 additions and 21 deletions

View File

@@ -1,9 +1,11 @@
package db package db
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
"strings"
"sync" "sync"
"time" "time"
@@ -21,6 +23,7 @@ type BookmarkManager struct {
type SearchOptions struct { type SearchOptions struct {
Query string Query string
Tags []string Tags []string
Sort string
} }
func NewBookmarkManager(db *DB) *BookmarkManager { func NewBookmarkManager(db *DB) *BookmarkManager {
@@ -31,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 {
@@ -138,10 +147,32 @@ func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error)
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...)) 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{} out := []entity.Bookmark{}
err := m.db.store.ForEach(&bhQuery, err := m.db.store.ForEach(&bhQuery,
func(bm *entity.Bookmark) error { func(bm *entity.Bookmark) error {
out = append(out, *bm) out = append(out, *bm)
return nil return nil
}) })
if err != nil { if err != nil {

View File

@@ -8,7 +8,7 @@ import (
"golang.org/x/mod/semver" "golang.org/x/mod/semver"
) )
const Tag = "v0.0.21" 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,24 +1,24 @@
<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>
<div class="grid-x grid-padding-x"> <div class="grid-x grid-padding-x">
<div class="medium-6 cell"> <div class="medium-6 cell">
<a href="#" class="button" hx-post="/add" <a href="#" class="button" hx-post="/add"
hx-target="#add-url-form">add</a> hx-target="#add-url-form">add</a>
</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

@@ -13,8 +13,9 @@
</div> </div>
</div> </div>
{{ template "tags_widget.html" . }} {{ template "tags_widget.html" . }}
{{ template "manage_results.html" . }}
</form> </form>
{{ template "manage_results.html" . }}
</div> </div>
</div> </div>

View File

@@ -2,10 +2,10 @@
<table id="manage-results"> <table id="manage-results">
<tr> <tr>
<th>&nbsp;</th> <th>&nbsp;</th>
<th>title/url</th> {{ template "manage_results_column_header.html" .column.title }}
<th>tags</th> <th>tags</th>
<th class="show-for-large">created</th> {{ template "manage_results_column_header.html" .column.created }}
<th class="show-for-large">scraped</th> {{ template "manage_results_column_header.html" .column.scraped }}
</tr> </tr>
{{ range .bookmarks }} {{ range .bookmarks }}
<tr> <tr>

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" id="tags-list"> <div class="small-6 cell" id="tags-list">
{{ range .tags }} {{ range .tags }}
<a href="#" <a href="#"
class="" class=""

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 {
@@ -92,13 +115,42 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
r.POST("/manage/results", func(c *gin.Context) { r.POST("/manage/results", func(c *gin.Context) {
query := c.PostForm("query") query := c.PostForm("query")
tags := []string{} tags := []string{}
sort := c.Query("sort")
if c.PostForm("tags_hidden") != "" { if c.PostForm("tags_hidden") != "" {
tags = strings.Split(c.PostForm("tags_hidden"), "|") tags = strings.Split(c.PostForm("tags_hidden"), "|")
} }
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags}) allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags, Sort: sort})
meta := gin.H{"config": config, "bookmarks": allBookmarks} meta := gin.H{"config": config, "bookmarks": allBookmarks}
log.Printf("query is %s, tags %v", query, tags) 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, c.HTML(http.StatusOK,
"manage_results.html", meta, "manage_results.html", meta,
@@ -153,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) {
@@ -350,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{}
@@ -374,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}
} }