[chore] deinterface the typeutils.Converter and update to use state structure (#2217)

* update typeconverter to use state structure

* deinterface the typeutils.TypeConverter -> typeutils.Converter

* finish copying over old type converter code comments

* fix cherry-pick merge issues, fix tests pointing to old typeutils interface type still
This commit is contained in:
kim
2023-09-23 17:44:11 +01:00
committed by GitHub
parent 1b848aa1e5
commit 8f67dd583d
119 changed files with 611 additions and 742 deletions

View File

@@ -34,7 +34,10 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/util"
)
func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, accountDomain string) (*gtsmodel.Account, error) {
// ASRepresentationToAccount converts a remote account/person/application representation into a gts model account.
//
// If accountDomain is provided then this value will be used as the account's Domain, else the AP ID host.
func (c *Converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, accountDomain string) (*gtsmodel.Account, error) {
// first check if we actually already know this account
uriProp := accountable.GetJSONLDId()
if uriProp == nil || !uriProp.IsIRI() {
@@ -213,7 +216,7 @@ func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable a
return acct, nil
}
func (c *converter) extractAttachments(i ap.WithAttachment) []*gtsmodel.MediaAttachment {
func (c *Converter) extractAttachments(i ap.WithAttachment) []*gtsmodel.MediaAttachment {
attachmentProp := i.GetActivityStreamsAttachment()
if attachmentProp == nil {
return nil
@@ -245,7 +248,8 @@ func (c *converter) extractAttachments(i ap.WithAttachment) []*gtsmodel.MediaAtt
return attachments
}
func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
// ASStatus converts a remote activitystreams 'status' representation into a gts model status.
func (c *Converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
status := new(gtsmodel.Status)
// status.URI
@@ -341,7 +345,7 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
}
accountURI := attributedTo.String()
account, err := c.db.GetAccountByURI(ctx, accountURI)
account, err := c.state.DB.GetAccountByURI(ctx, accountURI)
if err != nil {
err = gtserror.Newf("db error getting status author account %s: %w", accountURI, err)
return nil, err
@@ -364,7 +368,7 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
status.InReplyToURI = inReplyToURI
// Check if we already have the replied-to status.
inReplyTo, err := c.db.GetStatusByURI(ctx, inReplyToURI)
inReplyTo, err := c.state.DB.GetStatusByURI(ctx, inReplyToURI)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// Real database error.
err = gtserror.Newf("db error getting replied-to status %s: %w", inReplyToURI, err)
@@ -417,7 +421,8 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
return status, nil
}
func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) {
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request.
func (c *Converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) {
idProp := followable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
return nil, errors.New("no id property set on follow, or was not an iri")
@@ -428,7 +433,7 @@ func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.F
if err != nil {
return nil, errors.New("error extracting actor property from follow")
}
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
originAccount, err := c.state.DB.GetAccountByURI(ctx, origin.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -437,7 +442,7 @@ func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.F
if err != nil {
return nil, errors.New("error extracting object property from follow")
}
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
targetAccount, err := c.state.DB.GetAccountByURI(ctx, target.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -451,7 +456,8 @@ func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.F
return followRequest, nil
}
func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) {
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
func (c *Converter) ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) {
idProp := followable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
return nil, errors.New("no id property set on follow, or was not an iri")
@@ -462,7 +468,7 @@ func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followab
if err != nil {
return nil, errors.New("error extracting actor property from follow")
}
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
originAccount, err := c.state.DB.GetAccountByURI(ctx, origin.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -471,7 +477,7 @@ func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followab
if err != nil {
return nil, errors.New("error extracting object property from follow")
}
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
targetAccount, err := c.state.DB.GetAccountByURI(ctx, target.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -485,7 +491,8 @@ func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followab
return follow, nil
}
func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) {
// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
func (c *Converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) {
idProp := likeable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
return nil, errors.New("no id property set on like, or was not an iri")
@@ -496,7 +503,7 @@ func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gt
if err != nil {
return nil, errors.New("error extracting actor property from like")
}
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
originAccount, err := c.state.DB.GetAccountByURI(ctx, origin.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -506,7 +513,7 @@ func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gt
return nil, errors.New("error extracting object property from like")
}
targetStatus, err := c.db.GetStatusByURI(ctx, target.String())
targetStatus, err := c.state.DB.GetStatusByURI(ctx, target.String())
if err != nil {
return nil, fmt.Errorf("error extracting status with uri %s from the database: %s", target.String(), err)
}
@@ -515,7 +522,7 @@ func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gt
if targetStatus.Account != nil {
targetAccount = targetStatus.Account
} else {
a, err := c.db.GetAccountByID(ctx, targetStatus.AccountID)
a, err := c.state.DB.GetAccountByID(ctx, targetStatus.AccountID)
if err != nil {
return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err)
}
@@ -533,7 +540,8 @@ func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gt
}, nil
}
func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) {
// ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block.
func (c *Converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) {
idProp := blockable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
return nil, errors.New("ASBlockToBlock: no id property set on block, or was not an iri")
@@ -544,7 +552,7 @@ func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable)
if err != nil {
return nil, errors.New("ASBlockToBlock: error extracting actor property from block")
}
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
originAccount, err := c.state.DB.GetAccountByURI(ctx, origin.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -554,7 +562,7 @@ func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable)
return nil, errors.New("ASBlockToBlock: error extracting object property from block")
}
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
targetAccount, err := c.state.DB.GetAccountByURI(ctx, target.String())
if err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
@@ -568,6 +576,19 @@ func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable)
}, nil
}
// ASAnnounceToStatus converts an activitystreams 'announce' into a status.
//
// The returned bool indicates whether this status is new (true) or not new (false).
//
// In other words, if the status is already in the database with the ID set on the announceable, then that will be returned,
// the returned bool will be false, and no further processing is necessary. If the returned bool is true, indicating
// that this is a new announce, then further processing will be necessary, because the returned status will be bareboned and
// require further dereferencing.
//
// This is useful when multiple users on an instance might receive the same boost, and we only want to process the boost once.
//
// NOTE -- this is different from one status being boosted multiple times! In this case, new boosts should indeed be created.
//
// Implementation note: this function creates and returns a boost WRAPPER
// status which references the boosted status in its BoostOf field. No
// dereferencing is done on the boosted status by this function. Callers
@@ -577,7 +598,7 @@ func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable)
// The returned boolean indicates whether or not the boost has already been
// seen before by this instance. If it was, then status.BoostOf should be a
// fully filled-out status. If not, then only status.BoostOf.URI will be set.
func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (*gtsmodel.Status, bool, error) {
func (c *Converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (*gtsmodel.Status, bool, error) {
// Ensure item has an ID URI set.
_, statusURIStr, err := getURI(announceable)
if err != nil {
@@ -591,7 +612,7 @@ func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Anno
)
// Check if we already have this boost in the database.
status, err = c.db.GetStatusByURI(ctx, statusURIStr)
status, err = c.state.DB.GetStatusByURI(ctx, statusURIStr)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// Real database error.
err = gtserror.Newf("db error trying to get status with uri %s: %w", statusURIStr, err)
@@ -648,7 +669,7 @@ func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Anno
// This should have been dereferenced already before
// we hit this point so we can confidently error out
// if we don't have it.
account, err := c.db.GetAccountByURI(ctx, accountURIStr)
account, err := c.state.DB.GetAccountByURI(ctx, accountURIStr)
if err != nil {
err = gtserror.Newf("db error trying to get account with uri %s: %w", accountURIStr, err)
return nil, isNew, err
@@ -678,7 +699,8 @@ func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Anno
return status, isNew, nil
}
func (c *converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable) (*gtsmodel.Report, error) {
// ASFlagToReport converts a remote activitystreams 'flag' representation into a gts model report.
func (c *Converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable) (*gtsmodel.Report, error) {
// Extract flag uri.
idProp := flaggable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
@@ -692,7 +714,7 @@ func (c *converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable)
if err != nil {
return nil, fmt.Errorf("ASFlagToReport: error extracting actor: %w", err)
}
account, err := c.db.GetAccountByURI(ctx, actor.String())
account, err := c.state.DB.GetAccountByURI(ctx, actor.String())
if err != nil {
return nil, fmt.Errorf("ASFlagToReport: error in db fetching account with uri %s: %w", actor.String(), err)
}
@@ -744,7 +766,7 @@ func (c *converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable)
if targetAccountURI == nil {
return nil, errors.New("ASFlagToReport: flaggable objects contained no recognizable target account uri")
}
targetAccount, err := c.db.GetAccountByURI(ctx, targetAccountURI.String())
targetAccount, err := c.state.DB.GetAccountByURI(ctx, targetAccountURI.String())
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
return nil, fmt.Errorf("ASFlagToReport: account with uri %s could not be found in the db", targetAccountURI.String())
@@ -761,13 +783,13 @@ func (c *converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable)
statusURIString := statusURI.String()
// try getting this status by URI first, then URL
status, err := c.db.GetStatusByURI(ctx, statusURIString)
status, err := c.state.DB.GetStatusByURI(ctx, statusURIString)
if err != nil {
if !errors.Is(err, db.ErrNoEntries) {
return nil, fmt.Errorf("ASFlagToReport: db error getting status with uri %s: %w", statusURIString, err)
}
status, err = c.db.GetStatusByURL(ctx, statusURIString)
status, err = c.state.DB.GetStatusByURL(ctx, statusURIString)
if err != nil {
if !errors.Is(err, db.ErrNoEntries) {
return nil, fmt.Errorf("ASFlagToReport: db error getting status with url %s: %w", statusURIString, err)