mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-10 03:58:09 -06:00
Pg to bun (#148)
* start moving to bun * changing more stuff * more * and yet more * tests passing * seems stable now * more big changes * small fix * little fixes
This commit is contained in:
parent
071eca20ce
commit
2dc9fc1626
713 changed files with 98694 additions and 22704 deletions
|
|
@ -24,6 +24,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/go-fed/activity/streams"
|
||||
"github.com/go-fed/activity/streams/vocab"
|
||||
|
|
@ -34,18 +35,33 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
||||
)
|
||||
|
||||
func instanceAccount(account *gtsmodel.Account) bool {
|
||||
return strings.EqualFold(account.Username, account.Domain) ||
|
||||
account.FollowersURI == "" ||
|
||||
account.FollowingURI == "" ||
|
||||
(account.Username == "internal.fetch" && strings.Contains(account.Note, "internal service actor"))
|
||||
}
|
||||
|
||||
// EnrichRemoteAccount takes an account that's already been inserted into the database in a minimal form,
|
||||
// and populates it with additional fields, media, etc.
|
||||
//
|
||||
// EnrichRemoteAccount is mostly useful for calling after an account has been initially created by
|
||||
// the federatingDB's Create function, or during the federated authorization flow.
|
||||
func (d *deref) EnrichRemoteAccount(username string, account *gtsmodel.Account) (*gtsmodel.Account, error) {
|
||||
if err := d.PopulateAccountFields(account, username, false); err != nil {
|
||||
func (d *deref) EnrichRemoteAccount(ctx context.Context, username string, account *gtsmodel.Account) (*gtsmodel.Account, error) {
|
||||
|
||||
// if we're dealing with an instance account, we don't need to update anything
|
||||
if instanceAccount(account) {
|
||||
return account, nil
|
||||
}
|
||||
|
||||
if err := d.PopulateAccountFields(ctx, account, username, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := d.db.UpdateByID(account.ID, account); err != nil {
|
||||
return nil, fmt.Errorf("EnrichRemoteAccount: error updating account: %s", err)
|
||||
var err error
|
||||
account, err = d.db.UpdateAccount(ctx, account)
|
||||
if err != nil {
|
||||
d.log.Errorf("EnrichRemoteAccount: error updating account: %s", err)
|
||||
}
|
||||
|
||||
return account, nil
|
||||
|
|
@ -60,27 +76,27 @@ func (d *deref) EnrichRemoteAccount(username string, account *gtsmodel.Account)
|
|||
// the remote instance again.
|
||||
//
|
||||
// SIDE EFFECTS: remote account will be stored in the database, or updated if it already exists (and refresh is true).
|
||||
func (d *deref) GetRemoteAccount(username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) {
|
||||
func (d *deref) GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) {
|
||||
new := true
|
||||
|
||||
// check if we already have the account in our db
|
||||
maybeAccount, err := d.db.GetAccountByURI(remoteAccountID.String())
|
||||
maybeAccount, err := d.db.GetAccountByURI(ctx, remoteAccountID.String())
|
||||
if err == nil {
|
||||
// we've seen this account before so it's not new
|
||||
new = false
|
||||
if !refresh {
|
||||
// we're not being asked to refresh, but just in case we don't have the avatar/header cached yet....
|
||||
maybeAccount, err = d.EnrichRemoteAccount(username, maybeAccount)
|
||||
maybeAccount, err = d.EnrichRemoteAccount(ctx, username, maybeAccount)
|
||||
return maybeAccount, new, err
|
||||
}
|
||||
}
|
||||
|
||||
accountable, err := d.dereferenceAccountable(username, remoteAccountID)
|
||||
accountable, err := d.dereferenceAccountable(ctx, username, remoteAccountID)
|
||||
if err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error dereferencing accountable: %s", err)
|
||||
}
|
||||
|
||||
gtsAccount, err := d.typeConverter.ASRepresentationToAccount(accountable, refresh)
|
||||
gtsAccount, err := d.typeConverter.ASRepresentationToAccount(ctx, accountable, refresh)
|
||||
if err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error converting accountable to account: %s", err)
|
||||
}
|
||||
|
|
@ -93,23 +109,24 @@ func (d *deref) GetRemoteAccount(username string, remoteAccountID *url.URL, refr
|
|||
}
|
||||
gtsAccount.ID = ulid
|
||||
|
||||
if err := d.PopulateAccountFields(gtsAccount, username, refresh); err != nil {
|
||||
if err := d.PopulateAccountFields(ctx, gtsAccount, username, refresh); err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error populating further account fields: %s", err)
|
||||
}
|
||||
|
||||
if err := d.db.Put(gtsAccount); err != nil {
|
||||
if err := d.db.Put(ctx, gtsAccount); err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error putting new account: %s", err)
|
||||
}
|
||||
} else {
|
||||
// take the id we already have and do an update
|
||||
gtsAccount.ID = maybeAccount.ID
|
||||
|
||||
if err := d.PopulateAccountFields(gtsAccount, username, refresh); err != nil {
|
||||
if err := d.PopulateAccountFields(ctx, gtsAccount, username, refresh); err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error populating further account fields: %s", err)
|
||||
}
|
||||
|
||||
if err := d.db.UpdateByID(gtsAccount.ID, gtsAccount); err != nil {
|
||||
return nil, new, fmt.Errorf("FullyDereferenceAccount: error updating existing account: %s", err)
|
||||
gtsAccount, err = d.db.UpdateAccount(ctx, gtsAccount)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("EnrichRemoteAccount: error updating account: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,15 +137,15 @@ func (d *deref) GetRemoteAccount(username string, remoteAccountID *url.URL, refr
|
|||
// it finds as something that an account model can be constructed out of.
|
||||
//
|
||||
// Will work for Person, Application, or Service models.
|
||||
func (d *deref) dereferenceAccountable(username string, remoteAccountID *url.URL) (ap.Accountable, error) {
|
||||
func (d *deref) dereferenceAccountable(ctx context.Context, username string, remoteAccountID *url.URL) (ap.Accountable, error) {
|
||||
d.startHandshake(username, remoteAccountID)
|
||||
defer d.stopHandshake(username, remoteAccountID)
|
||||
|
||||
if blocked, err := d.blockedDomain(remoteAccountID.Host); blocked || err != nil {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, remoteAccountID.Host); blocked || err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: domain %s is blocked", remoteAccountID.Host)
|
||||
}
|
||||
|
||||
transport, err := d.transportController.NewTransportForUsername(username)
|
||||
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: transport err: %s", err)
|
||||
}
|
||||
|
|
@ -174,7 +191,7 @@ func (d *deref) dereferenceAccountable(username string, remoteAccountID *url.URL
|
|||
|
||||
// PopulateAccountFields populates any fields on the given account that weren't populated by the initial
|
||||
// dereferencing. This includes things like header and avatar etc.
|
||||
func (d *deref) PopulateAccountFields(account *gtsmodel.Account, requestingUsername string, refresh bool) error {
|
||||
func (d *deref) PopulateAccountFields(ctx context.Context, account *gtsmodel.Account, requestingUsername string, refresh bool) error {
|
||||
l := d.log.WithFields(logrus.Fields{
|
||||
"func": "PopulateAccountFields",
|
||||
"requestingUsername": requestingUsername,
|
||||
|
|
@ -184,17 +201,17 @@ func (d *deref) PopulateAccountFields(account *gtsmodel.Account, requestingUsern
|
|||
if err != nil {
|
||||
return fmt.Errorf("PopulateAccountFields: couldn't parse account URI %s: %s", account.URI, err)
|
||||
}
|
||||
if blocked, err := d.blockedDomain(accountURI.Host); blocked || err != nil {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil {
|
||||
return fmt.Errorf("PopulateAccountFields: domain %s is blocked", accountURI.Host)
|
||||
}
|
||||
|
||||
t, err := d.transportController.NewTransportForUsername(requestingUsername)
|
||||
t, err := d.transportController.NewTransportForUsername(ctx, requestingUsername)
|
||||
if err != nil {
|
||||
return fmt.Errorf("PopulateAccountFields: error getting transport for user: %s", err)
|
||||
}
|
||||
|
||||
// fetch the header and avatar
|
||||
if err := d.fetchHeaderAndAviForAccount(account, t, refresh); err != nil {
|
||||
if err := d.fetchHeaderAndAviForAccount(ctx, account, t, refresh); err != nil {
|
||||
// if this doesn't work, just skip it -- we can do it later
|
||||
l.Debugf("error fetching header/avi for account: %s", err)
|
||||
}
|
||||
|
|
@ -208,17 +225,17 @@ func (d *deref) PopulateAccountFields(account *gtsmodel.Account, requestingUsern
|
|||
// targetAccount's AvatarMediaAttachmentID and HeaderMediaAttachmentID will be updated as necessary.
|
||||
//
|
||||
// SIDE EFFECTS: remote header and avatar will be stored in local storage.
|
||||
func (d *deref) fetchHeaderAndAviForAccount(targetAccount *gtsmodel.Account, t transport.Transport, refresh bool) error {
|
||||
func (d *deref) fetchHeaderAndAviForAccount(ctx context.Context, targetAccount *gtsmodel.Account, t transport.Transport, refresh bool) error {
|
||||
accountURI, err := url.Parse(targetAccount.URI)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetchHeaderAndAviForAccount: couldn't parse account URI %s: %s", targetAccount.URI, err)
|
||||
}
|
||||
if blocked, err := d.blockedDomain(accountURI.Host); blocked || err != nil {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil {
|
||||
return fmt.Errorf("fetchHeaderAndAviForAccount: domain %s is blocked", accountURI.Host)
|
||||
}
|
||||
|
||||
if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) {
|
||||
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(t, >smodel.MediaAttachment{
|
||||
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{
|
||||
RemoteURL: targetAccount.AvatarRemoteURL,
|
||||
Avatar: true,
|
||||
}, targetAccount.ID)
|
||||
|
|
@ -229,7 +246,7 @@ func (d *deref) fetchHeaderAndAviForAccount(targetAccount *gtsmodel.Account, t t
|
|||
}
|
||||
|
||||
if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) {
|
||||
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(t, >smodel.MediaAttachment{
|
||||
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{
|
||||
RemoteURL: targetAccount.HeaderRemoteURL,
|
||||
Header: true,
|
||||
}, targetAccount.ID)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
package dereferencing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
|
@ -26,7 +27,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
func (d *deref) DereferenceAnnounce(announce *gtsmodel.Status, requestingUsername string) error {
|
||||
func (d *deref) DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error {
|
||||
if announce.BoostOf == nil || announce.BoostOf.URI == "" {
|
||||
// we can't do anything unfortunately
|
||||
return errors.New("DereferenceAnnounce: no URI to dereference")
|
||||
|
|
@ -36,16 +37,16 @@ func (d *deref) DereferenceAnnounce(announce *gtsmodel.Status, requestingUsernam
|
|||
if err != nil {
|
||||
return fmt.Errorf("DereferenceAnnounce: couldn't parse boosted status URI %s: %s", announce.BoostOf.URI, err)
|
||||
}
|
||||
if blocked, err := d.blockedDomain(boostedStatusURI.Host); blocked || err != nil {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, boostedStatusURI.Host); blocked || err != nil {
|
||||
return fmt.Errorf("DereferenceAnnounce: domain %s is blocked", boostedStatusURI.Host)
|
||||
}
|
||||
|
||||
// dereference statuses in the thread of the boosted status
|
||||
if err := d.DereferenceThread(requestingUsername, boostedStatusURI); err != nil {
|
||||
if err := d.DereferenceThread(ctx, requestingUsername, boostedStatusURI); err != nil {
|
||||
return fmt.Errorf("DereferenceAnnounce: error dereferencing thread of boosted status: %s", err)
|
||||
}
|
||||
|
||||
boostedStatus, _, _, err := d.GetRemoteStatus(requestingUsername, boostedStatusURI, false)
|
||||
boostedStatus, _, _, err := d.GetRemoteStatus(ctx, requestingUsername, boostedStatusURI, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("DereferenceAnnounce: error dereferencing remote status with id %s: %s", announce.BoostOf.URI, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
GoToSocial
|
||||
Copyright (C) 2021 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 dereferencing
|
||||
|
||||
import (
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
func (d *deref) blockedDomain(host string) (bool, error) {
|
||||
b := >smodel.DomainBlock{}
|
||||
err := d.db.GetWhere([]db.Where{{Key: "domain", Value: host, CaseInsensitive: true}}, b)
|
||||
if err == nil {
|
||||
// block exists
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if err == db.ErrNoEntries {
|
||||
// there are no entries so there's no block
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// there's an actual error
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -32,12 +32,12 @@ import (
|
|||
)
|
||||
|
||||
// DereferenceCollectionPage returns the activitystreams CollectionPage at the specified IRI, or an error if something goes wrong.
|
||||
func (d *deref) DereferenceCollectionPage(username string, pageIRI *url.URL) (ap.CollectionPageable, error) {
|
||||
if blocked, err := d.blockedDomain(pageIRI.Host); blocked || err != nil {
|
||||
func (d *deref) DereferenceCollectionPage(ctx context.Context, username string, pageIRI *url.URL) (ap.CollectionPageable, error) {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, pageIRI.Host); blocked || err != nil {
|
||||
return nil, fmt.Errorf("DereferenceCollectionPage: domain %s is blocked", pageIRI.Host)
|
||||
}
|
||||
|
||||
transport, err := d.transportController.NewTransportForUsername(username)
|
||||
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceCollectionPage: error creating transport: %s", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
package dereferencing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
|
|
@ -34,18 +35,18 @@ import (
|
|||
|
||||
// Dereferencer wraps logic and functionality for doing dereferencing of remote accounts, statuses, etc, from federated instances.
|
||||
type Dereferencer interface {
|
||||
GetRemoteAccount(username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error)
|
||||
EnrichRemoteAccount(username string, account *gtsmodel.Account) (*gtsmodel.Account, error)
|
||||
GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error)
|
||||
EnrichRemoteAccount(ctx context.Context, username string, account *gtsmodel.Account) (*gtsmodel.Account, error)
|
||||
|
||||
GetRemoteStatus(username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error)
|
||||
EnrichRemoteStatus(username string, status *gtsmodel.Status) (*gtsmodel.Status, error)
|
||||
GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error)
|
||||
EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status) (*gtsmodel.Status, error)
|
||||
|
||||
GetRemoteInstance(username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error)
|
||||
GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error)
|
||||
|
||||
DereferenceAnnounce(announce *gtsmodel.Status, requestingUsername string) error
|
||||
DereferenceThread(username string, statusIRI *url.URL) error
|
||||
DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error
|
||||
DereferenceThread(ctx context.Context, username string, statusIRI *url.URL) error
|
||||
|
||||
Handshaking(username string, remoteAccountID *url.URL) bool
|
||||
Handshaking(ctx context.Context, username string, remoteAccountID *url.URL) bool
|
||||
}
|
||||
|
||||
type deref struct {
|
||||
|
|
|
|||
|
|
@ -18,9 +18,12 @@
|
|||
|
||||
package dereferencing
|
||||
|
||||
import "net/url"
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *deref) Handshaking(username string, remoteAccountID *url.URL) bool {
|
||||
func (d *deref) Handshaking(ctx context.Context, username string, remoteAccountID *url.URL) bool {
|
||||
d.handshakeSync.Lock()
|
||||
defer d.handshakeSync.Unlock()
|
||||
|
||||
|
|
|
|||
|
|
@ -26,12 +26,12 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
func (d *deref) GetRemoteInstance(username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) {
|
||||
if blocked, err := d.blockedDomain(remoteInstanceURI.Host); blocked || err != nil {
|
||||
func (d *deref) GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, remoteInstanceURI.Host); blocked || err != nil {
|
||||
return nil, fmt.Errorf("GetRemoteInstance: domain %s is blocked", remoteInstanceURI.Host)
|
||||
}
|
||||
|
||||
transport, err := d.transportController.NewTransportForUsername(username)
|
||||
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("transport err: %s", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,12 +39,12 @@ import (
|
|||
//
|
||||
// EnrichRemoteStatus is mostly useful for calling after a status has been initially created by
|
||||
// the federatingDB's Create function, but additional dereferencing is needed on it.
|
||||
func (d *deref) EnrichRemoteStatus(username string, status *gtsmodel.Status) (*gtsmodel.Status, error) {
|
||||
if err := d.populateStatusFields(status, username); err != nil {
|
||||
func (d *deref) EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status) (*gtsmodel.Status, error) {
|
||||
if err := d.populateStatusFields(ctx, status, username); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := d.db.UpdateByID(status.ID, status); err != nil {
|
||||
if err := d.db.UpdateByID(ctx, status.ID, status); err != nil {
|
||||
return nil, fmt.Errorf("EnrichRemoteStatus: error updating status: %s", err)
|
||||
}
|
||||
|
||||
|
|
@ -62,11 +62,11 @@ func (d *deref) EnrichRemoteStatus(username string, status *gtsmodel.Status) (*g
|
|||
// If a dereference was performed, then the function also returns the ap.Statusable representation for further processing.
|
||||
//
|
||||
// SIDE EFFECTS: remote status will be stored in the database, and the remote status owner will also be stored.
|
||||
func (d *deref) GetRemoteStatus(username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) {
|
||||
func (d *deref) GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) {
|
||||
new := true
|
||||
|
||||
// check if we already have the status in our db
|
||||
maybeStatus, err := d.db.GetStatusByURI(remoteStatusID.String())
|
||||
maybeStatus, err := d.db.GetStatusByURI(ctx, remoteStatusID.String())
|
||||
if err == nil {
|
||||
// we've seen this status before so it's not new
|
||||
new = false
|
||||
|
|
@ -77,7 +77,7 @@ func (d *deref) GetRemoteStatus(username string, remoteStatusID *url.URL, refres
|
|||
}
|
||||
}
|
||||
|
||||
statusable, err := d.dereferenceStatusable(username, remoteStatusID)
|
||||
statusable, err := d.dereferenceStatusable(ctx, username, remoteStatusID)
|
||||
if err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error dereferencing statusable: %s", err)
|
||||
}
|
||||
|
|
@ -88,12 +88,12 @@ func (d *deref) GetRemoteStatus(username string, remoteStatusID *url.URL, refres
|
|||
}
|
||||
|
||||
// do this so we know we have the remote account of the status in the db
|
||||
_, _, err = d.GetRemoteAccount(username, accountURI, false)
|
||||
_, _, err = d.GetRemoteAccount(ctx, username, accountURI, false)
|
||||
if err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: couldn't derive status author: %s", err)
|
||||
}
|
||||
|
||||
gtsStatus, err := d.typeConverter.ASStatusToStatus(statusable)
|
||||
gtsStatus, err := d.typeConverter.ASStatusToStatus(ctx, statusable)
|
||||
if err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error converting statusable to status: %s", err)
|
||||
}
|
||||
|
|
@ -105,21 +105,21 @@ func (d *deref) GetRemoteStatus(username string, remoteStatusID *url.URL, refres
|
|||
}
|
||||
gtsStatus.ID = ulid
|
||||
|
||||
if err := d.populateStatusFields(gtsStatus, username); err != nil {
|
||||
if err := d.populateStatusFields(ctx, gtsStatus, username); err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error populating status fields: %s", err)
|
||||
}
|
||||
|
||||
if err := d.db.PutStatus(gtsStatus); err != nil {
|
||||
if err := d.db.PutStatus(ctx, gtsStatus); err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error putting new status: %s", err)
|
||||
}
|
||||
} else {
|
||||
gtsStatus.ID = maybeStatus.ID
|
||||
|
||||
if err := d.populateStatusFields(gtsStatus, username); err != nil {
|
||||
if err := d.populateStatusFields(ctx, gtsStatus, username); err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error populating status fields: %s", err)
|
||||
}
|
||||
|
||||
if err := d.db.UpdateByID(gtsStatus.ID, gtsStatus); err != nil {
|
||||
if err := d.db.UpdateByID(ctx, gtsStatus.ID, gtsStatus); err != nil {
|
||||
return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error updating status: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -127,12 +127,12 @@ func (d *deref) GetRemoteStatus(username string, remoteStatusID *url.URL, refres
|
|||
return gtsStatus, statusable, new, nil
|
||||
}
|
||||
|
||||
func (d *deref) dereferenceStatusable(username string, remoteStatusID *url.URL) (ap.Statusable, error) {
|
||||
if blocked, err := d.blockedDomain(remoteStatusID.Host); blocked || err != nil {
|
||||
func (d *deref) dereferenceStatusable(ctx context.Context, username string, remoteStatusID *url.URL) (ap.Statusable, error) {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, remoteStatusID.Host); blocked || err != nil {
|
||||
return nil, fmt.Errorf("DereferenceStatusable: domain %s is blocked", remoteStatusID.Host)
|
||||
}
|
||||
|
||||
transport, err := d.transportController.NewTransportForUsername(username)
|
||||
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceStatusable: transport err: %s", err)
|
||||
}
|
||||
|
|
@ -236,7 +236,7 @@ func (d *deref) dereferenceStatusable(username string, remoteStatusID *url.URL)
|
|||
// This function will deference all of the above, insert them in the database as necessary,
|
||||
// and attach them to the status. The status itself will not be added to the database yet,
|
||||
// that's up the caller to do.
|
||||
func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername string) error {
|
||||
func (d *deref) populateStatusFields(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
|
||||
l := d.log.WithFields(logrus.Fields{
|
||||
"func": "dereferenceStatusFields",
|
||||
"status": fmt.Sprintf("%+v", status),
|
||||
|
|
@ -248,12 +248,12 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
if err != nil {
|
||||
return fmt.Errorf("DereferenceStatusFields: couldn't parse status URI %s: %s", status.URI, err)
|
||||
}
|
||||
if blocked, err := d.blockedDomain(statusURI.Host); blocked || err != nil {
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, statusURI.Host); blocked || err != nil {
|
||||
return fmt.Errorf("DereferenceStatusFields: domain %s is blocked", statusURI.Host)
|
||||
}
|
||||
|
||||
// we can continue -- create a new transport here because we'll probably need it
|
||||
t, err := d.transportController.NewTransportForUsername(requestingUsername)
|
||||
t, err := d.transportController.NewTransportForUsername(ctx, requestingUsername)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating transport: %s", err)
|
||||
}
|
||||
|
|
@ -281,7 +281,7 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
|
||||
// it might have been processed elsewhere so check first if it's already in the database or not
|
||||
maybeAttachment := >smodel.MediaAttachment{}
|
||||
err := d.db.GetWhere([]db.Where{{Key: "remote_url", Value: a.RemoteURL}}, maybeAttachment)
|
||||
err := d.db.GetWhere(ctx, []db.Where{{Key: "remote_url", Value: a.RemoteURL}}, maybeAttachment)
|
||||
if err == nil {
|
||||
// we already have it in the db, dereferenced, no need to do it again
|
||||
l.Tracef("attachment already exists with id %s", maybeAttachment.ID)
|
||||
|
|
@ -294,7 +294,7 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
}
|
||||
// it just doesn't exist yet so carry on
|
||||
l.Debug("attachment doesn't exist yet, calling ProcessRemoteAttachment", a)
|
||||
deferencedAttachment, err := d.mediaHandler.ProcessRemoteAttachment(t, a, status.AccountID)
|
||||
deferencedAttachment, err := d.mediaHandler.ProcessRemoteAttachment(ctx, t, a, status.AccountID)
|
||||
if err != nil {
|
||||
l.Errorf("error dereferencing status attachment: %s", err)
|
||||
continue
|
||||
|
|
@ -302,7 +302,7 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
l.Debugf("dereferenced attachment: %+v", deferencedAttachment)
|
||||
deferencedAttachment.StatusID = status.ID
|
||||
deferencedAttachment.Description = a.Description
|
||||
if err := d.db.Put(deferencedAttachment); err != nil {
|
||||
if err := d.db.Put(ctx, deferencedAttachment); err != nil {
|
||||
return fmt.Errorf("error inserting dereferenced attachment with remote url %s: %s", a.RemoteURL, err)
|
||||
}
|
||||
attachmentIDs = append(attachmentIDs, deferencedAttachment.ID)
|
||||
|
|
@ -338,9 +338,9 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
}
|
||||
|
||||
var targetAccount *gtsmodel.Account
|
||||
if a, err := d.db.GetAccountByURL(targetAccountURI.String()); err == nil {
|
||||
if a, err := d.db.GetAccountByURL(ctx, targetAccountURI.String()); err == nil {
|
||||
targetAccount = a
|
||||
} else if a, _, err := d.GetRemoteAccount(requestingUsername, targetAccountURI, false); err == nil {
|
||||
} else if a, _, err := d.GetRemoteAccount(ctx, requestingUsername, targetAccountURI, false); err == nil {
|
||||
targetAccount = a
|
||||
} else {
|
||||
// we can't find the target account so bail
|
||||
|
|
@ -369,7 +369,7 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
TargetAccountURL: targetAccount.URL,
|
||||
}
|
||||
|
||||
if err := d.db.Put(m); err != nil {
|
||||
if err := d.db.Put(ctx, m); err != nil {
|
||||
return fmt.Errorf("error creating mention: %s", err)
|
||||
}
|
||||
mentionIDs = append(mentionIDs, m.ID)
|
||||
|
|
@ -382,13 +382,13 @@ func (d *deref) populateStatusFields(status *gtsmodel.Status, requestingUsername
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if replyToStatus, err := d.db.GetStatusByURI(status.InReplyToURI); err == nil {
|
||||
if replyToStatus, err := d.db.GetStatusByURI(ctx, status.InReplyToURI); err == nil {
|
||||
// we have the status
|
||||
status.InReplyToID = replyToStatus.ID
|
||||
status.InReplyTo = replyToStatus
|
||||
status.InReplyToAccountID = replyToStatus.AccountID
|
||||
status.InReplyToAccount = replyToStatus.Account
|
||||
} else if replyToStatus, _, _, err := d.GetRemoteStatus(requestingUsername, statusURI, false); err == nil {
|
||||
} else if replyToStatus, _, _, err := d.GetRemoteStatus(ctx, requestingUsername, statusURI, false); err == nil {
|
||||
// we got the status
|
||||
status.InReplyToID = replyToStatus.ID
|
||||
status.InReplyTo = replyToStatus
|
||||
|
|
|
|||
|
|
@ -19,12 +19,12 @@
|
|||
package dereferencing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ import (
|
|||
// This process involves working up and down the chain of replies, and parsing through the collections of IDs
|
||||
// presented by remote instances as part of their replies collections, and will likely involve making several calls to
|
||||
// multiple different hosts.
|
||||
func (d *deref) DereferenceThread(username string, statusIRI *url.URL) error {
|
||||
func (d *deref) DereferenceThread(ctx context.Context, username string, statusIRI *url.URL) error {
|
||||
l := d.log.WithFields(logrus.Fields{
|
||||
"func": "DereferenceThread",
|
||||
"username": username,
|
||||
|
|
@ -49,18 +49,18 @@ func (d *deref) DereferenceThread(username string, statusIRI *url.URL) error {
|
|||
}
|
||||
|
||||
// first make sure we have this status in our db
|
||||
_, statusable, _, err := d.GetRemoteStatus(username, statusIRI, true)
|
||||
_, statusable, _, err := d.GetRemoteStatus(ctx, username, statusIRI, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("DereferenceThread: error getting status with id %s: %s", statusIRI.String(), err)
|
||||
}
|
||||
|
||||
// first iterate up through ancestors, dereferencing if necessary as we go
|
||||
if err := d.iterateAncestors(username, *statusIRI); err != nil {
|
||||
if err := d.iterateAncestors(ctx, username, *statusIRI); err != nil {
|
||||
return fmt.Errorf("error iterating ancestors of status %s: %s", statusIRI.String(), err)
|
||||
}
|
||||
|
||||
// now iterate down through descendants, again dereferencing as we go
|
||||
if err := d.iterateDescendants(username, *statusIRI, statusable); err != nil {
|
||||
if err := d.iterateDescendants(ctx, username, *statusIRI, statusable); err != nil {
|
||||
return fmt.Errorf("error iterating descendants of status %s: %s", statusIRI.String(), err)
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +68,7 @@ func (d *deref) DereferenceThread(username string, statusIRI *url.URL) error {
|
|||
}
|
||||
|
||||
// iterateAncestors has the goal of reaching the oldest ancestor of a given status, and stashing all statuses along the way.
|
||||
func (d *deref) iterateAncestors(username string, statusIRI url.URL) error {
|
||||
func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI url.URL) error {
|
||||
l := d.log.WithFields(logrus.Fields{
|
||||
"func": "iterateAncestors",
|
||||
"username": username,
|
||||
|
|
@ -86,8 +86,8 @@ func (d *deref) iterateAncestors(username string, statusIRI url.URL) error {
|
|||
return err
|
||||
}
|
||||
|
||||
status := >smodel.Status{}
|
||||
if err := d.db.GetByID(id, status); err != nil {
|
||||
status, err := d.db.GetStatusByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -99,12 +99,12 @@ func (d *deref) iterateAncestors(username string, statusIRI url.URL) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.iterateAncestors(username, *nextIRI)
|
||||
return d.iterateAncestors(ctx, username, *nextIRI)
|
||||
}
|
||||
|
||||
// If we reach here, we're looking at a remote status -- make sure we have it in our db by calling GetRemoteStatus
|
||||
// We call it with refresh to true because we want the statusable representation to parse inReplyTo from.
|
||||
status, statusable, _, err := d.GetRemoteStatus(username, &statusIRI, true)
|
||||
status, statusable, _, err := d.GetRemoteStatus(ctx, username, &statusIRI, true)
|
||||
if err != nil {
|
||||
l.Debugf("error getting remote status: %s", err)
|
||||
return nil
|
||||
|
|
@ -117,22 +117,22 @@ func (d *deref) iterateAncestors(username string, statusIRI url.URL) error {
|
|||
}
|
||||
|
||||
// get the ancestor status into our database if we don't have it yet
|
||||
if _, _, _, err := d.GetRemoteStatus(username, inReplyTo, false); err != nil {
|
||||
if _, _, _, err := d.GetRemoteStatus(ctx, username, inReplyTo, false); err != nil {
|
||||
l.Debugf("error getting remote status: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// now enrich the current status, since we should have the ancestor in the db
|
||||
if _, err := d.EnrichRemoteStatus(username, status); err != nil {
|
||||
if _, err := d.EnrichRemoteStatus(ctx, username, status); err != nil {
|
||||
l.Debugf("error enriching remote status: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// now move up to the next ancestor
|
||||
return d.iterateAncestors(username, *inReplyTo)
|
||||
return d.iterateAncestors(ctx, username, *inReplyTo)
|
||||
}
|
||||
|
||||
func (d *deref) iterateDescendants(username string, statusIRI url.URL, statusable ap.Statusable) error {
|
||||
func (d *deref) iterateDescendants(ctx context.Context, username string, statusIRI url.URL, statusable ap.Statusable) error {
|
||||
l := d.log.WithFields(logrus.Fields{
|
||||
"func": "iterateDescendants",
|
||||
"username": username,
|
||||
|
|
@ -182,7 +182,7 @@ func (d *deref) iterateDescendants(username string, statusIRI url.URL, statusabl
|
|||
pageLoop:
|
||||
for {
|
||||
l.Debugf("dereferencing page %s", currentPageIRI)
|
||||
nextPage, err := d.DereferenceCollectionPage(username, currentPageIRI)
|
||||
nextPage, err := d.DereferenceCollectionPage(ctx, username, currentPageIRI)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -226,10 +226,10 @@ pageLoop:
|
|||
foundReplies = foundReplies + 1
|
||||
|
||||
// get the remote statusable and put it in the db
|
||||
_, statusable, new, err := d.GetRemoteStatus(username, itemURI, false)
|
||||
_, statusable, new, err := d.GetRemoteStatus(ctx, username, itemURI, false)
|
||||
if new && err == nil && statusable != nil {
|
||||
// now iterate descendants of *that* status
|
||||
if err := d.iterateDescendants(username, *itemURI, statusable); err != nil {
|
||||
if err := d.iterateDescendants(ctx, username, *itemURI, statusable); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue