mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
Database updates (#144)
* start moving some database stuff around * continue moving db stuff around * more fiddling * more updates * and some more * and yet more * i broke SOMETHING but what, it's a mystery * tidy up * vendor ttlcache * use ttlcache * fix up some tests * rename some stuff * little reminder * some more updates
This commit is contained in:
@ -37,21 +37,20 @@ func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update
|
||||
}
|
||||
uri := uriProp.GetIRI()
|
||||
|
||||
acct := >smodel.Account{}
|
||||
if !update {
|
||||
err := c.db.GetWhere([]db.Where{{Key: "uri", Value: uri.String()}}, acct)
|
||||
acct, err := c.db.GetAccountByURI(uri.String())
|
||||
if err == nil {
|
||||
// we already know this account so we can skip generating it
|
||||
return acct, nil
|
||||
}
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
if err != db.ErrNoEntries {
|
||||
// we don't know the account and there's been a real error
|
||||
return nil, fmt.Errorf("error getting account with uri %s from the database: %s", uri.String(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// we don't know the account, or we're being told to update it, so we need to generate it from the person -- at least we already have the URI!
|
||||
acct = >smodel.Account{}
|
||||
acct := >smodel.Account{}
|
||||
acct.URI = uri.String()
|
||||
|
||||
// Username aka preferredUsername
|
||||
@ -188,22 +187,22 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
|
||||
|
||||
// attachments to dereference and fetch later on (we don't do that here)
|
||||
if attachments, err := ap.ExtractAttachments(statusable); err == nil {
|
||||
status.GTSMediaAttachments = attachments
|
||||
status.Attachments = attachments
|
||||
}
|
||||
|
||||
// hashtags to dereference later on
|
||||
if hashtags, err := ap.ExtractHashtags(statusable); err == nil {
|
||||
status.GTSTags = hashtags
|
||||
status.Tags = hashtags
|
||||
}
|
||||
|
||||
// emojis to dereference and fetch later on
|
||||
if emojis, err := ap.ExtractEmojis(statusable); err == nil {
|
||||
status.GTSEmojis = emojis
|
||||
status.Emojis = emojis
|
||||
}
|
||||
|
||||
// mentions to dereference later on
|
||||
if mentions, err := ap.ExtractMentions(statusable); err == nil {
|
||||
status.GTSMentions = mentions
|
||||
status.Mentions = mentions
|
||||
}
|
||||
|
||||
// cw string for this status
|
||||
@ -225,13 +224,13 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status
|
||||
}
|
||||
status.AccountURI = attributedTo.String()
|
||||
|
||||
statusOwner := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: attributedTo.String(), CaseInsensitive: true}}, statusOwner); err != nil {
|
||||
statusOwner, err := c.db.GetAccountByURI(attributedTo.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't get status owner from db: %s", err)
|
||||
}
|
||||
status.AccountID = statusOwner.ID
|
||||
status.AccountURI = statusOwner.URI
|
||||
status.GTSAuthorAccount = statusOwner
|
||||
status.Account = statusOwner
|
||||
|
||||
// check if there's a post that this is a reply to
|
||||
inReplyToURI := ap.ExtractInReplyToURI(statusable)
|
||||
@ -241,18 +240,16 @@ 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
|
||||
inReplyToStatus := >smodel.Status{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: inReplyToURI.String()}}, inReplyToStatus); err == nil {
|
||||
if inReplyToStatus, err := c.db.GetStatusByURI(inReplyToURI.String()); err == nil {
|
||||
// we have the status in our database already
|
||||
// so we can set these fields here and then...
|
||||
// so we can set these fields here and now...
|
||||
status.InReplyToID = inReplyToStatus.ID
|
||||
status.InReplyToAccountID = inReplyToStatus.AccountID
|
||||
status.GTSReplyToStatus = inReplyToStatus
|
||||
|
||||
// ... check if we've seen the account already
|
||||
inReplyToAccount := >smodel.Account{}
|
||||
if err := c.db.GetByID(inReplyToStatus.AccountID, inReplyToAccount); err == nil {
|
||||
status.GTSReplyToAccount = inReplyToAccount
|
||||
status.InReplyTo = inReplyToStatus
|
||||
if status.InReplyToAccount == nil {
|
||||
if inReplyToAccount, err := c.db.GetAccountByID(inReplyToStatus.AccountID); err == nil {
|
||||
status.InReplyToAccount = inReplyToAccount
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -328,8 +325,8 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel
|
||||
if err != nil {
|
||||
return nil, errors.New("error extracting actor property from follow")
|
||||
}
|
||||
originAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: origin.String()}}, originAccount); err != nil {
|
||||
originAccount, err := c.db.GetAccountByURI(origin.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
@ -337,8 +334,8 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel
|
||||
if err != nil {
|
||||
return nil, errors.New("error extracting object property from follow")
|
||||
}
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: target.String()}}, targetAccount); err != nil {
|
||||
targetAccount, err := c.db.GetAccountByURI(target.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
@ -362,8 +359,8 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow
|
||||
if err != nil {
|
||||
return nil, errors.New("error extracting actor property from follow")
|
||||
}
|
||||
originAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: origin.String()}}, originAccount); err != nil {
|
||||
originAccount, err := c.db.GetAccountByURI(origin.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
@ -371,8 +368,8 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow
|
||||
if err != nil {
|
||||
return nil, errors.New("error extracting object property from follow")
|
||||
}
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: target.String()}}, targetAccount); err != nil {
|
||||
targetAccount, err := c.db.GetAccountByURI(target.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
@ -396,8 +393,8 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
|
||||
if err != nil {
|
||||
return nil, errors.New("error extracting actor property from like")
|
||||
}
|
||||
originAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: origin.String()}}, originAccount); err != nil {
|
||||
originAccount, err := c.db.GetAccountByURI(origin.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
@ -406,24 +403,30 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er
|
||||
return nil, errors.New("error extracting object property from like")
|
||||
}
|
||||
|
||||
targetStatus := >smodel.Status{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: target.String()}}, targetStatus); err != nil {
|
||||
targetStatus, err := c.db.GetStatusByURI(target.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting status with uri %s from the database: %s", target.String(), err)
|
||||
}
|
||||
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := c.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err)
|
||||
var targetAccount *gtsmodel.Account
|
||||
if targetStatus.Account != nil {
|
||||
targetAccount = targetStatus.Account
|
||||
} else {
|
||||
a, err := c.db.GetAccountByID(targetStatus.AccountID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err)
|
||||
}
|
||||
targetAccount = a
|
||||
}
|
||||
|
||||
return >smodel.StatusFave{
|
||||
TargetAccountID: targetAccount.ID,
|
||||
StatusID: targetStatus.ID,
|
||||
AccountID: originAccount.ID,
|
||||
URI: uri,
|
||||
GTSStatus: targetStatus,
|
||||
GTSTargetAccount: targetAccount,
|
||||
GTSFavingAccount: originAccount,
|
||||
AccountID: originAccount.ID,
|
||||
Account: originAccount,
|
||||
TargetAccountID: targetAccount.ID,
|
||||
TargetAccount: targetAccount,
|
||||
StatusID: targetStatus.ID,
|
||||
Status: targetStatus,
|
||||
URI: uri,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -438,9 +441,9 @@ 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 := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: origin.String()}}, originAccount); err != nil {
|
||||
return nil, fmt.Errorf("ASBlockToBlock: error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
originAccount, err := c.db.GetAccountByURI(origin.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
target, err := ap.ExtractObject(blockable)
|
||||
@ -448,9 +451,9 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err
|
||||
return nil, errors.New("ASBlockToBlock: error extracting object property from block")
|
||||
}
|
||||
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: target.String(), CaseInsensitive: true}}, targetAccount); err != nil {
|
||||
return nil, fmt.Errorf("ASBlockToBlock: error extracting account with uri %s from the database: %s", target.String(), err)
|
||||
targetAccount, err := c.db.GetAccountByURI(target.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
||||
}
|
||||
|
||||
return >smodel.Block{
|
||||
@ -473,7 +476,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.
|
||||
}
|
||||
uri := idProp.GetIRI().String()
|
||||
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: uri}}, status); err == nil {
|
||||
if status, err := c.db.GetStatusByURI(uri); err == nil {
|
||||
// we already have it, great, just return it as-is :)
|
||||
isNew = false
|
||||
return status, isNew, nil
|
||||
@ -487,7 +490,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.
|
||||
}
|
||||
|
||||
// set the URI on the new status for dereferencing later
|
||||
status.GTSBoostedStatus = >smodel.Status{
|
||||
status.BoostOf = >smodel.Status{
|
||||
URI: boostedStatusURI.String(),
|
||||
}
|
||||
|
||||
@ -507,18 +510,19 @@ 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 := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: actor.String()}}, boostingAccount); err != nil {
|
||||
boostingAccount, err := c.db.GetAccountByURI(actor.String())
|
||||
if err != nil {
|
||||
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error in db fetching account with uri %s: %s", actor.String(), err)
|
||||
}
|
||||
status.AccountID = boostingAccount.ID
|
||||
status.AccountURI = boostingAccount.URI
|
||||
status.Account = boostingAccount
|
||||
|
||||
// these will all be wrapped in the boosted status so set them empty here
|
||||
status.Attachments = []string{}
|
||||
status.Tags = []string{}
|
||||
status.Mentions = []string{}
|
||||
status.Emojis = []string{}
|
||||
status.AttachmentIDs = []string{}
|
||||
status.TagIDs = []string{}
|
||||
status.MentionIDs = []string{}
|
||||
status.EmojiIDs = []string{}
|
||||
|
||||
// parse the visibility from the To and CC entries
|
||||
var visibility gtsmodel.Visibility
|
||||
@ -552,7 +556,6 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.
|
||||
status.Visibility = visibility
|
||||
|
||||
// the rest of the fields will be taken from the target status, but it's not our job to do the dereferencing here
|
||||
|
||||
return status, isNew, nil
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"github.com/go-fed/activity/streams/vocab"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/cache"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
@ -175,14 +176,18 @@ type TypeConverter interface {
|
||||
}
|
||||
|
||||
type converter struct {
|
||||
config *config.Config
|
||||
db db.DB
|
||||
config *config.Config
|
||||
db db.DB
|
||||
frontendCache cache.Cache
|
||||
asCache cache.Cache
|
||||
}
|
||||
|
||||
// NewConverter returns a new Converter
|
||||
func NewConverter(config *config.Config, db db.DB) TypeConverter {
|
||||
return &converter{
|
||||
config: config,
|
||||
db: db,
|
||||
config: config,
|
||||
db: db,
|
||||
frontendCache: cache.New(),
|
||||
asCache: cache.New(),
|
||||
}
|
||||
}
|
||||
|
@ -54,10 +54,10 @@ func (c *converter) StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.
|
||||
InReplyToAccountID: "",
|
||||
|
||||
// these will all be wrapped in the boosted status so set them empty here
|
||||
Attachments: []string{},
|
||||
Tags: []string{},
|
||||
Mentions: []string{},
|
||||
Emojis: []string{},
|
||||
AttachmentIDs: []string{},
|
||||
TagIDs: []string{},
|
||||
MentionIDs: []string{},
|
||||
EmojiIDs: []string{},
|
||||
|
||||
// the below fields will be taken from the target status
|
||||
Content: s.Content,
|
||||
@ -74,7 +74,7 @@ func (c *converter) StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.
|
||||
// attach these here for convenience -- the boosted status/account won't go in the DB
|
||||
// but they're needed in the processor and for the frontend. Since we have them, we can
|
||||
// attach them so we don't need to fetch them again later (save some DB calls)
|
||||
GTSBoostedStatus: s,
|
||||
BoostOf: s,
|
||||
}
|
||||
|
||||
return boostWrapperStatus, nil
|
||||
|
@ -34,6 +34,14 @@ 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) {
|
||||
// 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 {
|
||||
// we have it, so just return it as-is
|
||||
return person, nil
|
||||
}
|
||||
}
|
||||
|
||||
person := streams.NewActivityStreamsPerson()
|
||||
|
||||
// id should be the activitypub URI of this user
|
||||
@ -256,6 +264,11 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso
|
||||
person.SetActivityStreamsImage(headerProperty)
|
||||
}
|
||||
|
||||
// put the person in our cache in case we need it again soon
|
||||
if err := c.asCache.Store(a.ID, person); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return person, nil
|
||||
}
|
||||
|
||||
@ -326,16 +339,24 @@ func (c *converter) AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStrea
|
||||
}
|
||||
|
||||
func (c *converter) StatusToAS(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 {
|
||||
// we have it, so just return it as-is
|
||||
return note, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ensure prerequisites here before we get stuck in
|
||||
|
||||
// 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.GTSAuthorAccount == nil {
|
||||
a := >smodel.Account{}
|
||||
if err := c.db.GetByID(s.AccountID, a); err != nil {
|
||||
if s.Account == nil {
|
||||
a, err := c.db.GetAccountByID(s.AccountID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error retrieving author account from db: %s", err)
|
||||
}
|
||||
s.GTSAuthorAccount = a
|
||||
s.Account = a
|
||||
}
|
||||
|
||||
// create the Note!
|
||||
@ -361,16 +382,16 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
// inReplyTo
|
||||
if s.InReplyToID != "" {
|
||||
// fetch the replied status if we don't have it on hand already
|
||||
if s.GTSReplyToStatus == nil {
|
||||
if s.InReplyTo == nil {
|
||||
rs := >smodel.Status{}
|
||||
if err := c.db.GetByID(s.InReplyToID, rs); err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error retrieving replied-to status from db: %s", err)
|
||||
}
|
||||
s.GTSReplyToStatus = rs
|
||||
s.InReplyTo = rs
|
||||
}
|
||||
rURI, err := url.Parse(s.GTSReplyToStatus.URI)
|
||||
rURI, err := url.Parse(s.InReplyTo.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.GTSReplyToStatus.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.InReplyTo.URI, err)
|
||||
}
|
||||
|
||||
inReplyToProp := streams.NewActivityStreamsInReplyToProperty()
|
||||
@ -396,9 +417,9 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
}
|
||||
|
||||
// attributedTo
|
||||
authorAccountURI, err := url.Parse(s.GTSAuthorAccount.URI)
|
||||
authorAccountURI, err := url.Parse(s.Account.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.GTSAuthorAccount.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.Account.URI, err)
|
||||
}
|
||||
attributedToProp := streams.NewActivityStreamsAttributedToProperty()
|
||||
attributedToProp.AppendIRI(authorAccountURI)
|
||||
@ -408,7 +429,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
tagProp := streams.NewActivityStreamsTagProperty()
|
||||
|
||||
// tag -- mentions
|
||||
for _, m := range s.GTSMentions {
|
||||
for _, m := range s.Mentions {
|
||||
asMention, err := c.MentionToAS(m)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error converting mention to AS mention: %s", err)
|
||||
@ -425,9 +446,9 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
status.SetActivityStreamsTag(tagProp)
|
||||
|
||||
// parse out some URIs we need here
|
||||
authorFollowersURI, err := url.Parse(s.GTSAuthorAccount.FollowersURI)
|
||||
authorFollowersURI, err := url.Parse(s.Account.FollowersURI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.GTSAuthorAccount.FollowersURI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.Account.FollowersURI, err)
|
||||
}
|
||||
|
||||
publicURI, err := url.Parse(asPublicURI)
|
||||
@ -441,10 +462,10 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
switch s.Visibility {
|
||||
case gtsmodel.VisibilityDirect:
|
||||
// if DIRECT, then only mentioned users should be added to TO, and nothing to CC
|
||||
for _, m := range s.GTSMentions {
|
||||
iri, err := url.Parse(m.GTSAccount.URI)
|
||||
for _, m := range s.Mentions {
|
||||
iri, err := url.Parse(m.OriginAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.GTSAccount.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
|
||||
}
|
||||
toProp.AppendIRI(iri)
|
||||
}
|
||||
@ -453,10 +474,10 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
case gtsmodel.VisibilityFollowersOnly:
|
||||
// if FOLLOWERS ONLY then we want to add followers to TO, and mentions to CC
|
||||
toProp.AppendIRI(authorFollowersURI)
|
||||
for _, m := range s.GTSMentions {
|
||||
iri, err := url.Parse(m.GTSAccount.URI)
|
||||
for _, m := range s.Mentions {
|
||||
iri, err := url.Parse(m.OriginAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.GTSAccount.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
|
||||
}
|
||||
ccProp.AppendIRI(iri)
|
||||
}
|
||||
@ -464,10 +485,10 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
// if UNLOCKED, we want to add followers to TO, and public and mentions to CC
|
||||
toProp.AppendIRI(authorFollowersURI)
|
||||
ccProp.AppendIRI(publicURI)
|
||||
for _, m := range s.GTSMentions {
|
||||
iri, err := url.Parse(m.GTSAccount.URI)
|
||||
for _, m := range s.Mentions {
|
||||
iri, err := url.Parse(m.OriginAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.GTSAccount.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
|
||||
}
|
||||
ccProp.AppendIRI(iri)
|
||||
}
|
||||
@ -475,10 +496,10 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
// if PUBLIC, we want to add public to TO, and followers and mentions to CC
|
||||
toProp.AppendIRI(publicURI)
|
||||
ccProp.AppendIRI(authorFollowersURI)
|
||||
for _, m := range s.GTSMentions {
|
||||
iri, err := url.Parse(m.GTSAccount.URI)
|
||||
for _, m := range s.Mentions {
|
||||
iri, err := url.Parse(m.OriginAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.GTSAccount.URI, err)
|
||||
return nil, fmt.Errorf("StatusToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
|
||||
}
|
||||
ccProp.AppendIRI(iri)
|
||||
}
|
||||
@ -496,7 +517,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
|
||||
// attachment
|
||||
attachmentProp := streams.NewActivityStreamsAttachmentProperty()
|
||||
for _, a := range s.GTSMediaAttachments {
|
||||
for _, a := range s.Attachments {
|
||||
doc, err := c.AttachmentToAS(a)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StatusToAS: error converting attachment: %s", err)
|
||||
@ -515,6 +536,11 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e
|
||||
repliesProp.SetActivityStreamsCollection(repliesCollection)
|
||||
status.SetActivityStreamsReplies(repliesProp)
|
||||
|
||||
// put the note in our cache in case we need it again soon
|
||||
if err := c.asCache.Store(s.ID, status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
@ -565,12 +591,12 @@ func (c *converter) FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Accou
|
||||
}
|
||||
|
||||
func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) {
|
||||
if m.GTSAccount == nil {
|
||||
if m.OriginAccount == nil {
|
||||
a := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]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.GTSAccount = a
|
||||
m.OriginAccount = a
|
||||
}
|
||||
|
||||
// create the mention
|
||||
@ -578,21 +604,21 @@ func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMenti
|
||||
|
||||
// href -- this should be the URI of the mentioned user
|
||||
hrefProp := streams.NewActivityStreamsHrefProperty()
|
||||
hrefURI, err := url.Parse(m.GTSAccount.URI)
|
||||
hrefURI, err := url.Parse(m.OriginAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MentionToAS: error parsing uri %s: %s", m.GTSAccount.URI, err)
|
||||
return nil, fmt.Errorf("MentionToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
|
||||
}
|
||||
hrefProp.SetIRI(hrefURI)
|
||||
mention.SetActivityStreamsHref(hrefProp)
|
||||
|
||||
// name -- this should be the namestring of the mentioned user, something like @whatever@example.org
|
||||
var domain string
|
||||
if m.GTSAccount.Domain == "" {
|
||||
if m.OriginAccount.Domain == "" {
|
||||
domain = c.config.AccountDomain
|
||||
} else {
|
||||
domain = m.GTSAccount.Domain
|
||||
domain = m.OriginAccount.Domain
|
||||
}
|
||||
username := m.GTSAccount.Username
|
||||
username := m.OriginAccount.Username
|
||||
nameString := fmt.Sprintf("@%s@%s", username, domain)
|
||||
nameProp := streams.NewActivityStreamsNameProperty()
|
||||
nameProp.AppendXMLSchemaString(nameString)
|
||||
@ -648,30 +674,30 @@ func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityS
|
||||
*/
|
||||
func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
|
||||
// check if targetStatus is already pinned to this fave, and fetch it if not
|
||||
if f.GTSStatus == nil {
|
||||
if f.Status == nil {
|
||||
s := >smodel.Status{}
|
||||
if err := c.db.GetByID(f.StatusID, s); err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error fetching target status from database: %s", err)
|
||||
}
|
||||
f.GTSStatus = s
|
||||
f.Status = s
|
||||
}
|
||||
|
||||
// check if the targetAccount is already pinned to this fave, and fetch it if not
|
||||
if f.GTSTargetAccount == nil {
|
||||
if f.TargetAccount == nil {
|
||||
a := >smodel.Account{}
|
||||
if err := c.db.GetByID(f.TargetAccountID, a); err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error fetching target account from database: %s", err)
|
||||
}
|
||||
f.GTSTargetAccount = a
|
||||
f.TargetAccount = a
|
||||
}
|
||||
|
||||
// check if the faving account is already pinned to this fave, and fetch it if not
|
||||
if f.GTSFavingAccount == nil {
|
||||
if f.Account == nil {
|
||||
a := >smodel.Account{}
|
||||
if err := c.db.GetByID(f.AccountID, a); err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error fetching faving account from database: %s", err)
|
||||
}
|
||||
f.GTSFavingAccount = a
|
||||
f.Account = a
|
||||
}
|
||||
|
||||
// create the like
|
||||
@ -679,9 +705,9 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
|
||||
|
||||
// set the actor property to the fave-ing account's URI
|
||||
actorProp := streams.NewActivityStreamsActorProperty()
|
||||
actorIRI, err := url.Parse(f.GTSFavingAccount.URI)
|
||||
actorIRI, err := url.Parse(f.Account.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSFavingAccount.URI, err)
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.Account.URI, err)
|
||||
}
|
||||
actorProp.AppendIRI(actorIRI)
|
||||
like.SetActivityStreamsActor(actorProp)
|
||||
@ -697,18 +723,18 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
|
||||
|
||||
// set the object property to the target status's URI
|
||||
objectProp := streams.NewActivityStreamsObjectProperty()
|
||||
statusIRI, err := url.Parse(f.GTSStatus.URI)
|
||||
statusIRI, err := url.Parse(f.Status.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSStatus.URI, err)
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.Status.URI, err)
|
||||
}
|
||||
objectProp.AppendIRI(statusIRI)
|
||||
like.SetActivityStreamsObject(objectProp)
|
||||
|
||||
// set the TO property to the target account's IRI
|
||||
toProp := streams.NewActivityStreamsToProperty()
|
||||
toIRI, err := url.Parse(f.GTSTargetAccount.URI)
|
||||
toIRI, err := url.Parse(f.TargetAccount.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSTargetAccount.URI, err)
|
||||
return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.TargetAccount.URI, err)
|
||||
}
|
||||
toProp.AppendIRI(toIRI)
|
||||
like.SetActivityStreamsTo(toProp)
|
||||
@ -718,12 +744,12 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike,
|
||||
|
||||
func (c *converter) BoostToAS(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.GTSBoostedStatus == nil {
|
||||
if boostWrapperStatus.BoostOf == nil {
|
||||
b := >smodel.Status{}
|
||||
if err := c.db.GetByID(boostWrapperStatus.BoostOfID, b); err != nil {
|
||||
return nil, fmt.Errorf("BoostToAS: error getting status with ID %s from the db: %s", boostWrapperStatus.BoostOfID, err)
|
||||
}
|
||||
boostWrapperStatus.GTSBoostedStatus = b
|
||||
boostWrapperStatus.BoostOf = b
|
||||
}
|
||||
|
||||
// create the announce
|
||||
@ -748,9 +774,9 @@ func (c *converter) BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccou
|
||||
announce.SetJSONLDId(idProp)
|
||||
|
||||
// set the object
|
||||
boostedStatusURI, err := url.Parse(boostWrapperStatus.GTSBoostedStatus.URI)
|
||||
boostedStatusURI, err := url.Parse(boostWrapperStatus.BoostOf.URI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("BoostToAS: error parsing uri %s: %s", boostWrapperStatus.GTSBoostedStatus.URI, err)
|
||||
return nil, fmt.Errorf("BoostToAS: error parsing uri %s: %s", boostWrapperStatus.BoostOf.URI, err)
|
||||
}
|
||||
objectProp := streams.NewActivityStreamsObjectProperty()
|
||||
objectProp.AppendIRI(boostedStatusURI)
|
||||
|
@ -38,15 +38,15 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account
|
||||
// then adding the Source object to it...
|
||||
|
||||
// check pending follow requests aimed at this account
|
||||
fr := []gtsmodel.FollowRequest{}
|
||||
if err := c.db.GetFollowRequestsForAccountID(a.ID, &fr); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
frs, err := c.db.GetAccountFollowRequests(a.ID)
|
||||
if err != nil {
|
||||
if err != db.ErrNoEntries {
|
||||
return nil, fmt.Errorf("error getting follow requests: %s", err)
|
||||
}
|
||||
}
|
||||
var frc int
|
||||
if fr != nil {
|
||||
frc = len(fr)
|
||||
if frs != nil {
|
||||
frc = len(frs)
|
||||
}
|
||||
|
||||
mastoAccount.Source = &model.Source{
|
||||
@ -62,68 +62,69 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account
|
||||
}
|
||||
|
||||
func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, error) {
|
||||
// count followers
|
||||
followers := []gtsmodel.Follow{}
|
||||
if err := c.db.GetFollowersByAccountID(a.ID, &followers, false); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting followers: %s", err)
|
||||
// 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 {
|
||||
// we have it, so just return it as-is
|
||||
return account, nil
|
||||
}
|
||||
}
|
||||
var followersCount int
|
||||
if followers != nil {
|
||||
followersCount = len(followers)
|
||||
|
||||
// count followers
|
||||
followersCount, err := c.db.CountAccountFollowedBy(a.ID, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting followers: %s", err)
|
||||
}
|
||||
|
||||
// count following
|
||||
following := []gtsmodel.Follow{}
|
||||
if err := c.db.GetFollowingByAccountID(a.ID, &following); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting following: %s", err)
|
||||
}
|
||||
}
|
||||
var followingCount int
|
||||
if following != nil {
|
||||
followingCount = len(following)
|
||||
followingCount, err := c.db.CountAccountFollows(a.ID, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting following: %s", err)
|
||||
}
|
||||
|
||||
// count statuses
|
||||
statusesCount, err := c.db.CountStatusesByAccountID(a.ID)
|
||||
statusesCount, err := c.db.CountAccountStatuses(a.ID)
|
||||
if err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting last statuses: %s", err)
|
||||
}
|
||||
return nil, fmt.Errorf("error counting statuses: %s", err)
|
||||
}
|
||||
|
||||
// check when the last status was
|
||||
lastStatus := >smodel.Status{}
|
||||
if err := c.db.GetLastStatusForAccountID(a.ID, lastStatus); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting last status: %s", err)
|
||||
}
|
||||
}
|
||||
var lastStatusAt string
|
||||
if lastStatus != nil {
|
||||
lastStatusAt = lastStatus.CreatedAt.Format(time.RFC3339)
|
||||
lastPosted, err := c.db.GetAccountLastPosted(a.ID)
|
||||
if err == nil && !lastPosted.IsZero() {
|
||||
lastStatusAt = lastPosted.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
// build the avatar and header URLs
|
||||
avi := >smodel.MediaAttachment{}
|
||||
if err := c.db.GetAvatarForAccountID(avi, a.ID); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting avatar: %s", err)
|
||||
var aviURL string
|
||||
var aviURLStatic string
|
||||
if a.AvatarMediaAttachmentID != "" {
|
||||
// make sure avi is pinned to this account
|
||||
if a.AvatarMediaAttachment == nil {
|
||||
avi, err := c.db.GetAttachmentByID(a.AvatarMediaAttachmentID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error retrieving avatar: %s", err)
|
||||
}
|
||||
a.AvatarMediaAttachment = avi
|
||||
}
|
||||
aviURL = a.AvatarMediaAttachment.URL
|
||||
aviURLStatic = a.AvatarMediaAttachment.Thumbnail.URL
|
||||
}
|
||||
aviURL := avi.URL
|
||||
aviURLStatic := avi.Thumbnail.URL
|
||||
|
||||
header := >smodel.MediaAttachment{}
|
||||
if err := c.db.GetHeaderForAccountID(header, a.ID); err != nil {
|
||||
if _, ok := err.(db.ErrNoEntries); !ok {
|
||||
return nil, fmt.Errorf("error getting header: %s", err)
|
||||
var headerURL string
|
||||
var headerURLStatic string
|
||||
if a.HeaderMediaAttachmentID != "" {
|
||||
// make sure header is pinned to this account
|
||||
if a.HeaderMediaAttachment == nil {
|
||||
avi, err := c.db.GetAttachmentByID(a.HeaderMediaAttachmentID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error retrieving avatar: %s", err)
|
||||
}
|
||||
a.HeaderMediaAttachment = avi
|
||||
}
|
||||
headerURL = a.HeaderMediaAttachment.URL
|
||||
headerURLStatic = a.HeaderMediaAttachment.Thumbnail.URL
|
||||
}
|
||||
headerURL := header.URL
|
||||
headerURLStatic := header.Thumbnail.URL
|
||||
|
||||
// get the fields set on this account
|
||||
fields := []model.Field{}
|
||||
@ -155,7 +156,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
|
||||
suspended = true
|
||||
}
|
||||
|
||||
return &model.Account{
|
||||
accountFrontend := &model.Account{
|
||||
ID: a.ID,
|
||||
Username: a.Username,
|
||||
Acct: acct,
|
||||
@ -176,7 +177,14 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
|
||||
Emojis: emojis, // TODO: implement this
|
||||
Fields: fields,
|
||||
Suspended: suspended,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// put the account in our cache in case we need it again soon
|
||||
if err := c.frontendCache.Store(a.ID, accountFrontend); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return accountFrontend, nil
|
||||
}
|
||||
|
||||
func (c *converter) AccountToMastoBlocked(a *gtsmodel.Account) (*model.Account, error) {
|
||||
@ -302,17 +310,17 @@ func (c *converter) TagToMasto(t *gtsmodel.Tag) (model.Tag, error) {
|
||||
}
|
||||
|
||||
func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
|
||||
repliesCount, err := c.db.GetReplyCountForStatus(s)
|
||||
repliesCount, err := c.db.CountStatusReplies(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting replies: %s", err)
|
||||
}
|
||||
|
||||
reblogsCount, err := c.db.GetReblogCountForStatus(s)
|
||||
reblogsCount, err := c.db.CountStatusReblogs(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting reblogs: %s", err)
|
||||
}
|
||||
|
||||
favesCount, err := c.db.GetFaveCountForStatus(s)
|
||||
favesCount, err := c.db.CountStatusFaves(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting faves: %s", err)
|
||||
}
|
||||
@ -320,27 +328,27 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
var mastoRebloggedStatus *model.Status
|
||||
if s.BoostOfID != "" {
|
||||
// the boosted status might have been set on this struct already so check first before doing db calls
|
||||
if s.GTSBoostedStatus == nil {
|
||||
if s.BoostOf == nil {
|
||||
// it's not set so fetch it from the db
|
||||
bs := >smodel.Status{}
|
||||
if err := c.db.GetByID(s.BoostOfID, bs); err != nil {
|
||||
return nil, fmt.Errorf("error getting boosted status with id %s: %s", s.BoostOfID, err)
|
||||
}
|
||||
s.GTSBoostedStatus = bs
|
||||
s.BoostOf = bs
|
||||
}
|
||||
|
||||
// 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.GTSBoostedAccount == nil {
|
||||
if s.BoostOfAccount == nil {
|
||||
// it's not set so fetch it from the db
|
||||
ba := >smodel.Account{}
|
||||
if err := c.db.GetByID(s.GTSBoostedStatus.AccountID, ba); err != nil {
|
||||
return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", s.GTSBoostedStatus.AccountID, s.BoostOfID, err)
|
||||
if err := c.db.GetByID(s.BoostOf.AccountID, ba); err != nil {
|
||||
return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", s.BoostOf.AccountID, s.BoostOfID, err)
|
||||
}
|
||||
s.GTSBoostedAccount = ba
|
||||
s.GTSBoostedStatus.GTSAuthorAccount = ba
|
||||
s.BoostOfAccount = ba
|
||||
s.BoostOf.Account = ba
|
||||
}
|
||||
|
||||
mastoRebloggedStatus, err = c.StatusToMasto(s.GTSBoostedStatus, requestingAccount)
|
||||
mastoRebloggedStatus, err = c.StatusToMasto(s.BoostOf, requestingAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err)
|
||||
}
|
||||
@ -358,15 +366,15 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
}
|
||||
}
|
||||
|
||||
if s.GTSAuthorAccount == nil {
|
||||
if s.Account == nil {
|
||||
a := >smodel.Account{}
|
||||
if err := c.db.GetByID(s.AccountID, a); err != nil {
|
||||
return nil, fmt.Errorf("error getting status author: %s", err)
|
||||
}
|
||||
s.GTSAuthorAccount = a
|
||||
s.Account = a
|
||||
}
|
||||
|
||||
mastoAuthorAccount, err := c.AccountToMastoPublic(s.GTSAuthorAccount)
|
||||
mastoAuthorAccount, err := c.AccountToMastoPublic(s.Account)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing account of status author: %s", err)
|
||||
}
|
||||
@ -374,8 +382,8 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
mastoAttachments := []model.Attachment{}
|
||||
// the status might already have some gts attachments on it if it's not been pulled directly from the database
|
||||
// if so, we can directly convert the gts attachments into masto ones
|
||||
if s.GTSMediaAttachments != nil {
|
||||
for _, gtsAttachment := range s.GTSMediaAttachments {
|
||||
if s.Attachments != nil {
|
||||
for _, gtsAttachment := range s.Attachments {
|
||||
mastoAttachment, err := c.AttachmentToMasto(gtsAttachment)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
|
||||
@ -385,7 +393,7 @@ 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.Attachments {
|
||||
for _, a := range s.AttachmentIDs {
|
||||
gtsAttachment := >smodel.MediaAttachment{}
|
||||
if err := c.db.GetByID(a, gtsAttachment); err != nil {
|
||||
return nil, fmt.Errorf("error getting attachment with id %s: %s", a, err)
|
||||
@ -401,8 +409,8 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
mastoMentions := []model.Mention{}
|
||||
// the status might already have some gts mentions on it if it's not been pulled directly from the database
|
||||
// if so, we can directly convert the gts mentions into masto ones
|
||||
if s.GTSMentions != nil {
|
||||
for _, gtsMention := range s.GTSMentions {
|
||||
if s.Mentions != nil {
|
||||
for _, gtsMention := range s.Mentions {
|
||||
mastoMention, err := c.MentionToMasto(gtsMention)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
|
||||
@ -412,7 +420,7 @@ 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.Mentions {
|
||||
for _, m := range s.MentionIDs {
|
||||
gtsMention := >smodel.Mention{}
|
||||
if err := c.db.GetByID(m, gtsMention); err != nil {
|
||||
return nil, fmt.Errorf("error getting mention with id %s: %s", m, err)
|
||||
@ -428,8 +436,8 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
mastoTags := []model.Tag{}
|
||||
// the status might already have some gts tags on it if it's not been pulled directly from the database
|
||||
// if so, we can directly convert the gts tags into masto ones
|
||||
if s.GTSTags != nil {
|
||||
for _, gtsTag := range s.GTSTags {
|
||||
if s.Tags != nil {
|
||||
for _, gtsTag := range s.Tags {
|
||||
mastoTag, err := c.TagToMasto(gtsTag)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
|
||||
@ -439,7 +447,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
// the status doesn't have gts tags on it, but it does have tag IDs
|
||||
// in this case, we need to pull the gts tags from the db to convert them into masto ones
|
||||
} else {
|
||||
for _, t := range s.Tags {
|
||||
for _, t := range s.TagIDs {
|
||||
gtsTag := >smodel.Tag{}
|
||||
if err := c.db.GetByID(t, gtsTag); err != nil {
|
||||
return nil, fmt.Errorf("error getting tag with id %s: %s", t, err)
|
||||
@ -455,8 +463,8 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
mastoEmojis := []model.Emoji{}
|
||||
// the status might already have some gts emojis on it if it's not been pulled directly from the database
|
||||
// if so, we can directly convert the gts emojis into masto ones
|
||||
if s.GTSEmojis != nil {
|
||||
for _, gtsEmoji := range s.GTSEmojis {
|
||||
if s.Emojis != nil {
|
||||
for _, gtsEmoji := range s.Emojis {
|
||||
mastoEmoji, err := c.EmojiToMasto(gtsEmoji)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
|
||||
@ -466,7 +474,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode
|
||||
// the status doesn't have gts emojis on it, but it does have emoji IDs
|
||||
// in this case, we need to pull the gts emojis from the db to convert them into masto ones
|
||||
} else {
|
||||
for _, e := range s.Emojis {
|
||||
for _, e := range s.EmojiIDs {
|
||||
gtsEmoji := >smodel.Emoji{}
|
||||
if err := c.db.GetByID(e, gtsEmoji); err != nil {
|
||||
return nil, fmt.Errorf("error getting emoji with id %s: %s", e, err)
|
||||
@ -559,17 +567,17 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro
|
||||
statusCountKey := "status_count"
|
||||
domainCountKey := "domain_count"
|
||||
|
||||
userCount, err := c.db.GetUserCountForInstance(c.config.Host)
|
||||
userCount, err := c.db.CountInstanceUsers(c.config.Host)
|
||||
if err == nil {
|
||||
mi.Stats[userCountKey] = userCount
|
||||
}
|
||||
|
||||
statusCount, err := c.db.GetStatusCountForInstance(c.config.Host)
|
||||
statusCount, err := c.db.CountInstanceStatuses(c.config.Host)
|
||||
if err == nil {
|
||||
mi.Stats[statusCountKey] = statusCount
|
||||
}
|
||||
|
||||
domainCount, err := c.db.GetDomainCountForInstance(c.config.Host)
|
||||
domainCount, err := c.db.CountInstanceDomains(c.config.Host)
|
||||
if err == nil {
|
||||
mi.Stats[domainCountKey] = domainCount
|
||||
}
|
||||
@ -585,13 +593,10 @@ 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 := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "username", Value: i.Domain}}, ia); err == nil {
|
||||
// instance account exists, get the header for the account if it exists
|
||||
attachment := >smodel.MediaAttachment{}
|
||||
if err := c.db.GetHeaderForAccountID(attachment, ia.ID); err == nil {
|
||||
// header exists, set it on the api model
|
||||
mi.Thumbnail = attachment.URL
|
||||
ia, err := c.db.GetInstanceAccount("")
|
||||
if err == nil {
|
||||
if ia.HeaderMediaAttachment != nil {
|
||||
mi.Thumbnail = ia.HeaderMediaAttachment.URL
|
||||
}
|
||||
}
|
||||
|
||||
@ -628,47 +633,47 @@ func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relati
|
||||
}
|
||||
|
||||
func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) {
|
||||
|
||||
if n.GTSTargetAccount == nil {
|
||||
tAccount := >smodel.Account{}
|
||||
if err := c.db.GetByID(n.TargetAccountID, tAccount); err != nil {
|
||||
if n.TargetAccount == nil {
|
||||
tAccount, err := c.db.GetAccountByID(n.TargetAccountID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
|
||||
}
|
||||
n.GTSTargetAccount = tAccount
|
||||
n.TargetAccount = tAccount
|
||||
}
|
||||
|
||||
if n.GTSOriginAccount == nil {
|
||||
ogAccount := >smodel.Account{}
|
||||
if err := c.db.GetByID(n.OriginAccountID, ogAccount); err != nil {
|
||||
if n.OriginAccount == nil {
|
||||
ogAccount, err := c.db.GetAccountByID(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.GTSOriginAccount = ogAccount
|
||||
n.OriginAccount = ogAccount
|
||||
}
|
||||
mastoAccount, err := c.AccountToMastoPublic(n.GTSOriginAccount)
|
||||
|
||||
mastoAccount, err := c.AccountToMastoPublic(n.OriginAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err)
|
||||
}
|
||||
|
||||
var mastoStatus *model.Status
|
||||
if n.StatusID != "" {
|
||||
if n.GTSStatus == nil {
|
||||
status := >smodel.Status{}
|
||||
if err := c.db.GetByID(n.StatusID, status); err != nil {
|
||||
if n.Status == nil {
|
||||
status, err := c.db.GetStatusByID(n.StatusID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error getting status with id %s from the db: %s", n.StatusID, err)
|
||||
}
|
||||
n.GTSStatus = status
|
||||
n.Status = status
|
||||
}
|
||||
|
||||
if n.GTSStatus.GTSAuthorAccount == nil {
|
||||
if n.GTSStatus.AccountID == n.GTSTargetAccount.ID {
|
||||
n.GTSStatus.GTSAuthorAccount = n.GTSTargetAccount
|
||||
} else if n.GTSStatus.AccountID == n.GTSOriginAccount.ID {
|
||||
n.GTSStatus.GTSAuthorAccount = n.GTSOriginAccount
|
||||
if n.Status.Account == nil {
|
||||
if n.Status.AccountID == n.TargetAccount.ID {
|
||||
n.Status.Account = n.TargetAccount
|
||||
} else if n.Status.AccountID == n.OriginAccount.ID {
|
||||
n.Status.Account = n.OriginAccount
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
mastoStatus, err = c.StatusToMasto(n.GTSStatus, nil)
|
||||
mastoStatus, err = c.StatusToMasto(n.Status, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err)
|
||||
}
|
||||
|
@ -10,25 +10,25 @@ func (c *converter) interactionsWithStatusForAccount(s *gtsmodel.Status, request
|
||||
si := &statusInteractions{}
|
||||
|
||||
if requestingAccount != nil {
|
||||
faved, err := c.db.StatusFavedBy(s, requestingAccount.ID)
|
||||
faved, err := c.db.IsStatusFavedBy(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.StatusRebloggedBy(s, requestingAccount.ID)
|
||||
reblogged, err := c.db.IsStatusRebloggedBy(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.StatusMutedBy(s, requestingAccount.ID)
|
||||
muted, err := c.db.IsStatusMutedBy(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.StatusBookmarkedBy(s, requestingAccount.ID)
|
||||
bookmarked, err := c.db.IsStatusBookmarkedBy(s, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user