2020-06-23 20:07:25 -04:00
|
|
|
package bookmarks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base32"
|
2020-06-24 13:18:23 -04:00
|
|
|
"sort"
|
2020-06-23 20:07:25 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var bkmkStore = config.BkmkStore
|
|
|
|
|
|
|
|
// bkmkKey returns the viper key for the given bookmark URL.
|
|
|
|
// Note that URLs are the keys, NOT the bookmark name.
|
|
|
|
func bkmkKey(url string) string {
|
2020-06-24 13:18:23 -04:00
|
|
|
// Keys are base32 encoded URLs to prevent any special chars like periods from being used
|
2020-06-23 20:07:25 -04:00
|
|
|
return "bookmarks." + base32.StdEncoding.EncodeToString([]byte(url))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Set(url, name string) {
|
|
|
|
bkmkStore.Set(bkmkKey(url), name)
|
|
|
|
bkmkStore.WriteConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the NAME of the bookmark, given the URL.
|
|
|
|
// It also returns a bool indicating whether it exists.
|
|
|
|
func Get(url string) (string, bool) {
|
|
|
|
name := bkmkStore.GetString(bkmkKey(url))
|
|
|
|
return name, name != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func Remove(url string) {
|
|
|
|
// XXX: Viper can't actually delete keys, which means the bookmarks file might get clouded
|
|
|
|
// with non-entries over time.
|
|
|
|
bkmkStore.Set(bkmkKey(url), "")
|
|
|
|
bkmkStore.WriteConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// All returns all the bookmarks in a map of URLs to names.
|
2020-06-24 13:18:23 -04:00
|
|
|
// It also returns a slice of map keys, sorted so that the map *values*
|
|
|
|
// are in alphabetical order, with case ignored.
|
|
|
|
func All() (map[string]string, []string) {
|
|
|
|
bkmks := make(map[string]string)
|
2020-06-23 20:07:25 -04:00
|
|
|
|
|
|
|
bkmksMap, ok := bkmkStore.AllSettings()["bookmarks"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
// No bookmarks stored yet, return empty map
|
2020-06-24 13:18:23 -04:00
|
|
|
return bkmks, []string{}
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|
2020-06-24 13:18:23 -04:00
|
|
|
|
|
|
|
inverted := make(map[string]string) // Holds inverted map, name->URL
|
|
|
|
var names []string // Holds bookmark names, for sorting
|
|
|
|
var keys []string // Final sorted keys (URLs), for returning at the end
|
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
for b32Url, name := range bkmksMap {
|
|
|
|
if n, ok := name.(string); n == "" || !ok {
|
|
|
|
// name is not a string, or it's empty - ignore
|
2020-06-24 13:18:23 -04:00
|
|
|
// Likely means it is a removed bookmark
|
2020-06-23 20:07:25 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
url, err := base32.StdEncoding.DecodeString(strings.ToUpper(b32Url))
|
|
|
|
if err != nil {
|
|
|
|
// This would only happen if a user messed around with the bookmarks file
|
|
|
|
continue
|
|
|
|
}
|
2020-06-24 13:18:23 -04:00
|
|
|
bkmks[string(url)] = name.(string)
|
|
|
|
inverted[name.(string)] = string(url)
|
|
|
|
names = append(names, name.(string))
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|
2020-06-24 13:18:23 -04:00
|
|
|
// Sort, then turn back into URL keys
|
|
|
|
sort.Strings(names)
|
|
|
|
for _, name := range names {
|
|
|
|
keys = append(keys, inverted[name])
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkmks, keys
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|