From c85c63680d136a8c28acb109c4f930292fe2a260 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Sat, 5 Jun 2021 12:47:30 +0200 Subject: [PATCH] mention regex better but not 100% there --- internal/db/pg/pg.go | 7 +++-- internal/gtsmodel/mention.go | 4 +++ .../processing/synchronous/status/create.go | 5 ++++ .../processing/synchronous/status/util.go | 26 +++++++++++++++++++ internal/timeline/manager.go | 1 + internal/timeline/timeline.go | 17 ++++++------ internal/util/regexes.go | 2 +- 7 files changed, 50 insertions(+), 12 deletions(-) diff --git a/internal/db/pg/pg.go b/internal/db/pg/pg.go index 15360bc..b029b10 100644 --- a/internal/db/pg/pg.go +++ b/internal/db/pg/pg.go @@ -1300,12 +1300,14 @@ func (ps *postgresService) MentionStringsToMentions(targetAccounts []string, ori // okay we're good now, we can start pulling accounts out of the database mentionedAccount := >smodel.Account{} var err error + + // match username + account, case insensitive if local { // local user -- should have a null domain - err = ps.conn.Model(mentionedAccount).Where("username = ?", username).Where("? IS NULL", pg.Ident("domain")).Select() + err = ps.conn.Model(mentionedAccount).Where("LOWER(?) = LOWER(?)", pg.Ident("username"), username).Where("? IS NULL", pg.Ident("domain")).Select() } else { // remote user -- should have domain defined - err = ps.conn.Model(mentionedAccount).Where("username = ?", username).Where("? = ?", pg.Ident("domain"), domain).Select() + err = ps.conn.Model(mentionedAccount).Where("LOWER(?) = LOWER(?)", pg.Ident("username"), username).Where("LOWER(?) = LOWER(?)", pg.Ident("domain"), domain).Select() } if err != nil { @@ -1326,6 +1328,7 @@ func (ps *postgresService) MentionStringsToMentions(targetAccounts []string, ori TargetAccountID: mentionedAccount.ID, NameString: a, MentionedAccountURI: mentionedAccount.URI, + MentionedAccountURL: mentionedAccount.URL, GTSAccount: mentionedAccount, }) } diff --git a/internal/gtsmodel/mention.go b/internal/gtsmodel/mention.go index 3abe9c9..013e9cc 100644 --- a/internal/gtsmodel/mention.go +++ b/internal/gtsmodel/mention.go @@ -56,6 +56,10 @@ type Mention struct { // // This will not be put in the database, it's just for convenience. MentionedAccountURI string `pg:"-"` + // MentionedAccountURL is the web url of the user mentioned. + // + // This will not be put in the database, it's just for convenience. + MentionedAccountURL string `pg:"-"` // A pointer to the gtsmodel account of the mentioned account. GTSAccount *Account `pg:"-"` } diff --git a/internal/processing/synchronous/status/create.go b/internal/processing/synchronous/status/create.go index e90855a..a5aa7a4 100644 --- a/internal/processing/synchronous/status/create.go +++ b/internal/processing/synchronous/status/create.go @@ -16,6 +16,7 @@ func (p *processor) Create(account *gtsmodel.Account, application *gtsmodel.Appl thisStatusID := uuid.NewString() thisStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, thisStatusID) thisStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, thisStatusID) + newStatus := >smodel.Status{ ID: thisStatusID, URI: thisStatusURI, @@ -66,6 +67,10 @@ func (p *processor) Create(account *gtsmodel.Account, application *gtsmodel.Appl return nil, gtserror.NewErrorInternalError(err) } + if err := p.processContent(form, account.ID, newStatus); err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + // put the new status in the database, generating an ID for it in the process if err := p.db.Put(newStatus); err != nil { return nil, gtserror.NewErrorInternalError(err) diff --git a/internal/processing/synchronous/status/util.go b/internal/processing/synchronous/status/util.go index 71ecac6..0f2e9f6 100644 --- a/internal/processing/synchronous/status/util.go +++ b/internal/processing/synchronous/status/util.go @@ -3,6 +3,7 @@ package status import ( "errors" "fmt" + "strings" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -228,3 +229,28 @@ func (p *processor) processEmojis(form *apimodel.AdvancedStatusCreateForm, accou status.Emojis = emojis return nil } + +func (p *processor) processContent(form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error { + if form.Status == "" { + status.Content = "" + return nil + } + + // surround the whole status in '

' + content := fmt.Sprintf(`

%s

`, form.Status) + + // format mentions nicely + for _, menchie := range status.GTSMentions { + targetAccount := >smodel.Account{} + if err := p.db.GetByID(menchie.TargetAccountID, targetAccount); err == nil { + mentionContent := fmt.Sprintf(`@%s`, targetAccount.URL, targetAccount.Username) + content = strings.ReplaceAll(content, menchie.NameString, mentionContent) + } + } + + // replace newlines with breaks + content = strings.ReplaceAll(content, "\n", "
") + + status.Content = content + return nil +} diff --git a/internal/timeline/manager.go b/internal/timeline/manager.go index c226922..65259b1 100644 --- a/internal/timeline/manager.go +++ b/internal/timeline/manager.go @@ -54,6 +54,7 @@ type Manager interface { // It should already be established before calling this function that the status/post actually belongs in the timeline! Ingest(status *gtsmodel.Status, timelineAccountID string) error // IngestAndPrepare takes one status and indexes it into the timeline for the given account ID, and then immediately prepares it for serving. + // This is useful in cases where we know the status will need to be shown at the top of a user's timeline immediately (eg., a new status is created). // // It should already be established before calling this function that the status/post actually belongs in the timeline! IngestAndPrepare(status *gtsmodel.Status, timelineAccountID string) error diff --git a/internal/timeline/timeline.go b/internal/timeline/timeline.go index 3898eb6..c8c2b90 100644 --- a/internal/timeline/timeline.go +++ b/internal/timeline/timeline.go @@ -68,8 +68,8 @@ type Timeline interface { // PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline. PrepareXFromTop(amount int) error - // PrepareXFromIndex instrucst the timeline to prepare the next amount of entries for serialization, from index onwards. - PrepareXFromIndex(amount int, index int) error + // PrepareXFromPosition instrucst the timeline to prepare the next amount of entries for serialization, from position onwards. + PrepareXFromPosition(amount int, position int) error // IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property, // and then immediately prepares it. IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error @@ -111,11 +111,11 @@ func NewTimeline(accountID string, db db.DB, typeConverter typeutils.TypeConvert } } -func (t *timeline) PrepareXFromIndex(amount int, index int) error { +func (t *timeline) PrepareXFromPosition(amount int, desiredPosition int) error { t.Lock() defer t.Unlock() - var indexed int + var position int var prepared int var preparing bool for e := t.postIndex.data.Front(); e != nil; e = e.Next() { @@ -125,12 +125,11 @@ func (t *timeline) PrepareXFromIndex(amount int, index int) error { } if !preparing { - // we haven't hit the index we need to prepare from yet - if indexed == index { + // we haven't hit the position we need to prepare from yet + if position == desiredPosition { preparing = true } - indexed = indexed + 1 - continue + position = position + 1 } else { if err := t.prepare(entry.statusID); err != nil { return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err) @@ -230,7 +229,7 @@ func (t *timeline) GetXFromIDOnwards(amount int, fromID string) ([]*apimodel.Sta // make sure we have enough posts prepared behind it to return what we're being asked for if t.preparedPosts.data.Len() < amount+position { - if err := t.PrepareXFromIndex(amount, position); err != nil { + if err := t.PrepareXFromPosition(amount, position); err != nil { return nil, err } } diff --git a/internal/util/regexes.go b/internal/util/regexes.go index 55773c3..efa0ecf 100644 --- a/internal/util/regexes.go +++ b/internal/util/regexes.go @@ -41,7 +41,7 @@ var ( mentionNameRegex = regexp.MustCompile(mentionNameRegexString) // mention regex can be played around with here: https://regex101.com/r/qwM9D3/1 - mentionFinderRegexString = `(?: |^|\W)(@[a-zA-Z0-9_]+(?:@[a-zA-Z0-9_\-\.]+)?)(?: |\n)` + mentionFinderRegexString = `(?: |^|\W)(@[a-zA-Z0-9_]+(?:@[a-zA-Z0-9_\-\.]+)?)(?:[^a-zA-Z0-9]|\W)` mentionFinderRegex = regexp.MustCompile(mentionFinderRegexString) // hashtag regex can be played with here: https://regex101.com/r/Vhy8pg/1