mention regex better but not 100% there

This commit is contained in:
tsmethurst 2021-06-05 12:47:30 +02:00
parent 197ef03ead
commit c85c63680d
7 changed files with 50 additions and 12 deletions

View File

@ -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 := &gtsmodel.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,
})
}

View File

@ -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:"-"`
}

View File

@ -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 := &gtsmodel.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)

View File

@ -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 '<p>'
content := fmt.Sprintf(`<p>%s</p>`, form.Status)
// format mentions nicely
for _, menchie := range status.GTSMentions {
targetAccount := &gtsmodel.Account{}
if err := p.db.GetByID(menchie.TargetAccountID, targetAccount); err == nil {
mentionContent := fmt.Sprintf(`<span class="h-card"><a href="%s" class="u-url mention">@<span>%s</span></a></span>`, targetAccount.URL, targetAccount.Username)
content = strings.ReplaceAll(content, menchie.NameString, mentionContent)
}
}
// replace newlines with breaks
content = strings.ReplaceAll(content, "\n", "<br />")
status.Content = content
return nil
}

View File

@ -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

View File

@ -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
}
}

View File

@ -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