From 329a5e8144eea78e607c8a218ae78ae8f346f2e8 Mon Sep 17 00:00:00 2001
From: Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>
Date: Wed, 11 Aug 2021 16:54:54 +0200
Subject: Text duplication fix (#137)
* start testing text duplication
* tests
* fixes + tests
---
internal/text/common.go | 42 ++++++++++++---
internal/text/common_test.go | 116 ++++++++++++++++++++++++++++++++++++++++
internal/text/formatter_test.go | 1 +
internal/text/plain_test.go | 26 +++++++++
4 files changed, 178 insertions(+), 7 deletions(-)
create mode 100644 internal/text/common_test.go
(limited to 'internal/text')
diff --git a/internal/text/common.go b/internal/text/common.go
index 98ec892a7..4f0bad9dc 100644
--- a/internal/text/common.go
+++ b/internal/text/common.go
@@ -50,26 +50,54 @@ func postformat(in string) string {
func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string {
return util.HashtagFinderRegex.ReplaceAllStringFunc(in, func(match string) string {
+ // we have a match
+ matchTrimmed := strings.TrimSpace(match)
+ tagAsEntered := strings.Split(matchTrimmed, "#")[1]
+
+ // check through the tags to find what we're matching
for _, tag := range tags {
- if strings.TrimSpace(match) == fmt.Sprintf("#%s", tag.Name) {
- tagContent := fmt.Sprintf(`#%s`, tag.URL, tag.Name)
+
+ if strings.EqualFold(matchTrimmed, fmt.Sprintf("#%s", tag.Name)) {
+ // replace the #tag with the formatted tag content
+ tagContent := fmt.Sprintf(`#%s`, tag.URL, tagAsEntered)
+
+ // in case the match picked up any previous space or newlines (thanks to the regex), include them as well
if strings.HasPrefix(match, " ") {
tagContent = " " + tagContent
+ } else if strings.HasPrefix(match, "\n") {
+ tagContent = "\n" + tagContent
}
+
+ // done
return tagContent
}
}
- return in
+ // the match wasn't in the list of tags for whatever reason, so just return the match as we found it so nothing changes
+ return match
})
}
func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) string {
for _, menchie := range mentions {
- targetAccount := >smodel.Account{}
- if err := f.db.GetByID(menchie.TargetAccountID, targetAccount); err == nil {
- mentionContent := fmt.Sprintf(`@%s`, targetAccount.URL, targetAccount.Username)
- in = strings.ReplaceAll(in, menchie.NameString, mentionContent)
+ // make sure we have a target account, either by getting one pinned on the mention,
+ // or by pulling it from the database
+ var targetAccount *gtsmodel.Account
+ if menchie.GTSAccount != nil {
+ // got it from the mention
+ targetAccount = menchie.GTSAccount
+ } else {
+ a := >smodel.Account{}
+ if err := f.db.GetByID(menchie.TargetAccountID, a); err == nil {
+ // got it from the db
+ targetAccount = a
+ } else {
+ // couldn't get it so we can't do replacement
+ return in
+ }
}
+
+ mentionContent := fmt.Sprintf(`@%s`, targetAccount.URL, targetAccount.Username)
+ in = strings.ReplaceAll(in, menchie.NameString, mentionContent)
}
return in
}
diff --git a/internal/text/common_test.go b/internal/text/common_test.go
new file mode 100644
index 000000000..69fe7d446
--- /dev/null
+++ b/internal/text/common_test.go
@@ -0,0 +1,116 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see
this is a simple status that uses hashtag #welcome!
" + + moreComplex = `Another test @foss_satan@fossbros-anonymous.io + +#Hashtag + +Text` + moreComplexExpected = `Another test @foss_satan
#Hashtag
Text