package view import ( "fmt" "html/template" "log" "net/http" "time" "git.streifling.com/jason/rss" "streifling.com/jason/cpolis/cmd/control" "streifling.com/jason/cpolis/cmd/model" ) func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { channel := &rss.Channel{ Title: title, Link: link, Description: desc, Items: make([]*rss.Item, 0), } articles, err := db.GetCertainArticles(true, false) 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) } tagNames = append(tagNames, fmt.Sprint("Orient Express ", article.IssueID)) user, err := db.GetUser(article.AuthorID) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } articleTitle, err := control.ConvertToPlain(article.Title) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } articleDescription, err := control.ConvertToPlain(article.Description) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } articleContent, err := control.ConvertToHTML(article.Content) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } channel.Items = append(channel.Items, &rss.Item{ Title: articleTitle, Author: user.FirstName + user.LastName, PubDate: article.Created.Format(time.RFC1123Z), Description: articleDescription, Content: &rss.Content{Value: articleContent}, Categories: tagNames, }) } feed := rss.NewFeed() feed.Channels = append(feed.Channels, channel) rss, err := feed.ToXML() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } files := []string{"web/templates/index.html", "web/templates/feed.rss"} tmpl, err := template.ParseFiles(files...) template.Must(tmpl, err).Execute(w, rss) } }