move mention parts to namestring

This commit is contained in:
tsmethurst 2022-05-31 12:26:31 +02:00
parent c746b00731
commit 863231cc97
5 changed files with 63 additions and 21 deletions

View file

@ -574,7 +574,7 @@ func ExtractMention(i Mentionable) (*gtsmodel.Mention, error) {
}
// just make sure the mention string is valid so we can handle it properly later on...
_, _, err = util.ExtractMentionParts(mentionString)
_, _, err = util.ExtractNamestringParts(mentionString)
if err != nil {
return nil, err
}

View file

@ -54,7 +54,7 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
var foundOne bool
// check if the query is something like @whatever_username@example.org -- this means it's a remote account
if _, domain, err := util.ExtractMentionParts(searchQuery.Query); err == nil && domain != "" {
if _, domain, err := util.ExtractNamestringParts(searchQuery.Query); err == nil && domain != "" {
l.Debug("search term is a mention, looking it up...")
foundAccount, err := p.searchAccountByMention(ctx, authed, searchQuery.Query, searchQuery.Resolve)
if err == nil && foundAccount != nil {
@ -158,7 +158,7 @@ func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth,
func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, mention string, resolve bool) (*gtsmodel.Account, error) {
// query is for a remote account
username, domain, err := util.ExtractMentionParts(mention)
username, domain, err := util.ExtractNamestringParts(mention)
if err != nil {
return nil, fmt.Errorf("searchAccountByMention: error extracting mention parts: %s", err)
}

View file

@ -205,7 +205,7 @@ func dereferenceByNodeInfo(c context.Context, t *transport, iri *url.URL) (*gtsm
// see if there's a 'name' in the map
if name, present := v["name"]; present {
// name could be just a username, or could be a mention string eg @whatever@aaaa.com
username, _, err := util.ExtractMentionParts(name)
username, _, err := util.ExtractNamestringParts(name)
if err == nil {
// it was a mention string
contactAccountUsername = username

View file

@ -0,0 +1,59 @@
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package util
import (
"fmt"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/regexes"
)
// ExtractNamestringParts extracts the username test_user and
// the domain example.org from a string like @test_user@example.org.
//
// If nothing is matched, it will return an error.
func ExtractNamestringParts(mention string) (username, host string, err error) {
matches := regexes.MentionName.FindStringSubmatch(mention)
switch len(matches) {
case 2:
return matches[1], "", nil
case 3:
return matches[1], matches[2], nil
default:
return "", "", fmt.Errorf("couldn't match mention %s", mention)
}
}
// ExtractWebfingerParts returns username test_user and
// domain example.org from a string like acct:test_user@example.org,
// or acct:@test_user@example.org.
//
// If nothing is extracted, it will return an error.
func ExtractWebfingerParts(webfinger string) (username, host string, err error) {
// remove the acct: prefix if it's present
webfinger = strings.TrimPrefix(webfinger, "acct:")
// prepend an @ if necessary
if webfinger[0] != '@' {
webfinger = "@" + webfinger
}
return ExtractNamestringParts(webfinger)
}

View file

@ -19,7 +19,6 @@
package util
import (
"fmt"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/regexes"
@ -59,19 +58,3 @@ func DeriveEmojisFromText(text string) []string {
}
return UniqueStrings(emojis)
}
// ExtractMentionParts extracts the username test_user and the domain example.org
// from a mention string like @test_user@example.org.
//
// If nothing is matched, it will return an error.
func ExtractMentionParts(mention string) (username, domain string, err error) {
matches := regexes.MentionName.FindStringSubmatch(mention)
switch len(matches) {
case 2:
return matches[1], "", nil
case 3:
return matches[1], matches[2], nil
default:
return "", "", fmt.Errorf("couldn't match mention %s", mention)
}
}