From a42e05eee05b5c90823153e4e79ba8a44fd4deea Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Wed, 16 Jun 2021 12:10:00 +0200 Subject: [PATCH] bit of tidying up! --- internal/db/db.go | 3 - internal/db/pg/pg.go | 65 --------------- internal/gtsmodel/status.go | 17 ---- internal/typeutils/internaltofrontend.go | 33 +------- internal/typeutils/util.go | 46 +++++++++++ internal/visibility/statushometimelineable.go | 5 ++ internal/visibility/statusvisible.go | 6 +- internal/visibility/util.go | 81 +++++++++++++++++++ 8 files changed, 134 insertions(+), 122 deletions(-) create mode 100644 internal/typeutils/util.go create mode 100644 internal/visibility/util.go diff --git a/internal/db/db.go b/internal/db/db.go index b12f692..f3a2889 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -208,9 +208,6 @@ type DB interface { // Mutuals returns true if account1 and account2 both follow each other, or an error if something goes wrong while finding out. Mutuals(account1 *gtsmodel.Account, account2 *gtsmodel.Account) (bool, error) - // PullRelevantAccountsFromStatus returns all accounts mentioned in a status, replied to by a status, or boosted by a status - PullRelevantAccountsFromStatus(status *gtsmodel.Status) (*gtsmodel.RelevantAccounts, error) - // GetReplyCountForStatus returns the amount of replies recorded for a status, or an error if something goes wrong GetReplyCountForStatus(status *gtsmodel.Status) (int, error) diff --git a/internal/db/pg/pg.go b/internal/db/pg/pg.go index dbf035d..5a03d81 100644 --- a/internal/db/pg/pg.go +++ b/internal/db/pg/pg.go @@ -836,71 +836,6 @@ func (ps *postgresService) Mutuals(account1 *gtsmodel.Account, account2 *gtsmode return f1 && f2, nil } -func (ps *postgresService) PullRelevantAccountsFromStatus(targetStatus *gtsmodel.Status) (*gtsmodel.RelevantAccounts, error) { - accounts := >smodel.RelevantAccounts{ - MentionedAccounts: []*gtsmodel.Account{}, - } - - // get the author account - if targetStatus.GTSAuthorAccount == nil { - statusAuthor := >smodel.Account{} - if err := ps.conn.Model(statusAuthor).Where("id = ?", targetStatus.AccountID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting statusAuthor with id %s: %s", targetStatus.AccountID, err) - } - targetStatus.GTSAuthorAccount = statusAuthor - } - accounts.StatusAuthor = targetStatus.GTSAuthorAccount - - // get the replied to account from the status and add it to the pile - if targetStatus.InReplyToAccountID != "" { - repliedToAccount := >smodel.Account{} - if err := ps.conn.Model(repliedToAccount).Where("id = ?", targetStatus.InReplyToAccountID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting repliedToAcount with id %s: %s", targetStatus.InReplyToAccountID, err) - } - accounts.ReplyToAccount = repliedToAccount - } - - // get the boosted account from the status and add it to the pile - if targetStatus.BoostOfID != "" { - // retrieve the boosted status first - boostedStatus := >smodel.Status{} - if err := ps.conn.Model(boostedStatus).Where("id = ?", targetStatus.BoostOfID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatus with id %s: %s", targetStatus.BoostOfID, err) - } - boostedAccount := >smodel.Account{} - if err := ps.conn.Model(boostedAccount).Where("id = ?", boostedStatus.AccountID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedAccount with id %s: %s", boostedStatus.AccountID, err) - } - accounts.BoostedAccount = boostedAccount - - // the boosted status might be a reply to another account so we should get that too - if boostedStatus.InReplyToAccountID != "" { - boostedStatusRepliedToAccount := >smodel.Account{} - if err := ps.conn.Model(boostedStatusRepliedToAccount).Where("id = ?", boostedStatus.InReplyToAccountID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatusRepliedToAccount with id %s: %s", boostedStatus.InReplyToAccountID, err) - } - accounts.BoostedReplyToAccount = boostedStatusRepliedToAccount - } - } - - // now get all accounts with IDs that are mentioned in the status - for _, mentionID := range targetStatus.Mentions { - - mention := >smodel.Mention{} - if err := ps.conn.Model(mention).Where("id = ?", mentionID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mention with id %s: %s", mentionID, err) - } - - mentionedAccount := >smodel.Account{} - if err := ps.conn.Model(mentionedAccount).Where("id = ?", mention.TargetAccountID).Select(); err != nil { - return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mentioned account: %s", err) - } - accounts.MentionedAccounts = append(accounts.MentionedAccounts, mentionedAccount) - } - - return accounts, nil -} - func (ps *postgresService) GetReplyCountForStatus(status *gtsmodel.Status) (int, error) { return ps.conn.Model(>smodel.Status{}).Where("in_reply_to_id = ?", status.ID).Count() } diff --git a/internal/gtsmodel/status.go b/internal/gtsmodel/status.go index 4ced55b..caa5a2a 100644 --- a/internal/gtsmodel/status.go +++ b/internal/gtsmodel/status.go @@ -140,20 +140,3 @@ type VisibilityAdvanced struct { // This status can be liked/faved Likeable bool `pg:"default:true"` } - -// RelevantAccounts denotes accounts that are replied to, boosted by, or mentioned in a status. -type RelevantAccounts struct { - StatusAuthor *Account - ReplyToAccount *Account - BoostedAccount *Account - BoostedReplyToAccount *Account - MentionedAccounts []*Account -} - -// StatusInteractions denotes interactions with a status on behalf of an account. -type StatusInteractions struct { - Faved bool - Muted bool - Bookmarked bool - Reblogged bool -} diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 7b419e8..90460ec 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -449,7 +449,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode var mastoCard *model.Card var mastoPoll *model.Poll - statusInteractions := >smodel.StatusInteractions{} + statusInteractions := &statusInteractions{} si, err := c.interactionsWithStatusForAccount(s, requestingAccount) if err == nil { statusInteractions = si @@ -610,34 +610,3 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi Status: mastoStatus, }, nil } - -func (c *converter) interactionsWithStatusForAccount(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*gtsmodel.StatusInteractions, error) { - si := >smodel.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 -} diff --git a/internal/typeutils/util.go b/internal/typeutils/util.go new file mode 100644 index 0000000..1e13f07 --- /dev/null +++ b/internal/typeutils/util.go @@ -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 +} diff --git a/internal/visibility/statushometimelineable.go b/internal/visibility/statushometimelineable.go index 07038fd..130c2df 100644 --- a/internal/visibility/statushometimelineable.go +++ b/internal/visibility/statushometimelineable.go @@ -14,6 +14,11 @@ func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, requestin "requestingAccountID": requestingAccount.ID, }) + // status owner should always be able to see their status in their timeline so we can return early if this is the case + if targetStatus.AccountID == requestingAccount.ID { + return true, nil + } + v, err := f.StatusVisible(targetStatus, requestingAccount) if err != nil { return false, fmt.Errorf("StatusHometimelineable: error checking visibility of status with id %s: %s", targetStatus.ID, err) diff --git a/internal/visibility/statusvisible.go b/internal/visibility/statusvisible.go index 389fa26..caf5cfc 100644 --- a/internal/visibility/statusvisible.go +++ b/internal/visibility/statusvisible.go @@ -17,7 +17,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount "requestingAccountID": requestingAccount.ID, }) - relevantAccounts, err := f.db.PullRelevantAccountsFromStatus(targetStatus) + relevantAccounts, err := f.pullRelevantAccountsFromStatus(targetStatus) if err != nil { l.Debugf("error pulling relevant accounts for status %s: %s", targetStatus.ID, err) } @@ -195,7 +195,3 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount return false, errors.New("reached the end of StatusVisible with no result") } - -func StatusVisibleInHomeTimeline() { - -} diff --git a/internal/visibility/util.go b/internal/visibility/util.go new file mode 100644 index 0000000..f52661d --- /dev/null +++ b/internal/visibility/util.go @@ -0,0 +1,81 @@ +package visibility + +import ( + "fmt" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +func (f *filter) pullRelevantAccountsFromStatus(targetStatus *gtsmodel.Status) (*relevantAccounts, error) { + accounts := &relevantAccounts{ + MentionedAccounts: []*gtsmodel.Account{}, + } + + // get the author account + if targetStatus.GTSAuthorAccount == nil { + statusAuthor := >smodel.Account{} + if err := f.db.GetByID(targetStatus.AccountID, statusAuthor); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting statusAuthor with id %s: %s", targetStatus.AccountID, err) + } + targetStatus.GTSAuthorAccount = statusAuthor + } + accounts.StatusAuthor = targetStatus.GTSAuthorAccount + + // get the replied to account from the status and add it to the pile + if targetStatus.InReplyToAccountID != "" { + repliedToAccount := >smodel.Account{} + if err := f.db.GetByID(targetStatus.InReplyToAccountID, repliedToAccount); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting repliedToAcount with id %s: %s", targetStatus.InReplyToAccountID, err) + } + accounts.ReplyToAccount = repliedToAccount + } + + // get the boosted account from the status and add it to the pile + if targetStatus.BoostOfID != "" { + // retrieve the boosted status first + boostedStatus := >smodel.Status{} + if err := f.db.GetByID(targetStatus.BoostOfID, boostedStatus); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatus with id %s: %s", targetStatus.BoostOfID, err) + } + boostedAccount := >smodel.Account{} + if err := f.db.GetByID(boostedStatus.AccountID, boostedAccount); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedAccount with id %s: %s", boostedStatus.AccountID, err) + } + accounts.BoostedAccount = boostedAccount + + // the boosted status might be a reply to another account so we should get that too + if boostedStatus.InReplyToAccountID != "" { + boostedStatusRepliedToAccount := >smodel.Account{} + if err := f.db.GetByID(boostedStatus.InReplyToAccountID, boostedStatusRepliedToAccount); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatusRepliedToAccount with id %s: %s", boostedStatus.InReplyToAccountID, err) + } + accounts.BoostedReplyToAccount = boostedStatusRepliedToAccount + } + } + + // now get all accounts with IDs that are mentioned in the status + for _, mentionID := range targetStatus.Mentions { + + mention := >smodel.Mention{} + if err := f.db.GetByID(mentionID, mention); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mention with id %s: %s", mentionID, err) + } + + mentionedAccount := >smodel.Account{} + if err := f.db.GetByID(mention.TargetAccountID, mentionedAccount); err != nil { + return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mentioned account: %s", err) + } + accounts.MentionedAccounts = append(accounts.MentionedAccounts, mentionedAccount) + } + + return accounts, nil +} + +// relevantAccounts denotes accounts that are replied to, boosted by, or mentioned in a status. +type relevantAccounts struct { + StatusAuthor *gtsmodel.Account + ReplyToAccount *gtsmodel.Account + BoostedAccount *gtsmodel.Account + BoostedReplyToAccount *gtsmodel.Account + MentionedAccounts []*gtsmodel.Account +}