From 862edb581d645561968fca0ba8bbacb57072dc74 Mon Sep 17 00:00:00 2001 From: boreq Date: Mon, 10 Oct 2022 16:30:58 +0200 Subject: [PATCH] Don't return errors for mentions which are an object Partially related to https://github.com/planetary-social/scuttlego/issues/69. Full fix probably involves actually scanning the markdown for links. It is unclear where this format is coming from and where it is specified. --- .../feeds/content/transport/mapping_about.go | 49 +++++++++++++++++++ .../feeds/content/transport/mapping_post.go | 24 ++------- .../content/transport/mapping_post_test.go | 19 +++++++ 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/service/domain/feeds/content/transport/mapping_about.go b/service/domain/feeds/content/transport/mapping_about.go index 2d5cdade..56ab8a19 100644 --- a/service/domain/feeds/content/transport/mapping_about.go +++ b/service/domain/feeds/content/transport/mapping_about.go @@ -2,8 +2,10 @@ package transport import ( "encoding/json" + "strings" "github.com/boreq/errors" + "github.com/hashicorp/go-multierror" "github.com/planetary-social/scuttlego/service/domain/feeds/content" "github.com/planetary-social/scuttlego/service/domain/refs" ) @@ -67,6 +69,53 @@ type transportAbout struct { Image json.RawMessage `json:"image"` } +func unmarshalMentions(j json.RawMessage) ([]refs.Blob, error) { + var returnErr error + + if len(j) == 0 { + return nil, nil + } + + var mentionsSlice []json.RawMessage + if err := json.Unmarshal(j, &mentionsSlice); err != nil { + returnErr = multierror.Append(returnErr, errors.Wrap(err, "slice unmarshal error")) + } else { + return unmarshalMentionsSlice(mentionsSlice) + } + + var mentionsMap map[string]json.RawMessage + if err := json.Unmarshal(j, &mentionsMap); err != nil { + returnErr = multierror.Append(returnErr, errors.Wrap(err, "map unmarshal error")) + } else { + return nil, nil + } + + return nil, returnErr +} + +func unmarshalMentionsSlice(slice []json.RawMessage) ([]refs.Blob, error) { + var blobs []refs.Blob + for _, rawJSON := range slice { + mention, err := unmarshalMention(rawJSON) + if err != nil { + return nil, errors.Wrap(err, "could not unmarshal a blob link") + } + + if !strings.HasPrefix(mention.Link, "&") { + continue + } + + blob, err := refs.NewBlob(mention.Link) + if err != nil { + return nil, errors.Wrap(err, "could not create a blob ref") + } + + blobs = append(blobs, blob) + } + + return blobs, nil +} + type mention struct { Link string `json:"link"` } diff --git a/service/domain/feeds/content/transport/mapping_post.go b/service/domain/feeds/content/transport/mapping_post.go index 62e112af..855be7c1 100644 --- a/service/domain/feeds/content/transport/mapping_post.go +++ b/service/domain/feeds/content/transport/mapping_post.go @@ -2,11 +2,9 @@ package transport import ( "encoding/json" - "strings" "github.com/boreq/errors" "github.com/planetary-social/scuttlego/service/domain/feeds/content" - "github.com/planetary-social/scuttlego/service/domain/refs" ) var postMapping = MessageContentMapping{ @@ -20,23 +18,9 @@ var postMapping = MessageContentMapping{ return nil, errors.Wrap(err, "json unmarshal failed") } - var blobs []refs.Blob - for _, rawJSON := range t.Mentions { - mention, err := unmarshalMention(rawJSON) - if err != nil { - return nil, errors.Wrap(err, "could not unmarshal a blob link") - } - - if !strings.HasPrefix(mention.Link, "&") { - continue - } - - blob, err := refs.NewBlob(mention.Link) - if err != nil { - return nil, errors.Wrap(err, "could not create a blob ref") - } - - blobs = append(blobs, blob) + blobs, err := unmarshalMentions(t.Mentions) + if err != nil { + return nil, errors.Wrap(err, "could not unmarshal mentions") } return content.NewPost(blobs) @@ -46,5 +30,5 @@ var postMapping = MessageContentMapping{ type transportPost struct { messageContentType // todo this is stupid - Mentions []json.RawMessage `json:"mentions"` + Mentions json.RawMessage `json:"mentions"` } diff --git a/service/domain/feeds/content/transport/mapping_post_test.go b/service/domain/feeds/content/transport/mapping_post_test.go index bb0da4f8..2b5fc53e 100644 --- a/service/domain/feeds/content/transport/mapping_post_test.go +++ b/service/domain/feeds/content/transport/mapping_post_test.go @@ -19,6 +19,14 @@ func TestMappingPostUnmarshal(t *testing.T) { Content string ExpectedPost msgcontents.Post }{ + { + Name: "no_mentions", + Content: `{ + "type": "post", + "text": "YES WE CAN! :heart: :smiley_cat:" + }`, + ExpectedPost: postWithoutBlobs, + }, { Name: "complex", Content: `{ @@ -53,6 +61,17 @@ func TestMappingPostUnmarshal(t *testing.T) { }`, ExpectedPost: postWithoutBlobs, }, + { + Name: "mentions_which_are_a_map", + Content: `{ + "type": "post", + "text": "a new photo from #dweb-camp 2022! ![photo.bmp](&O0h21NiGLLmjCF1kD2xWllvPExwe6t5P+F7YK3HAX4g=.sha256)", + "mentions": { + "0":{"name":"photo.bmp","type": "image/bmp"} + } + }`, + ExpectedPost: postWithoutBlobs, // todo probably actually scan the markdown + }, } for _, testCase := range testCases {