items in rss feed. #3

This commit is contained in:
Niko Abeler 2022-08-13 18:47:27 +02:00
parent 1b99eaa016
commit e93808ac88
7 changed files with 101 additions and 16 deletions

View File

@ -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
}

View File

@ -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")

View File

@ -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) {

34
rss.go
View File

@ -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)

View File

@ -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)
}
}

View File

@ -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")
}

View File

@ -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())
}
}