From 469903dc9cdc97f446b52600221eade43c840e1a Mon Sep 17 00:00:00 2001 From: partisan Date: Mon, 26 Aug 2024 18:08:24 +0200 Subject: [PATCH] fixed incorrect notifications and removed deprecated lib --- main.go | 30 +++++++++++++++++++----------- save.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 18d022b..24b0fc1 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "html/template" - "io/ioutil" "log" "net/http" "os" @@ -101,6 +100,9 @@ func main() { log.Fatalf("Error getting blogs: %v", err) } + // Start the periodic notification checker + go startNotificationChecker(10 * time.Minute) + // Start watching for changes in the data directory go watchForChanges(dataDir) @@ -185,6 +187,18 @@ func main() { log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } +func startNotificationChecker(interval time.Duration) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + checkAndSendNotifications() + } + } +} + func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) { tmplPath := filepath.Join(templateDir, tmpl) t, err := template.ParseFiles(tmplPath) @@ -253,19 +267,12 @@ func renderBlogEntry(w http.ResponseWriter, r *http.Request, blogName string, en } renderTemplate(w, "news.html", pageData) - - // Handle notifications if they haven't been sent yet - if !entry.Notified { - sendNotifications(entry) - entry.Notified = true - saveNotifiedEntry(entry.Number) - } } func getBlogs(dir string) ([]Blog, error) { var blogs []Blog - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return nil, err } @@ -286,7 +293,7 @@ func getBlogs(dir string) ([]Blog, error) { func getBlogEntries(dir string) (Blog, error) { var entries []BlogEntry - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return Blog{}, err } @@ -315,7 +322,7 @@ func getBlogEntries(dir string) (Blog, error) { } func parseMarkdownFile(path string) (BlogEntry, error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return BlogEntry{}, err } @@ -497,6 +504,7 @@ func handleFileChange(path string, isNew bool) { dir := filepath.Dir(path) blogName := filepath.Base(dir) updateBlogEntries(blogName, path) + checkAndSendNotifications() } } diff --git a/save.go b/save.go index 02533cc..c26ab19 100644 --- a/save.go +++ b/save.go @@ -2,35 +2,49 @@ package main import ( "encoding/json" - "io/ioutil" "log" "os" + "time" ) func loadNotifiedEntries() { - data, err := ioutil.ReadFile(notifiedFilePath) + file, err := os.Open(notifiedFilePath) if err != nil { if os.IsNotExist(err) { - return // No file, no entries notified yet + return } - log.Fatalf("Error reading notified entries file: %v", err) + log.Fatalf("Error loading notified entries: %v", err) } + defer file.Close() - err = json.Unmarshal(data, ¬ifiedEntries) + err = json.NewDecoder(file).Decode(¬ifiedEntries) if err != nil { - log.Fatalf("Error parsing notified entries file: %v", err) + log.Fatalf("Error decoding notified entries: %v", err) } } func saveNotifiedEntry(entryNumber int) { notifiedEntries[entryNumber] = true - data, err := json.Marshal(notifiedEntries) + file, err := os.Create(notifiedFilePath) if err != nil { - log.Fatalf("Error serializing notified entries: %v", err) + log.Fatalf("Error saving notified entries: %v", err) } + defer file.Close() - err = ioutil.WriteFile(notifiedFilePath, data, 0644) + err = json.NewEncoder(file).Encode(notifiedEntries) if err != nil { - log.Fatalf("Error writing notified entries file: %v", err) + log.Fatalf("Error encoding notified entries: %v", err) + } +} + +func checkAndSendNotifications() { + for _, blog := range blogs { + for _, entry := range blog.Entries { + if !entry.Notified && !time.Now().Before(entry.Date) { + sendNotifications(entry) + entry.Notified = true + saveNotifiedEntry(entry.Number) + } + } } }