Refactoring Posts to allow multiple types

This commit is contained in:
Niko Abeler 2022-11-29 20:58:34 +01:00
parent e009505cb3
commit 521f7b16aa
6 changed files with 49 additions and 28 deletions

View File

@ -86,7 +86,7 @@ var webmentionCmd = &cobra.Command{
return return
} }
posts, err := user.Posts() posts, err := user.PublishedPosts()
if err != nil { if err != nil {
println("Error getting posts: ", err.Error()) println("Error getting posts: ", err.Error())
} }

View File

@ -120,7 +120,7 @@ func RenderPost(post *Post) (string, error) {
} }
func RenderIndexPage(user User) (string, error) { func RenderIndexPage(user User) (string, error) {
posts, _ := user.Posts() posts, _ := user.PrimaryFeedPosts()
postHtml, err := renderEmbedTemplate("embed/post-list.html", posts) postHtml, err := renderEmbedTemplate("embed/post-list.html", posts)
if err != nil { if err != nil {

View File

@ -210,7 +210,7 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) {
content += "This is a test" content += "This is a test"
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 1) assertions.AssertLen(t, posts, 1)
var aliases map[string]*owl.Post var aliases map[string]*owl.Post
@ -244,7 +244,7 @@ func TestAliasesHaveCorrectPost(t *testing.T) {
content += "This is a test" content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644) os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 2) assertions.AssertLen(t, posts, 2)
var aliases map[string]*owl.Post var aliases map[string]*owl.Post

2
rss.go
View File

@ -41,7 +41,7 @@ func RenderRSSFeed(user User) (string, error) {
}, },
} }
posts, _ := user.Posts() posts, _ := user.PrimaryFeedPosts()
for _, post := range posts { for _, post := range posts {
meta := post.Meta() meta := post.Meta()
content, _ := renderPostContent(post) content, _ := renderPostContent(post)

47
user.go
View File

@ -166,7 +166,7 @@ func (user User) FaviconUrl() string {
return "" return ""
} }
func (user User) Posts() ([]*Post, error) { func (user User) AllPosts() ([]*Post, error) {
postFiles := listDir(path.Join(user.Dir(), "public")) postFiles := listDir(path.Join(user.Dir(), "public"))
posts := make([]*Post, 0) posts := make([]*Post, 0)
for _, id := range postFiles { for _, id := range postFiles {
@ -179,17 +179,6 @@ func (user User) Posts() ([]*Post, error) {
} }
} }
// remove drafts
n := 0
for _, post := range posts {
meta := post.Meta()
if !meta.Draft {
posts[n] = post
n++
}
}
posts = posts[:n]
type PostWithDate struct { type PostWithDate struct {
post *Post post *Post
date time.Time date time.Time
@ -213,6 +202,38 @@ func (user User) Posts() ([]*Post, error) {
return posts, nil return posts, nil
} }
func (user User) PublishedPosts() ([]*Post, error) {
posts, _ := user.AllPosts()
// remove drafts
n := 0
for _, post := range posts {
meta := post.Meta()
if !meta.Draft {
posts[n] = post
n++
}
}
posts = posts[:n]
return posts, nil
}
func (user User) PrimaryFeedPosts() ([]*Post, error) {
posts, _ := user.PublishedPosts()
// remove non-primary feed posts
n := 0
for _, post := range posts {
meta := post.Meta()
if meta.Type == "article" {
posts[n] = post
n++
}
}
posts = posts[:n]
return posts, nil
}
func (user User) GetPost(id string) (*Post, error) { func (user User) GetPost(id string) (*Post, error) {
// check if posts index.md exists // check if posts index.md exists
if !fileExists(path.Join(user.Dir(), "public", id, "index.md")) { if !fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
@ -301,7 +322,7 @@ func (user User) SetConfig(new_config UserConfig) error {
func (user User) PostAliases() (map[string]*Post, error) { func (user User) PostAliases() (map[string]*Post, error) {
post_aliases := make(map[string]*Post) post_aliases := make(map[string]*Post)
posts, err := user.Posts() posts, err := user.PublishedPosts()
if err != nil { if err != nil {
return post_aliases, err return post_aliases, err
} }

View File

@ -34,7 +34,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
user := getTestUser() user := getTestUser()
// Create a new post // Create a new post
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
post, _ := user.GetPost(posts[0].Id()) post, _ := user.GetPost(posts[0].Id())
meta := post.Meta() meta := post.Meta()
assertions.AssertNot(t, meta.Date.IsZero(), "Date not set") assertions.AssertNot(t, meta.Date.IsZero(), "Date not set")
@ -61,7 +61,7 @@ func TestCanListUserPosts(t *testing.T) {
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
posts, err := user.Posts() posts, err := user.PublishedPosts()
assertions.AssertNoError(t, err, "Error reading posts") assertions.AssertNoError(t, err, "Error reading posts")
assertions.AssertLen(t, posts, 3) assertions.AssertLen(t, posts, 3)
} }
@ -83,7 +83,7 @@ func TestCannotListUserPostsInSubdirectories(t *testing.T) {
os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644)
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
postIds := []string{} postIds := []string{}
for _, p := range posts { for _, p := range posts {
postIds = append(postIds, p.Id()) postIds = append(postIds, p.Id())
@ -119,7 +119,7 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
content += "Write your post here.\n" content += "Write your post here.\n"
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
postIds := []string{} postIds := []string{}
for _, p := range posts { for _, p := range posts {
postIds = append(postIds, p.Id()) postIds = append(postIds, p.Id())
@ -147,7 +147,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
content += "Write your post here.\n" content += "Write your post here.\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 0) assertions.AssertLen(t, posts, 0)
} }
@ -173,7 +173,7 @@ func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 0) assertions.AssertLen(t, posts, 0)
} }
@ -182,7 +182,7 @@ func TestCanLoadPost(t *testing.T) {
// Create a new post // Create a new post
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
post, _ := user.GetPost(posts[0].Id()) post, _ := user.GetPost(posts[0].Id())
assertions.Assert(t, post.Title() == "testpost", "Post title is not correct") assertions.Assert(t, post.Title() == "testpost", "Post title is not correct")
} }
@ -217,7 +217,7 @@ func TestPostsSortedByPublishingDateLatestFirst(t *testing.T) {
content += "This is a test" content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644) os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id") assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id")
assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id") assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id")
} }
@ -237,7 +237,7 @@ func TestPostsSortedByPublishingDateLatestFirst2(t *testing.T) {
posts = append(posts, post) posts = append(posts, post)
} }
retPosts, _ := user.Posts() retPosts, _ := user.PublishedPosts()
for i, p := range retPosts { for i, p := range retPosts {
assertions.Assert(t, p.Id() == posts[i].Id(), "Wrong Id") assertions.Assert(t, p.Id() == posts[i].Id(), "Wrong Id")
} }
@ -263,7 +263,7 @@ func TestPostsSortedByPublishingDateBrokenAtBottom(t *testing.T) {
content += "This is a test" content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644) os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.PublishedPosts()
assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id") assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id")
assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id") assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id")
} }