Sending Webmentions #10

Merged
h4kor merged 18 commits from webmention into master 2022-09-06 19:49:00 +00:00
5 changed files with 45 additions and 20 deletions
Showing only changes of commit da197c7e4d - Show all commits

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. -- This will be rendered as the blog post.
-- Must be present for the blog post to be valid. -- Must be present for the blog post to be valid.
-- All other folders will be ignored -- 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/ \- media/
-- Contains all media files used in the blog post. -- Contains all media files used in the blog post.
-- All files in this folder will be publicly available -- All files in this folder will be publicly available
@ -59,3 +62,14 @@ aliases:
Actual post 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"` Draft bool `yaml:"draft"`
} }
type PostStatus struct {
Webmentions []WebmentionOut
}
func (post Post) Id() string { func (post Post) Id() string {
return post.id return post.id
} }
@ -156,7 +160,7 @@ func (post *Post) WebmentionFile(source string) string {
return path.Join(post.WebmentionDir(), hashStr+".yml") return path.Join(post.WebmentionDir(), hashStr+".yml")
} }
func (post *Post) PersistWebmention(webmention Webmention) error { func (post *Post) PersistWebmention(webmention WebmentionIn) error {
// ensure dir exists // ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755) os.MkdirAll(post.WebmentionDir(), 0755)
@ -169,7 +173,7 @@ func (post *Post) PersistWebmention(webmention Webmention) error {
return os.WriteFile(fileName, data, 0644) 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 // ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755) os.MkdirAll(post.WebmentionDir(), 0755)
@ -177,18 +181,18 @@ func (post *Post) Webmention(source string) (Webmention, error) {
fileName := post.WebmentionFile(source) fileName := post.WebmentionFile(source)
if !fileExists(fileName) { if !fileExists(fileName) {
// return error if file doesn't exist // 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) data, err := os.ReadFile(fileName)
if err != nil { if err != nil {
return Webmention{}, err return WebmentionIn{}, err
} }
mention := Webmention{} mention := WebmentionIn{}
err = yaml.Unmarshal(data, &mention) err = yaml.Unmarshal(data, &mention)
if err != nil { if err != nil {
return Webmention{}, err return WebmentionIn{}, err
} }
return mention, nil return mention, nil
@ -198,7 +202,7 @@ func (post *Post) AddWebmention(source string) error {
// Check if file already exists // Check if file already exists
_, err := post.Webmention(source) _, err := post.Webmention(source)
if err != nil { if err != nil {
webmention := Webmention{ webmention := WebmentionIn{
Source: source, Source: source,
} }
defer post.EnrichWebmention(source) defer post.EnrichWebmention(source)
@ -223,17 +227,17 @@ func (post *Post) EnrichWebmention(source string) error {
return err return err
} }
func (post *Post) Webmentions() []Webmention { func (post *Post) Webmentions() []WebmentionIn {
// ensure dir exists // ensure dir exists
os.MkdirAll(post.WebmentionDir(), 0755) os.MkdirAll(post.WebmentionDir(), 0755)
files := listDir(post.WebmentionDir()) files := listDir(post.WebmentionDir())
webmentions := []Webmention{} webmentions := []WebmentionIn{}
for _, file := range files { for _, file := range files {
data, err := os.ReadFile(path.Join(post.WebmentionDir(), file)) data, err := os.ReadFile(path.Join(post.WebmentionDir(), file))
if err != nil { if err != nil {
continue continue
} }
mention := Webmention{} mention := WebmentionIn{}
err = yaml.Unmarshal(data, &mention) err = yaml.Unmarshal(data, &mention)
if err != nil { if err != nil {
continue continue
@ -244,9 +248,9 @@ func (post *Post) Webmentions() []Webmention {
return webmentions return webmentions
} }
func (post *Post) ApprovedWebmentions() []Webmention { func (post *Post) ApprovedWebmentions() []WebmentionIn {
webmentions := post.Webmentions() webmentions := post.Webmentions()
approved := []Webmention{} approved := []WebmentionIn{}
for _, webmention := range webmentions { for _, webmention := range webmentions {
if webmention.ApprovalStatus == "approved" { if webmention.ApprovalStatus == "approved" {
approved = append(approved, webmention) approved = append(approved, webmention)

View File

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

View File

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

View File

@ -10,13 +10,20 @@ import (
"golang.org/x/net/html" "golang.org/x/net/html"
) )
type Webmention struct { type WebmentionIn struct {
Source string `yaml:"source"` Source string `yaml:"source"`
Title string `yaml:"title"` Title string `yaml:"title"`
ApprovalStatus string `yaml:"approval_status"` ApprovalStatus string `yaml:"approval_status"`
RetrievedAt time.Time `yaml:"retrieved_at"` 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 { type HttpRetriever interface {
Get(url string) ([]byte, error) Get(url string) ([]byte, error)
} }