mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-02-11 23:30:16 +00:00
simplify GetParseMentionFunc
This commit is contained in:
parent
5e993f6799
commit
8ac9001df2
|
@ -21,13 +21,14 @@
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
|
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc {
|
func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc {
|
||||||
|
@ -38,91 +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)
|
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
|
username, domain, err := util.ExtractNamestringParts(targetAccount)
|
||||||
// -- we can guarantee this from the regex that targetAccounts should have been derived from.
|
if err != nil {
|
||||||
// But we still need to do a bit of fiddling to get what we need here -- the username and domain (if given).
|
return nil, fmt.Errorf("couldn't extract namestring parts from %s: %s", targetAccount, err)
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var mentionedAccount *gtsmodel.Account
|
var mentionedAccount *gtsmodel.Account
|
||||||
|
if domain == "" || domain == config.GetHost() || domain == config.GetAccountDomain() {
|
||||||
if local {
|
|
||||||
localAccount, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
localAccount, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mentionedAccount = localAccount
|
mentionedAccount = localAccount
|
||||||
} else {
|
} else {
|
||||||
remoteAccount := >smodel.Account{}
|
var requestingUsername string
|
||||||
|
if originAccount.Domain == "" {
|
||||||
where := []db.Where{
|
requestingUsername = originAccount.Username
|
||||||
{
|
}
|
||||||
Key: "username",
|
remoteAccount, err := federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
|
||||||
Value: username,
|
RequestingUsername: requestingUsername,
|
||||||
CaseInsensitive: true,
|
RemoteAccountUsername: username,
|
||||||
},
|
RemoteAccountHost: domain,
|
||||||
{
|
})
|
||||||
Key: "domain",
|
if err != nil {
|
||||||
Value: domain,
|
return nil, fmt.Errorf("error dereferencing account: %s", err)
|
||||||
CaseInsensitive: true,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := dbConn.GetWhere(ctx, where, remoteAccount)
|
// we were able to resolve it!
|
||||||
if err == nil {
|
mentionedAccount = remoteAccount
|
||||||
// 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 just don't have the account, so try getting it.
|
|
||||||
var requestingUsername string
|
|
||||||
if originAccount.Domain == "" {
|
|
||||||
requestingUsername = originAccount.Username
|
|
||||||
}
|
|
||||||
resolvedAccount, err := federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
|
|
||||||
RequestingUsername: requestingUsername,
|
|
||||||
RemoteAccountUsername: username,
|
|
||||||
RemoteAccountHost: domain,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error dereferencing account: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// we were able to resolve it!
|
|
||||||
mentionedAccount = resolvedAccount
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mentionID, err := id.NewRandomULID()
|
mentionID, err := id.NewRandomULID()
|
||||||
|
|
Loading…
Reference in a new issue