+
+
+
+
+ {{if .note.Viewers}}
+
This note is owned by {{.note.Owner}} and is further visible to
+
+ {{else}}
+
This note is owned by {{.note.Owner}}.
+ {{end}}
-
+
+
-
{{end}}
{{end}}
diff --git a/internal/notes/notes.go b/internal/notes/notes.go
index 9ab0338..c542aad 100644
--- a/internal/notes/notes.go
+++ b/internal/notes/notes.go
@@ -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
}
diff --git a/internal/templ/templ.go b/internal/templ/templ.go
index b1759ba..e37ba6a 100644
--- a/internal/templ/templ.go
+++ b/internal/templ/templ.go
@@ -7,6 +7,7 @@ import (
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
+ "forgejo.gwairfelin.com/max/gonotes/internal/notes"
)
type Ctx map[string]any
@@ -31,7 +32,9 @@ func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, context
return err
}
- context["user"] = r.Context().Value(middleware.ContextKey("user")).(string)
+ user := r.Context().Value(middleware.ContextKey("user")).(string)
+ context["user"] = user
+ context["userTags"] = notes.Notes.UserTags(user)
err = t.ExecuteTemplate(w, "base", context)
return err