Skip to content

Commit

Permalink
Don't return errors for mentions which are an object
Browse files Browse the repository at this point in the history
Partially related to #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.
  • Loading branch information
boreq committed Oct 15, 2022
1 parent 06b2e00 commit 862edb5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
49 changes: 49 additions & 0 deletions service/domain/feeds/content/transport/mapping_about.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"`
}
Expand Down
24 changes: 4 additions & 20 deletions service/domain/feeds/content/transport/mapping_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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"`
}
19 changes: 19 additions & 0 deletions service/domain/feeds/content/transport/mapping_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 862edb5

Please sign in to comment.