summaryrefslogtreecommitdiff
path: root/internal/typeutils
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-08-25 15:34:33 +0200
committerLibravatar GitHub <noreply@github.com>2021-08-25 15:34:33 +0200
commit2dc9fc1626507bb54417fc4a1920b847cafb27a2 (patch)
tree4ddeac479b923db38090aac8bd9209f3646851c1 /internal/typeutils
parentManually approves followers (#146) (diff)
downloadgotosocial-2dc9fc1626507bb54417fc4a1920b847cafb27a2.tar.xz
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
Diffstat (limited to 'internal/typeutils')
-rw-r--r--internal/typeutils/astointernal.go46
-rw-r--r--internal/typeutils/astointernal_test.go4
-rw-r--r--internal/typeutils/converter.go71
-rw-r--r--internal/typeutils/internal.go5
-rw-r--r--internal/typeutils/internaltoas.go73
-rw-r--r--internal/typeutils/internaltoas_test.go3
-rw-r--r--internal/typeutils/internaltofrontend.go175
-rw-r--r--internal/typeutils/util.go11
8 files changed, 205 insertions, 183 deletions
diff --git a/internal/typeutils/astointernal.go b/internal/typeutils/astointernal.go
index 887716a69..46132233b 100644
--- a/internal/typeutils/astointernal.go
+++ b/internal/typeutils/astointernal.go
@@ -19,6 +19,7 @@
package typeutils
import (
+ "context"
"errors"
"fmt"
"net/url"
@@ -29,7 +30,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
-func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update bool) (*gtsmodel.Account, error) {
+func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error) {
// first check if we actually already know this account
uriProp := accountable.GetJSONLDId()
if uriProp == nil || !uriProp.IsIRI() {
@@ -38,7 +39,7 @@ func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update
uri := uriProp.GetIRI()
if !update {
- acct, err := c.db.GetAccountByURI(uri.String())
+ acct, err := c.db.GetAccountByURI(ctx, uri.String())
if err == nil {
// we already know this account so we can skip generating it
return acct, nil
@@ -170,7 +171,7 @@ func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update
return acct, nil
}
-func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status, error) {
+func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
status := &gtsmodel.Status{}
// uri at which this status is reachable
@@ -219,6 +220,7 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
published, err := ap.ExtractPublished(statusable)
if err == nil {
status.CreatedAt = published
+ status.UpdatedAt = published
}
// which account posted this status?
@@ -229,7 +231,7 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
}
status.AccountURI = attributedTo.String()
- statusOwner, err := c.db.GetAccountByURI(attributedTo.String())
+ statusOwner, err := c.db.GetAccountByURI(ctx, attributedTo.String())
if err != nil {
return nil, fmt.Errorf("couldn't get status owner from db: %s", err)
}
@@ -245,14 +247,14 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
status.InReplyToURI = inReplyToURI.String()
// now we can check if we have the replied-to status in our db already
- if inReplyToStatus, err := c.db.GetStatusByURI(inReplyToURI.String()); err == nil {
+ if inReplyToStatus, err := c.db.GetStatusByURI(ctx, inReplyToURI.String()); err == nil {
// we have the status in our database already
// so we can set these fields here and now...
status.InReplyToID = inReplyToStatus.ID
status.InReplyToAccountID = inReplyToStatus.AccountID
status.InReplyTo = inReplyToStatus
if status.InReplyToAccount == nil {
- if inReplyToAccount, err := c.db.GetAccountByID(inReplyToStatus.AccountID); err == nil {
+ if inReplyToAccount, err := c.db.GetAccountByID(ctx, inReplyToStatus.AccountID); err == nil {
status.InReplyToAccount = inReplyToAccount
}
}
@@ -318,7 +320,7 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
return status, nil
}
-func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel.FollowRequest, error) {
+func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) {
idProp := followable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
@@ -330,7 +332,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel
if err != nil {
return nil, errors.New("error extracting actor property from follow")
}
- originAccount, err := c.db.GetAccountByURI(origin.String())
+ originAccount, err := c.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)
}
@@ -339,7 +341,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel
if err != nil {
return nil, errors.New("error extracting object property from follow")
}
- targetAccount, err := c.db.GetAccountByURI(target.String())
+ targetAccount, err := c.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)
}
@@ -353,7 +355,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel
return followRequest, nil
}
-func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow, error) {
+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")
@@ -364,7 +366,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow
if err != nil {
return nil, errors.New("error extracting actor property from follow")
}
- originAccount, err := c.db.GetAccountByURI(origin.String())
+ originAccount, err := c.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)
}
@@ -373,7 +375,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow
if err != nil {
return nil, errors.New("error extracting object property from follow")
}
- targetAccount, err := c.db.GetAccountByURI(target.String())
+ targetAccount, err := c.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)
}
@@ -387,7 +389,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow
return follow, nil
}
-func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, error) {
+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")
@@ -398,7 +400,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
if err != nil {
return nil, errors.New("error extracting actor property from like")
}
- originAccount, err := c.db.GetAccountByURI(origin.String())
+ originAccount, err := c.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)
}
@@ -408,7 +410,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
return nil, errors.New("error extracting object property from like")
}
- targetStatus, err := c.db.GetStatusByURI(target.String())
+ targetStatus, err := c.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)
}
@@ -417,7 +419,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
if targetStatus.Account != nil {
targetAccount = targetStatus.Account
} else {
- a, err := c.db.GetAccountByID(targetStatus.AccountID)
+ a, err := c.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)
}
@@ -435,7 +437,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
}, nil
}
-func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, error) {
+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")
@@ -446,7 +448,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err
if err != nil {
return nil, errors.New("ASBlockToBlock: error extracting actor property from block")
}
- originAccount, err := c.db.GetAccountByURI(origin.String())
+ originAccount, err := c.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)
}
@@ -456,7 +458,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err
return nil, errors.New("ASBlockToBlock: error extracting object property from block")
}
- targetAccount, err := c.db.GetAccountByURI(target.String())
+ targetAccount, err := c.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)
}
@@ -470,7 +472,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err
}, nil
}
-func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.Status, bool, error) {
+func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (*gtsmodel.Status, bool, error) {
status := &gtsmodel.Status{}
isNew := true
@@ -481,7 +483,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.
}
uri := idProp.GetIRI().String()
- if status, err := c.db.GetStatusByURI(uri); err == nil {
+ if status, err := c.db.GetStatusByURI(ctx, uri); err == nil {
// we already have it, great, just return it as-is :)
isNew = false
return status, isNew, nil
@@ -515,7 +517,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.
// get the boosting account based on the URI
// this should have been dereferenced already before we hit this point so we can confidently error out if we don't have it
- boostingAccount, err := c.db.GetAccountByURI(actor.String())
+ boostingAccount, err := c.db.GetAccountByURI(ctx, actor.String())
if err != nil {
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error in db fetching account with uri %s: %s", actor.String(), err)
}
diff --git a/internal/typeutils/astointernal_test.go b/internal/typeutils/astointernal_test.go
index 1d02dec5a..a01e79202 100644
--- a/internal/typeutils/astointernal_test.go
+++ b/internal/typeutils/astointernal_test.go
@@ -348,7 +348,7 @@ func (suite *ASToInternalTestSuite) SetupTest() {
func (suite *ASToInternalTestSuite) TestParsePerson() {
testPerson := suite.people["new_person_1"]
- acct, err := suite.typeconverter.ASRepresentationToAccount(testPerson, false)
+ acct, err := suite.typeconverter.ASRepresentationToAccount(context.Background(), testPerson, false)
assert.NoError(suite.T(), err)
suite.Equal("https://unknown-instance.com/users/brand_new_person", acct.URI)
@@ -379,7 +379,7 @@ func (suite *ASToInternalTestSuite) TestParseGargron() {
rep, ok := t.(ap.Accountable)
assert.True(suite.T(), ok)
- acct, err := suite.typeconverter.ASRepresentationToAccount(rep, false)
+ acct, err := suite.typeconverter.ASRepresentationToAccount(context.Background(), rep, false)
assert.NoError(suite.T(), err)
fmt.Printf("%+v", acct)
diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go
index e477a6135..4af9767bc 100644
--- a/internal/typeutils/converter.go
+++ b/internal/typeutils/converter.go
@@ -19,6 +19,7 @@
package typeutils
import (
+ "context"
"net/url"
"github.com/go-fed/activity/streams/vocab"
@@ -47,45 +48,45 @@ type TypeConverter interface {
// AccountToMastoSensitive takes a db model account as a param, and returns a populated mastotype account, or an error
// if something goes wrong. The returned account should be ready to serialize on an API level, and may have sensitive fields,
// so serve it only to an authorized user who should have permission to see it.
- AccountToMastoSensitive(account *gtsmodel.Account) (*model.Account, error)
+ AccountToMastoSensitive(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
// AccountToMastoPublic takes a db model account as a param, and returns a populated mastotype account, or an error
// if something goes wrong. The returned account should be ready to serialize on an API level, and may NOT have sensitive fields.
// In other words, this is the public record that the server has of an account.
- AccountToMastoPublic(account *gtsmodel.Account) (*model.Account, error)
+ AccountToMastoPublic(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
// AccountToMastoBlocked takes a db model account as a param, and returns a mastotype account, or an error if
// something goes wrong. The returned account will be a bare minimum representation of the account. This function should be used
// when someone wants to view an account they've blocked.
- AccountToMastoBlocked(account *gtsmodel.Account) (*model.Account, error)
+ AccountToMastoBlocked(ctx context.Context, account *gtsmodel.Account) (*model.Account, error)
// AppToMastoSensitive takes a db model application as a param, and returns a populated mastotype application, or an error
// if something goes wrong. The returned application should be ready to serialize on an API level, and may have sensitive fields
// (such as client id and client secret), so serve it only to an authorized user who should have permission to see it.
- AppToMastoSensitive(application *gtsmodel.Application) (*model.Application, error)
+ AppToMastoSensitive(ctx context.Context, application *gtsmodel.Application) (*model.Application, error)
// AppToMastoPublic takes a db model application as a param, and returns a populated mastotype application, or an error
// if something goes wrong. The returned application should be ready to serialize on an API level, and has sensitive
// fields sanitized so that it can be served to non-authorized accounts without revealing any private information.
- AppToMastoPublic(application *gtsmodel.Application) (*model.Application, error)
+ AppToMastoPublic(ctx context.Context, application *gtsmodel.Application) (*model.Application, error)
// AttachmentToMasto converts a gts model media attacahment into its mastodon representation for serialization on the API.
- AttachmentToMasto(attachment *gtsmodel.MediaAttachment) (model.Attachment, error)
+ AttachmentToMasto(ctx context.Context, attachment *gtsmodel.MediaAttachment) (model.Attachment, error)
// MentionToMasto converts a gts model mention into its mastodon (frontend) representation for serialization on the API.
- MentionToMasto(m *gtsmodel.Mention) (model.Mention, error)
+ MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error)
// EmojiToMasto converts a gts model emoji into its mastodon (frontend) representation for serialization on the API.
- EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error)
+ EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error)
// TagToMasto converts a gts model tag into its mastodon (frontend) representation for serialization on the API.
- TagToMasto(t *gtsmodel.Tag) (model.Tag, error)
+ TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error)
// StatusToMasto converts a gts model status into its mastodon (frontend) representation for serialization on the API.
//
// Requesting account can be nil.
- StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error)
+ StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error)
// VisToMasto converts a gts visibility into its mastodon equivalent
- VisToMasto(m gtsmodel.Visibility) model.Visibility
+ VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility
// InstanceToMasto converts a gts instance into its mastodon equivalent for serving at /api/v1/instance
- InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, error)
+ InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error)
// RelationshipToMasto converts a gts relationship into its mastodon equivalent for serving in various places
- RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relationship, error)
+ RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error)
// NotificationToMasto converts a gts notification into a mastodon notification
- NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error)
+ NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error)
// DomainBlockTomasto converts a gts model domin block into a mastodon domain block, for serving at /api/v1/admin/domain_blocks
- DomainBlockToMasto(b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error)
+ DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error)
/*
FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL
@@ -103,17 +104,17 @@ type TypeConverter interface {
// If update is false, and the account is already known in the database, then the existing account entry will be returned.
// If update is true, then even if the account is already known, all fields in the accountable will be parsed and a new *gtsmodel.Account
// will be generated. This is useful when one needs to force refresh of an account, eg., during an Update of a Profile.
- ASRepresentationToAccount(accountable ap.Accountable, update bool) (*gtsmodel.Account, error)
+ ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error)
// ASStatus converts a remote activitystreams 'status' representation into a gts model status.
- ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status, error)
+ ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error)
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request.
- ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel.FollowRequest, error)
+ ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error)
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
- ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow, error)
+ ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error)
// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
- ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, error)
+ ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error)
// ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block.
- ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, error)
+ ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error)
// ASAnnounceToStatus converts an activitystreams 'announce' into a status.
//
// The returned bool indicates whether this status is new (true) or not new (false).
@@ -126,46 +127,46 @@ type TypeConverter interface {
// 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.
- ASAnnounceToStatus(announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error)
+ ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error)
/*
INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL
*/
// AccountToAS converts a gts model account into an activity streams person, suitable for federation
- AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
+ AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
// AccountToASMinimal converts a gts model account into an activity streams person, suitable for federation.
//
// The returned account will just have the Type, Username, PublicKey, and ID properties set. This is
// suitable for serving to requesters to whom we want to give as little information as possible because
// we don't trust them (yet).
- AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
+ AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error)
// StatusToAS converts a gts model status into an activity streams note, suitable for federation
- StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, error)
+ StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error)
// FollowToASFollow converts a gts model Follow into an activity streams Follow, suitable for federation
- FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error)
+ FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error)
// MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation
- MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error)
+ MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error)
// AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation
- AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
+ AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
// FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation.
- FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error)
+ FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error)
// BoostToAS converts a gts model boost into an activityStreams ANNOUNCE, suitable for federation
- BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error)
+ BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error)
// BlockToAS converts a gts model block into an activityStreams BLOCK, suitable for federation.
- BlockToAS(block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error)
+ BlockToAS(ctx context.Context, block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error)
// StatusToASRepliesCollection converts a gts model status into an activityStreams REPLIES collection.
- StatusToASRepliesCollection(status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error)
+ StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error)
// StatusURIsToASRepliesPage returns a collection page with appropriate next/part of pagination.
- StatusURIsToASRepliesPage(status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error)
+ StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error)
/*
INTERNAL (gts) MODEL TO INTERNAL MODEL
*/
// FollowRequestToFollow just converts a follow request into a follow, that's it! No bells and whistles.
- FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.Follow
+ FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow
// StatusToBoost wraps the given status into a boosting status.
- StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error)
+ StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error)
/*
WRAPPER CONVENIENCE FUNCTIONS
diff --git a/internal/typeutils/internal.go b/internal/typeutils/internal.go
index ad15ecbee..23839b9a8 100644
--- a/internal/typeutils/internal.go
+++ b/internal/typeutils/internal.go
@@ -1,6 +1,7 @@
package typeutils
import (
+ "context"
"fmt"
"time"
@@ -9,7 +10,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/util"
)
-func (c *converter) FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.Follow {
+func (c *converter) FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow {
return &gtsmodel.Follow{
ID: f.ID,
CreatedAt: f.CreatedAt,
@@ -22,7 +23,7 @@ func (c *converter) FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.F
}
}
-func (c *converter) StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) {
+func (c *converter) StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) {
// the wrapper won't use the same ID as the boosted status so we generate some new UUIDs
uris := util.GenerateURIsForAccount(boostingAccount.Username, c.config.Protocol, c.config.Host)
boostWrapperStatusID, err := id.NewULID()
diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go
index 178567dc6..14ed094c5 100644
--- a/internal/typeutils/internaltoas.go
+++ b/internal/typeutils/internaltoas.go
@@ -19,6 +19,7 @@
package typeutils
import (
+ "context"
"crypto/x509"
"encoding/pem"
"fmt"
@@ -33,7 +34,7 @@ import (
// Converts a gts model account into an Activity Streams person type, following
// the spec laid out for mastodon here: https://docs.joinmastodon.org/spec/activitypub/
-func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) {
+func (c *converter) AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) {
// first check if we have this person in our asCache already
if personI, err := c.asCache.Fetch(a.ID); err == nil {
if person, ok := personI.(vocab.ActivityStreamsPerson); ok {
@@ -213,9 +214,12 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
// icon
// Used as profile avatar.
if a.AvatarMediaAttachmentID != "" {
- avatar := &gtsmodel.MediaAttachment{}
- if err := c.db.GetByID(a.AvatarMediaAttachmentID, avatar); err != nil {
- return nil, err
+ if a.AvatarMediaAttachment == nil {
+ avatar := &gtsmodel.MediaAttachment{}
+ if err := c.db.GetByID(ctx, a.AvatarMediaAttachmentID, avatar); err != nil {
+ return nil, err
+ }
+ a.AvatarMediaAttachment = avatar
}
iconProperty := streams.NewActivityStreamsIconProperty()
@@ -223,11 +227,11 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
iconImage := streams.NewActivityStreamsImage()
mediaType := streams.NewActivityStreamsMediaTypeProperty()
- mediaType.Set(avatar.File.ContentType)
+ mediaType.Set(a.AvatarMediaAttachment.File.ContentType)
iconImage.SetActivityStreamsMediaType(mediaType)
avatarURLProperty := streams.NewActivityStreamsUrlProperty()
- avatarURL, err := url.Parse(avatar.URL)
+ avatarURL, err := url.Parse(a.AvatarMediaAttachment.URL)
if err != nil {
return nil, err
}
@@ -241,9 +245,12 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
// image
// Used as profile header.
if a.HeaderMediaAttachmentID != "" {
- header := &gtsmodel.MediaAttachment{}
- if err := c.db.GetByID(a.HeaderMediaAttachmentID, header); err != nil {
- return nil, err
+ if a.HeaderMediaAttachment == nil {
+ header := &gtsmodel.MediaAttachment{}
+ if err := c.db.GetByID(ctx, a.HeaderMediaAttachmentID, header); err != nil {
+ return nil, err
+ }
+ a.HeaderMediaAttachment = header
}
headerProperty := streams.NewActivityStreamsImageProperty()
@@ -251,11 +258,11 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
headerImage := streams.NewActivityStreamsImage()
mediaType := streams.NewActivityStreamsMediaTypeProperty()
- mediaType.Set(header.File.ContentType)
+ mediaType.Set(a.HeaderMediaAttachment.File.ContentType)
headerImage.SetActivityStreamsMediaType(mediaType)
headerURLProperty := streams.NewActivityStreamsUrlProperty()
- headerURL, err := url.Parse(header.URL)
+ headerURL, err := url.Parse(a.HeaderMediaAttachment.URL)
if err != nil {
return nil, err
}
@@ -278,7 +285,7 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
// the spec laid out for mastodon here: https://docs.joinmastodon.org/spec/activitypub/
//
// The returned account will just have the Type, Username, PublicKey, and ID properties set.
-func (c *converter) AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) {
+func (c *converter) AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) {
person := streams.NewActivityStreamsPerson()
// id should be the activitypub URI of this user
@@ -340,7 +347,7 @@ func (c *converter) AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStrea
return person, nil
}
-func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) {
+func (c *converter) StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) {
// first check if we have this note in our asCache already
if noteI, err := c.asCache.Fetch(s.ID); err == nil {
if note, ok := noteI.(vocab.ActivityStreamsNote); ok {
@@ -354,7 +361,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
// check if author account is already attached to status and attach it if not
// if we can't retrieve this, bail here already because we can't attribute the status to anyone
if s.Account == nil {
- a, err := c.db.GetAccountByID(s.AccountID)
+ a, err := c.db.GetAccountByID(ctx, s.AccountID)
if err != nil {
return nil, fmt.Errorf("StatusToAS: error retrieving author account from db: %s", err)
}
@@ -386,7 +393,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
// fetch the replied status if we don't have it on hand already
if s.InReplyTo == nil {
rs := &gtsmodel.Status{}
- if err := c.db.GetByID(s.InReplyToID, rs); err != nil {
+ if err := c.db.GetByID(ctx, s.InReplyToID, rs); err != nil {
return nil, fmt.Errorf("StatusToAS: error retrieving replied-to status from db: %s", err)
}
s.InReplyTo = rs
@@ -432,7 +439,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
// tag -- mentions
for _, m := range s.Mentions {
- asMention, err := c.MentionToAS(m)
+ asMention, err := c.MentionToAS(ctx, m)
if err != nil {
return nil, fmt.Errorf("StatusToAS: error converting mention to AS mention: %s", err)
}
@@ -520,7 +527,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
// attachment
attachmentProp := streams.NewActivityStreamsAttachmentProperty()
for _, a := range s.Attachments {
- doc, err := c.AttachmentToAS(a)
+ doc, err := c.AttachmentToAS(ctx, a)
if err != nil {
return nil, fmt.Errorf("StatusToAS: error converting attachment: %s", err)
}
@@ -529,7 +536,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
status.SetActivityStreamsAttachment(attachmentProp)
// replies
- repliesCollection, err := c.StatusToASRepliesCollection(s, false)
+ repliesCollection, err := c.StatusToASRepliesCollection(ctx, s, false)
if err != nil {
return nil, fmt.Errorf("error creating repliesCollection: %s", err)
}
@@ -546,7 +553,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
return status, nil
}
-func (c *converter) FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) {
+func (c *converter) FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) {
// parse out the various URIs we need for this
// origin account (who's doing the follow)
originAccountURI, err := url.Parse(originAccount.URI)
@@ -592,10 +599,10 @@ func (c *converter) FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Accou
return follow, nil
}
-func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) {
+func (c *converter) MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) {
if m.OriginAccount == nil {
a := &gtsmodel.Account{}
- if err := c.db.GetWhere([]db.Where{{Key: "target_account_id", Value: m.TargetAccountID}}, a); err != nil {
+ if err := c.db.GetWhere(ctx, []db.Where{{Key: "target_account_id", Value: m.TargetAccountID}}, a); err != nil {
return nil, fmt.Errorf("MentionToAS: error getting target account from db: %s", err)
}
m.OriginAccount = a
@@ -629,7 +636,7 @@ func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMenti
return mention, nil
}
-func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) {
+func (c *converter) AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) {
// type -- Document
doc := streams.NewActivityStreamsDocument()
@@ -674,11 +681,11 @@ func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityS
"type": "Like"
}
*/
-func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
+func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
// check if targetStatus is already pinned to this fave, and fetch it if not
if f.Status == nil {
s := &gtsmodel.Status{}
- if err := c.db.GetByID(f.StatusID, s); err != nil {
+ if err := c.db.GetByID(ctx, f.StatusID, s); err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching target status from database: %s", err)
}
f.Status = s
@@ -687,7 +694,7 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
// check if the targetAccount is already pinned to this fave, and fetch it if not
if f.TargetAccount == nil {
a := &gtsmodel.Account{}
- if err := c.db.GetByID(f.TargetAccountID, a); err != nil {
+ if err := c.db.GetByID(ctx, f.TargetAccountID, a); err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching target account from database: %s", err)
}
f.TargetAccount = a
@@ -696,7 +703,7 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
// check if the faving account is already pinned to this fave, and fetch it if not
if f.Account == nil {
a := &gtsmodel.Account{}
- if err := c.db.GetByID(f.AccountID, a); err != nil {
+ if err := c.db.GetByID(ctx, f.AccountID, a); err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching faving account from database: %s", err)
}
f.Account = a
@@ -744,11 +751,11 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
return like, nil
}
-func (c *converter) BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) {
+func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) {
// the boosted status is probably pinned to the boostWrapperStatus but double check to make sure
if boostWrapperStatus.BoostOf == nil {
b := &gtsmodel.Status{}
- if err := c.db.GetByID(boostWrapperStatus.BoostOfID, b); err != nil {
+ if err := c.db.GetByID(ctx, boostWrapperStatus.BoostOfID, b); err != nil {
return nil, fmt.Errorf("BoostToAS: error getting status with ID %s from the db: %s", boostWrapperStatus.BoostOfID, err)
}
boostWrapperStatus.BoostOf = b
@@ -828,10 +835,10 @@ func (c *converter) BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccou
"type":"Block"
}
*/
-func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) {
+func (c *converter) BlockToAS(ctx context.Context, b *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) {
if b.Account == nil {
a := &gtsmodel.Account{}
- if err := c.db.GetByID(b.AccountID, a); err != nil {
+ if err := c.db.GetByID(ctx, b.AccountID, a); err != nil {
return nil, fmt.Errorf("BlockToAS: error getting block account from database: %s", err)
}
b.Account = a
@@ -839,7 +846,7 @@ func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, er
if b.TargetAccount == nil {
a := &gtsmodel.Account{}
- if err := c.db.GetByID(b.TargetAccountID, a); err != nil {
+ if err := c.db.GetByID(ctx, b.TargetAccountID, a); err != nil {
return nil, fmt.Errorf("BlockToAS: error getting block target account from database: %s", err)
}
b.TargetAccount = a
@@ -903,7 +910,7 @@ func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, er
}
}
*/
-func (c *converter) StatusToASRepliesCollection(status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) {
+func (c *converter) StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) {
collectionID := fmt.Sprintf("%s/replies", status.URI)
collectionIDURI, err := url.Parse(collectionID)
if err != nil {
@@ -966,7 +973,7 @@ func (c *converter) StatusToASRepliesCollection(status *gtsmodel.Status, onlyOth
]
}
*/
-func (c *converter) StatusURIsToASRepliesPage(status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) {
+func (c *converter) StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) {
collectionID := fmt.Sprintf("%s/replies", status.URI)
page := streams.NewActivityStreamsCollectionPage()
diff --git a/internal/typeutils/internaltoas_test.go b/internal/typeutils/internaltoas_test.go
index caa56ce0d..46f04df2f 100644
--- a/internal/typeutils/internaltoas_test.go
+++ b/internal/typeutils/internaltoas_test.go
@@ -19,6 +19,7 @@
package typeutils_test
import (
+ "context"
"encoding/json"
"fmt"
"testing"
@@ -58,7 +59,7 @@ func (suite *InternalToASTestSuite) TearDownTest() {
func (suite *InternalToASTestSuite) TestAccountToAS() {
testAccount := suite.accounts["local_account_1"] // take zork for this test
- asPerson, err := suite.typeconverter.AccountToAS(testAccount)
+ asPerson, err := suite.typeconverter.AccountToAS(context.Background(), testAccount)
assert.NoError(suite.T(), err)
ser, err := streams.Serialize(asPerson)
diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go
index caa14e211..89da9eb01 100644
--- a/internal/typeutils/internaltofrontend.go
+++ b/internal/typeutils/internaltofrontend.go
@@ -19,6 +19,7 @@
package typeutils
import (
+ "context"
"fmt"
"strings"
"time"
@@ -28,9 +29,9 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
-func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account, error) {
+func (c *converter) AccountToMastoSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
// we can build this sensitive account easily by first getting the public account....
- mastoAccount, err := c.AccountToMastoPublic(a)
+ mastoAccount, err := c.AccountToMastoPublic(ctx, a)
if err != nil {
return nil, err
}
@@ -38,7 +39,7 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account
// then adding the Source object to it...
// check pending follow requests aimed at this account
- frs, err := c.db.GetAccountFollowRequests(a.ID)
+ frs, err := c.db.GetAccountFollowRequests(ctx, a.ID)
if err != nil {
if err != db.ErrNoEntries {
return nil, fmt.Errorf("error getting follow requests: %s", err)
@@ -50,7 +51,7 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account
}
mastoAccount.Source = &model.Source{
- Privacy: c.VisToMasto(a.Privacy),
+ Privacy: c.VisToMasto(ctx, a.Privacy),
Sensitive: a.Sensitive,
Language: a.Language,
Note: a.Note,
@@ -61,7 +62,11 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account
return mastoAccount, nil
}
-func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, error) {
+func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
+ if a == nil {
+ return nil, fmt.Errorf("given account was nil")
+ }
+
// first check if we have this account in our frontEnd cache
if accountI, err := c.frontendCache.Fetch(a.ID); err == nil {
if account, ok := accountI.(*model.Account); ok {
@@ -71,26 +76,26 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
}
// count followers
- followersCount, err := c.db.CountAccountFollowedBy(a.ID, false)
+ followersCount, err := c.db.CountAccountFollowedBy(ctx, a.ID, false)
if err != nil {
return nil, fmt.Errorf("error counting followers: %s", err)
}
// count following
- followingCount, err := c.db.CountAccountFollows(a.ID, false)
+ followingCount, err := c.db.CountAccountFollows(ctx, a.ID, false)
if err != nil {
return nil, fmt.Errorf("error counting following: %s", err)
}
// count statuses
- statusesCount, err := c.db.CountAccountStatuses(a.ID)
+ statusesCount, err := c.db.CountAccountStatuses(ctx, a.ID)
if err != nil {
return nil, fmt.Errorf("error counting statuses: %s", err)
}
// check when the last status was
var lastStatusAt string
- lastPosted, err := c.db.GetAccountLastPosted(a.ID)
+ lastPosted, err := c.db.GetAccountLastPosted(ctx, a.ID)
if err == nil && !lastPosted.IsZero() {
lastStatusAt = lastPosted.Format(time.RFC3339)
}
@@ -101,7 +106,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
if a.AvatarMediaAttachmentID != "" {
// make sure avi is pinned to this account
if a.AvatarMediaAttachment == nil {
- avi, err := c.db.GetAttachmentByID(a.AvatarMediaAttachmentID)
+ avi, err := c.db.GetAttachmentByID(ctx, a.AvatarMediaAttachmentID)
if err != nil {
return nil, fmt.Errorf("error retrieving avatar: %s", err)
}
@@ -116,7 +121,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
if a.HeaderMediaAttachmentID != "" {
// make sure header is pinned to this account
if a.HeaderMediaAttachment == nil {
- avi, err := c.db.GetAttachmentByID(a.HeaderMediaAttachmentID)
+ avi, err := c.db.GetAttachmentByID(ctx, a.HeaderMediaAttachmentID)
if err != nil {
return nil, fmt.Errorf("error retrieving avatar: %s", err)
}
@@ -187,7 +192,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
return accountFrontend, nil
}
-func (c *converter) AccountToMastoBlocked(a *gtsmodel.Account) (*model.Account, error) {
+func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
var acct string
if a.Domain != "" {
// this is a remote user
@@ -214,7 +219,7 @@ func (c *converter) AccountToMastoBlocked(a *gtsmodel.Account) (*model.Account,
}, nil
}
-func (c *converter) AppToMastoSensitive(a *gtsmodel.Application) (*model.Application, error) {
+func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
return &model.Application{
ID: a.ID,
Name: a.Name,
@@ -226,14 +231,14 @@ func (c *converter) AppToMastoSensitive(a *gtsmodel.Application) (*model.Applica
}, nil
}
-func (c *converter) AppToMastoPublic(a *gtsmodel.Application) (*model.Application, error) {
+func (c *converter) AppToMastoPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
return &model.Application{
Name: a.Name,
Website: a.Website,
}, nil
}
-func (c *converter) AttachmentToMasto(a *gtsmodel.MediaAttachment) (model.Attachment, error) {
+func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) {
return model.Attachment{
ID: a.ID,
Type: strings.ToLower(string(a.Type)),
@@ -264,33 +269,36 @@ func (c *converter) AttachmentToMasto(a *gtsmodel.MediaAttachment) (model.Attach
}, nil
}
-func (c *converter) MentionToMasto(m *gtsmodel.Mention) (model.Mention, error) {
- target := &gtsmodel.Account{}
- if err := c.db.GetByID(m.TargetAccountID, target); err != nil {
- return model.Mention{}, err
+func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) {
+ if m.TargetAccount == nil {
+ targetAccount, err := c.db.GetAccountByID(ctx, m.TargetAccountID)
+ if err != nil {
+ return model.Mention{}, err
+ }
+ m.TargetAccount = targetAccount
}
var local bool
- if target.Domain == "" {
+ if m.TargetAccount.Domain == "" {
local = true
}
var acct string
if local {
- acct = target.Username
+ acct = m.TargetAccount.Username
} else {
- acct = fmt.Sprintf("%s@%s", target.Username, target.Domain)
+ acct = fmt.Sprintf("%s@%s", m.TargetAccount.Username, m.TargetAccount.Domain)
}
return model.Mention{
- ID: target.ID,
- Username: target.Username,
- URL: target.URL,
+ ID: m.TargetAccount.ID,
+ Username: m.TargetAccount.Username,
+ URL: m.TargetAccount.URL,
Acct: acct,
}, nil
}
-func (c *converter) EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error) {
+func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) {
return model.Emoji{
Shortcode: e.Shortcode,
URL: e.ImageURL,
@@ -300,27 +308,25 @@ func (c *converter) EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error) {
}, nil
}
-func (c *converter) TagToMasto(t *gtsmodel.Tag) (model.Tag, error) {
- tagURL := fmt.Sprintf("%s://%s/tags/%s", c.config.Protocol, c.config.Host, t.Name)
-
+func (c *converter) TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) {
return model.Tag{
Name: t.Name,
- URL: tagURL, // we don't serve URLs with collections of tagged statuses (FOR NOW) so this is purely for mastodon compatibility ¯\_(ツ)_/¯
+ URL: t.URL,
}, nil
}
-func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
- repliesCount, err := c.db.CountStatusReplies(s)
+func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
+ repliesCount, err := c.db.CountStatusReplies(ctx, s)
if err != nil {
return nil, fmt.Errorf("error counting replies: %s", err)
}
- reblogsCount, err := c.db.CountStatusReblogs(s)
+ reblogsCount, err := c.db.CountStatusReblogs(ctx, s)
if err != nil {
return nil, fmt.Errorf("error counting reblogs: %s", err)
}
- favesCount, err := c.db.CountStatusFaves(s)
+ favesCount, err := c.db.CountStatusFaves(ctx, s)
if err != nil {
return nil, fmt.Errorf("error counting faves: %s", err)
}
@@ -330,8 +336,8 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// the boosted status might have been set on this struct already so check first before doing db calls
if s.BoostOf == nil {
// it's not set so fetch it from the db
- bs := &gtsmodel.Status{}
- if err := c.db.GetByID(s.BoostOfID, bs); err != nil {
+ bs, err := c.db.GetStatusByID(ctx, s.BoostOfID)
+ if err != nil {
return nil, fmt.Errorf("error getting boosted status with id %s: %s", s.BoostOfID, err)
}
s.BoostOf = bs
@@ -340,15 +346,15 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// the boosted account might have been set on this struct already or passed as a param so check first before doing db calls
if s.BoostOfAccount == nil {
// it's not set so fetch it from the db
- ba := &gtsmodel.Account{}
- if err := c.db.GetByID(s.BoostOf.AccountID, ba); err != nil {
+ ba, err := c.db.GetAccountByID(ctx, s.BoostOf.AccountID)
+ if err != nil {
return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", s.BoostOf.AccountID, s.BoostOfID, err)
}
s.BoostOfAccount = ba
s.BoostOf.Account = ba
}
- mastoRebloggedStatus, err = c.StatusToMasto(s.BoostOf, requestingAccount)
+ mastoRebloggedStatus, err = c.StatusToMasto(ctx, s.BoostOf, requestingAccount)
if err != nil {
return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err)
}
@@ -357,24 +363,24 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
var mastoApplication *model.Application
if s.CreatedWithApplicationID != "" {
gtsApplication := &gtsmodel.Application{}
- if err := c.db.GetByID(s.CreatedWithApplicationID, gtsApplication); err != nil {
+ if err := c.db.GetByID(ctx, s.CreatedWithApplicationID, gtsApplication); err != nil {
return nil, fmt.Errorf("error fetching application used to create status: %s", err)
}
- mastoApplication, err = c.AppToMastoPublic(gtsApplication)
+ mastoApplication, err = c.AppToMastoPublic(ctx, gtsApplication)
if err != nil {
return nil, fmt.Errorf("error parsing application used to create status: %s", err)
}
}
if s.Account == nil {
- a := &gtsmodel.Account{}
- if err := c.db.GetByID(s.AccountID, a); err != nil {
+ a, err := c.db.GetAccountByID(ctx, s.AccountID)
+ if err != nil {
return nil, fmt.Errorf("error getting status author: %s", err)
}
s.Account = a
}
- mastoAuthorAccount, err := c.AccountToMastoPublic(s.Account)
+ mastoAuthorAccount, err := c.AccountToMastoPublic(ctx, s.Account)
if err != nil {
return nil, fmt.Errorf("error parsing account of status author: %s", err)
}
@@ -384,7 +390,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// if so, we can directly convert the gts attachments into masto ones
if s.Attachments != nil {
for _, gtsAttachment := range s.Attachments {
- mastoAttachment, err := c.AttachmentToMasto(gtsAttachment)
+ mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
if err != nil {
return nil, fmt.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
}
@@ -393,14 +399,14 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// the status doesn't have gts attachments on it, but it does have attachment IDs
// in this case, we need to pull the gts attachments from the db to convert them into masto ones
} else {
- for _, a := range s.AttachmentIDs {
- gtsAttachment := &gtsmodel.MediaAttachment{}
- if err := c.db.GetByID(a, gtsAttachment); err != nil {
- return nil, fmt.Errorf("error getting attachment with id %s: %s", a, err)
+ for _, aID := range s.AttachmentIDs {
+ gtsAttachment, err := c.db.GetAttachmentByID(ctx, aID)
+ if err != nil {
+ return nil, fmt.Errorf("error getting attachment with id %s: %s", aID, err)
}
- mastoAttachment, err := c.AttachmentToMasto(gtsAttachment)
+ mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
if err != nil {
- return nil, fmt.Errorf("error converting attachment with id %s: %s", a, err)
+ return nil, fmt.Errorf("error converting attachment with id %s: %s", aID, err)
}
mastoAttachments = append(mastoAttachments, mastoAttachment)
}
@@ -411,7 +417,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// if so, we can directly convert the gts mentions into masto ones
if s.Mentions != nil {
for _, gtsMention := range s.Mentions {
- mastoMention, err := c.MentionToMasto(gtsMention)
+ mastoMention, err := c.MentionToMasto(ctx, gtsMention)
if err != nil {
return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
}
@@ -420,12 +426,12 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// the status doesn't have gts mentions on it, but it does have mention IDs
// in this case, we need to pull the gts mentions from the db to convert them into masto ones
} else {
- for _, m := range s.MentionIDs {
- gtsMention := &gtsmodel.Mention{}
- if err := c.db.GetByID(m, gtsMention); err != nil {
- return nil, fmt.Errorf("error getting mention with id %s: %s", m, err)
+ for _, mID := range s.MentionIDs {
+ gtsMention, err := c.db.GetMention(ctx, mID)
+ if err != nil {
+ return nil, fmt.Errorf("error getting mention with id %s: %s", mID, err)
}
- mastoMention, err := c.MentionToMasto(gtsMention)
+ mastoMention, err := c.MentionToMasto(ctx, gtsMention)
if err != nil {
return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
}
@@ -438,7 +444,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// if so, we can directly convert the gts tags into masto ones
if s.Tags != nil {
for _, gtsTag := range s.Tags {
- mastoTag, err := c.TagToMasto(gtsTag)
+ mastoTag, err := c.TagToMasto(ctx, gtsTag)
if err != nil {
return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
}
@@ -449,10 +455,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
} else {
for _, t := range s.TagIDs {
gtsTag := &gtsmodel.Tag{}
- if err := c.db.GetByID(t, gtsTag); err != nil {
+ if err := c.db.GetByID(ctx, t, gtsTag); err != nil {
return nil, fmt.Errorf("error getting tag with id %s: %s", t, err)
}
- mastoTag, err := c.TagToMasto(gtsTag)
+ mastoTag, err := c.TagToMasto(ctx, gtsTag)
if err != nil {
return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
}
@@ -465,7 +471,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
// if so, we can directly convert the gts emojis into masto ones
if s.Emojis != nil {
for _, gtsEmoji := range s.Emojis {
- mastoEmoji, err := c.EmojiToMasto(gtsEmoji)
+ mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
if err != nil {
return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
}
@@ -476,10 +482,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
} else {
for _, e := range s.EmojiIDs {
gtsEmoji := &gtsmodel.Emoji{}
- if err := c.db.GetByID(e, gtsEmoji); err != nil {
+ if err := c.db.GetByID(ctx, e, gtsEmoji); err != nil {
return nil, fmt.Errorf("error getting emoji with id %s: %s", e, err)
}
- mastoEmoji, err := c.EmojiToMasto(gtsEmoji)
+ mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
if err != nil {
return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
}
@@ -491,7 +497,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
var mastoPoll *model.Poll
statusInteractions := &statusInteractions{}
- si, err := c.interactionsWithStatusForAccount(s, requestingAccount)
+ si, err := c.interactionsWithStatusForAccount(ctx, s, requestingAccount)
if err == nil {
statusInteractions = si
}
@@ -503,7 +509,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
InReplyToAccountID: s.InReplyToAccountID,
Sensitive: s.Sensitive,
SpoilerText: s.ContentWarning,
- Visibility: c.VisToMasto(s.Visibility),
+ Visibility: c.VisToMasto(ctx, s.Visibility),
Language: s.Language,
URI: s.URI,
URL: s.URL,
@@ -535,7 +541,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
}
// VisToMasto converts a gts visibility into its mastodon equivalent
-func (c *converter) VisToMasto(m gtsmodel.Visibility) model.Visibility {
+func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility {
switch m {
case gtsmodel.VisibilityPublic:
return model.VisibilityPublic
@@ -549,7 +555,7 @@ func (c *converter) VisToMasto(m gtsmodel.Visibility) model.Visibility {
return ""
}
-func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, error) {
+func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) {
mi := &model.Instance{
URI: i.URI,
Title: i.Title,
@@ -567,17 +573,17 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro
statusCountKey := "status_count"
domainCountKey := "domain_count"
- userCount, err := c.db.CountInstanceUsers(c.config.Host)
+ userCount, err := c.db.CountInstanceUsers(ctx, c.config.Host)
if err == nil {
mi.Stats[userCountKey] = userCount
}
- statusCount, err := c.db.CountInstanceStatuses(c.config.Host)
+ statusCount, err := c.db.CountInstanceStatuses(ctx, c.config.Host)
if err == nil {
mi.Stats[statusCountKey] = statusCount
}
- domainCount, err := c.db.CountInstanceDomains(c.config.Host)
+ domainCount, err := c.db.CountInstanceDomains(ctx, c.config.Host)
if err == nil {
mi.Stats[domainCountKey] = domainCount
}
@@ -593,7 +599,7 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro
}
// get the instance account if it exists and just skip if it doesn't
- ia, err := c.db.GetInstanceAccount("")
+ ia, err := c.db.GetInstanceAccount(ctx, "")
if err == nil {
if ia.HeaderMediaAttachment != nil {
mi.Thumbnail = ia.HeaderMediaAttachment.URL
@@ -602,19 +608,22 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro
// contact account is optional but let's try to get it
if i.ContactAccountID != "" {
- ia := &gtsmodel.Account{}
- if err := c.db.GetByID(i.ContactAccountID, ia); err == nil {
- ma, err := c.AccountToMastoPublic(ia)
+ if i.ContactAccount == nil {
+ contactAccount, err := c.db.GetAccountByID(ctx, i.ContactAccountID)
if err == nil {
- mi.ContactAccount = ma
+ i.ContactAccount = contactAccount
}
}
+ ma, err := c.AccountToMastoPublic(ctx, i.ContactAccount)
+ if err == nil {
+ mi.ContactAccount = ma
+ }
}
return mi, nil
}
-func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relationship, error) {
+func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) {
return &model.Relationship{
ID: r.ID,
Following: r.Following,
@@ -632,9 +641,9 @@ func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relati
}, nil
}
-func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) {
+func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) {
if n.TargetAccount == nil {
- tAccount, err := c.db.GetAccountByID(n.TargetAccountID)
+ tAccount, err := c.db.GetAccountByID(ctx, n.TargetAccountID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
}
@@ -642,14 +651,14 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
}
if n.OriginAccount == nil {
- ogAccount, err := c.db.GetAccountByID(n.OriginAccountID)
+ ogAccount, err := c.db.GetAccountByID(ctx, n.OriginAccountID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting origin account with id %s from the db: %s", n.OriginAccountID, err)
}
n.OriginAccount = ogAccount
}
- mastoAccount, err := c.AccountToMastoPublic(n.OriginAccount)
+ mastoAccount, err := c.AccountToMastoPublic(ctx, n.OriginAccount)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err)
}
@@ -657,7 +666,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
var mastoStatus *model.Status
if n.StatusID != "" {
if n.Status == nil {
- status, err := c.db.GetStatusByID(n.StatusID)
+ status, err := c.db.GetStatusByID(ctx, n.StatusID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting status with id %s from the db: %s", n.StatusID, err)
}
@@ -673,7 +682,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
}
var err error
- mastoStatus, err = c.StatusToMasto(n.Status, nil)
+ mastoStatus, err = c.StatusToMasto(ctx, n.Status, nil)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err)
}
@@ -688,7 +697,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
}, nil
}
-func (c *converter) DomainBlockToMasto(b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) {
+func (c *converter) DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) {
domainBlock := &model.DomainBlock{
Domain: b.Domain,
diff --git a/internal/typeutils/util.go b/internal/typeutils/util.go
index 5751fbc84..1d1903afc 100644
--- a/internal/typeutils/util.go
+++ b/internal/typeutils/util.go
@@ -1,34 +1,35 @@
package typeutils
import (
+ "context"
"fmt"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
-func (c *converter) interactionsWithStatusForAccount(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) {
+func (c *converter) interactionsWithStatusForAccount(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) {
si := &statusInteractions{}
if requestingAccount != nil {
- faved, err := c.db.IsStatusFavedBy(s, requestingAccount.ID)
+ faved, err := c.db.IsStatusFavedBy(ctx, s, requestingAccount.ID)
if err != nil {
return nil, fmt.Errorf("error checking if requesting account has faved status: %s", err)
}
si.Faved = faved
- reblogged, err := c.db.IsStatusRebloggedBy(s, requestingAccount.ID)
+ reblogged, err := c.db.IsStatusRebloggedBy(ctx, s, requestingAccount.ID)
if err != nil {
return nil, fmt.Errorf("error checking if requesting account has reblogged status: %s", err)
}
si.Reblogged = reblogged
- muted, err := c.db.IsStatusMutedBy(s, requestingAccount.ID)
+ muted, err := c.db.IsStatusMutedBy(ctx, s, requestingAccount.ID)
if err != nil {
return nil, fmt.Errorf("error checking if requesting account has muted status: %s", err)
}
si.Muted = muted
- bookmarked, err := c.db.IsStatusBookmarkedBy(s, requestingAccount.ID)
+ bookmarked, err := c.db.IsStatusBookmarkedBy(ctx, s, requestingAccount.ID)
if err != nil {
return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err)
}