mirror of
https://codeberg.org/superseriousbusiness/gotosocial.git
synced 2024-11-28 09:58:53 +03:00
rework mention replacement func (#258)
This commit is contained in:
parent
28b6ce59d6
commit
365c3bf5d7
2 changed files with 82 additions and 24 deletions
|
@ -90,26 +90,38 @@ func (f *formatter) ReplaceTags(ctx context.Context, in string, tags []*gtsmodel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *formatter) ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string {
|
func (f *formatter) ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string {
|
||||||
|
return regexes.MentionFinder.ReplaceAllStringFunc(in, func(match string) string {
|
||||||
|
// we have a match
|
||||||
|
matchTrimmed := strings.TrimSpace(match)
|
||||||
|
// check through mentions to find what we're matching
|
||||||
for _, menchie := range mentions {
|
for _, menchie := range mentions {
|
||||||
// make sure we have a target account, either by getting one pinned on the mention,
|
if strings.EqualFold(matchTrimmed, menchie.NameString) {
|
||||||
// or by pulling it from the database
|
// make sure we have an account attached to this mention
|
||||||
var targetAccount *gtsmodel.Account
|
if menchie.TargetAccount == nil {
|
||||||
if menchie.OriginAccount != nil {
|
|
||||||
// got it from the mention
|
|
||||||
targetAccount = menchie.OriginAccount
|
|
||||||
} else {
|
|
||||||
a, err := f.db.GetAccountByID(ctx, menchie.TargetAccountID)
|
a, err := f.db.GetAccountByID(ctx, menchie.TargetAccountID)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
// got it from the db
|
f.log.Errorf("error getting account with id %s from the db: %s", menchie.TargetAccountID, err)
|
||||||
targetAccount = a
|
return match
|
||||||
} else {
|
|
||||||
// couldn't get it so we can't do replacement
|
|
||||||
return in
|
|
||||||
}
|
}
|
||||||
|
menchie.TargetAccount = a
|
||||||
|
}
|
||||||
|
targetAccount := menchie.TargetAccount
|
||||||
|
|
||||||
|
// replace the mention with the formatted mention content
|
||||||
|
mentionContent := fmt.Sprintf(`<span class="h-card"><a href="%s" class="u-url mention">@<span>%s</span></a></span>`, targetAccount.URL, targetAccount.Username)
|
||||||
|
|
||||||
|
// in case the match picked up any previous space or newlines (thanks to the regex), include them as well
|
||||||
|
if strings.HasPrefix(match, " ") {
|
||||||
|
mentionContent = " " + mentionContent
|
||||||
|
} else if strings.HasPrefix(match, "\n") {
|
||||||
|
mentionContent = "\n" + mentionContent
|
||||||
}
|
}
|
||||||
|
|
||||||
mentionContent := fmt.Sprintf(`<span class="h-card"><a href="%s" class="u-url mention">@<span>%s</span></a></span>`, targetAccount.URL, targetAccount.Username)
|
// done
|
||||||
in = strings.ReplaceAll(in, menchie.NameString, mentionContent)
|
return mentionContent
|
||||||
}
|
}
|
||||||
return in
|
}
|
||||||
|
// the match wasn't in the list of mentions for whatever reason, so just return the match as we found it so nothing changes
|
||||||
|
return match
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,8 @@ package text_test
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/text"
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
||||||
|
@ -52,6 +52,22 @@ Text`
|
||||||
<a href="http://localhost:8080/tags/Hashtag" class="mention hashtag" rel="tag">#<span>Hashtag</span></a>
|
<a href="http://localhost:8080/tags/Hashtag" class="mention hashtag" rel="tag">#<span>Hashtag</span></a>
|
||||||
|
|
||||||
Text`
|
Text`
|
||||||
|
|
||||||
|
replaceMentionsWithLinkString = `Another test @foss_satan@fossbros-anonymous.io
|
||||||
|
|
||||||
|
https://fossbros-anonymous.io/@foss_satan/statuses/6675ee73-fccc-4562-a46a-3e8cd9798060`
|
||||||
|
|
||||||
|
replaceMentionsWithLinkStringExpected = `Another test <span class="h-card"><a href="http://fossbros-anonymous.io/@foss_satan" class="u-url mention">@<span>foss_satan</span></a></span>
|
||||||
|
|
||||||
|
https://fossbros-anonymous.io/@foss_satan/statuses/6675ee73-fccc-4562-a46a-3e8cd9798060`
|
||||||
|
|
||||||
|
replaceMentionsWithLinkSelfString = `Mentioning myself: @the_mighty_zork
|
||||||
|
|
||||||
|
and linking to my own status: https://localhost:8080/@the_mighty_zork/statuses/01FGXKJRX2PMERJQ9EQF8Y6HCR`
|
||||||
|
|
||||||
|
replaceMemtionsWithLinkSelfExpected = `Mentioning myself: <span class="h-card"><a href="http://localhost:8080/@the_mighty_zork" class="u-url mention">@<span>the_mighty_zork</span></a></span>
|
||||||
|
|
||||||
|
and linking to my own status: https://localhost:8080/@the_mighty_zork/statuses/01FGXKJRX2PMERJQ9EQF8Y6HCR`
|
||||||
)
|
)
|
||||||
|
|
||||||
type CommonTestSuite struct {
|
type CommonTestSuite struct {
|
||||||
|
@ -89,7 +105,7 @@ func (suite *CommonTestSuite) TestReplaceMentions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsString, foundMentions)
|
f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsString, foundMentions)
|
||||||
assert.Equal(suite.T(), replaceMentionsExpected, f)
|
suite.Equal(replaceMentionsExpected, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *CommonTestSuite) TestReplaceHashtags() {
|
func (suite *CommonTestSuite) TestReplaceHashtags() {
|
||||||
|
@ -99,7 +115,7 @@ func (suite *CommonTestSuite) TestReplaceHashtags() {
|
||||||
|
|
||||||
f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsString, foundTags)
|
f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsString, foundTags)
|
||||||
|
|
||||||
assert.Equal(suite.T(), replaceHashtagsExpected, f)
|
suite.Equal(replaceHashtagsExpected, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *CommonTestSuite) TestReplaceHashtagsAfterReplaceMentions() {
|
func (suite *CommonTestSuite) TestReplaceHashtagsAfterReplaceMentions() {
|
||||||
|
@ -109,7 +125,37 @@ func (suite *CommonTestSuite) TestReplaceHashtagsAfterReplaceMentions() {
|
||||||
|
|
||||||
f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsExpected, foundTags)
|
f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsExpected, foundTags)
|
||||||
|
|
||||||
assert.Equal(suite.T(), replaceHashtagsAfterMentionsExpected, f)
|
suite.Equal(replaceHashtagsAfterMentionsExpected, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *CommonTestSuite) TestReplaceMentionsWithLink() {
|
||||||
|
foundMentions := []*gtsmodel.Mention{
|
||||||
|
suite.testMentions["zork_mention_foss_satan"],
|
||||||
|
}
|
||||||
|
|
||||||
|
f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsWithLinkString, foundMentions)
|
||||||
|
suite.Equal(replaceMentionsWithLinkStringExpected, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *CommonTestSuite) TestReplaceMentionsWithLinkSelf() {
|
||||||
|
mentioningAccount := suite.testAccounts["local_account_1"]
|
||||||
|
|
||||||
|
foundMentions := []*gtsmodel.Mention{
|
||||||
|
{
|
||||||
|
ID: "01FGXKN5F815DVFVD53PN9NYM6",
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
|
StatusID: "01FGXKP0S5THQXFC1D9R141DDR",
|
||||||
|
OriginAccountID: mentioningAccount.ID,
|
||||||
|
TargetAccountID: mentioningAccount.ID,
|
||||||
|
NameString: "@the_mighty_zork",
|
||||||
|
TargetAccountURI: mentioningAccount.URI,
|
||||||
|
TargetAccountURL: mentioningAccount.URL,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsWithLinkSelfString, foundMentions)
|
||||||
|
suite.Equal(replaceMemtionsWithLinkSelfExpected, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCommonTestSuite(t *testing.T) {
|
func TestCommonTestSuite(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue