Load LastModified time on note

This commit is contained in:
Maximilian Friedersdorff 2025-09-30 11:14:29 +01:00
parent 33cc62b3fc
commit 3de1b8b714

View file

@ -13,6 +13,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"forgejo.gwairfelin.com/max/gonotes/internal/conf" "forgejo.gwairfelin.com/max/gonotes/internal/conf"
markdown "github.com/teekennedy/goldmark-markdown" markdown "github.com/teekennedy/goldmark-markdown"
@ -36,6 +37,7 @@ type Note struct {
Owner string Owner string
Viewers map[string]struct{} Viewers map[string]struct{}
Uid string Uid string
LastModified time.Time
} }
type NoteStore struct { type NoteStore struct {
@ -63,7 +65,7 @@ func Init() error {
files, err := os.ReadDir(notesDir) files, err := os.ReadDir(notesDir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
os.MkdirAll(notesDir, os.FileMode(0750)) os.MkdirAll(notesDir, os.FileMode(0o750))
} else { } else {
log.Print(err.Error()) log.Print(err.Error())
return err return err
@ -165,7 +167,7 @@ func (n *Note) Save() error {
return os.WriteFile( return os.WriteFile(
filename, filename,
[]byte(fmt.Sprintf("---\n%s---\n%s", frontmatter, n.Body)), []byte(fmt.Sprintf("---\n%s---\n%s", frontmatter, n.Body)),
0600, 0o600,
) )
} }
@ -190,6 +192,11 @@ func loadNote(uid string) (*Note, error) {
} }
defer f.Close() defer f.Close()
stat, err := os.Stat(filename)
if err != nil {
return nil, err
}
bodyScanner := bufio.NewScanner(f) bodyScanner := bufio.NewScanner(f)
body := make([]byte, 0, 10) body := make([]byte, 0, 10)
fullBody := make([]byte, 0, 10) fullBody := make([]byte, 0, 10)
@ -236,6 +243,7 @@ func loadNote(uid string) (*Note, error) {
note := NewNoteNoSave(title, owner) note := NewNoteNoSave(title, owner)
note.Uid = uid note.Uid = uid
note.Body = body note.Body = body
note.LastModified = stat.ModTime()
viewers := metaData["viewers"].([]interface{}) viewers := metaData["viewers"].([]interface{})