diff options
author | 2023-10-31 12:05:17 +0100 | |
---|---|---|
committer | 2023-10-31 11:05:17 +0000 | |
commit | 51d0a0bba5cc7c5c170b2137f169d65e49966c84 (patch) | |
tree | d668b90bafab3c66b79c2a0f9b39f8702186a4c8 /internal/ap | |
parent | [bugfix] Allow blocked accounts to show in precise search (#2321) (diff) | |
download | gotosocial-51d0a0bba5cc7c5c170b2137f169d65e49966c84.tar.xz |
[bugfix] Relax `Mention` parsing, allowing either href or name (#2320)
Diffstat (limited to 'internal/ap')
-rw-r--r-- | internal/ap/extract.go | 27 | ||||
-rw-r--r-- | internal/ap/extractmentions_test.go | 99 |
2 files changed, 111 insertions, 15 deletions
diff --git a/internal/ap/extract.go b/internal/ap/extract.go index 6d224e9a8..74c4497b3 100644 --- a/internal/ap/extract.go +++ b/internal/ap/extract.go @@ -32,7 +32,6 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/text" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // ExtractObjects will extract object vocab.Types from given implementing interface. @@ -841,27 +840,27 @@ func ExtractMentions(i WithTag) ([]*gtsmodel.Mention, error) { // ExtractMention extracts a minimal gtsmodel.Mention from a Mentionable. func ExtractMention(i Mentionable) (*gtsmodel.Mention, error) { + // See if a name has been set in the + // format `@someone@example.org`. nameString := ExtractName(i) - if nameString == "" { - return nil, gtserror.New("name prop empty") - } - - // Ensure namestring is valid so we - // can handle it properly later on. - if _, _, err := util.ExtractNamestringParts(nameString); err != nil { - return nil, err - } // The href prop should be the AP URI - // of the target account. + // of the target account; it could also + // be the URL, but we'll check this later. + var href string hrefProp := i.GetActivityStreamsHref() - if hrefProp == nil || !hrefProp.IsIRI() { - return nil, gtserror.New("no href prop") + if hrefProp != nil && hrefProp.IsIRI() { + href = hrefProp.GetIRI().String() + } + + // One of nameString and hrefProp must be set. + if nameString == "" && href == "" { + return nil, gtserror.Newf("neither Name nor Href were set") } return >smodel.Mention{ NameString: nameString, - TargetAccountURI: hrefProp.GetIRI().String(), + TargetAccountURI: href, }, nil } diff --git a/internal/ap/extractmentions_test.go b/internal/ap/extractmentions_test.go index fbfee34f5..e49347e5c 100644 --- a/internal/ap/extractmentions_test.go +++ b/internal/ap/extractmentions_test.go @@ -21,14 +21,16 @@ import ( "testing" "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/activity/streams" "github.com/superseriousbusiness/gotosocial/internal/ap" + "github.com/superseriousbusiness/gotosocial/testrig" ) type ExtractMentionsTestSuite struct { APTestSuite } -func (suite *ExtractMentionsTestSuite) TestExtractMentions() { +func (suite *ExtractMentionsTestSuite) TestExtractMentionsFromNote() { note := suite.noteWithMentions1 mentions, err := ap.ExtractMentions(note) @@ -44,6 +46,101 @@ func (suite *ExtractMentionsTestSuite) TestExtractMentions() { suite.Equal("https://gts.superseriousbusiness.org/users/f0x", m2.TargetAccountURI) } +func (suite *ExtractMentionsTestSuite) TestExtractMentions() { + newMention := func(nameString string, href string) ap.Mentionable { + mention := streams.NewActivityStreamsMention() + + if nameString != "" { + nameProp := streams.NewActivityStreamsNameProperty() + nameProp.AppendXMLSchemaString(nameString) + mention.SetActivityStreamsName(nameProp) + } + + if href != "" { + hrefProp := streams.NewActivityStreamsHrefProperty() + hrefProp.SetIRI(testrig.URLMustParse(href)) + mention.SetActivityStreamsHref(hrefProp) + } + + return mention + } + + type test struct { + nameString string + href string + expectedNameString string + expectedHref string + expectedErr string + } + + for i, t := range []test{ + { + // Mention with both Name and Href set, should be fine. + nameString: "@someone@example.org", + href: "https://example.org/@someone", + expectedNameString: "@someone@example.org", + expectedHref: "https://example.org/@someone", + expectedErr: "", + }, + { + // Mention with just Href set, should be fine. + nameString: "", + href: "https://example.org/@someone", + expectedNameString: "", + expectedHref: "https://example.org/@someone", + expectedErr: "", + }, + { + // Mention with just Name set, should be fine. + nameString: "@someone@example.org", + href: "", + expectedNameString: "@someone@example.org", + expectedHref: "", + expectedErr: "", + }, + { + // Mention with nothing set, not fine! + nameString: "", + href: "", + expectedNameString: "", + expectedHref: "", + expectedErr: "ExtractMention: neither Name nor Href were set", + }, + } { + apMention := newMention(t.nameString, t.href) + mention, err := ap.ExtractMention(apMention) + + if err != nil { + if errString := err.Error(); errString != t.expectedErr { + suite.Fail("", + "test %d expected error %s, got %s", + i+1, t.expectedErr, errString, + ) + } + continue + } else if t.expectedErr != "" { + suite.Fail("", + "test %d expected error %s, got no error", + i+1, t.expectedErr, + ) + } + + if mention.NameString != t.expectedNameString { + suite.Fail("", + "test %d expected nameString %s, got %s", + i+1, t.expectedNameString, mention.NameString, + ) + } + + if mention.TargetAccountURI != t.expectedHref { + suite.Fail("", + "test %d expected href %s, got %s", + i+1, t.expectedHref, mention.TargetAccountURI, + ) + } + } +} + func TestExtractMentionsTestSuite(t *testing.T) { suite.Run(t, &ExtractMentionsTestSuite{}) } |