From 40a5ceb2c2a170af09188ff599e6a17a9fef5f64 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 19 Nov 2021 22:53:25 +0100 Subject: [PATCH] Fix wm verification for special case --- testdata/colin.html | 377 +++++++++++++++++++++++++++++++++ webmentionVerification.go | 11 +- webmentionVerification_test.go | 43 ++++ 3 files changed, 430 insertions(+), 1 deletion(-) create mode 100644 testdata/colin.html diff --git a/testdata/colin.html b/testdata/colin.html new file mode 100644 index 0000000..1a1e80d --- /dev/null +++ b/testdata/colin.html @@ -0,0 +1,377 @@ + + + + + + Colin Walker - Nov 14, 2021 + + + + + + + + + + + + + + + + + + +
(b)log-insert imagen
+
+ + + + + +
+
+ +

14/11/2021 +

+ + + +
+
2021/11/14#p1
+
+
+# +0 comments: click to read or leave your own +

Lest we forget.

+

Lest we forget

+
+
+ +
+ Colin Walker + Colin Walker +
+
+
+ +
+ + + cancelDraft:   Publish: + media +
+
Leave a reply
+
+ + + + + +
+ +
+
+ +
+ Cancel comment + +
+
+ +
+ +
+
2021/11/14#p3
+
+
+ # + 0 comments: click to read or leave your own +

In reply to: jlelse’s Blog...

Congratulations on a year of GoBlog 👏

+

My year anniversary with (b)log-In is in January.

+
+
+ +
+ Colin Walker + Colin Walker +
+
+
+ +
+ + + cancelDraft:   Publish: + media +
+
Leave a reply
+
+ + + + + +
+ +
+
+ +
+ Cancel comment + +
+
+ +
+ +
+
2021/11/14#p2
+
+
+# +0 comments: click to read or leave your own +

Just had my flu jab for the winter. My Covid booster is due in just under a month.

+
+
+ +
+ Colin Walker + Colin Walker +
+
+
+ +
+ + + cancelDraft:   Publish: + media +
+
Leave a reply
+
+ + + + + +
+ +
+
+ +
+ Cancel comment + +
+
+ +
+ + +
+
+
+
+ + + + + + + + + + + + + + +
+ + About + +
+ + COLOPHON +
+ +
+ Colin Walker + Colin Walker + colin@colinwalker.blog +

+
+ + + + + + \ No newline at end of file diff --git a/webmentionVerification.go b/webmentionVerification.go index 6ea230c..2e266ea 100644 --- a/webmentionVerification.go +++ b/webmentionVerification.go @@ -260,7 +260,7 @@ func (m *mention) fill(mf *microformats.Microformat) bool { m.fillContent(mf) // Author m.fillAuthor(mf) - return true + return m.hasUrl } for _, mfc := range mf.Children { if m.fill(mfc) { @@ -271,6 +271,9 @@ func (m *mention) fill(mf *microformats.Microformat) bool { } func (m *mention) fillTitle(mf *microformats.Microformat) { + if m.Title != "" { + return + } if name, ok := mf.Properties["name"]; ok && len(name) > 0 { if title, ok := name[0].(string); ok { m.Title = strings.TrimSpace(title) @@ -279,6 +282,9 @@ func (m *mention) fillTitle(mf *microformats.Microformat) { } func (m *mention) fillContent(mf *microformats.Microformat) { + if m.Content != "" { + return + } if contents, ok := mf.Properties["content"]; ok && len(contents) > 0 { if content, ok := contents[0].(map[string]string); ok { if contentHTML, ok := content["html"]; ok { @@ -295,6 +301,9 @@ func (m *mention) fillContent(mf *microformats.Microformat) { } func (m *mention) fillAuthor(mf *microformats.Microformat) { + if m.Author != "" { + return + } if authors, ok := mf.Properties["author"]; ok && len(authors) > 0 { if author, ok := authors[0].(*microformats.Microformat); ok { if names, ok := author.Properties["name"]; ok && len(names) > 0 { diff --git a/webmentionVerification_test.go b/webmentionVerification_test.go index a5a8956..2ef8e78 100644 --- a/webmentionVerification_test.go +++ b/webmentionVerification_test.go @@ -101,3 +101,46 @@ func Test_verifyMentionBidgy(t *testing.T) { require.Equal(t, "comment test", m.Content) require.Equal(t, "m4rk", m.Author) } + +func Test_verifyMentionColin(t *testing.T) { + + testHtmlBytes, err := os.ReadFile("testdata/colin.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://jlelse.blog", + }, + }, + d: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // do nothing + }), + } + + _ = app.initDatabase(false) + app.initComponents(false) + + m := &mention{ + Source: "https://colinwalker.blog/?date=2021-11-14#p3", + Target: "https://jlelse.blog/micro/2021/11/2021-11-13-lrhvj", + } + + err = app.verifyMention(m) + require.NoError(t, err) + + require.Equal(t, "https://jlelse.blog/micro/2021/11/2021-11-13-lrhvj", m.Target) + require.Equal(t, "https://colinwalker.blog/?date=2021-11-14#p3", m.Source) + require.Equal(t, "https://colinwalker.blog/?date=2021-11-14#p3", m.Url) + require.True(t, strings.HasPrefix(m.Title, "Congratulations")) + require.True(t, strings.HasPrefix(m.Content, "Congratulations")) + require.Equal(t, "Colin Walker", m.Author) +}