diff --git a/testdata/wmtest.html b/testdata/wmtest.html new file mode 100644 index 0000000..69d1471 --- /dev/null +++ b/testdata/wmtest.html @@ -0,0 +1,33 @@ + + + + + +Test’s Blog — Micropub, Crossposting to Twitter, and Enabling “Tweetstorms” + + + + + +
+
+
+

Micropub, Crossposting to Twitter, and Enabling “Tweetstorms”

+ + +
+
+

I’ve previously talked about how I crosspost from this blog to my Mastodon account without the need for a third-party service, and how I leverage WordPress’s hook system to even enable toot threading.

+

In this post, I’m going to really quickly explain my (extremely similar) Twitter setup. (Note: I don’t actually syndicate this blog’s posts to Twitter, but I do use this very setup on another site of mine.)

+

I liked the idea of a dead-simple Twitter plugin, so I forked my Mastodon plugin and tweaked a few things here and there. Once I’ve installed it, and created a developer account, generated the necessary keys, and let WordPress know about them, things look, well, very familiar. In fact, crossposting should now just work.

+

Now, to enable this when posting through Micropub rather than WordPress’s admin interface! Again, since posting through Micropub means no WordPress interface, and thus no “meta box” and no checkbox, and no way for WordPress to know if I wanted to crosspost a certain article or not, I’m going to have to use … syndication targets (which were invented precisely for this reason).

+
+ +
+
+ + \ No newline at end of file diff --git a/webmentionVerification.go b/webmentionVerification.go index 65f9616..03c59f2 100644 --- a/webmentionVerification.go +++ b/webmentionVerification.go @@ -85,9 +85,10 @@ func (a *goBlog) verifyMention(m *mention) error { } } // Check if source has a redirect - ru := resp.Request.URL - if m.Source != ru.String() { - m.NewSource = ru.String() + if respReq := resp.Request; respReq != nil { + if ru := respReq.URL; m.Source != ru.String() { + m.NewSource = ru.String() + } } // Parse response body err = m.verifyReader(resp.Body) @@ -105,6 +106,7 @@ func (a *goBlog) verifyMention(m *mention) error { if cr := []rune(m.Content); len(cr) > 500 { m.Content = string(cr[0:497]) + "…" } + m.Content = strings.ReplaceAll(m.Content, "\n", " ") if tr := []rune(m.Title); len(tr) > 60 { m.Title = string(tr[0:57]) + "…" } @@ -132,11 +134,17 @@ func (a *goBlog) verifyMention(m *mention) error { sql.Named("source", m.Source), sql.Named("target", m.Target), ) + if err != nil { + return err + } } else { if m.NewSource != "" { m.Source = m.NewSource } - _ = a.db.insertWebmention(m, newStatus) + err = a.db.insertWebmention(m, newStatus) + if err != nil { + return err + } a.sendNotification(fmt.Sprintf("New webmention from %s to %s", m.Source, m.Target)) } return err @@ -223,8 +231,8 @@ func (m *mention) fillTitle(mf *microformats.Microformat) { func (m *mention) fillContent(mf *microformats.Microformat) { if contents, ok := mf.Properties["content"]; ok && len(contents) > 0 { if content, ok := contents[0].(map[string]string); ok { - if contentValue, ok := content["value"]; ok { - m.Content = strings.TrimSpace(contentValue) + if contentHTML, ok := content["html"]; ok { + m.Content = cleanHTMLText(contentHTML) } } } diff --git a/webmentionVerification_test.go b/webmentionVerification_test.go new file mode 100644 index 0000000..a60066f --- /dev/null +++ b/webmentionVerification_test.go @@ -0,0 +1,53 @@ +package main + +import ( + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_verifyMention(t *testing.T) { + + testHtmlBytes, err := os.ReadFile("testdata/wmtest.html") + require.NoError(t, err) + testHtml := string(testHtmlBytes) + + mockClient := &fakeHttpClient{} + mockClient.setFakeResponse(http.StatusOK, testHtml) + + app := &goBlog{ + httpClient: mockClient, + cfg: &config{ + Db: &configDb{ + File: filepath.Join(t.TempDir(), "test.db"), + }, + Server: &configServer{ + PublicAddress: "https://example.org", + }, + }, + } + + _ = app.initDatabase(false) + app.initComponents(false) + + m := &mention{ + Source: "https://example.net/articles/micropub-crossposting-to-twitter-and-enabling-tweetstorms", + Target: "https://example.org/articles/micropub-syndication-targets-and-crossposting-to-mastodon", + } + + err = app.verifyMention(m) + require.NoError(t, err) + + require.Equal(t, "https://example.org/articles/micropub-syndication-targets-and-crossposting-to-mastodon", m.Target) + require.Equal(t, "https://example.net/articles/micropub-crossposting-to-twitter-and-enabling-tweetstorms", m.Source) + require.Equal(t, "Micropub, Crossposting to Twitter, and Enabling “Tweetsto…", m.Title) + require.Equal(t, "I’ve previously talked about how I crosspost from this blog to my Mastodon account without the need for a third-party service, and how I leverage WordPress’s hook system to even enable toot threading. In this post, I’m going to really quickly explain my (extremely similar) Twitter setup. (Note: I don’t actually syndicate this blog’s posts to Twitter, but I do use this very setup on another site of mine.) I liked the idea of a dead-simple Twitter plugin, so I forked my Mastodon plugin and twea…", m.Content) + require.Equal(t, "Test Blogger", m.Author) + + err = app.verifyMention(m) + require.NoError(t, err) + +}