summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabe Kangas <gabek@real-ity.com>2022-01-20 20:01:05 -0800
committerGabe Kangas <gabek@real-ity.com>2022-01-20 20:01:05 -0800
commitbd14b70679338ad01956496936817d2ca1e6b2fc (patch)
tree070c5c6a7f3758cb18b367ebdb2b1bd2998d6a5f
parentbb3bbc0eaf2d0be3232f62ce3061c5f4ce0cbbdf (diff)
Fix janky hashtag detection. Closes #1686
-rw-r--r--activitypub/outbox/outbox.go26
-rw-r--r--utils/utils.go8
-rw-r--r--utils/utils_test.go16
3 files changed, 34 insertions, 16 deletions
diff --git a/activitypub/outbox/outbox.go b/activitypub/outbox/outbox.go
index 53369a7af..cf9c2da46 100644
--- a/activitypub/outbox/outbox.go
+++ b/activitypub/outbox/outbox.go
@@ -112,20 +112,18 @@ func SendPublicMessage(textContent string) error {
tagProp := streams.NewActivityStreamsTagProperty()
- // Iterate through the post text and find #Hashtags.
- words := strings.Split(originalContent, " ")
- for _, word := range words {
- if strings.HasPrefix(word, "#") {
- tagWithoutHashtag := strings.TrimPrefix(word, "#")
-
- // Replace the instances of the tag with a link to the tag page.
- tagHTML := getHashtagLinkHTMLFromTagString(tagWithoutHashtag)
- textContent = strings.ReplaceAll(textContent, word, tagHTML)
-
- // Create Hashtag object for the tag.
- hashtag := apmodels.MakeHashtag(tagWithoutHashtag)
- tagProp.AppendTootHashtag(hashtag)
- }
+ hashtagStrings := utils.GetHashtagsFromText(originalContent)
+
+ for _, hashtag := range hashtagStrings {
+ tagWithoutHashtag := strings.TrimPrefix(hashtag, "#")
+
+ // Replace the instances of the tag with a link to the tag page.
+ tagHTML := getHashtagLinkHTMLFromTagString(tagWithoutHashtag)
+ textContent = strings.ReplaceAll(textContent, hashtag, tagHTML)
+
+ // Create Hashtag object for the tag.
+ hashtag := apmodels.MakeHashtag(tagWithoutHashtag)
+ tagProp.AppendTootHashtag(hashtag)
}
activity, _, note, noteID := createBaseOutboundMessage(textContent)
diff --git a/utils/utils.go b/utils/utils.go
index e4610c97c..e3cb121f4 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -10,6 +10,7 @@ import (
"os/exec"
"path"
"path/filepath"
+ "regexp"
"strings"
"github.com/mssola/user_agent"
@@ -321,10 +322,15 @@ func GetHostnameFromURL(u url.URL) string {
// GetHostnameFromURLString will return the hostname component from a URL object.
func GetHostnameFromURLString(s string) string {
u, err := url.Parse(s)
-
if err != nil {
return ""
}
return u.Host
}
+
+// GetHashtagsFromText returns all the #Hashtags from a string.
+func GetHashtagsFromText(text string) []string {
+ re := regexp.MustCompile(`#[a-zA-Z0-9_]+`)
+ return re.FindAllString(text, -1)
+}
diff --git a/utils/utils_test.go b/utils/utils_test.go
index ababc083a..ea41f48f6 100644
--- a/utils/utils_test.go
+++ b/utils/utils_test.go
@@ -1,6 +1,8 @@
package utils
-import "testing"
+import (
+ "testing"
+)
func TestUserAgent(t *testing.T) {
testAgents := []string{
@@ -16,3 +18,15 @@ func TestUserAgent(t *testing.T) {
}
}
}
+
+func TestGetHashtagsFromText(t *testing.T) {
+ text := `Some text with a #hashtag goes here.\n\n
+ Another #secondhashtag, goes here.\n\n
+ #thirdhashtag`
+
+ hashtags := GetHashtagsFromText(text)
+
+ if hashtags[0] != "#hashtag" || hashtags[1] != "#secondhashtag" || hashtags[2] != "#thirdhashtag" {
+ t.Error("Incorrect hashtags fetched from text.")
+ }
+}