diff --git a/cmd/owl/webmention.go b/cmd/owl/webmention.go index ff558bc..b845e36 100644 --- a/cmd/owl/webmention.go +++ b/cmd/owl/webmention.go @@ -2,6 +2,7 @@ package main import ( "h4kor/owl-blogs" + "sync" "github.com/spf13/cobra" ) @@ -56,13 +57,18 @@ var webmentionCmd = &cobra.Command{ webmentions := post.OutgoingWebmentions() println("Found ", len(webmentions), " links") + wg := sync.WaitGroup{} + wg.Add(len(webmentions)) for _, webmention := range webmentions { - err = post.SendWebmention(webmention) - if err != nil { - println("Error sending webmentions: ", err.Error()) - } else { - println("Webmention sent to ", webmention.Target) - } + go func(webmention owl.WebmentionOut) { + defer wg.Done() + err = post.SendWebmention(webmention) + if err != nil { + println("Error sending webmentions: ", err.Error()) + } else { + println("Webmention sent to ", webmention.Target) + } + }(webmention) } return nil } diff --git a/post.go b/post.go index f136910..70b898f 100644 --- a/post.go +++ b/post.go @@ -305,6 +305,12 @@ func (post *Post) ScanForLinks() error { func (post *Post) SendWebmention(webmention WebmentionOut) error { defer post.PersistOutgoingWebmention(&webmention) + + // if last scan is less than 7 days ago, don't send webmention + if webmention.ScannedAt.After(time.Now().Add(-7*24*time.Hour)) && !webmention.Supported { + return nil + } + webmention.ScannedAt = time.Now() resp, err := post.user.repo.HttpClient.Get(webmention.Target) diff --git a/post_test.go b/post_test.go index c4669b1..d14a035 100644 --- a/post_test.go +++ b/post_test.go @@ -375,6 +375,38 @@ func TestCanSendWebmention(t *testing.T) { } } +func TestSendWebmentionOnlyScansOncePerWeek(t *testing.T) { + repo := getTestRepo(owl.RepoConfig{}) + repo.HttpClient = &MockHttpClient{} + repo.Parser = &MockHtmlParser{} + user, _ := repo.CreateUser("testuser") + post, _ := user.CreateNewPost("testpost") + + webmention := owl.WebmentionOut{ + Target: "http://example.com", + ScannedAt: time.Now().Add(time.Hour * -24 * 6), + } + + post.PersistOutgoingWebmention(&webmention) + webmentions := post.OutgoingWebmentions() + webmention = webmentions[0] + + err := post.SendWebmention(webmention) + if err != nil { + t.Errorf("Error sending webmention: %v", err) + } + + webmentions = post.OutgoingWebmentions() + + if len(webmentions) != 1 { + t.Errorf("Expected 1 webmention, got %d", len(webmentions)) + } + + if webmentions[0].ScannedAt != webmention.ScannedAt { + t.Errorf("Expected ScannedAt to be unchanged. Expected: %v, got %v", webmention.ScannedAt, webmentions[0].ScannedAt) + } +} + func TestSendingMultipleWebmentions(t *testing.T) { repo := getTestRepo(owl.RepoConfig{}) repo.HttpClient = &MockHttpClient{}