From 79d7b02371ba07f4274dc9b69656ff4597573c5e Mon Sep 17 00:00:00 2001 From: Jack Williamson <53199061+JackWilli@users.noreply.github.com> Date: Sun, 8 Dec 2024 13:31:36 -0500 Subject: [PATCH 1/6] feat: add pinned notes using bbolt --- go.mod | 1 + go.sum | 7 +- internal/api/endpoints.go | 5 ++ internal/api/note_handlers.go | 122 +++++++++++++++++++++++++++++++++- internal/db/db.go | 31 +++++++++ main.go | 6 ++ 6 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 internal/db/db.go diff --git a/go.mod b/go.mod index 8676020..2526bb5 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( require ( github.com/BurntSushi/toml v1.4.0 github.com/oolong-sh/sync v0.0.0-20241128232107-4aff73e779a0 + go.etcd.io/bbolt v1.3.11 ) require ( diff --git a/go.sum b/go.sum index 052a8dd..2cba327 100644 --- a/go.sum +++ b/go.sum @@ -21,11 +21,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= +go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0= +go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= @@ -41,6 +44,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/api/endpoints.go b/internal/api/endpoints.go index 02b2de8..949f3b3 100644 --- a/internal/api/endpoints.go +++ b/internal/api/endpoints.go @@ -26,6 +26,11 @@ func SpawnServer() { mux.HandleFunc("PUT /note", handleUpdateNote) mux.HandleFunc("DELETE /note", handleDeleteNote) + // pinning endpoints + mux.HandleFunc("GET /pins", handleGetPinnedNotes) + mux.HandleFunc("POST /pins", handleAddPinnedNote) + mux.HandleFunc("DELETE /pins", handleDeletePinnedNote) + // start server log.Println("Starting server on :11975...") if err := http.ListenAndServe(":11975", mux); err != nil { diff --git a/internal/api/note_handlers.go b/internal/api/note_handlers.go index ed275c0..3c3777c 100644 --- a/internal/api/note_handlers.go +++ b/internal/api/note_handlers.go @@ -8,7 +8,9 @@ import ( "os" "path/filepath" + "github.com/oolong-sh/oolong/internal/db" "github.com/oolong-sh/oolong/internal/state" + "go.etcd.io/bbolt" ) type createUpdateRequest struct { @@ -142,7 +144,7 @@ func handleUpdateNote(w http.ResponseWriter, r *http.Request) { } } -// 'Delete /note?path=/path/to/note' endpoint handler deletess a note file based on query input +// 'Delete /note?path=/path/to/note' endpoint handler deletes a note file based on query input func handleDeleteNote(w http.ResponseWriter, r *http.Request) { log.Println("Request received:", r.Method, r.URL, r.Host) @@ -169,3 +171,121 @@ func handleDeleteNote(w http.ResponseWriter, r *http.Request) { // NOTE: this function may need to call the update function due to files no longer existing // - check this case in state, this may require substantial logic missing there } + +func addPinnedNote(path string) error { + return db.Database.Update(func(tx *bbolt.Tx) error { + bucket := tx.Bucket([]byte(db.PinnedBucket)) + if bucket == nil { + return fmt.Errorf("bucket %s not found", db.PinnedBucket) + } + + if bucket.Get([]byte(path)) != nil { + return fmt.Errorf("note already pinned") + } + + return bucket.Put([]byte(path), []byte{}) + }) +} + +func getPinnedNotes() ([]string, error) { + notes := []string{} + + err := db.Database.View(func(tx *bbolt.Tx) error { + bucket := tx.Bucket([]byte(db.PinnedBucket)) + if bucket == nil { + return fmt.Errorf("bucket %s not found", db.PinnedBucket) + } + + return bucket.ForEach(func(k, _ []byte) error { + notes = append(notes, string(k)) + return nil + }) + }) + + return notes, err +} + +func deletePinnedNote(path string) error { + return db.Database.Update(func(tx *bbolt.Tx) error { + bucket := tx.Bucket([]byte(db.PinnedBucket)) + if bucket == nil { + return fmt.Errorf("bucket %s not found", db.PinnedBucket) + } + + return bucket.Delete([]byte(path)) + }) +} + +func handleGetPinnedNotes(w http.ResponseWriter, r *http.Request) { + log.Println("Request received:", r.Method, r.URL, r.Host) + + // CORS handling + if err := checkOrigin(w, r); err != nil { + log.Println(err) + http.Error(w, fmt.Sprintln(err), 500) + return + } + + pinnedNotes, err := getPinnedNotes() + if err != nil { + log.Println(err) + http.Error(w, "Failed to fetch pinned notes", 500) + return + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(pinnedNotes); err != nil { + log.Println(err) + http.Error(w, "Failed to encode response", 500) + } +} + +func handleAddPinnedNote(w http.ResponseWriter, r *http.Request) { + log.Println("Request received:", r.Method, r.URL, r.Host) + + // CORS handling + if err := checkOrigin(w, r); err != nil { + log.Println(err) + http.Error(w, fmt.Sprintln(err), 500) + return + } + + path := r.URL.Query().Get("path") + if path == "" { + http.Error(w, "Path parameter not specified", http.StatusBadRequest) + return + } + + if err := addPinnedNote(path); err != nil { + log.Println(err) + http.Error(w, err.Error(), 400) + return + } + + w.WriteHeader(http.StatusCreated) +} + +func handleDeletePinnedNote(w http.ResponseWriter, r *http.Request) { + log.Println("Request received:", r.Method, r.URL, r.Host) + + // CORS handling + if err := checkOrigin(w, r); err != nil { + log.Println(err) + http.Error(w, fmt.Sprintln(err), 500) + return + } + + path := r.URL.Query().Get("path") + if path == "" { + http.Error(w, "Path parameter not specified", http.StatusBadRequest) + return + } + + if err := deletePinnedNote(path); err != nil { + log.Println(err) + http.Error(w, "Failed to delete pinned note", 400) + return + } + + w.WriteHeader(http.StatusNoContent) +} diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..cfffc86 --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,31 @@ +package db + +import ( + "go.etcd.io/bbolt" +) + +var Database *bbolt.DB + +const PinnedBucket = "PinnedNotes" +const db_path = "pinned_notes.db" + +// Initialize the database and ensure the bucket exists +func InitializeDB() error { + var err error + Database, err = bbolt.Open(db_path, 0666, nil) + if err != nil { + return err + } + + return Database.Update(func(tx *bbolt.Tx) error { + _, err := tx.CreateBucketIfNotExists([]byte(PinnedBucket)) + return err + }) +} + +// Close the database when the server shuts down +func CloseDB() { + if Database != nil { + Database.Close() + } +} diff --git a/main.go b/main.go index f04dbc2..7267265 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "github.com/oolong-sh/oolong/internal/config" "github.com/oolong-sh/oolong/internal/daemon" + "github.com/oolong-sh/oolong/internal/db" "github.com/oolong-sh/oolong/internal/documents" "github.com/oolong-sh/oolong/internal/linking/ngrams" "github.com/oolong-sh/oolong/internal/state" @@ -29,6 +30,11 @@ func main() { panic(err) } + if err := db.InitializeDB(); err != nil { + panic(err) + } + defer db.CloseDB() + // go plugins.InitPlugins() // run daemon if --no-daemon flag is not passed From 77334ed9df50e8bce53cd47638619ff3b2a43e69 Mon Sep 17 00:00:00 2001 From: Jack Williamson <53199061+JackWilli@users.noreply.github.com> Date: Sun, 8 Dec 2024 18:09:05 -0500 Subject: [PATCH 2/6] feat: update db path to be ~/.config/oolong/oolong.db should create the dir if it doesn't exist --- internal/db/db.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index cfffc86..17efd97 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -1,17 +1,30 @@ package db import ( + "os" + "path/filepath" + "go.etcd.io/bbolt" ) var Database *bbolt.DB const PinnedBucket = "PinnedNotes" -const db_path = "pinned_notes.db" // Initialize the database and ensure the bucket exists func InitializeDB() error { - var err error + homeDir, err := os.UserHomeDir() + if err != nil { + return err + } + + db_path := filepath.Join(homeDir, ".config", "oolong", "oolong.db") + + err = os.MkdirAll(filepath.Dir(db_path), 0755) + if err != nil { + return err + } + Database, err = bbolt.Open(db_path, 0666, nil) if err != nil { return err From c7eb838d9dfae39a9cb9846112cc67a89ff23166 Mon Sep 17 00:00:00 2001 From: Jack Williamson <53199061+JackWilli@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:06:15 -0500 Subject: [PATCH 3/6] feat: pinning config toml, change db dir, add goroutine --- internal/api/endpoints.go | 12 ++++++++---- internal/api/note_handlers.go | 4 ++-- internal/config/config.go | 13 +++++++++++++ internal/db/db.go | 8 +++++++- main.go | 11 ++++++++--- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/internal/api/endpoints.go b/internal/api/endpoints.go index 454e5c6..4d4a52b 100644 --- a/internal/api/endpoints.go +++ b/internal/api/endpoints.go @@ -3,6 +3,8 @@ package api import ( "log" "net/http" + + "github.com/oolong-sh/oolong/internal/config" ) // spawn the oolong api server @@ -29,10 +31,12 @@ func SpawnServer() { mux.HandleFunc("DELETE /note", handleDeleteNote) mux.HandleFunc("GET /open/note", handleOpenNote) - // pinning endpoints - mux.HandleFunc("GET /pins", handleGetPinnedNotes) - mux.HandleFunc("POST /pins", handleAddPinnedNote) - mux.HandleFunc("DELETE /pins", handleDeletePinnedNote) + if config.PinningEnabled() { + // pinning endpoints + mux.HandleFunc("GET /pins", handleGetPinnedNotes) + mux.HandleFunc("POST /pins", handleAddPinnedNote) + mux.HandleFunc("DELETE /pins", handleDeletePinnedNote) + } // start server log.Println("Starting server on :11975...") diff --git a/internal/api/note_handlers.go b/internal/api/note_handlers.go index 627091b..4af5449 100644 --- a/internal/api/note_handlers.go +++ b/internal/api/note_handlers.go @@ -10,8 +10,8 @@ import ( "os/exec" "path/filepath" - "github.com/oolong-sh/oolong/internal/db" "github.com/oolong-sh/oolong/internal/config" + "github.com/oolong-sh/oolong/internal/db" "github.com/oolong-sh/oolong/internal/state" "go.etcd.io/bbolt" ) @@ -324,4 +324,4 @@ func handleOpenNote(w http.ResponseWriter, r *http.Request) { log.Println(err) http.Error(w, "Error opening file in editor.", 500) } -} \ No newline at end of file +} diff --git a/internal/config/config.go b/internal/config/config.go index a57d899..786ca07 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -31,6 +31,8 @@ type OolongConfig struct { PluginsConfig OolongPluginConfig `toml:"plugins"` GraphConfig OolongGraphConfig `toml:"graph"` SyncConfig OolongSyncConfig `toml:"sync"` + + PinningEnabled *bool `toml:"pinning_enabled"` } type OolongPluginConfig struct { @@ -60,6 +62,12 @@ func StopWords() []string { return cfg.StopWords } func WeightThresholds() OolongGraphConfig { return cfg.GraphConfig } func GraphMode() string { return cfg.GraphConfig.DefaultMode } func SyncConfig() OolongSyncConfig { return cfg.SyncConfig } +func PinningEnabled() bool { + if cfg.PinningEnabled != nil { + return *cfg.PinningEnabled + } + return true +} func Setup() error { // CHANGE: for hot-reloading, only one config path location should be supported (~/.config/oolong/oolong.toml) @@ -97,6 +105,11 @@ func readConfig(configPath string) { } // TODO: set default values for thresholds if not set + + if cfg.PinningEnabled == nil { + defaultValue := true + cfg.PinningEnabled = &defaultValue + } } func initWatcher(configPath string) error { diff --git a/internal/db/db.go b/internal/db/db.go index 17efd97..6fcf405 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -3,6 +3,7 @@ package db import ( "os" "path/filepath" + "runtime" "go.etcd.io/bbolt" ) @@ -18,7 +19,12 @@ func InitializeDB() error { return err } - db_path := filepath.Join(homeDir, ".config", "oolong", "oolong.db") + var db_path string + if runtime.GOOS == "windows" { + db_path = filepath.Join(homeDir, "AppData", "Local", "oolong", "oolong.db") + } else { + db_path = filepath.Join(homeDir, ".local", "share", "oolong", "oolong.db") + } err = os.MkdirAll(filepath.Dir(db_path), 0755) if err != nil { diff --git a/main.go b/main.go index 7267265..50da78b 100644 --- a/main.go +++ b/main.go @@ -30,9 +30,13 @@ func main() { panic(err) } - if err := db.InitializeDB(); err != nil { - panic(err) - } + go func() { + if config.PinningEnabled() { + if err := db.InitializeDB(); err != nil { + panic(err) + } + } + }() defer db.CloseDB() // go plugins.InitPlugins() @@ -42,4 +46,5 @@ func main() { if !*daemonFlag { daemon.Run() } + } From 456f66ad6e2854e5f042cfd0100a8ff5c537fff1 Mon Sep 17 00:00:00 2001 From: Patrick Dewey <57921252+ptdewey@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:32:50 -0500 Subject: [PATCH 4/6] Update main.go Moved db close defer to avoid potential race condition --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index 50da78b..dee0528 100644 --- a/main.go +++ b/main.go @@ -35,9 +35,9 @@ func main() { if err := db.InitializeDB(); err != nil { panic(err) } + defer db.CloseDB() } }() - defer db.CloseDB() // go plugins.InitPlugins() @@ -46,5 +46,4 @@ func main() { if !*daemonFlag { daemon.Run() } - } From 8498b0f78eb2c7cbd5689f254a3946d4959f643b Mon Sep 17 00:00:00 2001 From: Jack Williamson <53199061+JackWilli@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:08:45 -0500 Subject: [PATCH 5/6] docs: add note pinning to README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 1b0b083..106a984 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,12 @@ For more situations where you want to run a more complex command, separate conse |--------|-------------|-------------| | plugin_paths | List of scripts to load | `[]` | +**Note pinning** (optional): + +| Option | Description | Default | +|--------|-------------|-------------| +| pinning_enabled | Boolean representing if note pinning is enabled in oolongd | `false` | + ### Example Configuration @@ -118,6 +124,9 @@ allowed_extensions = [ ".typ", ] +# Enables note pinning +pinning_enabled = true + # Command to run when open endpoint it called (a note node is clicked on the graph) # Note: All arguments MUST be separated into separate strings (see config for more details) open_command = [ "code" ] From 7c6fec6a2705ccfff1879f25e8fe2a86c35b9ff8 Mon Sep 17 00:00:00 2001 From: Patrick Dewey <57921252+ptdewey@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:16:17 -0500 Subject: [PATCH 6/6] Update README.md --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 106a984..67a10cc 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Oolong looks for a configuration file at `~/.config/oolong.toml` | `ignored_directories` | Subdirectories to exclude from reading and linking | `[".git"]` | | `allowed_extensions` | Whitelist of file extensions to use in linking | `[".md", ".txt", ".mdx", ".tex", ".typ"]` | | `open_command` | Command to run when clicking a graph node | `["code"]` (See below for more details) | - +| `pinning_enabled` | **Optional** boolean indicating if note pinning should be enabled | `false` | The `open_command` option is used by the graph to allow you to open a clicked note in an editor of your choice. @@ -93,11 +93,6 @@ For more situations where you want to run a more complex command, separate conse |--------|-------------|-------------| | plugin_paths | List of scripts to load | `[]` | -**Note pinning** (optional): - -| Option | Description | Default | -|--------|-------------|-------------| -| pinning_enabled | Boolean representing if note pinning is enabled in oolongd | `false` | ### Example Configuration