WIP outgoing webmentions

This commit is contained in:
Niko Abeler 2022-09-04 15:03:16 +02:00
parent 4aaa14e30c
commit da197c7e4d
5 changed files with 45 additions and 20 deletions

View File

@ -23,6 +23,9 @@ Each directory in the `/users/` directory of a repository is considered a user.
-- This will be rendered as the blog post.
-- Must be present for the blog post to be valid.
-- All other folders will be ignored
\- status.yml
-- Used to track various process status related to the post,
-- such as if a webmention was sent.
\- media/
-- Contains all media files used in the blog post.
-- All files in this folder will be publicly available
@ -59,3 +62,14 @@ aliases:
Actual post
```
#### status.yml
```
webmentions:
- target: https://example.com/post
supported: true
scanned_at: 2021-08-13T17:07:00Z
last_sent_at: 2021-08-13T17:07:00Z
```

28
post.go
View File

@ -32,6 +32,10 @@ type PostMeta struct {
Draft bool `yaml:"draft"`
}
type PostStatus struct {
Webmentions []WebmentionOut
}
func (post Post) Id() string {
return post.id
}
@ -156,7 +160,7 @@ func (post *Post) WebmentionFile(source string) string {
return path.Join(post.WebmentionDir(), hashStr+".yml")
}
func (post *Post) PersistWebmention(webmention Webmention) error {
func (post *Post) PersistWebmention(webmention WebmentionIn) error {
// ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755)
@ -169,7 +173,7 @@ func (post *Post) PersistWebmention(webmention Webmention) error {
return os.WriteFile(fileName, data, 0644)
}
func (post *Post) Webmention(source string) (Webmention, error) {
func (post *Post) Webmention(source string) (WebmentionIn, error) {
// ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755)
@ -177,18 +181,18 @@ func (post *Post) Webmention(source string) (Webmention, error) {
fileName := post.WebmentionFile(source)
if !fileExists(fileName) {
// return error if file doesn't exist
return Webmention{}, fmt.Errorf("Webmention file not found: %s", source)
return WebmentionIn{}, fmt.Errorf("Webmention file not found: %s", source)
}
data, err := os.ReadFile(fileName)
if err != nil {
return Webmention{}, err
return WebmentionIn{}, err
}
mention := Webmention{}
mention := WebmentionIn{}
err = yaml.Unmarshal(data, &mention)
if err != nil {
return Webmention{}, err
return WebmentionIn{}, err
}
return mention, nil
@ -198,7 +202,7 @@ func (post *Post) AddWebmention(source string) error {
// Check if file already exists
_, err := post.Webmention(source)
if err != nil {
webmention := Webmention{
webmention := WebmentionIn{
Source: source,
}
defer post.EnrichWebmention(source)
@ -223,17 +227,17 @@ func (post *Post) EnrichWebmention(source string) error {
return err
}
func (post *Post) Webmentions() []Webmention {
func (post *Post) Webmentions() []WebmentionIn {
// ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755)
files := listDir(post.WebmentionDir())
webmentions := []Webmention{}
webmentions := []WebmentionIn{}
for _, file := range files {
data, err := os.ReadFile(path.Join(post.WebmentionDir(), file))
if err != nil {
continue
}
mention := Webmention{}
mention := WebmentionIn{}
err = yaml.Unmarshal(data, &mention)
if err != nil {
continue
@ -244,9 +248,9 @@ func (post *Post) Webmentions() []Webmention {
return webmentions
}
func (post *Post) ApprovedWebmentions() []Webmention {
func (post *Post) ApprovedWebmentions() []WebmentionIn {
webmentions := post.Webmentions()
approved := []Webmention{}
approved := []WebmentionIn{}
for _, webmention := range webmentions {
if webmention.ApprovalStatus == "approved" {
approved = append(approved, webmention)

View File

@ -173,7 +173,7 @@ func TestPersistWebmention(t *testing.T) {
repo := getTestRepo()
user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost")
webmention := owl.Webmention{
webmention := owl.WebmentionIn{
Source: "http://example.com/source",
}
err := post.PersistWebmention(webmention)
@ -265,25 +265,25 @@ func TestApprovedWebmentions(t *testing.T) {
repo := getTestRepo()
user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost")
webmention := owl.Webmention{
webmention := owl.WebmentionIn{
Source: "http://example.com/source",
ApprovalStatus: "approved",
RetrievedAt: time.Now(),
}
post.PersistWebmention(webmention)
webmention = owl.Webmention{
webmention = owl.WebmentionIn{
Source: "http://example.com/source2",
ApprovalStatus: "",
RetrievedAt: time.Now().Add(time.Hour * -1),
}
post.PersistWebmention(webmention)
webmention = owl.Webmention{
webmention = owl.WebmentionIn{
Source: "http://example.com/source3",
ApprovalStatus: "approved",
RetrievedAt: time.Now().Add(time.Hour * -2),
}
post.PersistWebmention(webmention)
webmention = owl.Webmention{
webmention = owl.WebmentionIn{
Source: "http://example.com/source4",
ApprovalStatus: "rejected",
RetrievedAt: time.Now().Add(time.Hour * -3),

View File

@ -162,14 +162,14 @@ func TestRenderPostIncludesRelToWebMention(t *testing.T) {
func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost")
webmention := owl.Webmention{
webmention := owl.WebmentionIn{
Source: "http://example.com/source3",
Title: "Test Title",
ApprovalStatus: "approved",
RetrievedAt: time.Now().Add(time.Hour * -2),
}
post.PersistWebmention(webmention)
webmention = owl.Webmention{
webmention = owl.WebmentionIn{
Source: "http://example.com/source4",
ApprovalStatus: "rejected",
RetrievedAt: time.Now().Add(time.Hour * -3),

View File

@ -10,13 +10,20 @@ import (
"golang.org/x/net/html"
)
type Webmention struct {
type WebmentionIn struct {
Source string `yaml:"source"`
Title string `yaml:"title"`
ApprovalStatus string `yaml:"approval_status"`
RetrievedAt time.Time `yaml:"retrieved_at"`
}
type WebmentionOut struct {
Target string `yaml:"target"`
Supported bool `yaml:"supported"`
ScannedAt time.Time `yaml:"scanned_at"`
LastSentAt time.Time `yaml:"last_sent_at"`
}
type HttpRetriever interface {
Get(url string) ([]byte, error)
}