Timeline improvements (#41)
Tidying up. Parent/child statuses now display correctly in status/id/context.
This commit is contained in:
@ -222,13 +222,14 @@ func (c *converter) ASStatusToStatus(statusable Statusable) (*gtsmodel.Status, e
|
||||
if err != nil {
|
||||
return nil, errors.New("attributedTo was empty")
|
||||
}
|
||||
status.APStatusOwnerURI = attributedTo.String()
|
||||
status.AccountURI = attributedTo.String()
|
||||
|
||||
statusOwner := >smodel.Account{}
|
||||
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: attributedTo.String(), CaseInsensitive: true}}, statusOwner); 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
|
||||
|
||||
// check if there's a post that this is a reply to
|
||||
@ -236,7 +237,7 @@ func (c *converter) ASStatusToStatus(statusable Statusable) (*gtsmodel.Status, e
|
||||
if err == nil {
|
||||
// something is set so we can at least set this field on the
|
||||
// status and dereference using this later if we need to
|
||||
status.APReplyToStatusURI = inReplyToURI.String()
|
||||
status.InReplyToURI = inReplyToURI.String()
|
||||
|
||||
// now we can check if we have the replied-to status in our db already
|
||||
inReplyToStatus := >smodel.Status{}
|
||||
@ -475,6 +476,7 @@ func (c *converter) ASAnnounceToStatus(announceable Announceable) (*gtsmodel.Sta
|
||||
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
|
||||
|
||||
// these will all be wrapped in the boosted status so set them empty here
|
||||
status.Attachments = []string{}
|
||||
|
@ -65,7 +65,9 @@ type TypeConverter interface {
|
||||
// TagToMasto converts a gts model tag into its mastodon (frontend) representation for serialization on the API.
|
||||
TagToMasto(t *gtsmodel.Tag) (model.Tag, error)
|
||||
// StatusToMasto converts a gts model status into its mastodon (frontend) representation for serialization on the API.
|
||||
StatusToMasto(s *gtsmodel.Status, statusAuthor *gtsmodel.Account, requestingAccount *gtsmodel.Account, boostOfAccount *gtsmodel.Account, replyToAccount *gtsmodel.Account, reblogOfStatus *gtsmodel.Status) (*model.Status, error)
|
||||
//
|
||||
// Requesting account can be nil.
|
||||
StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error)
|
||||
// VisToMasto converts a gts visibility into its mastodon equivalent
|
||||
VisToMasto(m gtsmodel.Visibility) model.Visibility
|
||||
// InstanceToMasto converts a gts instance into its mastodon equivalent for serving at /api/v1/instance
|
||||
|
@ -43,10 +43,11 @@ func (c *converter) StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.
|
||||
URL: boostWrapperStatusURL,
|
||||
|
||||
// the boosted status is not created now, but the boost certainly is
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Local: local,
|
||||
AccountID: boostingAccount.ID,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Local: local,
|
||||
AccountID: boostingAccount.ID,
|
||||
AccountURI: boostingAccount.URI,
|
||||
|
||||
// replies can be boosted, but boosts are never replies
|
||||
InReplyToID: "",
|
||||
|
@ -268,14 +268,7 @@ func (c *converter) TagToMasto(t *gtsmodel.Tag) (model.Tag, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *converter) StatusToMasto(
|
||||
s *gtsmodel.Status,
|
||||
statusAuthor *gtsmodel.Account,
|
||||
requestingAccount *gtsmodel.Account,
|
||||
boostOfAccount *gtsmodel.Account,
|
||||
replyToAccount *gtsmodel.Account,
|
||||
reblogOfStatus *gtsmodel.Status) (*model.Status, error) {
|
||||
|
||||
func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
|
||||
repliesCount, err := c.db.GetReplyCountForStatus(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error counting replies: %s", err)
|
||||
@ -291,82 +284,32 @@ func (c *converter) StatusToMasto(
|
||||
return nil, fmt.Errorf("error counting faves: %s", err)
|
||||
}
|
||||
|
||||
var faved bool
|
||||
var reblogged bool
|
||||
var bookmarked bool
|
||||
var muted bool
|
||||
|
||||
// requestingAccount will be nil for public requests without auth
|
||||
// But if it's not nil, we can also get information about the requestingAccount's interaction with this status
|
||||
if requestingAccount != nil {
|
||||
faved, err = c.db.StatusFavedBy(s, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has faved status: %s", err)
|
||||
}
|
||||
|
||||
reblogged, err = c.db.StatusRebloggedBy(s, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has reblogged status: %s", err)
|
||||
}
|
||||
|
||||
muted, err = c.db.StatusMutedBy(s, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has muted status: %s", err)
|
||||
}
|
||||
|
||||
bookmarked, err = c.db.StatusBookmarkedBy(s, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
var gtsBoostedStatus *gtsmodel.Status
|
||||
if s.GTSBoostedStatus != nil {
|
||||
// it's set, great!
|
||||
gtsBoostedStatus = s.GTSBoostedStatus
|
||||
} else {
|
||||
if s.GTSBoostedStatus == nil {
|
||||
// it's not set so fetch it from the db
|
||||
gtsBoostedStatus = >smodel.Status{}
|
||||
if err := c.db.GetByID(s.BoostOfID, gtsBoostedStatus); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
// the boosted account might have been set on this struct already or passed as a param so check first before doing db calls
|
||||
var gtsBoostedAccount *gtsmodel.Account
|
||||
if s.GTSBoostedAccount != nil {
|
||||
// it's set, great!
|
||||
gtsBoostedAccount = s.GTSBoostedAccount
|
||||
} else if boostOfAccount != nil {
|
||||
// it's been given as a param, great!
|
||||
gtsBoostedAccount = boostOfAccount
|
||||
} else if boostOfAccount == nil && s.GTSBoostedAccount == nil {
|
||||
if s.GTSBoostedAccount == nil {
|
||||
// it's not set so fetch it from the db
|
||||
gtsBoostedAccount = >smodel.Account{}
|
||||
if err := c.db.GetByID(gtsBoostedStatus.AccountID, gtsBoostedAccount); err != nil {
|
||||
return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", gtsBoostedStatus.AccountID, s.BoostOfID, err)
|
||||
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)
|
||||
}
|
||||
s.GTSBoostedAccount = ba
|
||||
s.GTSBoostedStatus.GTSAuthorAccount = ba
|
||||
}
|
||||
|
||||
// the boosted status might be a reply so check this
|
||||
var gtsBoostedReplyToAccount *gtsmodel.Account
|
||||
if gtsBoostedStatus.InReplyToAccountID != "" {
|
||||
gtsBoostedReplyToAccount = >smodel.Account{}
|
||||
if err := c.db.GetByID(gtsBoostedStatus.InReplyToAccountID, gtsBoostedReplyToAccount); err != nil {
|
||||
return nil, fmt.Errorf("error getting account that boosted status was a reply to: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if gtsBoostedStatus != nil || gtsBoostedAccount != nil {
|
||||
mastoRebloggedStatus, err = c.StatusToMasto(gtsBoostedStatus, gtsBoostedAccount, requestingAccount, nil, gtsBoostedReplyToAccount, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err)
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("boost of id was set to %s but that status or account was nil", s.BoostOfID)
|
||||
mastoRebloggedStatus, err = c.StatusToMasto(s.GTSBoostedStatus, requestingAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -382,7 +325,15 @@ func (c *converter) StatusToMasto(
|
||||
}
|
||||
}
|
||||
|
||||
mastoAuthorAccount, err := c.AccountToMastoPublic(statusAuthor)
|
||||
if s.GTSAuthorAccount == 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
|
||||
}
|
||||
|
||||
mastoAuthorAccount, err := c.AccountToMastoPublic(s.GTSAuthorAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing account of status author: %s", err)
|
||||
}
|
||||
@ -498,6 +449,12 @@ func (c *converter) StatusToMasto(
|
||||
var mastoCard *model.Card
|
||||
var mastoPoll *model.Poll
|
||||
|
||||
statusInteractions := &statusInteractions{}
|
||||
si, err := c.interactionsWithStatusForAccount(s, requestingAccount)
|
||||
if err == nil {
|
||||
statusInteractions = si
|
||||
}
|
||||
|
||||
return &model.Status{
|
||||
ID: s.ID,
|
||||
CreatedAt: s.CreatedAt.Format(time.RFC3339),
|
||||
@ -512,10 +469,10 @@ func (c *converter) StatusToMasto(
|
||||
RepliesCount: repliesCount,
|
||||
ReblogsCount: reblogsCount,
|
||||
FavouritesCount: favesCount,
|
||||
Favourited: faved,
|
||||
Reblogged: reblogged,
|
||||
Muted: muted,
|
||||
Bookmarked: bookmarked,
|
||||
Favourited: statusInteractions.Faved,
|
||||
Bookmarked: statusInteractions.Bookmarked,
|
||||
Muted: statusInteractions.Muted,
|
||||
Reblogged: statusInteractions.Reblogged,
|
||||
Pinned: s.Pinned,
|
||||
Content: s.Content,
|
||||
Reblog: mastoRebloggedStatus,
|
||||
@ -630,15 +587,6 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
|
||||
n.GTSStatus = status
|
||||
}
|
||||
|
||||
var replyToAccount *gtsmodel.Account
|
||||
if n.GTSStatus.InReplyToAccountID != "" {
|
||||
r := >smodel.Account{}
|
||||
if err := c.db.GetByID(n.GTSStatus.InReplyToAccountID, r); err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error getting replied to account with id %s from the db: %s", n.GTSStatus.InReplyToAccountID, err)
|
||||
}
|
||||
replyToAccount = r
|
||||
}
|
||||
|
||||
if n.GTSStatus.GTSAuthorAccount == nil {
|
||||
if n.GTSStatus.AccountID == n.GTSTargetAccount.ID {
|
||||
n.GTSStatus.GTSAuthorAccount = n.GTSTargetAccount
|
||||
@ -648,7 +596,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi
|
||||
}
|
||||
|
||||
var err error
|
||||
mastoStatus, err = c.StatusToMasto(n.GTSStatus, n.GTSStatus.GTSAuthorAccount, n.GTSTargetAccount, nil, replyToAccount, nil)
|
||||
mastoStatus, err = c.StatusToMasto(n.GTSStatus, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err)
|
||||
}
|
||||
|
46
internal/typeutils/util.go
Normal file
46
internal/typeutils/util.go
Normal file
@ -0,0 +1,46 @@
|
||||
package typeutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
func (c *converter) interactionsWithStatusForAccount(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) {
|
||||
si := &statusInteractions{}
|
||||
|
||||
if requestingAccount != nil {
|
||||
faved, err := c.db.StatusFavedBy(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)
|
||||
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)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err)
|
||||
}
|
||||
si.Bookmarked = bookmarked
|
||||
}
|
||||
return si, nil
|
||||
}
|
||||
|
||||
// StatusInteractions denotes interactions with a status on behalf of an account.
|
||||
type statusInteractions struct {
|
||||
Faved bool
|
||||
Muted bool
|
||||
Bookmarked bool
|
||||
Reblogged bool
|
||||
}
|
Reference in New Issue
Block a user