From be467521d9b468389a6b938307196648549302b9 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 15:17:14 +0200 Subject: [PATCH] Only provide link in item instead of the entire article via content --- cmd/backend/rss.go | 23 +++++++++-------------- cmd/calls/articles.go | 33 +++++++++++++++++++++++++++++++++ cmd/frontend/articles.go | 4 ++-- cmd/main.go | 1 + 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 cmd/calls/articles.go diff --git a/cmd/backend/rss.go b/cmd/backend/rss.go index c68395e..1807da0 100644 --- a/cmd/backend/rss.go +++ b/cmd/backend/rss.go @@ -50,11 +50,11 @@ func GetChannel(db *DB, title, link, description string) (*rss.Channel, error) { return channel, nil } -func GenerateRSS(db *DB, title, link, desc string) (*string, error) { +func GenerateRSS(c *Config, db *DB) (*string, error) { channel := &rss.Channel{ - Title: title, - Link: link, - Description: desc, + Title: c.Title, + Link: c.Link, + Description: c.Description, Items: make([]*rss.Item, 0), } @@ -89,18 +89,13 @@ func GenerateRSS(db *DB, title, link, desc string) (*string, error) { return nil, fmt.Errorf("error converting description to plain text for RSS feed: %v", err) } - articleContent, err := ConvertToHTML(article.Content) - if err != nil { - return nil, fmt.Errorf("error converting content to HTML for RSS feed: %v", err) - } - 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}, + Author: fmt.Sprint(user.FirstName, user.LastName), Categories: tagNames, + Description: articleDescription, + Link: fmt.Sprintf("http://%s/article/serve/%d", c.Domain, article.ID), + PubDate: article.Created.Format(time.RFC1123Z), + Title: articleTitle, }) } diff --git a/cmd/calls/articles.go b/cmd/calls/articles.go new file mode 100644 index 0000000..891f7a0 --- /dev/null +++ b/cmd/calls/articles.go @@ -0,0 +1,33 @@ +package calls + +import ( + "fmt" + "log" + "net/http" + "strconv" + + b "streifling.com/jason/cpolis/cmd/backend" +) + +func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if tokenIsVerified(w, r, c) { + idString := r.PathValue("id") + id, err := strconv.ParseInt(idString, 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + article, err := db.GetArticle(id) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + fmt.Fprint(w, article.Content) + } + } +} diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 5fc9ee6..dac02e8 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -363,7 +363,7 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description) + feed, err := b.GenerateRSS(c, db) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -582,7 +582,7 @@ func DeleteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description) + feed, err := b.GenerateRSS(c, db) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/cmd/main.go b/cmd/main.go index 7bb9a61..718ebec 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -49,6 +49,7 @@ func main() { http.FileServer(http.Dir(config.WebDir+"/static/")))) mux.HandleFunc("/", f.HomePage(config, db, store)) + mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db)) mux.HandleFunc("GET /create-tag", f.CreateTag(config, store)) mux.HandleFunc("GET /create-user", f.CreateUser(config, store)) mux.HandleFunc("GET /delete-article/{id}", f.DeleteArticle(config, db, store))