[chore] Webfinger rework (#627)

* move finger to dereferencer

* totally break GetRemoteAccount

* start reworking finger func a bit

* start reworking getRemoteAccount a bit

* move mention parts to namestring

* rework webfingerget

* use util function to extract webfinger parts

* use accountDomain

* rework finger again, final form

* just a real nasty commit, the worst

* remove refresh from account

* use new ASRepToAccount signature

* fix incorrect debug call

* fix for new getRemoteAccount

* rework GetRemoteAccount

* start updating tests to remove repetition

* break a lot of tests
Move shared test logic into the testrig,
rather than having it scattered all over
the place. This allows us to just mock
the transport controller once, and have
all tests use it (unless they need not to
for some other reason).

* fix up tests to use main mock httpclient

* webfinger only if necessary

* cheeky linting with the lads

* update mentionName regex
recognize instance accounts

* don't finger instance accounts

* test webfinger part extraction

* increase default worker count to 4 per cpu

* don't repeat regex parsing

* final search for discovered accountDomain

* be more permissive in namestring lookup

* add more extraction tests

* simplify GetParseMentionFunc

* skip long search if local account

* fix broken test
This commit is contained in:
tobi
2022-06-11 11:01:34 +02:00
committed by GitHub
parent 694a490589
commit dfdc473cef
62 changed files with 993 additions and 824 deletions

View File

@ -21,12 +21,14 @@ package processing
import (
"context"
"fmt"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc {
@ -37,98 +39,35 @@ func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.
return nil, fmt.Errorf("couldn't get mention origin account with id %s", originAccountID)
}
// A mentioned account looks like "@test@example.org" or just "@test" for a local account
// -- we can guarantee this from the regex that targetAccounts should have been derived from.
// But we still need to do a bit of fiddling to get what we need here -- the username and domain (if given).
// 1. trim off the first @
trimmed := strings.TrimPrefix(targetAccount, "@")
// 2. split the username and domain
split := strings.Split(trimmed, "@")
// 3. if it's length 1 it's a local account, length 2 means remote, anything else means something is wrong
var local bool
switch len(split) {
case 1:
local = true
case 2:
local = false
default:
return nil, fmt.Errorf("mentioned account format '%s' was not valid", targetAccount)
}
var username, domain string
username = split[0]
if !local {
domain = split[1]
}
// 4. check we now have a proper username and domain
if username == "" || (!local && domain == "") {
return nil, fmt.Errorf("username or domain for '%s' was nil", targetAccount)
username, domain, err := util.ExtractNamestringParts(targetAccount)
if err != nil {
return nil, fmt.Errorf("couldn't extract namestring parts from %s: %s", targetAccount, err)
}
var mentionedAccount *gtsmodel.Account
if local {
if domain == "" || domain == config.GetHost() || domain == config.GetAccountDomain() {
localAccount, err := dbConn.GetLocalAccountByUsername(ctx, username)
if err != nil {
return nil, err
}
mentionedAccount = localAccount
} else {
remoteAccount := &gtsmodel.Account{}
where := []db.Where{
{
Key: "username",
Value: username,
CaseInsensitive: true,
},
{
Key: "domain",
Value: domain,
CaseInsensitive: true,
},
var requestingUsername string
if originAccount.Domain == "" {
requestingUsername = originAccount.Username
}
remoteAccount, err := federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
RequestingUsername: requestingUsername,
RemoteAccountUsername: username,
RemoteAccountHost: domain,
})
if err != nil {
return nil, fmt.Errorf("error dereferencing account: %s", err)
}
err := dbConn.GetWhere(ctx, where, remoteAccount)
if err == nil {
// the account was already in the database
mentionedAccount = remoteAccount
} else {
// we couldn't get it from the database
if err != db.ErrNoEntries {
// a serious error has happened so bail
return nil, fmt.Errorf("error getting account with username '%s' and domain '%s': %s", username, domain, err)
}
// we were able to resolve it!
mentionedAccount = remoteAccount
// We just don't have the account, so try webfingering it.
//
// If the mention originates from our instance we should use the username of the origin account to do the dereferencing,
// otherwise we should just use our instance account (that is, provide an empty string), since obviously we can't use
// a remote account to do remote dereferencing!
var fingeringUsername string
if originAccount.Domain == "" {
fingeringUsername = originAccount.Username
}
acctURI, err := federator.FingerRemoteAccount(ctx, fingeringUsername, username, domain)
if err != nil {
// something went wrong doing the webfinger lookup so we can't process the request
return nil, fmt.Errorf("error fingering remote account with username %s and domain %s: %s", username, domain, err)
}
resolvedAccount, err := federator.GetRemoteAccount(ctx, fingeringUsername, acctURI, true, true)
if err != nil {
return nil, fmt.Errorf("error dereferencing account with uri %s: %s", acctURI.String(), err)
}
// we were able to resolve it!
mentionedAccount = resolvedAccount
}
}
mentionID, err := id.NewRandomULID()