From 1f38a42c771d058e1458c9e4d10319266a881b1d Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sat, 1 May 2021 20:37:52 +0200 Subject: [PATCH] if no picture found in microformat data, revert to anonymous pic --- app/mf/microformats.go | 2 +- app/webmention/recv/receive.go | 28 +++++++++++++++++----------- app/webmention/recv/receive_test.go | 22 +++++++++++++++++----- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/mf/microformats.go b/app/mf/microformats.go index 1b1d8d7..d61de58 100644 --- a/app/mf/microformats.go +++ b/app/mf/microformats.go @@ -36,7 +36,7 @@ type IndiewebAuthor struct { Picture string `json:"picture"` } -func (ia *IndiewebAuthor) Anonymize() { +func (ia *IndiewebAuthor) AnonymizePicture() { ia.Picture = fmt.Sprintf("/pictures/%s", Anonymous) } diff --git a/app/webmention/recv/receive.go b/app/webmention/recv/receive.go index 860aa7c..c0fb725 100644 --- a/app/webmention/recv/receive.go +++ b/app/webmention/recv/receive.go @@ -57,17 +57,7 @@ func (recv *Receiver) processSourceBody(body string, wm mf.Mention) { data := microformats.Parse(strings.NewReader(body), wm.SourceUrl()) indieweb := recv.convertBodyToIndiewebData(body, wm, data) - if indieweb.Author.Picture != "" { - err := recv.saveAuthorPictureLocally(indieweb) - if err != nil { - log.Error().Err(err).Str("url", indieweb.Author.Picture).Msg("Failed to save picture. Reverting to anonymous") - indieweb.Author.Anonymize() - - if err == errWontDownloadBecauseOfPrivacy { - indieweb.Author.AnonymizeName() - } - } - } + recv.processAuthorPicture(indieweb) key, err := recv.Repo.Save(wm, indieweb) if err != nil { @@ -76,6 +66,22 @@ func (recv *Receiver) processSourceBody(body string, wm mf.Mention) { log.Info().Str("key", key).Msg("OK: Webmention processed.") } +func (recv *Receiver) processAuthorPicture(indieweb *mf.IndiewebData) { + if indieweb.Author.Picture != "" { + err := recv.saveAuthorPictureLocally(indieweb) + if err != nil { + log.Error().Err(err).Str("url", indieweb.Author.Picture).Msg("Failed to save picture. Reverting to anonymous") + indieweb.Author.AnonymizePicture() + + if err == errWontDownloadBecauseOfPrivacy { + indieweb.Author.AnonymizeName() + } + } + } else { + indieweb.Author.AnonymizePicture() + } +} + func (recv *Receiver) convertBodyToIndiewebData(body string, wm mf.Mention, mfRoot *microformats.Data) *mf.IndiewebData { hEntry := mf.HEntry(mfRoot) hCard := mf.HCard(mfRoot) diff --git a/app/webmention/recv/receive_test.go b/app/webmention/recv/receive_test.go index bf17065..9948c2c 100644 --- a/app/webmention/recv/receive_test.go +++ b/app/webmention/recv/receive_test.go @@ -174,7 +174,7 @@ func TestReceive(t *testing.T) { Source: "https://brainbaking.com/valid-nonindieweb-source.html", Target: "https://brainbaking.com/valid-indieweb-target.html", }, - json: `{"author":{"name":"https://brainbaking.com/valid-nonindieweb-source.html", "picture":""}, "content":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "name":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "published":"2020-01-01T12:30:00+00:00", "source":"https://brainbaking.com/valid-nonindieweb-source.html", "target":"https://brainbaking.com/valid-indieweb-target.html", "type":"mention", "url":"https://brainbaking.com/valid-nonindieweb-source.html"}`, + json: `{"author":{"name":"https://brainbaking.com/valid-nonindieweb-source.html", "picture":"/pictures/anonymous"}, "content":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "name":"Diablo 2 Twenty Years Later: A Retrospective | Jefklaks Codex", "published":"2020-01-01T12:30:00+00:00", "source":"https://brainbaking.com/valid-nonindieweb-source.html", "target":"https://brainbaking.com/valid-indieweb-target.html", "type":"mention", "url":"https://brainbaking.com/valid-nonindieweb-source.html"}`, }, } @@ -254,7 +254,7 @@ func TestProcessSourceBodyAnonymizesBothAuthorPictureAndNameIfComingFromSilo(t * Target: "https://brainbaking.com/", } repo := db.NewMentionRepo(conf) - receiver := &Receiver{ + recv := &Receiver{ Conf: conf, Repo: repo, } @@ -262,7 +262,7 @@ func TestProcessSourceBodyAnonymizesBothAuthorPictureAndNameIfComingFromSilo(t * src, err := ioutil.ReadFile("../../../mocks/valid-bridgy-source.html") assert.NoError(t, err) - receiver.processSourceBody(string(src), wm) + recv.processSourceBody(string(src), wm) savedMention := repo.Get(wm) assert.Equal(t, "Anonymous", savedMention.Author.Name) @@ -275,11 +275,23 @@ func TestProcessSourceBodyAbortsIfNoMentionOfTargetFoundInSourceHtml(t *testing. Target: "https://jefklakscodex.com/articles", } repo := db.NewMentionRepo(conf) - receiver := &Receiver{ + recv := &Receiver{ Conf: conf, Repo: repo, } - receiver.processSourceBody("my nice body", wm) + recv.processSourceBody("my nice body", wm) assert.Empty(t, repo.Get(wm)) } + +func TestProcessAuthorPictureAnonymizesIfEmpty(t *testing.T) { + recv := &Receiver{} + indieweb := &mf.IndiewebData{ + Author: mf.IndiewebAuthor{ + Picture: "", + }, + } + recv.processAuthorPicture(indieweb) + + assert.Equal(t, "/pictures/anonymous", indieweb.Author.Picture) +}