From 76597bb008bb37bce6563e62eefcb0b593bf3cea Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Mon, 31 May 2021 13:54:02 +0200 Subject: [PATCH] who's faved and who's boosted, reblog notifs --- internal/api/client/status/status.go | 2 + .../api/client/status/statusboosted_by.go | 60 +++++++++++++++++ internal/db/db.go | 4 ++ internal/db/pg/pg.go | 29 ++++++++- internal/processing/fromcommon.go | 49 ++++++++++++++ internal/processing/processor.go | 2 + internal/processing/status.go | 64 +++++++++++++++++++ 7 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 internal/api/client/status/statusboosted_by.go diff --git a/internal/api/client/status/status.go b/internal/api/client/status/status.go index 7e4da021e..c5959edb0 100644 --- a/internal/api/client/status/status.go +++ b/internal/api/client/status/status.go @@ -95,8 +95,10 @@ func (m *Module) Route(r router.Router) error { r.AttachHandler(http.MethodPost, FavouritePath, m.StatusFavePOSTHandler) r.AttachHandler(http.MethodPost, UnfavouritePath, m.StatusUnfavePOSTHandler) + r.AttachHandler(http.MethodGet, FavouritedPath, m.StatusFavedByGETHandler) r.AttachHandler(http.MethodPost, ReblogPath, m.StatusBoostPOSTHandler) + r.AttachHandler(http.MethodGet, RebloggedPath, m.StatusBoostedByGETHandler) r.AttachHandler(http.MethodGet, ContextPath, m.StatusContextGETHandler) diff --git a/internal/api/client/status/statusboosted_by.go b/internal/api/client/status/statusboosted_by.go new file mode 100644 index 000000000..debcc1b22 --- /dev/null +++ b/internal/api/client/status/statusboosted_by.go @@ -0,0 +1,60 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package status + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// StatusBoostedByGETHandler is for serving a list of accounts that have boosted/reblogged a given status +func (m *Module) StatusBoostedByGETHandler(c *gin.Context) { + l := m.log.WithFields(logrus.Fields{ + "func": "StatusBoostedByGETHandler", + "request_uri": c.Request.RequestURI, + "user_agent": c.Request.UserAgent(), + "origin_ip": c.ClientIP(), + }) + l.Debugf("entering function") + + authed, err := oauth.Authed(c, true, true, true, true) // we don't really need an app here but we want everything else + if err != nil { + l.Errorf("error authing status boosted by request: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "not authed"}) + return + } + + targetStatusID := c.Param(IDKey) + if targetStatusID == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "no status id provided"}) + return + } + + mastoAccounts, err := m.processor.StatusBoostedBy(authed, targetStatusID) + if err != nil { + l.Debugf("error processing status boosted by request: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) + return + } + + c.JSON(http.StatusOK, mastoAccounts) +} diff --git a/internal/db/db.go b/internal/db/db.go index c7a3df683..1774420c6 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -253,6 +253,10 @@ type DB interface { // This slice will be unfiltered, not taking account of blocks and whatnot, so filter it before serving it back to a user. WhoFavedStatus(status *gtsmodel.Status) ([]*gtsmodel.Account, error) + // WhoBoostedStatus returns a slice of accounts who boosted the given status. + // This slice will be unfiltered, not taking account of blocks and whatnot, so filter it before serving it back to a user. + WhoBoostedStatus(status *gtsmodel.Status) ([]*gtsmodel.Account, error) + // GetHomeTimelineForAccount fetches the account's HOME timeline -- ie., posts and replies from people they *follow*. // It will use the given filters and try to return as many statuses up to the limit as possible. GetHomeTimelineForAccount(accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*gtsmodel.Status, error) diff --git a/internal/db/pg/pg.go b/internal/db/pg/pg.go index 91e5b1ac0..c2bb7032b 100644 --- a/internal/db/pg/pg.go +++ b/internal/db/pg/pg.go @@ -1115,11 +1115,36 @@ func (ps *postgresService) WhoFavedStatus(status *gtsmodel.Status) ([]*gtsmodel. return accounts, nil } +func (ps *postgresService) WhoBoostedStatus(status *gtsmodel.Status) ([]*gtsmodel.Account, error) { + accounts := []*gtsmodel.Account{} + + boosts := []*gtsmodel.Status{} + if err := ps.conn.Model(&boosts).Where("boost_of_id = ?", status.ID).Select(); err != nil { + if err == pg.ErrNoRows { + return accounts, nil // no rows just means nobody has boosted this status, so that's fine + } + return nil, err // an actual error has occurred + } + + for _, f := range boosts { + acc := >smodel.Account{} + if err := ps.conn.Model(acc).Where("id = ?", f.AccountID).Select(); err != nil { + if err == pg.ErrNoRows { + continue // the account doesn't exist for some reason??? but this isn't the place to worry about that so just skip it + } + return nil, err // an actual error has occurred + } + accounts = append(accounts, acc) + } + return accounts, nil +} + func (ps *postgresService) GetHomeTimelineForAccount(accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*gtsmodel.Status, error) { statuses := []*gtsmodel.Status{} - q := ps.conn.Model(&statuses). - ColumnExpr("status.*"). + q := ps.conn.Model(&statuses) + + q = q.ColumnExpr("status.*"). Join("JOIN follows AS f ON f.target_account_id = status.account_id"). Where("f.account_id = ?", accountID). Limit(limit). diff --git a/internal/processing/fromcommon.go b/internal/processing/fromcommon.go index cb38d4bb4..bdb2a599b 100644 --- a/internal/processing/fromcommon.go +++ b/internal/processing/fromcommon.go @@ -160,5 +160,54 @@ func (p *processor) notifyFave(fave *gtsmodel.StatusFave, receivingAccount *gtsm } func (p *processor) notifyAnnounce(status *gtsmodel.Status) error { + if status.BoostOfID == "" { + // not a boost, nothing to do + return nil + } + + boostedStatus := >smodel.Status{} + if err := p.db.GetByID(status.BoostOfID, boostedStatus); err != nil { + return fmt.Errorf("notifyAnnounce: error getting status with id %s: %s", status.BoostOfID, err) + } + + boostedAcct := >smodel.Account{} + if err := p.db.GetByID(boostedStatus.AccountID, boostedAcct); err != nil { + return fmt.Errorf("notifyAnnounce: error getting account with id %s: %s", boostedStatus.AccountID, err) + } + + if boostedAcct.Domain != "" { + // remote account, nothing to do + return nil + } + + if boostedStatus.AccountID == status.AccountID { + // it's a self boost, nothing to do + return nil + } + + // make sure a notif doesn't already exist for this announce + err := p.db.GetWhere([]db.Where{ + {Key: "notification_type", Value: gtsmodel.NotificationReblog}, + {Key: "target_account_id", Value: boostedAcct.ID}, + {Key: "origin_account_id", Value: status.AccountID}, + {Key: "status_id", Value: status.ID}, + }, >smodel.Notification{}) + if err == nil { + // notification exists already so just bail + return nil + } + + // now create the new reblog notification + notif := >smodel.Notification{ + NotificationType: gtsmodel.NotificationReblog, + TargetAccountID: boostedAcct.ID, + OriginAccountID: status.AccountID, + StatusID: status.ID, + } + + if err := p.db.Put(notif); err != nil { + return fmt.Errorf("notifyAnnounce: error putting notification in database: %s", err) + } + return nil } diff --git a/internal/processing/processor.go b/internal/processing/processor.go index f54fb45ee..b31c37be3 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -120,6 +120,8 @@ type Processor interface { StatusFave(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, error) // StatusBoost processes the boost/reblog of a given status, returning the newly-created boost if all is well. StatusBoost(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, ErrorWithCode) + // StatusBoostedBy returns a slice of accounts that have boosted the given status, filtered according to privacy settings. + StatusBoostedBy(authed *oauth.Auth, targetStatusID string) ([]*apimodel.Account, ErrorWithCode) // StatusFavedBy returns a slice of accounts that have liked the given status, filtered according to privacy settings. StatusFavedBy(authed *oauth.Auth, targetStatusID string) ([]*apimodel.Account, error) // StatusGet gets the given status, taking account of privacy settings and blocks etc. diff --git a/internal/processing/status.go b/internal/processing/status.go index edaf70cd4..897972839 100644 --- a/internal/processing/status.go +++ b/internal/processing/status.go @@ -309,6 +309,70 @@ func (p *processor) StatusBoost(authed *oauth.Auth, targetStatusID string) (*api return mastoStatus, nil } +func (p *processor) StatusBoostedBy(authed *oauth.Auth, targetStatusID string) ([]*apimodel.Account, ErrorWithCode) { + l := p.log.WithField("func", "StatusBoostedBy") + + l.Tracef("going to search for target status %s", targetStatusID) + targetStatus := >smodel.Status{} + if err := p.db.GetByID(targetStatusID, targetStatus); err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error fetching status %s: %s", targetStatusID, err)) + } + + l.Tracef("going to search for target account %s", targetStatus.AccountID) + targetAccount := >smodel.Account{} + if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error fetching target account %s: %s", targetStatus.AccountID, err)) + } + + l.Trace("going to get relevant accounts") + relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus) + if err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error fetching related accounts for status %s: %s", targetStatusID, err)) + } + + l.Trace("going to see if status is visible") + visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that + if err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error seeing if status %s is visible: %s", targetStatus.ID, err)) + } + + if !visible { + return nil, NewErrorNotFound(errors.New("StatusBoostedBy: status is not visible")) + } + + // get ALL accounts that faved a status -- doesn't take account of blocks and mutes and stuff + favingAccounts, err := p.db.WhoBoostedStatus(targetStatus) + if err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error seeing who boosted status: %s", err)) + } + + // filter the list so the user doesn't see accounts they blocked or which blocked them + filteredAccounts := []*gtsmodel.Account{} + for _, acc := range favingAccounts { + blocked, err := p.db.Blocked(authed.Account.ID, acc.ID) + if err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusBoostedBy: error checking blocks: %s", err)) + } + if !blocked { + filteredAccounts = append(filteredAccounts, acc) + } + } + + // TODO: filter other things here? suspended? muted? silenced? + + // now we can return the masto representation of those accounts + mastoAccounts := []*apimodel.Account{} + for _, acc := range filteredAccounts { + mastoAccount, err := p.tc.AccountToMastoPublic(acc) + if err != nil { + return nil, NewErrorNotFound(fmt.Errorf("StatusFavedBy: error converting account to api model: %s", err)) + } + mastoAccounts = append(mastoAccounts, mastoAccount) + } + + return mastoAccounts, nil +} + func (p *processor) StatusFavedBy(authed *oauth.Auth, targetStatusID string) ([]*apimodel.Account, error) { l := p.log.WithField("func", "StatusFavedBy")