Compare commits

..

No commits in common. "main" and "0.11.0" have entirely different histories.
main ... 0.11.0

2 changed files with 22 additions and 35 deletions

View file

@ -5,16 +5,13 @@ package notes
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmp"
"crypto/rand" "crypto/rand"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
"maps"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"time" "time"
@ -101,26 +98,14 @@ func Init() error {
return nil return nil
} }
func (ns *noteSet) Sort() []*Note {
orderByTitle := func(a, b *Note) int {
return cmp.Compare(a.Title, b.Title)
}
return slices.SortedFunc(maps.Keys(*ns), orderByTitle)
}
func (ns *NoteStore) Get(owner string) noteSet { func (ns *NoteStore) Get(owner string) noteSet {
return ns.notes[owner] return ns.notes[owner]
} }
func (ns *NoteStore) GetOrdered(owner string) []*Note {
notes := ns.Get(owner)
return notes.Sort()
}
func (ns *NoteStore) GetOne(owner string, uid string) (*Note, bool) { func (ns *NoteStore) GetOne(owner string, uid string) (*Note, bool) {
notes := ns.Get(owner) notes := ns.Get(owner)
for note := range notes { for note, _ := range notes {
if note.Uid == uid { if note.Uid == uid {
log.Printf("Found single note during GetOne %s", note.Title) log.Printf("Found single note during GetOne %s", note.Title)
return note, true return note, true
@ -136,9 +121,8 @@ func (ns *NoteStore) Add(note *Note, user string) {
ns.notes[user][note] = true ns.notes[user][note] = true
} }
func (ns *NoteStore) Del(note *Note, user string) error { func (ns *NoteStore) Del(note *Note, user string) {
delete(ns.notes[user], note) delete(ns.notes[user], note)
return note.Delete()
} }
func (ns *NoteStore) UserTags(user string) []string { func (ns *NoteStore) UserTags(user string) []string {
@ -161,7 +145,13 @@ func (ns *NoteStore) UserTags(user string) []string {
log.Printf("Tagset is %v", tagSet) log.Printf("Tagset is %v", tagSet)
return slices.Sorted(maps.Keys(tagSet)) tags := make([]string, len(tagSet))
i := 0
for tag := range tagSet {
tags[i] = tag
i++
}
return tags
} }
func fmtPath(path string) string { func fmtPath(path string) string {
@ -341,8 +331,8 @@ func (n *Note) DelViewer(viewer string) {
delete(n.Viewers, viewer) delete(n.Viewers, viewer)
} }
func (n *Note) Delete() error { func DeleteNote(uid string) error {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(n.Uid)) filename := filepath.Join(conf.Conf.NotesDir, fmtPath(uid))
return os.Remove(filename) return os.Remove(filename)
} }
@ -418,5 +408,10 @@ func (n *Note) ToggleBox(nthBox int) (bool, bool) {
} }
func (n *Note) HasTag(tag string) bool { func (n *Note) HasTag(tag string) bool {
return slices.Contains(n.Tags, tag) for _, tag_ := range n.Tags {
if tag_ == tag {
return true
}
}
return false
} }

View file

@ -107,18 +107,10 @@ func new(w http.ResponseWriter, r *http.Request) {
} }
func delete(w http.ResponseWriter, r *http.Request) { func delete(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(middleware.ContextKey("user")).(string) // user := r.Context().Value(middleware.ContextKey("user")).(string)
uid := r.PathValue("note")
note, ok := notes.Notes.GetOne(user, uid)
if !ok {
http.NotFound(w, r)
}
err := notes.Notes.Del(note, user)
encodedTitle := r.PathValue("note")
err := notes.DeleteNote(encodedTitle)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
http.Error(w, "Couldn't delete note", http.StatusInternalServerError) http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
@ -223,11 +215,11 @@ func list(w http.ResponseWriter, r *http.Request) {
titlesAndUrls := make([]titleAndURL, 0) titlesAndUrls := make([]titleAndURL, 0)
ns := notes.Notes.GetOrdered(user) ns := notes.Notes.Get(user)
log.Printf("Notes: %+v", notes.Notes) log.Printf("Notes: %+v", notes.Notes)
log.Printf("Notes for %s: %+v", user, ns) log.Printf("Notes for %s: %+v", user, ns)
for _, note := range ns { for note := range ns {
if tag == "" || note.HasTag(tag) { if tag == "" || note.HasTag(tag) {
titlesAndUrls = append( titlesAndUrls = append(
titlesAndUrls, titlesAndUrls,