Implement beginnings of tagging

This commit is contained in:
Maximilian Friedersdorff 2025-11-04 17:05:49 +00:00
parent 4fb4bec5a8
commit 105275f3e0
5 changed files with 142 additions and 40 deletions

View file

@ -38,6 +38,7 @@ type Note struct {
Viewers map[string]struct{}
Uid string
LastModified time.Time
Tags []string
}
type noteSet map[*Note]bool
@ -124,6 +125,35 @@ func (ns *NoteStore) Del(note *Note, user string) {
delete(ns.notes[user], note)
}
func (ns *NoteStore) UserTags(user string) []string {
tagSet := make(map[string]bool)
notes, ok := ns.notes[user]
if !ok {
return make([]string, 0)
}
log.Printf("Got notes for user %v", notes)
for note := range notes {
log.Printf("considering note %s (%s)", note.Title, note.Tags)
for _, tag := range note.Tags {
tagSet[tag] = true
}
}
log.Printf("Tagset is %v", tagSet)
tags := make([]string, len(tagSet))
i := 0
for tag := range tagSet {
tags[i] = tag
i++
}
return tags
}
func fmtPath(path string) string {
return fmt.Sprintf("%s.%s", path, conf.Conf.Extension)
}
@ -134,10 +164,11 @@ func (n *Note) marshalFrontmatter() ([]byte, error) {
for viewer := range n.Viewers {
viewers = append(viewers, viewer)
}
frontmatter := make(map[string]interface{})
frontmatter := make(map[string]any)
frontmatter["owner"] = n.Owner
frontmatter["viewers"] = viewers
frontmatter["title"] = n.Title
frontmatter["tags"] = n.Tags
marshaled, err := yaml.Marshal(&frontmatter)
return marshaled, err
@ -154,6 +185,7 @@ func (n *Note) ViewersAsList() []string {
func NewNoteNoSave(title string, owner string) *Note {
note := &Note{Title: title, Owner: owner}
note.Viewers = make(map[string]struct{})
note.Tags = make([]string, 0, 5)
return note
}
@ -254,7 +286,7 @@ func loadNote(uid string) (*Note, error) {
note.Body = body
note.LastModified = stat.ModTime()
viewers := metaData["viewers"].([]interface{})
viewers := metaData["viewers"].([]any)
for _, viewer := range viewers {
v, ok := viewer.(string)
@ -270,6 +302,24 @@ func loadNote(uid string) (*Note, error) {
}
log.Printf("Note %s shared with %v", note.Title, note.Viewers)
tags, ok := metaData["tags"]
if ok {
tags := tags.([]any)
for _, tag := range tags {
t, ok := tag.(string)
if !ok {
return nil, errors.New("invalid note, non string type in 'tags' in frontmatter")
}
if t == "" {
continue
}
note.Tags = append(note.Tags, t)
}
}
return note, nil
}