Refactor generate feed function
alex wennerberg alex@alexwennerberg.com
Mon, 28 Dec 2020 09:05:51 -0800
2 files changed,
11 insertions(+),
11 deletions(-)
M
gemfeed.go
→
gemfeed.go
@@ -41,7 +41,7 @@ }
// Non-standard extension // Requires yyyy-mm-dd formatted files -func generateFeedFromUser(user string) []FeedEntry { +func generateFeedFromUser(user string) *Gemfeed { gemlogFolder := "gemlog" // TODO make configurable gemlogFolderPath := path.Join(c.FilesDirectory, user, gemlogFolder) // NOTE: assumes sanitized input@@ -51,7 +51,6 @@ Title: user + "'s Gemlog",
Creator: user, Url: &u, } - var feedEntries []FeedEntry err := filepath.Walk(gemlogFolderPath, func(thepath string, info os.FileInfo, err error) error { base := path.Base(thepath) if len(base) >= 10 {@@ -91,17 +90,18 @@ }
entry.File = getLocalPath(thepath) u := urlFromPath(thepath) entry.Url = &u - feedEntries = append(feedEntries, entry) + feed.Entries = append(feed.Entries, entry) } return nil }) if err != nil { return nil } - sort.Slice(feedEntries, func(i, j int) bool { - return feedEntries[i].Date.After(feedEntries[j].Date) + // Reverse chronological sort + sort.Slice(feed.Entries, func(i, j int) bool { + return feed.Entries[i].Date.After(feed.Entries[j].Date) }) - return feedEntries + return &feed } // TODO definitely cache this function@@ -117,9 +117,9 @@ return nil, nil, err
} else { for _, user := range users { fe := generateFeedFromUser(user) - if len(fe) > 0 { - feeds = append(feeds, *fe[0].Feed) - for _, e := range fe { + if len(fe.Entries) > 0 { + feeds = append(feeds, *fe.Entries[0].Feed) + for _, e := range fe.Entries { fmt.Println(e) feedEntries = append(feedEntries, e) }
M
gemini.go
→
gemini.go
@@ -19,12 +19,12 @@
var gt *template.Template func generateGemfeedPage(user string) string { - feedItems := generateFeedFromUser(user) + feed := generateFeedFromUser(user) data := struct { Host string Title string FeedEntries []FeedEntry - }{c.Host, user + "'s Gemlog", feedItems} + }{c.Host, user + "'s Gemlog", feed.Entries} var buff bytes.Buffer gt.ExecuteTemplate(&buff, "gemfeed.gmi", data) return buff.String()