who's faved and who's boosted, reblog notifs

This commit is contained in:
tsmethurst 2021-05-31 13:54:02 +02:00
parent 69e7a89549
commit 76597bb008
7 changed files with 208 additions and 2 deletions

View File

@ -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)

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}

View File

@ -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)

View File

@ -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 := &gtsmodel.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).

View File

@ -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 := &gtsmodel.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 := &gtsmodel.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},
}, &gtsmodel.Notification{})
if err == nil {
// notification exists already so just bail
return nil
}
// now create the new reblog notification
notif := &gtsmodel.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
}

View File

@ -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.

View File

@ -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 := &gtsmodel.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 := &gtsmodel.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")