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 + "/" 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 { func (post Post) UrlMediaPath(filename string) string {
return post.UrlPath() + "media/" + filename 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) { func TestPostUrlMediaPath(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")

View File

@ -113,9 +113,9 @@ func (repo Repository) UserUrlPath(user User) string {
return "/user/" + user.name + "/" return "/user/" + user.name + "/"
} }
func (repo Repository) FullUserUrl(user User) string { func (repo Repository) FullUrl() string {
config, _ := repo.Config() config, _ := repo.Config()
return config.Domain + repo.UserUrlPath(user) return config.Domain
} }
func (repo Repository) Template() (string, error) { func (repo Repository) Template() (string, error) {

28
rss.go
View File

@ -19,9 +19,9 @@ type RSSChannel struct {
} }
type RSSItem struct { type RSSItem struct {
Guid string `xml:"guid"`
Title string `xml:"title"` Title string `xml:"title"`
Link string `xml:"link"` Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"` PubDate string `xml:"pubDate"`
} }
@ -33,21 +33,27 @@ func RenderRSSFeed(user User) (string, error) {
Version: "2.0", Version: "2.0",
Channel: RSSChannel{ Channel: RSSChannel{
Title: config.Title, Title: config.Title,
Link: user.repo.FullUserUrl(user), Link: user.FullUrl(),
Description: config.SubTitle, Description: config.SubTitle,
Items: make([]RSSItem, 0), Items: make([]RSSItem, 0),
}, },
} }
// posts, _ := user.Posts() posts, _ := user.Posts()
// for _, post := range posts { for _, postId := range posts {
// rss.Channel.Items = append(rss.Channel.Items, RSSItem{ post, _ := user.GetPost(postId)
// Title: post.Title(), _, meta := post.MarkdownData()
// Link: post.Link(), date, ok := meta["date"]
// Description: post.Description(), if !ok {
// PubDate: post.PubDate(), date = ""
// }) }
// } rss.Channel.Items = append(rss.Channel.Items, RSSItem{
Guid: postId,
Title: post.Title(),
Link: post.FullUrl(),
PubDate: date.(string),
})
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := xml.NewEncoder(buf).Encode(rss) err := xml.NewEncoder(buf).Encode(rss)

View File

@ -2,6 +2,7 @@ package owl_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"os"
"strings" "strings"
"testing" "testing"
) )
@ -47,3 +48,53 @@ func TestRenderRSSFeedUserData(t *testing.T) {
t.Error("SubTitle not rendered. Got: " + res) 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 ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/url"
"os" "os"
"path" "path"
"time" "time"
@ -29,6 +30,11 @@ func (user User) UrlPath() string {
return user.repo.UserUrlPath(user) 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 { func (user User) PostDir() string {
return path.Join(user.Dir(), "public") 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()) 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())
}
}