From 521f7b16aa7033d58b6e8e24b501374e78c5b8bf Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Tue, 29 Nov 2022 20:58:34 +0100 Subject: [PATCH] Refactoring Posts to allow multiple types --- cmd/owl/webmention.go | 2 +- renderer.go | 2 +- repository_test.go | 4 ++-- rss.go | 2 +- user.go | 47 +++++++++++++++++++++++++++++++------------ user_test.go | 20 +++++++++--------- 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/cmd/owl/webmention.go b/cmd/owl/webmention.go index 6580f16..35cbbd9 100644 --- a/cmd/owl/webmention.go +++ b/cmd/owl/webmention.go @@ -86,7 +86,7 @@ var webmentionCmd = &cobra.Command{ return } - posts, err := user.Posts() + posts, err := user.PublishedPosts() if err != nil { println("Error getting posts: ", err.Error()) } diff --git a/renderer.go b/renderer.go index f8a14a7..edbbc92 100644 --- a/renderer.go +++ b/renderer.go @@ -120,7 +120,7 @@ func RenderPost(post *Post) (string, error) { } func RenderIndexPage(user User) (string, error) { - posts, _ := user.Posts() + posts, _ := user.PrimaryFeedPosts() postHtml, err := renderEmbedTemplate("embed/post-list.html", posts) if err != nil { diff --git a/repository_test.go b/repository_test.go index 00ae31a..95eb9a8 100644 --- a/repository_test.go +++ b/repository_test.go @@ -210,7 +210,7 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) { content += "This is a test" os.WriteFile(post.ContentFile(), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() assertions.AssertLen(t, posts, 1) var aliases map[string]*owl.Post @@ -244,7 +244,7 @@ func TestAliasesHaveCorrectPost(t *testing.T) { content += "This is a test" os.WriteFile(post2.ContentFile(), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() assertions.AssertLen(t, posts, 2) var aliases map[string]*owl.Post diff --git a/rss.go b/rss.go index 8a4c95e..efc29d0 100644 --- a/rss.go +++ b/rss.go @@ -41,7 +41,7 @@ func RenderRSSFeed(user User) (string, error) { }, } - posts, _ := user.Posts() + posts, _ := user.PrimaryFeedPosts() for _, post := range posts { meta := post.Meta() content, _ := renderPostContent(post) diff --git a/user.go b/user.go index f5e9437..d8a65c6 100644 --- a/user.go +++ b/user.go @@ -166,7 +166,7 @@ func (user User) FaviconUrl() string { return "" } -func (user User) Posts() ([]*Post, error) { +func (user User) AllPosts() ([]*Post, error) { postFiles := listDir(path.Join(user.Dir(), "public")) posts := make([]*Post, 0) 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 { post *Post date time.Time @@ -213,6 +202,38 @@ func (user User) Posts() ([]*Post, error) { 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) { // check if posts index.md exists 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) { post_aliases := make(map[string]*Post) - posts, err := user.Posts() + posts, err := user.PublishedPosts() if err != nil { return post_aliases, err } diff --git a/user_test.go b/user_test.go index c34fb87..89f07b7 100644 --- a/user_test.go +++ b/user_test.go @@ -34,7 +34,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) { user := getTestUser() // Create a new post user.CreateNewPost("testpost", false) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() post, _ := user.GetPost(posts[0].Id()) meta := post.Meta() 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) - posts, err := user.Posts() + posts, err := user.PublishedPosts() assertions.AssertNoError(t, err, "Error reading posts") 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/bar/index.md"), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() postIds := []string{} for _, p := range posts { postIds = append(postIds, p.Id()) @@ -119,7 +119,7 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { content += "Write your post here.\n" os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() postIds := []string{} for _, p := range posts { postIds = append(postIds, p.Id()) @@ -147,7 +147,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { content += "Write your post here.\n" os.WriteFile(post.ContentFile(), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() assertions.AssertLen(t, posts, 0) } @@ -173,7 +173,7 @@ func TestListUsersDraftsExcludedRealWorld(t *testing.T) { os.WriteFile(post.ContentFile(), []byte(content), 0644) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() assertions.AssertLen(t, posts, 0) } @@ -182,7 +182,7 @@ func TestCanLoadPost(t *testing.T) { // Create a new post user.CreateNewPost("testpost", false) - posts, _ := user.Posts() + posts, _ := user.PublishedPosts() post, _ := user.GetPost(posts[0].Id()) 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" 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[1].Id() == post1.Id(), "Wrong Id") } @@ -237,7 +237,7 @@ func TestPostsSortedByPublishingDateLatestFirst2(t *testing.T) { posts = append(posts, post) } - retPosts, _ := user.Posts() + retPosts, _ := user.PublishedPosts() for i, p := range retPosts { assertions.Assert(t, p.Id() == posts[i].Id(), "Wrong Id") } @@ -263,7 +263,7 @@ func TestPostsSortedByPublishingDateBrokenAtBottom(t *testing.T) { content += "This is a test" 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[1].Id() == post1.Id(), "Wrong Id") }