mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-10 10:08:08 -06:00
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
This commit is contained in:
parent
071eca20ce
commit
2dc9fc1626
713 changed files with 98694 additions and 22704 deletions
|
|
@ -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 := >smodel.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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 />")
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue