diff --git a/post.go b/post.go index b42fdcc..f2a6ff9 100644 --- a/post.go +++ b/post.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path" + "sort" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" @@ -242,3 +243,19 @@ func (post *Post) Webmentions() []Webmention { return webmentions } + +func (post *Post) ApprovedWebmentions() []Webmention { + webmentions := post.Webmentions() + approved := []Webmention{} + for _, webmention := range webmentions { + if webmention.ApprovalStatus == "approved" { + approved = append(approved, webmention) + } + } + + // sort by retrieved date + sort.Slice(approved, func(i, j int) bool { + return approved[i].RetrievedAt.After(approved[j].RetrievedAt) + }) + return approved +} diff --git a/post_test.go b/post_test.go index 62e0f86..5eafb59 100644 --- a/post_test.go +++ b/post_test.go @@ -6,6 +6,7 @@ import ( "path" "strings" "testing" + "time" ) func TestCanGetPostTitle(t *testing.T) { @@ -259,3 +260,46 @@ func TestAddWebmentionAddsParsedTitle(t *testing.T) { t.Errorf("Got: %v", string(fileContent)) } } + +func TestApprovedWebmentions(t *testing.T) { + repo := getTestRepo() + user, _ := repo.CreateUser("testuser") + post, _ := user.CreateNewPost("testpost") + webmention := owl.Webmention{ + Source: "http://example.com/source", + ApprovalStatus: "approved", + RetrievedAt: time.Now(), + } + post.PersistWebmention(webmention) + webmention = owl.Webmention{ + Source: "http://example.com/source2", + ApprovalStatus: "", + RetrievedAt: time.Now().Add(time.Hour * -1), + } + post.PersistWebmention(webmention) + webmention = owl.Webmention{ + Source: "http://example.com/source3", + ApprovalStatus: "approved", + RetrievedAt: time.Now().Add(time.Hour * -2), + } + post.PersistWebmention(webmention) + webmention = owl.Webmention{ + Source: "http://example.com/source4", + ApprovalStatus: "rejected", + RetrievedAt: time.Now().Add(time.Hour * -3), + } + post.PersistWebmention(webmention) + + webmentions := post.ApprovedWebmentions() + if len(webmentions) != 2 { + t.Errorf("Expected 2 webmentions, got %d", len(webmentions)) + } + + if webmentions[0].Source != "http://example.com/source" { + t.Errorf("Expected source: %s, got %s", "http://example.com/source", webmentions[0].Source) + } + if webmentions[1].Source != "http://example.com/source3" { + t.Errorf("Expected source: %s, got %s", "http://example.com/source3", webmentions[1].Source) + } + +} diff --git a/webmention.go b/webmention.go index ba9ff82..fccd815 100644 --- a/webmention.go +++ b/webmention.go @@ -5,14 +5,16 @@ import ( "errors" "net/http" "strings" + "time" "golang.org/x/net/html" ) type Webmention struct { - Source string `yaml:"source"` - Title string `yaml:"title"` - Approved bool `yaml:"approved"` + Source string `yaml:"source"` + Title string `yaml:"title"` + ApprovalStatus string `yaml:"approval_status"` + RetrievedAt time.Time `yaml:"retrieved_at"` } type HttpRetriever interface {