approved webmentions
This commit is contained in:
parent
73ec606b95
commit
8d1dacc1f5
17
post.go
17
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
|
||||
}
|
||||
|
|
44
post_test.go
44
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
@ -12,7 +13,8 @@ import (
|
|||
type Webmention struct {
|
||||
Source string `yaml:"source"`
|
||||
Title string `yaml:"title"`
|
||||
Approved bool `yaml:"approved"`
|
||||
ApprovalStatus string `yaml:"approval_status"`
|
||||
RetrievedAt time.Time `yaml:"retrieved_at"`
|
||||
}
|
||||
|
||||
type HttpRetriever interface {
|
||||
|
|
Loading…
Reference in New Issue