From e93808ac88b56bc5c553fba907265850e5277254 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 18:47:27 +0200 Subject: [PATCH] items in rss feed. #3 --- post.go | 4 ++++ post_test.go | 11 +++++++++++ repository.go | 4 ++-- rss.go | 34 ++++++++++++++++++++-------------- rss_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ user.go | 6 ++++++ user_test.go | 7 +++++++ 7 files changed, 101 insertions(+), 16 deletions(-) diff --git a/post.go b/post.go index c7ae3b8..9666039 100644 --- a/post.go +++ b/post.go @@ -33,6 +33,10 @@ func (post Post) UrlPath() string { return post.user.UrlPath() + "posts/" + post.id + "/" } +func (post Post) FullUrl() string { + return post.user.FullUrl() + "posts/" + post.id + "/" +} + func (post Post) UrlMediaPath(filename string) string { return post.UrlPath() + "media/" + filename } diff --git a/post_test.go b/post_test.go index efd8412..d399782 100644 --- a/post_test.go +++ b/post_test.go @@ -34,6 +34,17 @@ func TestPostUrlPath(t *testing.T) { } } +func TestPostFullUrl(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/" + if !(post.FullUrl() == expected) { + t.Error("Wrong url path") + t.Error("Expected: " + expected) + t.Error(" Got: " + post.FullUrl()) + } +} + func TestPostUrlMediaPath(t *testing.T) { user := getTestUser() post, _ := user.CreateNewPost("testpost") diff --git a/repository.go b/repository.go index 529af30..201ee74 100644 --- a/repository.go +++ b/repository.go @@ -113,9 +113,9 @@ func (repo Repository) UserUrlPath(user User) string { return "/user/" + user.name + "/" } -func (repo Repository) FullUserUrl(user User) string { +func (repo Repository) FullUrl() string { config, _ := repo.Config() - return config.Domain + repo.UserUrlPath(user) + return config.Domain } func (repo Repository) Template() (string, error) { diff --git a/rss.go b/rss.go index 948dbf5..e975350 100644 --- a/rss.go +++ b/rss.go @@ -19,10 +19,10 @@ type RSSChannel struct { } type RSSItem struct { - Title string `xml:"title"` - Link string `xml:"link"` - Description string `xml:"description"` - PubDate string `xml:"pubDate"` + Guid string `xml:"guid"` + Title string `xml:"title"` + Link string `xml:"link"` + PubDate string `xml:"pubDate"` } func RenderRSSFeed(user User) (string, error) { @@ -33,21 +33,27 @@ func RenderRSSFeed(user User) (string, error) { Version: "2.0", Channel: RSSChannel{ Title: config.Title, - Link: user.repo.FullUserUrl(user), + Link: user.FullUrl(), Description: config.SubTitle, Items: make([]RSSItem, 0), }, } - // posts, _ := user.Posts() - // for _, post := range posts { - // rss.Channel.Items = append(rss.Channel.Items, RSSItem{ - // Title: post.Title(), - // Link: post.Link(), - // Description: post.Description(), - // PubDate: post.PubDate(), - // }) - // } + posts, _ := user.Posts() + for _, postId := range posts { + post, _ := user.GetPost(postId) + _, meta := post.MarkdownData() + date, ok := meta["date"] + if !ok { + date = "" + } + rss.Channel.Items = append(rss.Channel.Items, RSSItem{ + Guid: postId, + Title: post.Title(), + Link: post.FullUrl(), + PubDate: date.(string), + }) + } buf := new(bytes.Buffer) err := xml.NewEncoder(buf).Encode(rss) diff --git a/rss_test.go b/rss_test.go index b273288..6073cef 100644 --- a/rss_test.go +++ b/rss_test.go @@ -2,6 +2,7 @@ package owl_test import ( "h4kor/owl-blogs" + "os" "strings" "testing" ) @@ -47,3 +48,53 @@ func TestRenderRSSFeedUserData(t *testing.T) { t.Error("SubTitle not rendered. Got: " + res) } } + +func TestRenderRSSFeedPostData(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: Test Post\n" + content += "date: 2015-01-01\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "Test Post") { + t.Error("Title not rendered. Got: " + res) + } + if !strings.Contains(res, post.FullUrl()) { + t.Error("SubTitle not rendered. Got: " + res) + } + if !strings.Contains(res, "2015-01-01") { + t.Error("Date not rendered. Got: " + res) + } +} + +func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: Test Post\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "Test Post") { + t.Error("Title not rendered. Got: " + res) + } + if !strings.Contains(res, post.FullUrl()) { + t.Error("SubTitle not rendered. Got: " + res) + } +} diff --git a/user.go b/user.go index 82fe15f..8b2131b 100644 --- a/user.go +++ b/user.go @@ -3,6 +3,7 @@ package owl import ( "fmt" "io/ioutil" + "net/url" "os" "path" "time" @@ -29,6 +30,11 @@ func (user User) UrlPath() string { return user.repo.UserUrlPath(user) } +func (user User) FullUrl() string { + url, _ := url.JoinPath(user.repo.FullUrl(), user.UrlPath()) + return url +} + func (user User) PostDir() string { return path.Join(user.Dir(), "public") } diff --git a/user_test.go b/user_test.go index e45e01d..3b372c9 100644 --- a/user_test.go +++ b/user_test.go @@ -145,3 +145,10 @@ func TestUserUrlPath(t *testing.T) { t.Error("Wrong url path, Expected: " + "/user/" + user.Name() + "/" + " Got: " + user.UrlPath()) } } + +func TestUserFullUrl(t *testing.T) { + user := getTestUser() + if !(user.FullUrl() == "http://localhost:8080/user/"+user.Name()+"/") { + t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl()) + } +}