Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a03baca498 | |||
| 1904bfd265 | |||
| 2d488ffdcc | |||
| f4c5834f0c | |||
| db72e8fb7d | |||
| 6f93f2ff50 | |||
| 2913d510bd | |||
| 8bd2c87fc6 | |||
| ccf373b863 | |||
| c5957e38db | |||
| 5be599589a | |||
| da970239a5 | |||
| 451335e17d | |||
| 6175c77478 | |||
| 742f42115f | |||
| 9e7914c9a1 | |||
| fe9d8e71f4 | |||
| 0c3bf0701d | |||
| 0bc777c6f1 | |||
| 2055bba5f0 | |||
| a2a1f9c06d | |||
| 79280135a1 | |||
| 4422f3840e | |||
| ffcf7c0438 | |||
| a910aac946 | |||
| eed41aebbb | |||
| 037be4d7e8 | |||
| 6cf327226d | |||
| 42fd1973b8 | |||
| 1563c7b21d |
@@ -90,3 +90,4 @@ changelog:
|
|||||||
- '^docs:'
|
- '^docs:'
|
||||||
- '^test:'
|
- '^test:'
|
||||||
- '^[Bb]ump'
|
- '^[Bb]ump'
|
||||||
|
- '^[Cc]lean'
|
||||||
|
|||||||
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -2,6 +2,7 @@
|
|||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
"bolthold",
|
"bolthold",
|
||||||
"colly",
|
"colly",
|
||||||
|
"htmx",
|
||||||
"incpatch",
|
"incpatch",
|
||||||
"linkwallet",
|
"linkwallet",
|
||||||
"nicetime",
|
"nicetime",
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ A self-hosted bookmark database with full-text page content search.
|
|||||||
mountpoint.
|
mountpoint.
|
||||||
* Run `docker-compose up -d`
|
* Run `docker-compose up -d`
|
||||||
|
|
||||||
|
To upgrade:
|
||||||
|
|
||||||
|
* `docker-compose pull`
|
||||||
|
* `docker-compose up -d`
|
||||||
|
|
||||||
## Packages (deb/rpm)
|
## Packages (deb/rpm)
|
||||||
|
|
||||||
* Download the .deb or .rpm from the releases
|
* Download the .deb or .rpm from the releases
|
||||||
|
|||||||
@@ -25,17 +25,29 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bmm := db.NewBookmarkManager(&dbh)
|
bmm := db.NewBookmarkManager(&dbh)
|
||||||
cmm := db.NewConfigManager(&dbh)
|
cmm := db.NewConfigManager(&dbh)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
version.UpdateVersionInfo()
|
version.VersionInfo.UpdateVersionInfo()
|
||||||
time.Sleep(time.Hour * 6)
|
time.Sleep(time.Hour * 6)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Printf("linkwallet version %s starting", version.Is())
|
// update stats every 5 minutes
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
err := dbh.UpdateBookmarkStats()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Minute * 5)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Printf("linkwallet version %s starting", version.VersionInfo.Local.Tag)
|
||||||
|
|
||||||
server := web.Create(bmm, cmm)
|
server := web.Create(bmm, cmm)
|
||||||
go bmm.RunQueue()
|
go bmm.RunQueue()
|
||||||
|
|||||||
@@ -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 {
|
||||||
@@ -86,7 +100,6 @@ func (m *BookmarkManager) SaveBookmark(bm *entity.Bookmark) error {
|
|||||||
func (m *BookmarkManager) LoadBookmarkByID(id uint64) entity.Bookmark {
|
func (m *BookmarkManager) LoadBookmarkByID(id uint64) entity.Bookmark {
|
||||||
// log.Printf("loading %v", ids)
|
// log.Printf("loading %v", ids)
|
||||||
ret := entity.Bookmark{}
|
ret := entity.Bookmark{}
|
||||||
log.Printf("loading id %d", id)
|
|
||||||
err := m.db.store.Get(id, &ret)
|
err := m.db.store.Get(id, &ret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -94,28 +107,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 +130,57 @@ 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.db.IncrementSearches()
|
||||||
|
|
||||||
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
|
func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
|
||||||
@@ -166,7 +206,6 @@ func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
|
|||||||
func (m *BookmarkManager) UpdateIndexForBookmark(bm *entity.Bookmark) {
|
func (m *BookmarkManager) UpdateIndexForBookmark(bm *entity.Bookmark) {
|
||||||
words := content.Words(bm)
|
words := content.Words(bm)
|
||||||
words = append(words, bm.Tags...)
|
words = append(words, bm.Tags...)
|
||||||
log.Printf("index for %d %s (%d words)", bm.ID, bm.URL, len(words))
|
|
||||||
m.db.UpdateIndexForWordsByID(words, bm.ID)
|
m.db.UpdateIndexForWordsByID(words, bm.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,3 +279,12 @@ func (m *BookmarkManager) UpdateContent() {
|
|||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *BookmarkManager) Stats() (entity.DBStats, error) {
|
||||||
|
stats := entity.DBStats{}
|
||||||
|
err := m.db.store.Get("stats", &stats)
|
||||||
|
if err != nil && err != bolthold.ErrNotFound {
|
||||||
|
return stats, fmt.Errorf("could not load stats: %s", err)
|
||||||
|
}
|
||||||
|
return stats, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -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"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
86
db/db.go
86
db/db.go
@@ -2,7 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"time"
|
||||||
|
|
||||||
"github.com/tardisx/linkwallet/entity"
|
"github.com/tardisx/linkwallet/entity"
|
||||||
bolthold "github.com/timshannon/bolthold"
|
bolthold "github.com/timshannon/bolthold"
|
||||||
@@ -28,8 +28,84 @@ func (db *DB) Close() {
|
|||||||
db.store.Close()
|
db.store.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Dumpy() {
|
// func (db *DB) Dumpy() {
|
||||||
res := make([]entity.Bookmark, 0, 0)
|
// res := make([]entity.Bookmark, 0, 0)
|
||||||
db.store.Find(&res, &bolthold.Query{})
|
// db.store.Find(&res, &bolthold.Query{})
|
||||||
log.Printf("%v", res)
|
// log.Printf("%v", res)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// IncrementSearches increments the number of searches we have ever performed by one.
|
||||||
|
func (db *DB) IncrementSearches() error {
|
||||||
|
txn, err := db.store.Bolt().Begin(true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not start transaction for increment searches: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := entity.DBStats{}
|
||||||
|
err = db.store.TxGet(txn, "stats", &stats)
|
||||||
|
if err != nil && err != bolthold.ErrNotFound {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not get stats for incrementing searches: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats.Searches += 1
|
||||||
|
err = db.store.TxUpsert(txn, "stats", &stats)
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not upsert stats for incrementing searches: %s", err)
|
||||||
|
}
|
||||||
|
err = txn.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not commit increment searches transaction: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateBookmarkStats updates the history on the number of bookmarks and words indexed.
|
||||||
|
func (db *DB) UpdateBookmarkStats() error {
|
||||||
|
|
||||||
|
txn, err := db.store.Bolt().Begin(true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not start transaction for update stats: %s", err)
|
||||||
|
}
|
||||||
|
// count bookmarks and words indexed
|
||||||
|
bmI := entity.Bookmark{}
|
||||||
|
wiI := entity.WordIndex{}
|
||||||
|
bookmarkCount, err := db.store.TxCount(txn, &bmI, &bolthold.Query{})
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not get bookmark count: %s", err)
|
||||||
|
}
|
||||||
|
indexWordCount, err := db.store.TxCount(txn, &wiI, &bolthold.Query{})
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not get index word count: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bucket these stats by day
|
||||||
|
now := time.Now().Truncate(time.Hour * 24)
|
||||||
|
|
||||||
|
stats := entity.DBStats{}
|
||||||
|
err = db.store.TxGet(txn, "stats", &stats)
|
||||||
|
if err != nil && err != bolthold.ErrNotFound {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not get stats: %s", err)
|
||||||
|
}
|
||||||
|
if stats.History == nil {
|
||||||
|
stats.History = make(map[time.Time]entity.BookmarkInfo)
|
||||||
|
}
|
||||||
|
stats.History[now] = entity.BookmarkInfo{Bookmarks: bookmarkCount, IndexedWords: indexWordCount}
|
||||||
|
err = db.store.TxUpsert(txn, "stats", &stats)
|
||||||
|
if err != nil {
|
||||||
|
txn.Rollback()
|
||||||
|
return fmt.Errorf("could not upsert stats: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = txn.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not commit stats transaction: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
15
db/index.go
15
db/index.go
@@ -20,7 +20,20 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
|
|||||||
}
|
}
|
||||||
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) error {
|
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) error {
|
||||||
delete(wi.Bitmap, id)
|
delete(wi.Bitmap, id)
|
||||||
db.store.TxUpdate(txn, "word_index_"+wi.Word, wi)
|
// if the index is now completely empty, nuke it entirely
|
||||||
|
empty := true
|
||||||
|
for _, v := range wi.Bitmap {
|
||||||
|
if v {
|
||||||
|
empty = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if empty {
|
||||||
|
db.store.TxDelete(txn, "word_index_"+wi.Word, wi)
|
||||||
|
} else {
|
||||||
|
db.store.TxUpdate(txn, "word_index_"+wi.Word, wi)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
47
entity/meta.go
Normal file
47
entity/meta.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DBStats struct {
|
||||||
|
History map[time.Time]BookmarkInfo
|
||||||
|
Searches int
|
||||||
|
}
|
||||||
|
|
||||||
|
type BookmarkInfo struct {
|
||||||
|
Bookmarks int
|
||||||
|
IndexedWords int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stats DBStats) String() string {
|
||||||
|
out := fmt.Sprintf("searches: %d\n", stats.Searches)
|
||||||
|
|
||||||
|
dates := []time.Time{}
|
||||||
|
|
||||||
|
for k := range stats.History {
|
||||||
|
dates = append(dates, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(dates, func(i, j int) bool { return dates[i].Before(dates[j]) })
|
||||||
|
|
||||||
|
for _, k := range dates {
|
||||||
|
out += fmt.Sprintf("%s - %d bookmarks, %d words indexed\n", k, stats.History[k].Bookmarks, stats.History[k].IndexedWords)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stats DBStats) MostRecentBookmarkInfo() BookmarkInfo {
|
||||||
|
mostRecent := time.Time{}
|
||||||
|
for k := range stats.History {
|
||||||
|
if k.After(mostRecent) {
|
||||||
|
mostRecent = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !mostRecent.IsZero() {
|
||||||
|
return stats.History[mostRecent]
|
||||||
|
}
|
||||||
|
return BookmarkInfo{}
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@@ -37,6 +37,7 @@ require (
|
|||||||
github.com/gobwas/glob v0.2.3 // indirect
|
github.com/gobwas/glob v0.2.3 // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb
|
||||||
github.com/google/go-github/v44 v44.1.0
|
github.com/google/go-github/v44 v44.1.0
|
||||||
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
|
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
|
||||||
github.com/kennygrant/sanitize v1.2.4 // indirect
|
github.com/kennygrant/sanitize v1.2.4 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -38,6 +38,8 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
|
|||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb h1:5b/eFaSaKPFG9ygDBaPKkydKU5nFJYk08g9jPIVogMg=
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||||
|
|||||||
14
meta/meta.go
Normal file
14
meta/meta.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package meta
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MemInfo() string {
|
||||||
|
stats := runtime.MemStats{}
|
||||||
|
runtime.ReadMemStats(&stats)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%.3fMb", float64(stats.Alloc)/1024.0/1024.0)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,15 +2,17 @@ package version
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/go-github/v44/github"
|
"github.com/google/go-github/v44/github"
|
||||||
"golang.org/x/mod/semver"
|
"golang.org/x/mod/semver"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Tag = "v0.0.20"
|
const Tag = "v0.0.32"
|
||||||
|
|
||||||
var versionInfo struct {
|
type Info struct {
|
||||||
Local struct {
|
Local struct {
|
||||||
Tag string
|
Tag string
|
||||||
}
|
}
|
||||||
@@ -18,51 +20,58 @@ var versionInfo struct {
|
|||||||
Valid bool
|
Valid bool
|
||||||
Tag string
|
Tag string
|
||||||
}
|
}
|
||||||
m sync.Mutex
|
UpgradeReleaseNotes string
|
||||||
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var VersionInfo Info
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
versionInfo.Remote.Valid = false
|
VersionInfo.Remote.Valid = false
|
||||||
versionInfo.Local.Tag = Tag
|
VersionInfo.Local.Tag = Tag
|
||||||
}
|
}
|
||||||
|
|
||||||
func Is() string {
|
func (vi *Info) UpgradeAvailable() bool {
|
||||||
return versionInfo.Local.Tag
|
vi.m.Lock()
|
||||||
}
|
defer vi.m.Unlock()
|
||||||
|
if !vi.Remote.Valid {
|
||||||
func UpgradeAvailable() (bool, string) {
|
return false
|
||||||
versionInfo.m.Lock()
|
|
||||||
defer versionInfo.m.Unlock()
|
|
||||||
if !versionInfo.Remote.Valid {
|
|
||||||
return false, ""
|
|
||||||
}
|
}
|
||||||
if semver.Compare(versionInfo.Local.Tag, versionInfo.Remote.Tag) < 0 {
|
if semver.Compare(vi.Local.Tag, vi.Remote.Tag) < 0 {
|
||||||
return true, versionInfo.Remote.Tag
|
return true
|
||||||
}
|
}
|
||||||
return false, ""
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpgradeAvailableString() string {
|
func (vi *Info) UpdateVersionInfo() {
|
||||||
upgrade, ver := UpgradeAvailable()
|
|
||||||
if upgrade {
|
|
||||||
return ver
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateVersionInfo() {
|
|
||||||
client := github.NewClient(nil)
|
client := github.NewClient(nil)
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
versionInfo.m.Lock()
|
|
||||||
versionInfo.Remote.Tag = *rels[0].TagName
|
vi.m.Lock()
|
||||||
versionInfo.Remote.Valid = true
|
vi.Remote.Tag = *rels[0].TagName
|
||||||
versionInfo.m.Unlock()
|
vi.Remote.Valid = true
|
||||||
|
vi.UpgradeReleaseNotes = ""
|
||||||
|
for _, r := range rels {
|
||||||
|
if semver.Compare(VersionInfo.Local.Tag, *r.TagName) < 0 {
|
||||||
|
vi.UpgradeReleaseNotes += fmt.Sprintf("*Version %s*\n\n", *r.TagName)
|
||||||
|
bodyLines := strings.Split(*r.Body, "\n")
|
||||||
|
for _, l := range bodyLines {
|
||||||
|
if strings.Index(l, "#") == 0 && strings.Contains(l, "Changelog") {
|
||||||
|
// do nothing, ignore the changelog heading
|
||||||
|
} else {
|
||||||
|
vi.UpgradeReleaseNotes += l + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.m.Unlock()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,13 +34,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="top-bar-right">
|
<div class="top-bar-right">
|
||||||
<ul class="menu">
|
<ul class="menu">
|
||||||
{{ if newVersion }}
|
|
||||||
<li>
|
<li>
|
||||||
<div><a href="https://github.com/tardisx/linkwallet/releases/tag/{{ newVersion }}">{{ newVersion }} available</a></div>
|
<a href="/info">{{ version.Local.Tag }}
|
||||||
</li>
|
{{ if version.UpgradeAvailable }}
|
||||||
{{ end }}
|
❗
|
||||||
<li class="menu-text">
|
{{ end }}
|
||||||
{{ version }}
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a href="https://github.com/tardisx/linkwallet">
|
<a href="https://github.com/tardisx/linkwallet">
|
||||||
@@ -64,6 +63,8 @@
|
|||||||
{{ template "config.html" . }}
|
{{ template "config.html" . }}
|
||||||
{{ else if eq .page "edit" }}
|
{{ else if eq .page "edit" }}
|
||||||
{{ template "edit.html" . }}
|
{{ template "edit.html" . }}
|
||||||
|
{{ else if eq .page "info" }}
|
||||||
|
{{ template "info.html" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{/* template "foundation_sample.html" . */}}
|
{{/* template "foundation_sample.html" . */}}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -30,7 +30,10 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<p>
|
<p>
|
||||||
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">delete</button>
|
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">Delete</button>
|
||||||
<button type="button" class="button" hx-post="/edit/{{.bookmark.ID}}">save</button>
|
<button type="button" class="button" hx-indicator="#saving" hx-post="/edit/{{.bookmark.ID}}"> {{ if .saved }} Saved {{ else }} Save {{ end }}</button>
|
||||||
|
<span id="saving" class="htmx-indicator">
|
||||||
|
<img style="height:1em;" src="/assets/image/beating.gif" /> Saving...
|
||||||
|
</span>
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
30
web/templates/info.html
Normal file
30
web/templates/info.html
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<div class="grid-x grid-padding-x">
|
||||||
|
<div class="large-12 cell">
|
||||||
|
|
||||||
|
<h5>System information</h5>
|
||||||
|
<table>
|
||||||
|
<tr><th>Memory in use</th><td>{{ meminfo }}</td></tr>
|
||||||
|
<tr><th>Bookmarks</th><td>{{ .stats.MostRecentBookmarkInfo.Bookmarks }}</td></tr>
|
||||||
|
<tr><th>Words Indexed</th><td>{{ .stats.MostRecentBookmarkInfo.IndexedWords }}</td></tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h5>Release info</h5>
|
||||||
|
{{ if not version.Remote.Valid }}
|
||||||
|
<p>GitHub version information not yet fetched.</p>
|
||||||
|
{{ else }}
|
||||||
|
{{ if version.UpgradeAvailable }}
|
||||||
|
<p>
|
||||||
|
A new version is available:
|
||||||
|
<a href="https://github.com/tardisx/linkwallet/releases/tag/{{ version.Remote.Tag }}">
|
||||||
|
{{ version.Remote.Tag }}
|
||||||
|
</a>
|
||||||
|
(you have {{ version.Local.Tag }}).
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{{ markdown version.UpgradeReleaseNotes }}
|
||||||
|
|
||||||
|
{{ else }}
|
||||||
|
<p>You are currently running the most recent version.</p>
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
@@ -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> </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>
|
||||||
31
web/templates/manage_results.html
Normal file
31
web/templates/manage_results.html
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
<table id="manage-results">
|
||||||
|
<tr>
|
||||||
|
<th> </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>
|
||||||
3
web/templates/manage_results_column_header.html
Normal file
3
web/templates/manage_results_column_header.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
<th class="{{ .Class }}" hx-post="/manage/results?sort={{ .URLString }}" hx-target="#manage-results">{{ .Name }} {{ .TitleArrow }}
|
||||||
|
</th>
|
||||||
@@ -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>
|
||||||
117
web/web.go
117
web/web.go
@@ -14,8 +14,10 @@ import (
|
|||||||
|
|
||||||
"github.com/tardisx/linkwallet/db"
|
"github.com/tardisx/linkwallet/db"
|
||||||
"github.com/tardisx/linkwallet/entity"
|
"github.com/tardisx/linkwallet/entity"
|
||||||
|
"github.com/tardisx/linkwallet/meta"
|
||||||
"github.com/tardisx/linkwallet/version"
|
"github.com/tardisx/linkwallet/version"
|
||||||
|
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/hako/durafmt"
|
"github.com/hako/durafmt"
|
||||||
|
|
||||||
"github.com/gin-contrib/gzip"
|
"github.com/gin-contrib/gzip"
|
||||||
@@ -42,6 +44,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 {
|
||||||
|
|
||||||
@@ -52,7 +77,15 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// templ := template.Must(template.New("").Funcs(template.FuncMap{"dict": dictHelper}).ParseFS(templateFiles, "templates/*.html"))
|
// templ := template.Must(template.New("").Funcs(template.FuncMap{"dict": dictHelper}).ParseFS(templateFiles, "templates/*.html"))
|
||||||
templ := template.Must(template.New("").Funcs(template.FuncMap{"nicetime": niceTime, "niceURL": niceURL, "join": strings.Join, "version": version.Is, "newVersion": version.UpgradeAvailableString}).ParseFS(templateFiles, "templates/*.html"))
|
templ := template.Must(template.New("").Funcs(
|
||||||
|
template.FuncMap{
|
||||||
|
"nicetime": niceTime,
|
||||||
|
"niceURL": niceURL,
|
||||||
|
"join": strings.Join,
|
||||||
|
"version": func() *version.Info { return &version.VersionInfo },
|
||||||
|
"meminfo": meta.MemInfo,
|
||||||
|
"markdown": func(s string) template.HTML { return template.HTML(string(markdown.ToHTML([]byte(s), nil, nil))) },
|
||||||
|
}).ParseFS(templateFiles, "templates/*.html"))
|
||||||
|
|
||||||
config, err := cmm.LoadConfig()
|
config, err := cmm.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -81,6 +114,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 +122,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 +187,14 @@ 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)
|
// no query, return an empty response
|
||||||
|
if len(query) == 0 {
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
c.Writer.Write([]byte{})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sr, err := bmm.Search(db.SearchOptions{Query: query})
|
||||||
data := gin.H{
|
data := gin.H{
|
||||||
"results": sr,
|
"results": sr,
|
||||||
"error": err,
|
"error": err,
|
||||||
@@ -135,6 +222,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) {
|
||||||
@@ -284,7 +378,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
|||||||
bmm.SaveBookmark(&bookmark)
|
bmm.SaveBookmark(&bookmark)
|
||||||
bmm.UpdateIndexForBookmark(&bookmark) // because title may have changed
|
bmm.UpdateIndexForBookmark(&bookmark) // because title may have changed
|
||||||
|
|
||||||
meta := gin.H{"page": "edit", "bookmark": bookmark, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
|
meta := gin.H{"page": "edit", "bookmark": bookmark, "saved": true, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
|
||||||
|
|
||||||
c.HTML(http.StatusOK,
|
c.HTML(http.StatusOK,
|
||||||
"edit_form.html", meta,
|
"edit_form.html", meta,
|
||||||
@@ -309,6 +403,17 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.GET("/info", func(c *gin.Context) {
|
||||||
|
dbStats, err := bmm.Stats()
|
||||||
|
if err != nil {
|
||||||
|
panic("could not load stats for info page")
|
||||||
|
}
|
||||||
|
meta := gin.H{"page": "info", "stats": dbStats, "config": config}
|
||||||
|
c.HTML(http.StatusOK,
|
||||||
|
"_layout.html", meta,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,7 +437,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 +464,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}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user