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 +++++++++++++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 7 deletions(-)
(limited to 'internal/text/common.go')
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
}
--
cgit v1.2.3