Fix janky hashtag detection. Closes #1686

This commit is contained in:
Gabe Kangas 2022-01-20 20:01:05 -08:00
parent bb3bbc0eaf
commit bd14b70679
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
3 changed files with 32 additions and 14 deletions

View File

@ -112,20 +112,18 @@ func SendPublicMessage(textContent string) error {
tagProp := streams.NewActivityStreamsTagProperty() tagProp := streams.NewActivityStreamsTagProperty()
// Iterate through the post text and find #Hashtags. hashtagStrings := utils.GetHashtagsFromText(originalContent)
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. for _, hashtag := range hashtagStrings {
tagHTML := getHashtagLinkHTMLFromTagString(tagWithoutHashtag) tagWithoutHashtag := strings.TrimPrefix(hashtag, "#")
textContent = strings.ReplaceAll(textContent, word, tagHTML)
// Create Hashtag object for the tag. // Replace the instances of the tag with a link to the tag page.
hashtag := apmodels.MakeHashtag(tagWithoutHashtag) tagHTML := getHashtagLinkHTMLFromTagString(tagWithoutHashtag)
tagProp.AppendTootHashtag(hashtag) textContent = strings.ReplaceAll(textContent, hashtag, tagHTML)
}
// Create Hashtag object for the tag.
hashtag := apmodels.MakeHashtag(tagWithoutHashtag)
tagProp.AppendTootHashtag(hashtag)
} }
activity, _, note, noteID := createBaseOutboundMessage(textContent) activity, _, note, noteID := createBaseOutboundMessage(textContent)

View File

@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"github.com/mssola/user_agent" "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. // GetHostnameFromURLString will return the hostname component from a URL object.
func GetHostnameFromURLString(s string) string { func GetHostnameFromURLString(s string) string {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
return "" return ""
} }
return u.Host 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)
}

View File

@ -1,6 +1,8 @@
package utils package utils
import "testing" import (
"testing"
)
func TestUserAgent(t *testing.T) { func TestUserAgent(t *testing.T) {
testAgents := []string{ 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.")
}
}