fix mention extracting when no domain exists (usually intra-instance mentions) (#272)

* fix mention extracting when no domain exists (usually when intra-instance mentions)

Signed-off-by: kim <grufwub@gmail.com>

* fix search logic to match new mention matching logic

Signed-off-by: kim <grufwub@gmail.com>

* appease the linter :p

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2021-10-17 13:19:49 +01:00 committed by GitHub
parent 15621f5324
commit 7e4c3fa5c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 35 deletions

View file

@ -565,13 +565,10 @@ func ExtractMention(i Mentionable) (*gtsmodel.Mention, error) {
} }
// just make sure the mention string is valid so we can handle it properly later on... // just make sure the mention string is valid so we can handle it properly later on...
username, domain, err := util.ExtractMentionParts(mentionString) _, _, err = util.ExtractMentionParts(mentionString)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if username == "" || domain == "" {
return nil, errors.New("username or domain was empty")
}
mention.NameString = mentionString mention.NameString = mentionString
// the href prop should be the AP URI of a user we know, eg https://example.org/users/whatever_user // the href prop should be the AP URI of a user we know, eg https://example.org/users/whatever_user

View file

@ -53,7 +53,7 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
var foundOne bool var foundOne bool
// check if the query is something like @whatever_username@example.org -- this means it's a remote account // check if the query is something like @whatever_username@example.org -- this means it's a remote account
if !foundOne && util.IsMention(searchQuery.Query) { if _, domain, err := util.ExtractMentionParts(searchQuery.Query); err == nil && domain != "" {
l.Debug("search term is a mention, looking it up...") l.Debug("search term is a mention, looking it up...")
foundAccount, err := p.searchAccountByMention(ctx, authed, searchQuery.Query, searchQuery.Resolve) foundAccount, err := p.searchAccountByMention(ctx, authed, searchQuery.Query, searchQuery.Resolve)
if err == nil && foundAccount != nil { if err == nil && foundAccount != nil {
@ -64,25 +64,20 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
} }
// check if the query is a URI and just do a lookup for that, straight up // check if the query is a URI and just do a lookup for that, straight up
if uri, err := url.Parse(query); err == nil && !foundOne { if !foundOne {
if uri, err := url.Parse(query); err == nil {
// 1. check if it's a status // 1. check if it's a status
if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundStatus != nil { if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundStatus != nil {
foundStatuses = append(foundStatuses, foundStatus) foundStatuses = append(foundStatuses, foundStatus)
foundOne = true
l.Debug("got a status by searching by URI") l.Debug("got a status by searching by URI")
} }
// 2. check if it's an account // 2. check if it's an account
if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundAccount != nil { if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundAccount != nil {
foundAccounts = append(foundAccounts, foundAccount) foundAccounts = append(foundAccounts, foundAccount)
foundOne = true
l.Debug("got an account by searching by URI") l.Debug("got an account by searching by URI")
} }
} }
if !foundOne {
// we haven't found anything yet so search for text now
l.Debug("nothing found by mention or by URI, will fall back to searching by text now")
} }
/* /*

View file

@ -47,7 +47,7 @@
) )
var ( var (
mentionName = `^@(\w+)(?:@([a-zA-Z0-9_\-\.:]+)?)$` mentionName = `^@(\w+)(?:@([a-zA-Z0-9_\-\.:]+))?$`
// MentionName captures the username and domain part from a mention string // MentionName captures the username and domain part from a mention string
// such as @whatever_user@example.org, returning whatever_user and example.org (without the @ symbols) // such as @whatever_user@example.org, returning whatever_user and example.org (without the @ symbols)
MentionName = regexp.MustCompile(mentionName) MentionName = regexp.MustCompile(mentionName)

View file

@ -68,16 +68,12 @@ func DeriveEmojisFromText(text string) []string {
// If nothing is matched, it will return an error. // If nothing is matched, it will return an error.
func ExtractMentionParts(mention string) (username, domain string, err error) { func ExtractMentionParts(mention string) (username, domain string, err error) {
matches := regexes.MentionName.FindStringSubmatch(mention) matches := regexes.MentionName.FindStringSubmatch(mention)
if matches == nil || len(matches) != 3 { switch len(matches) {
err = fmt.Errorf("could't match mention %s", mention) case 2:
return return matches[1], "", nil
case 3:
return matches[1], matches[2], nil
default:
return "", "", fmt.Errorf("couldn't match mention %s", mention)
} }
username = matches[1]
domain = matches[2]
return
}
// IsMention returns true if the passed string looks like @whatever@example.org
func IsMention(mention string) bool {
return regexes.MentionName.MatchString(strings.ToLower(mention))
} }