if no picture found in microformat data, revert to anonymous pic

This commit is contained in:
Wouter Groeneveld 2021-05-01 20:37:52 +02:00
parent c5dcb45de9
commit 1f38a42c77
3 changed files with 35 additions and 17 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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("<html>my nice body</html>", wm)
recv.processSourceBody("<html>my nice body</html>", 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)
}