only scan once per week

This commit is contained in:
Niko Abeler 2022-09-10 14:16:22 +02:00
parent 881940cd88
commit fe66d5842e
3 changed files with 50 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"sync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -56,13 +57,18 @@ var webmentionCmd = &cobra.Command{
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
println("Found ", len(webmentions), " links") println("Found ", len(webmentions), " links")
wg := sync.WaitGroup{}
wg.Add(len(webmentions))
for _, webmention := range webmentions { for _, webmention := range webmentions {
err = post.SendWebmention(webmention) go func(webmention owl.WebmentionOut) {
if err != nil { defer wg.Done()
println("Error sending webmentions: ", err.Error()) err = post.SendWebmention(webmention)
} else { if err != nil {
println("Webmention sent to ", webmention.Target) println("Error sending webmentions: ", err.Error())
} } else {
println("Webmention sent to ", webmention.Target)
}
}(webmention)
} }
return nil return nil
} }

View File

@ -305,6 +305,12 @@ func (post *Post) ScanForLinks() error {
func (post *Post) SendWebmention(webmention WebmentionOut) error { func (post *Post) SendWebmention(webmention WebmentionOut) error {
defer post.PersistOutgoingWebmention(&webmention) 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() webmention.ScannedAt = time.Now()
resp, err := post.user.repo.HttpClient.Get(webmention.Target) resp, err := post.user.repo.HttpClient.Get(webmention.Target)

View File

@ -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) { func TestSendingMultipleWebmentions(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{}) repo := getTestRepo(owl.RepoConfig{})
repo.HttpClient = &MockHttpClient{} repo.HttpClient = &MockHttpClient{}