diff --git a/internal/ap/extract.go b/internal/ap/extract.go index 7d980f486..9417077f3 100644 --- a/internal/ap/extract.go +++ b/internal/ap/extract.go @@ -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 } diff --git a/internal/processing/search.go b/internal/processing/search.go index 1b0813fd9..0851a9fc8 100644 --- a/internal/processing/search.go +++ b/internal/processing/search.go @@ -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) } diff --git a/internal/transport/derefinstance.go b/internal/transport/derefinstance.go index 1acbcc364..00a7daf32 100644 --- a/internal/transport/derefinstance.go +++ b/internal/transport/derefinstance.go @@ -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 diff --git a/internal/util/namestring.go b/internal/util/namestring.go new file mode 100644 index 000000000..611a1c0eb --- /dev/null +++ b/internal/util/namestring.go @@ -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 . +*/ + +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) +} diff --git a/internal/util/statustools.go b/internal/util/statustools.go index 08032a8f4..b2b7fffa1 100644 --- a/internal/util/statustools.go +++ b/internal/util/statustools.go @@ -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) - } -}