summaryrefslogtreecommitdiff
path: root/internal/text
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-08-25 15:34:33 +0200
committerLibravatar GitHub <noreply@github.com>2021-08-25 15:34:33 +0200
commit2dc9fc1626507bb54417fc4a1920b847cafb27a2 (patch)
tree4ddeac479b923db38090aac8bd9209f3646851c1 /internal/text
parentManually approves followers (#146) (diff)
downloadgotosocial-2dc9fc1626507bb54417fc4a1920b847cafb27a2.tar.xz
Pg to bun (#148)
* start moving to bun * changing more stuff * more * and yet more * tests passing * seems stable now * more big changes * small fix * little fixes
Diffstat (limited to 'internal/text')
-rw-r--r--internal/text/common.go9
-rw-r--r--internal/text/common_test.go7
-rw-r--r--internal/text/formatter.go12
-rw-r--r--internal/text/link.go3
-rw-r--r--internal/text/link_test.go13
-rw-r--r--internal/text/markdown.go8
-rw-r--r--internal/text/markdown_test.go7
-rw-r--r--internal/text/plain.go9
-rw-r--r--internal/text/plain_test.go7
9 files changed, 43 insertions, 32 deletions
diff --git a/internal/text/common.go b/internal/text/common.go
index af77521dd..a8d585a09 100644
--- a/internal/text/common.go
+++ b/internal/text/common.go
@@ -19,6 +19,7 @@
package text
import (
+ "context"
"fmt"
"html"
"strings"
@@ -59,7 +60,7 @@ func postformat(in string) string {
return mini
}
-func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string {
+func (f *formatter) ReplaceTags(ctx context.Context, in string, tags []*gtsmodel.Tag) string {
return util.HashtagFinderRegex.ReplaceAllStringFunc(in, func(match string) string {
// we have a match
matchTrimmed := strings.TrimSpace(match)
@@ -88,7 +89,7 @@ func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string {
})
}
-func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) string {
+func (f *formatter) ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string {
for _, menchie := range mentions {
// make sure we have a target account, either by getting one pinned on the mention,
// or by pulling it from the database
@@ -97,8 +98,8 @@ func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) str
// got it from the mention
targetAccount = menchie.OriginAccount
} else {
- a := &gtsmodel.Account{}
- if err := f.db.GetByID(menchie.TargetAccountID, a); err == nil {
+ a, err := f.db.GetAccountByID(ctx, menchie.TargetAccountID)
+ if err == nil {
// got it from the db
targetAccount = a
} else {
diff --git a/internal/text/common_test.go b/internal/text/common_test.go
index 69fe7d446..174b79177 100644
--- a/internal/text/common_test.go
+++ b/internal/text/common_test.go
@@ -19,6 +19,7 @@
package text_test
import (
+ "context"
"testing"
"github.com/stretchr/testify/assert"
@@ -87,7 +88,7 @@ func (suite *CommonTestSuite) TestReplaceMentions() {
suite.testMentions["zork_mention_foss_satan"],
}
- f := suite.formatter.ReplaceMentions(replaceMentionsString, foundMentions)
+ f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsString, foundMentions)
assert.Equal(suite.T(), replaceMentionsExpected, f)
}
@@ -96,7 +97,7 @@ func (suite *CommonTestSuite) TestReplaceHashtags() {
suite.testTags["Hashtag"],
}
- f := suite.formatter.ReplaceTags(replaceMentionsString, foundTags)
+ f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsString, foundTags)
assert.Equal(suite.T(), replaceHashtagsExpected, f)
}
@@ -106,7 +107,7 @@ func (suite *CommonTestSuite) TestReplaceHashtagsAfterReplaceMentions() {
suite.testTags["Hashtag"],
}
- f := suite.formatter.ReplaceTags(replaceMentionsExpected, foundTags)
+ f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsExpected, foundTags)
assert.Equal(suite.T(), replaceHashtagsAfterMentionsExpected, f)
}
diff --git a/internal/text/formatter.go b/internal/text/formatter.go
index 39aaae559..769ecafbb 100644
--- a/internal/text/formatter.go
+++ b/internal/text/formatter.go
@@ -19,6 +19,8 @@
package text
import (
+ "context"
+
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -28,16 +30,16 @@ import (
// Formatter wraps some logic and functions for parsing statuses and other text input into nice html.
type Formatter interface {
// FromMarkdown parses an HTML text from a markdown-formatted text.
- FromMarkdown(md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string
+ FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string
// FromPlain parses an HTML text from a plaintext.
- FromPlain(plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string
+ FromPlain(ctx context.Context, plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string
// ReplaceTags takes a piece of text and a slice of tags, and returns the same text with the tags nicely formatted as hrefs.
- ReplaceTags(in string, tags []*gtsmodel.Tag) string
+ ReplaceTags(ctx context.Context, in string, tags []*gtsmodel.Tag) string
// ReplaceMentions takes a piece of text and a slice of mentions, and returns the same text with the mentions nicely formatted as hrefs.
- ReplaceMentions(in string, mentions []*gtsmodel.Mention) string
+ ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string
// ReplaceLinks takes a piece of text, finds all recognizable links in that text, and replaces them with hrefs.
- ReplaceLinks(in string) string
+ ReplaceLinks(ctx context.Context, in string) string
}
type formatter struct {
diff --git a/internal/text/link.go b/internal/text/link.go
index d42cc3b68..0a0f0c60d 100644
--- a/internal/text/link.go
+++ b/internal/text/link.go
@@ -19,6 +19,7 @@
package text
import (
+ "context"
"fmt"
"net/url"
@@ -82,7 +83,7 @@ func contains(urls []*url.URL, url *url.URL) bool {
// Note: because Go doesn't allow negative lookbehinds in regex, it's possible that an already-formatted
// href will end up double-formatted, if the text you pass here contains one or more hrefs already.
// To avoid this, you should sanitize any HTML out of text before you pass it into this function.
-func (f *formatter) ReplaceLinks(in string) string {
+func (f *formatter) ReplaceLinks(ctx context.Context, in string) string {
rxStrict, err := xurls.StrictMatchingScheme(schemes)
if err != nil {
panic(err)
diff --git a/internal/text/link_test.go b/internal/text/link_test.go
index 83c42f045..f8d6a1adc 100644
--- a/internal/text/link_test.go
+++ b/internal/text/link_test.go
@@ -19,6 +19,7 @@
package text_test
import (
+ "context"
"testing"
"github.com/stretchr/testify/assert"
@@ -94,7 +95,7 @@ func (suite *LinkTestSuite) TearDownTest() {
}
func (suite *LinkTestSuite) TestParseSimple() {
- f := suite.formatter.FromPlain(simple, nil, nil)
+ f := suite.formatter.FromPlain(context.Background(), simple, nil, nil)
assert.Equal(suite.T(), simpleExpected, f)
}
@@ -126,7 +127,7 @@ func (suite *LinkTestSuite) TestParseURLsFromText3() {
}
func (suite *LinkTestSuite) TestReplaceLinksFromText1() {
- replaced := suite.formatter.ReplaceLinks(text1)
+ replaced := suite.formatter.ReplaceLinks(context.Background(), text1)
assert.Equal(suite.T(), `
This is a text with some links in it. Here's link number one: <a href="https://example.org/link/to/something#fragment" rel="noopener">example.org/link/to/something#fragment</a>
@@ -141,7 +142,7 @@ really.cool.website <-- this one shouldn't be parsed as a link because it doesn'
}
func (suite *LinkTestSuite) TestReplaceLinksFromText2() {
- replaced := suite.formatter.ReplaceLinks(text2)
+ replaced := suite.formatter.ReplaceLinks(context.Background(), text2)
assert.Equal(suite.T(), `
this is one link: <a href="https://example.org" rel="noopener">example.org</a>
@@ -153,14 +154,14 @@ these should be deduplicated
func (suite *LinkTestSuite) TestReplaceLinksFromText3() {
// we know mailto links won't be replaced with hrefs -- we only accept https and http
- replaced := suite.formatter.ReplaceLinks(text3)
+ replaced := suite.formatter.ReplaceLinks(context.Background(), text3)
assert.Equal(suite.T(), `
here's a mailto link: mailto:whatever@test.org
`, replaced)
}
func (suite *LinkTestSuite) TestReplaceLinksFromText4() {
- replaced := suite.formatter.ReplaceLinks(text4)
+ replaced := suite.formatter.ReplaceLinks(context.Background(), text4)
assert.Equal(suite.T(), `
two similar links:
@@ -172,7 +173,7 @@ two similar links:
func (suite *LinkTestSuite) TestReplaceLinksFromText5() {
// we know this one doesn't work properly, which is why html should always be sanitized before being passed into the ReplaceLinks function
- replaced := suite.formatter.ReplaceLinks(text5)
+ replaced := suite.formatter.ReplaceLinks(context.Background(), text5)
assert.Equal(suite.T(), `
what happens when we already have a link within an href?
diff --git a/internal/text/markdown.go b/internal/text/markdown.go
index 5a7603615..eeeae0edf 100644
--- a/internal/text/markdown.go
+++ b/internal/text/markdown.go
@@ -19,21 +19,23 @@
package text
import (
+ "context"
+
"github.com/russross/blackfriday/v2"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
-func (f *formatter) FromMarkdown(md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
+func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
content := preformat(md)
// do the markdown parsing *first*
contentBytes := blackfriday.Run([]byte(content))
// format tags nicely
- content = f.ReplaceTags(string(contentBytes), tags)
+ content = f.ReplaceTags(ctx, string(contentBytes), tags)
// format mentions nicely
- content = f.ReplaceMentions(content, mentions)
+ content = f.ReplaceMentions(ctx, content, mentions)
return postformat(content)
}
diff --git a/internal/text/markdown_test.go b/internal/text/markdown_test.go
index 432e9a4ec..085f211d2 100644
--- a/internal/text/markdown_test.go
+++ b/internal/text/markdown_test.go
@@ -19,6 +19,7 @@
package text_test
import (
+ "context"
"fmt"
"testing"
@@ -92,13 +93,13 @@ func (suite *MarkdownTestSuite) TearDownTest() {
}
func (suite *MarkdownTestSuite) TestParseSimple() {
- s := suite.formatter.FromMarkdown(simpleMarkdown, nil, nil)
+ s := suite.formatter.FromMarkdown(context.Background(), simpleMarkdown, nil, nil)
suite.Equal(simpleMarkdownExpected, s)
}
func (suite *MarkdownTestSuite) TestParseWithCodeBlock() {
fmt.Println(withCodeBlock)
- s := suite.formatter.FromMarkdown(withCodeBlock, nil, nil)
+ s := suite.formatter.FromMarkdown(context.Background(), withCodeBlock, nil, nil)
suite.Equal(withCodeBlockExpected, s)
}
@@ -107,7 +108,7 @@ func (suite *MarkdownTestSuite) TestParseWithHashtag() {
suite.testTags["Hashtag"],
}
- s := suite.formatter.FromMarkdown(withHashtag, nil, foundTags)
+ s := suite.formatter.FromMarkdown(context.Background(), withHashtag, nil, foundTags)
suite.Equal(withHashtagExpected, s)
}
diff --git a/internal/text/plain.go b/internal/text/plain.go
index a44e02c80..34cc3fa06 100644
--- a/internal/text/plain.go
+++ b/internal/text/plain.go
@@ -19,26 +19,27 @@
package text
import (
+ "context"
"fmt"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
-func (f *formatter) FromPlain(plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
+func (f *formatter) FromPlain(ctx context.Context, plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string {
content := preformat(plain)
// sanitize any html elements
content = RemoveHTML(content)
// format links nicely
- content = f.ReplaceLinks(content)
+ content = f.ReplaceLinks(ctx, content)
// format tags nicely
- content = f.ReplaceTags(content, tags)
+ content = f.ReplaceTags(ctx, content, tags)
// format mentions nicely
- content = f.ReplaceMentions(content, mentions)
+ content = f.ReplaceMentions(ctx, content, mentions)
// replace newlines with breaks
content = strings.ReplaceAll(content, "\n", "<br />")
diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go
index 33c95234c..62c43406d 100644
--- a/internal/text/plain_test.go
+++ b/internal/text/plain_test.go
@@ -19,6 +19,7 @@
package text_test
import (
+ "context"
"fmt"
"testing"
@@ -74,7 +75,7 @@ func (suite *PlainTestSuite) TearDownTest() {
}
func (suite *PlainTestSuite) TestParseSimple() {
- f := suite.formatter.FromPlain(simple, nil, nil)
+ f := suite.formatter.FromPlain(context.Background(), simple, nil, nil)
assert.Equal(suite.T(), simpleExpected, f)
}
@@ -84,7 +85,7 @@ func (suite *PlainTestSuite) TestParseWithTag() {
suite.testTags["welcome"],
}
- f := suite.formatter.FromPlain(withTag, nil, foundTags)
+ f := suite.formatter.FromPlain(context.Background(), withTag, nil, foundTags)
assert.Equal(suite.T(), withTagExpected, f)
}
@@ -98,7 +99,7 @@ func (suite *PlainTestSuite) TestParseMoreComplex() {
suite.testMentions["zork_mention_foss_satan"],
}
- f := suite.formatter.FromPlain(moreComplex, foundMentions, foundTags)
+ f := suite.formatter.FromPlain(context.Background(), moreComplex, foundMentions, foundTags)
fmt.Println(f)