mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2026-01-03 02:03:17 -06:00
[feature] Dereference remote mentions when the account is not already known (#442)
* remove mention util function from db * add ParseMentionFunc to gtsmodel * add parseMentionFunc to processor * refactor search to simplify it a bit * add parseMentionFunc to account * add parseMentionFunc to status * some renaming for clarity * test dereference of unknown mentioned account
This commit is contained in:
parent
983e696bd6
commit
37d310f981
16 changed files with 420 additions and 183 deletions
|
|
@ -87,10 +87,11 @@ type processor struct {
|
|||
formatter text.Formatter
|
||||
db db.DB
|
||||
federator federation.Federator
|
||||
parseMention gtsmodel.ParseMentionFunc
|
||||
}
|
||||
|
||||
// New returns a new account processor.
|
||||
func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator) Processor {
|
||||
func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator, parseMention gtsmodel.ParseMentionFunc) Processor {
|
||||
return &processor{
|
||||
tc: tc,
|
||||
mediaManager: mediaManager,
|
||||
|
|
@ -100,5 +101,6 @@ func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauth
|
|||
formatter: text.NewFormatter(db),
|
||||
db: db,
|
||||
federator: federator,
|
||||
parseMention: parseMention,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/media"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/account"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
||||
|
|
@ -88,7 +89,7 @@ func (suite *AccountStandardTestSuite) SetupTest() {
|
|||
suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage, suite.mediaManager)
|
||||
suite.sentEmails = make(map[string]string)
|
||||
suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails)
|
||||
suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, suite.fromClientAPIChan, suite.federator)
|
||||
suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, suite.fromClientAPIChan, suite.federator, processing.GetParseMentionFunc(suite.db, suite.federator))
|
||||
testrig.StandardDBSetup(suite.db, nil)
|
||||
testrig.StandardStorageSetup(suite.storage, "../../../testrig/media")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,10 +199,14 @@ func (p *processor) processNote(ctx context.Context, note string, accountID stri
|
|||
return "", err
|
||||
}
|
||||
|
||||
mentionStrings := util.DeriveMentionsFromText(note)
|
||||
mentions, err := p.db.MentionStringsToMentions(ctx, mentionStrings, accountID, "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
mentionStrings := util.DeriveMentionNamesFromText(note)
|
||||
mentions := []*gtsmodel.Mention{}
|
||||
for _, mentionString := range mentionStrings {
|
||||
mention, err := p.parseMention(ctx, mentionString, accountID, "")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
mentions = append(mentions, mention)
|
||||
}
|
||||
|
||||
// TODO: support emojis in account notes
|
||||
|
|
|
|||
|
|
@ -267,12 +267,14 @@ func NewProcessor(
|
|||
storage *kv.KVStore,
|
||||
db db.DB,
|
||||
emailSender email.Sender) Processor {
|
||||
|
||||
fromClientAPI := make(chan messages.FromClientAPI, 1000)
|
||||
fromFederator := make(chan messages.FromFederator, 1000)
|
||||
parseMentionFunc := GetParseMentionFunc(db, federator)
|
||||
|
||||
statusProcessor := status.New(db, tc, fromClientAPI)
|
||||
statusProcessor := status.New(db, tc, fromClientAPI, parseMentionFunc)
|
||||
streamingProcessor := streaming.New(db, oauthServer)
|
||||
accountProcessor := account.New(db, tc, mediaManager, oauthServer, fromClientAPI, federator)
|
||||
accountProcessor := account.New(db, tc, mediaManager, oauthServer, fromClientAPI, federator, parseMentionFunc)
|
||||
adminProcessor := admin.New(db, tc, mediaManager, fromClientAPI)
|
||||
mediaProcessor := mediaProcessor.New(db, tc, mediaManager, federator.TransportController(), storage)
|
||||
userProcessor := user.New(db, emailSender)
|
||||
|
|
|
|||
|
|
@ -194,20 +194,15 @@ func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Au
|
|||
// we got a db.ErrNoEntries, so we just don't have the account locally stored -- check if we can dereference it
|
||||
if resolve {
|
||||
// we're allowed to resolve it so let's try
|
||||
|
||||
// first we need to webfinger the remote account to convert the username and domain into the activitypub URI for the account
|
||||
acctURI, err := p.federator.FingerRemoteAccount(ctx, authed.Account.Username, username, domain)
|
||||
if err != nil {
|
||||
// something went wrong doing the webfinger lookup so we can't process the request
|
||||
return nil, fmt.Errorf("searchAccountByMention: error fingering remote account with username %s and domain %s: %s", username, domain, err)
|
||||
return nil, fmt.Errorf("error fingering remote account with username %s and domain %s: %s", username, domain, err)
|
||||
}
|
||||
|
||||
// we don't have it locally so try and dereference it
|
||||
account, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("searchAccountByMention: error dereferencing account with uri %s: %s", acctURI.String(), err)
|
||||
}
|
||||
return account, nil
|
||||
// return the attempt to get the remove account
|
||||
return p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true, true)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
|
|
|||
|
|
@ -74,15 +74,17 @@ type processor struct {
|
|||
filter visibility.Filter
|
||||
formatter text.Formatter
|
||||
fromClientAPI chan messages.FromClientAPI
|
||||
parseMention gtsmodel.ParseMentionFunc
|
||||
}
|
||||
|
||||
// New returns a new status processor.
|
||||
func New(db db.DB, tc typeutils.TypeConverter, fromClientAPI chan messages.FromClientAPI) Processor {
|
||||
func New(db db.DB, tc typeutils.TypeConverter, fromClientAPI chan messages.FromClientAPI, parseMention gtsmodel.ParseMentionFunc) Processor {
|
||||
return &processor{
|
||||
tc: tc,
|
||||
db: db,
|
||||
filter: visibility.NewFilter(db),
|
||||
formatter: text.NewFormatter(db),
|
||||
fromClientAPI: fromClientAPI,
|
||||
parseMention: parseMention,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,11 +19,16 @@
|
|||
package status_test
|
||||
|
||||
import (
|
||||
"codeberg.org/gruf/go-store/kv"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/media"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/status"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
||||
"github.com/superseriousbusiness/gotosocial/testrig"
|
||||
)
|
||||
|
|
@ -32,6 +37,10 @@ type StatusStandardTestSuite struct {
|
|||
suite.Suite
|
||||
db db.DB
|
||||
typeConverter typeutils.TypeConverter
|
||||
tc transport.Controller
|
||||
storage *kv.KVStore
|
||||
mediaManager media.Manager
|
||||
federator federation.Federator
|
||||
fromClientAPIChan chan messages.FromClientAPI
|
||||
|
||||
// standard suite models
|
||||
|
|
@ -62,17 +71,23 @@ func (suite *StatusStandardTestSuite) SetupSuite() {
|
|||
}
|
||||
|
||||
func (suite *StatusStandardTestSuite) SetupTest() {
|
||||
testrig.InitTestLog()
|
||||
testrig.InitTestConfig()
|
||||
testrig.InitTestLog()
|
||||
|
||||
suite.db = testrig.NewTestDB()
|
||||
suite.typeConverter = testrig.NewTestTypeConverter(suite.db)
|
||||
suite.fromClientAPIChan = make(chan messages.FromClientAPI, 100)
|
||||
suite.status = status.New(suite.db, suite.typeConverter, suite.fromClientAPIChan)
|
||||
suite.tc = testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db)
|
||||
suite.storage = testrig.NewTestStorage()
|
||||
suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage)
|
||||
suite.federator = testrig.NewTestFederator(suite.db, suite.tc, suite.storage, suite.mediaManager)
|
||||
suite.status = status.New(suite.db, suite.typeConverter, suite.fromClientAPIChan, processing.GetParseMentionFunc(suite.db, suite.federator))
|
||||
|
||||
testrig.StandardDBSetup(suite.db, nil)
|
||||
testrig.StandardDBSetup(suite.db, suite.testAccounts)
|
||||
testrig.StandardStorageSetup(suite.storage, "../../../testrig/media")
|
||||
}
|
||||
|
||||
func (suite *StatusStandardTestSuite) TearDownTest() {
|
||||
testrig.StandardDBTeardown(suite.db)
|
||||
testrig.StandardStorageTeardown(suite.storage)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,10 +23,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/text"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
|
@ -192,27 +192,30 @@ func (p *processor) ProcessLanguage(ctx context.Context, form *apimodel.Advanced
|
|||
}
|
||||
|
||||
func (p *processor) ProcessMentions(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
||||
menchies := []string{}
|
||||
gtsMenchies, err := p.db.MentionStringsToMentions(ctx, util.DeriveMentionsFromText(form.Status), accountID, status.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating mentions from status: %s", err)
|
||||
}
|
||||
for _, menchie := range gtsMenchies {
|
||||
menchieID, err := id.NewRandomULID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
menchie.ID = menchieID
|
||||
mentionedAccountNames := util.DeriveMentionNamesFromText(form.Status)
|
||||
mentions := []*gtsmodel.Mention{}
|
||||
mentionIDs := []string{}
|
||||
|
||||
if err := p.db.Put(ctx, menchie); err != nil {
|
||||
return fmt.Errorf("error putting mentions in db: %s", err)
|
||||
for _, mentionedAccountName := range mentionedAccountNames {
|
||||
gtsMention, err := p.parseMention(ctx, mentionedAccountName, accountID, status.ID)
|
||||
if err != nil {
|
||||
logrus.Errorf("ProcessMentions: error parsing mention %s from status: %s", mentionedAccountName, err)
|
||||
continue
|
||||
}
|
||||
menchies = append(menchies, menchie.ID)
|
||||
|
||||
if err := p.db.Put(ctx, gtsMention); err != nil {
|
||||
logrus.Errorf("ProcessMentions: error putting mention in db: %s", err)
|
||||
}
|
||||
|
||||
mentions = append(mentions, gtsMention)
|
||||
mentionIDs = append(mentionIDs, gtsMention.ID)
|
||||
}
|
||||
|
||||
// add full populated gts menchies to the status for passing them around conveniently
|
||||
status.Mentions = gtsMenchies
|
||||
status.Mentions = mentions
|
||||
// add just the ids of the mentioned accounts to the status for putting in the db
|
||||
status.MentionIDs = menchies
|
||||
status.MentionIDs = mentionIDs
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
151
internal/processing/util.go
Normal file
151
internal/processing/util.go
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
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 processing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
)
|
||||
|
||||
func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc {
|
||||
return func(ctx context.Context, targetAccount string, originAccountID string, statusID string) (*gtsmodel.Mention, error) {
|
||||
// get the origin account first since we'll need it to create the mention
|
||||
originAccount, err := dbConn.GetAccountByID(ctx, originAccountID)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
var mentionedAccount *gtsmodel.Account
|
||||
|
||||
if local {
|
||||
localAccount, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mentionedAccount = localAccount
|
||||
} else {
|
||||
remoteAccount := >smodel.Account{}
|
||||
|
||||
where := []db.Where{
|
||||
{
|
||||
Key: "username",
|
||||
Value: username,
|
||||
CaseInsensitive: true,
|
||||
},
|
||||
{
|
||||
Key: "domain",
|
||||
Value: domain,
|
||||
CaseInsensitive: true,
|
||||
},
|
||||
}
|
||||
|
||||
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 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()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return >smodel.Mention{
|
||||
ID: mentionID,
|
||||
StatusID: statusID,
|
||||
OriginAccountID: originAccount.ID,
|
||||
OriginAccountURI: originAccount.URI,
|
||||
TargetAccountID: mentionedAccount.ID,
|
||||
NameString: targetAccount,
|
||||
TargetAccountURI: mentionedAccount.URI,
|
||||
TargetAccountURL: mentionedAccount.URL,
|
||||
OriginAccount: mentionedAccount,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue