approved webmentions

This commit is contained in:
Niko Abeler 2022-09-01 21:53:06 +02:00
parent 73ec606b95
commit 8d1dacc1f5
3 changed files with 66 additions and 3 deletions

17
post.go
View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"sort"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/extension"
@ -242,3 +243,19 @@ func (post *Post) Webmentions() []Webmention {
return webmentions 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
}

View File

@ -6,6 +6,7 @@ import (
"path" "path"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestCanGetPostTitle(t *testing.T) { func TestCanGetPostTitle(t *testing.T) {
@ -259,3 +260,46 @@ func TestAddWebmentionAddsParsedTitle(t *testing.T) {
t.Errorf("Got: %v", string(fileContent)) 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)
}
}

View File

@ -5,14 +5,16 @@ import (
"errors" "errors"
"net/http" "net/http"
"strings" "strings"
"time"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
type Webmention struct { type Webmention struct {
Source string `yaml:"source"` Source string `yaml:"source"`
Title string `yaml:"title"` Title string `yaml:"title"`
Approved bool `yaml:"approved"` ApprovalStatus string `yaml:"approval_status"`
RetrievedAt time.Time `yaml:"retrieved_at"`
} }
type HttpRetriever interface { type HttpRetriever interface {