mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 13:22:25 -05:00 
			
		
		
		
	mention regex better but not 100% there
This commit is contained in:
		
					parent
					
						
							
								197ef03ead
							
						
					
				
			
			
				commit
				
					
						c85c63680d
					
				
			
		
					 7 changed files with 50 additions and 12 deletions
				
			
		|  | @ -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 | 		// okay we're good now, we can start pulling accounts out of the database | ||||||
| 		mentionedAccount := >smodel.Account{} | 		mentionedAccount := >smodel.Account{} | ||||||
| 		var err error | 		var err error | ||||||
|  | 
 | ||||||
|  | 		// match username + account, case insensitive | ||||||
| 		if local { | 		if local { | ||||||
| 			// local user -- should have a null domain | 			// 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 { | 		} else { | ||||||
| 			// remote user -- should have domain defined | 			// 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 { | 		if err != nil { | ||||||
|  | @ -1326,6 +1328,7 @@ func (ps *postgresService) MentionStringsToMentions(targetAccounts []string, ori | ||||||
| 			TargetAccountID:     mentionedAccount.ID, | 			TargetAccountID:     mentionedAccount.ID, | ||||||
| 			NameString:          a, | 			NameString:          a, | ||||||
| 			MentionedAccountURI: mentionedAccount.URI, | 			MentionedAccountURI: mentionedAccount.URI, | ||||||
|  | 			MentionedAccountURL: mentionedAccount.URL, | ||||||
| 			GTSAccount:          mentionedAccount, | 			GTSAccount:          mentionedAccount, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -56,6 +56,10 @@ type Mention struct { | ||||||
| 	// | 	// | ||||||
| 	// This will not be put in the database, it's just for convenience. | 	// This will not be put in the database, it's just for convenience. | ||||||
| 	MentionedAccountURI string `pg:"-"` | 	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. | 	// A pointer to the gtsmodel account of the mentioned account. | ||||||
| 	GTSAccount *Account `pg:"-"` | 	GTSAccount *Account `pg:"-"` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ func (p *processor) Create(account *gtsmodel.Account, application *gtsmodel.Appl | ||||||
| 	thisStatusID := uuid.NewString() | 	thisStatusID := uuid.NewString() | ||||||
| 	thisStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, thisStatusID) | 	thisStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, thisStatusID) | ||||||
| 	thisStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, thisStatusID) | 	thisStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, thisStatusID) | ||||||
|  | 
 | ||||||
| 	newStatus := >smodel.Status{ | 	newStatus := >smodel.Status{ | ||||||
| 		ID:                       thisStatusID, | 		ID:                       thisStatusID, | ||||||
| 		URI:                      thisStatusURI, | 		URI:                      thisStatusURI, | ||||||
|  | @ -66,6 +67,10 @@ func (p *processor) Create(account *gtsmodel.Account, application *gtsmodel.Appl | ||||||
| 		return nil, gtserror.NewErrorInternalError(err) | 		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 | 	// put the new status in the database, generating an ID for it in the process | ||||||
| 	if err := p.db.Put(newStatus); err != nil { | 	if err := p.db.Put(newStatus); err != nil { | ||||||
| 		return nil, gtserror.NewErrorInternalError(err) | 		return nil, gtserror.NewErrorInternalError(err) | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package status | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/db" | 	"github.com/superseriousbusiness/gotosocial/internal/db" | ||||||
|  | @ -228,3 +229,28 @@ func (p *processor) processEmojis(form *apimodel.AdvancedStatusCreateForm, accou | ||||||
| 	status.Emojis = emojis | 	status.Emojis = emojis | ||||||
| 	return nil | 	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 := >smodel.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 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -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! | 	// 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 | 	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. | 	// 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! | 	// 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 | 	IngestAndPrepare(status *gtsmodel.Status, timelineAccountID string) error | ||||||
|  |  | ||||||
|  | @ -68,8 +68,8 @@ type Timeline interface { | ||||||
| 
 | 
 | ||||||
| 	// PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline. | 	// PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline. | ||||||
| 	PrepareXFromTop(amount int) error | 	PrepareXFromTop(amount int) error | ||||||
| 	// PrepareXFromIndex instrucst the timeline to prepare the next amount of entries for serialization, from index onwards. | 	// PrepareXFromPosition instrucst the timeline to prepare the next amount of entries for serialization, from position onwards. | ||||||
| 	PrepareXFromIndex(amount int, index int) error | 	PrepareXFromPosition(amount int, position int) error | ||||||
| 	// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property, | 	// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property, | ||||||
| 	// and then immediately prepares it. | 	// and then immediately prepares it. | ||||||
| 	IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error | 	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() | 	t.Lock() | ||||||
| 	defer t.Unlock() | 	defer t.Unlock() | ||||||
| 
 | 
 | ||||||
| 	var indexed int | 	var position int | ||||||
| 	var prepared int | 	var prepared int | ||||||
| 	var preparing bool | 	var preparing bool | ||||||
| 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 	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 { | 		if !preparing { | ||||||
| 			// we haven't hit the index we need to prepare from yet | 			// we haven't hit the position we need to prepare from yet | ||||||
| 			if indexed == index { | 			if position == desiredPosition { | ||||||
| 				preparing = true | 				preparing = true | ||||||
| 			} | 			} | ||||||
| 			indexed = indexed + 1 | 			position = position + 1 | ||||||
| 			continue |  | ||||||
| 		} else { | 		} else { | ||||||
| 			if err := t.prepare(entry.statusID); err != nil { | 			if err := t.prepare(entry.statusID); err != nil { | ||||||
| 				return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err) | 				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 | 	// 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 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 | 			return nil, err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -41,7 +41,7 @@ var ( | ||||||
| 	mentionNameRegex = regexp.MustCompile(mentionNameRegexString) | 	mentionNameRegex = regexp.MustCompile(mentionNameRegexString) | ||||||
| 
 | 
 | ||||||
| 	// mention regex can be played around with here: https://regex101.com/r/qwM9D3/1 | 	// 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) | 	mentionFinderRegex       = regexp.MustCompile(mentionFinderRegexString) | ||||||
| 
 | 
 | ||||||
| 	// hashtag regex can be played with here: https://regex101.com/r/Vhy8pg/1 | 	// hashtag regex can be played with here: https://regex101.com/r/Vhy8pg/1 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue