2024-03-09 10:25:20 +01:00
|
|
|
package view
|
2024-02-27 09:03:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-09 10:12:46 +01:00
|
|
|
"time"
|
2024-02-27 09:03:21 +01:00
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
"git.streifling.com/jason/rss"
|
2024-03-09 10:25:20 +01:00
|
|
|
"streifling.com/jason/cpolis/cmd/model"
|
2024-02-27 09:03:21 +01:00
|
|
|
)
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc {
|
2024-02-27 09:03:21 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-09 10:12:46 +01:00
|
|
|
channel := &rss.Channel{
|
|
|
|
Title: title,
|
|
|
|
Link: link,
|
|
|
|
Description: desc,
|
|
|
|
Items: make([]*rss.Item, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
articles, err := db.GetCertainArticles(true)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, article := range articles {
|
|
|
|
tags, err := db.GetArticleTags(article.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tagNames := make([]string, 0)
|
|
|
|
for _, tag := range tags {
|
|
|
|
tagNames = append(tagNames, tag.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := db.GetUser(article.AuthorID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.Items = append(channel.Items, &rss.Item{
|
|
|
|
Title: article.Title,
|
|
|
|
Author: user.FirstName + user.LastName,
|
|
|
|
PubDate: article.Created.Format(time.RFC1123Z),
|
|
|
|
Description: article.Desc,
|
|
|
|
Content: &rss.Content{Value: article.Content},
|
|
|
|
Categories: tagNames,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
feed := rss.NewFeed()
|
2024-03-09 10:12:46 +01:00
|
|
|
feed.Channels = append(feed.Channels, channel)
|
2024-03-05 17:13:59 +01:00
|
|
|
rss, err := feed.ToXML()
|
2024-02-27 09:03:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-02 09:09:55 +01:00
|
|
|
|
|
|
|
files := []string{"web/templates/index.html", "web/templates/feed.rss"}
|
|
|
|
tmpl, err := template.ParseFiles(files...)
|
|
|
|
template.Must(tmpl, err).Execute(w, rss)
|
2024-02-27 09:03:21 +01:00
|
|
|
}
|
|
|
|
}
|