2021-08-25 14:34:33 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 11:43:00 +00:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2021-08-25 14:34:33 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-13 17:42:28 +01:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2021-06-13 17:42:28 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2022-11-24 15:12:43 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 17:42:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) BoostedBy(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) ([]*apimodel.Account, gtserror.WithCode) {
|
|
|
|
targetStatus, err := p.db.GetStatusByID(ctx, targetStatusID)
|
2021-08-20 11:26:56 +01:00
|
|
|
if err != nil {
|
2022-11-24 15:12:43 +00:00
|
|
|
wrapped := fmt.Errorf("BoostedBy: error fetching status %s: %s", targetStatusID, err)
|
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, gtserror.NewErrorInternalError(wrapped)
|
|
|
|
}
|
|
|
|
return nil, gtserror.NewErrorNotFound(wrapped)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
2022-11-24 15:12:43 +00:00
|
|
|
|
|
|
|
if boostOfID := targetStatus.BoostOfID; boostOfID != "" {
|
|
|
|
// the target status is a boost wrapper, redirect this request to the status it boosts
|
|
|
|
boostedStatus, err := p.db.GetStatusByID(ctx, boostOfID)
|
|
|
|
if err != nil {
|
|
|
|
wrapped := fmt.Errorf("BoostedBy: error fetching status %s: %s", boostOfID, err)
|
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, gtserror.NewErrorInternalError(wrapped)
|
|
|
|
}
|
|
|
|
return nil, gtserror.NewErrorNotFound(wrapped)
|
|
|
|
}
|
|
|
|
targetStatus = boostedStatus
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
visible, err := p.filter.StatusVisible(ctx, targetStatus, requestingAccount)
|
2021-06-13 17:42:28 +01:00
|
|
|
if err != nil {
|
2022-11-24 15:12:43 +00:00
|
|
|
err = fmt.Errorf("BoostedBy: error seeing if status %s is visible: %s", targetStatus.ID, err)
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
if !visible {
|
2022-11-24 15:12:43 +00:00
|
|
|
err = errors.New("BoostedBy: status is not visible")
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
statusReblogs, err := p.db.GetStatusReblogs(ctx, targetStatus)
|
2021-06-13 17:42:28 +01:00
|
|
|
if err != nil {
|
2022-11-24 15:12:43 +00:00
|
|
|
err = fmt.Errorf("BoostedBy: error seeing who boosted status: %s", err)
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 15:12:43 +00:00
|
|
|
// filter account IDs so the user doesn't see accounts they blocked or which blocked them
|
|
|
|
accountIDs := make([]string, 0, len(statusReblogs))
|
2021-08-20 11:26:56 +01:00
|
|
|
for _, s := range statusReblogs {
|
2021-08-25 14:34:33 +01:00
|
|
|
blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, s.AccountID, true)
|
2021-06-13 17:42:28 +01:00
|
|
|
if err != nil {
|
2022-11-24 15:12:43 +00:00
|
|
|
err = fmt.Errorf("BoostedBy: error checking blocks: %s", err)
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
if !blocked {
|
2022-11-24 15:12:43 +00:00
|
|
|
accountIDs = append(accountIDs, s.AccountID)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: filter other things here? suspended? muted? silenced?
|
|
|
|
|
2022-11-24 15:12:43 +00:00
|
|
|
// fetch accounts + create their API representations
|
|
|
|
apiAccounts := make([]*apimodel.Account, 0, len(accountIDs))
|
|
|
|
for _, accountID := range accountIDs {
|
|
|
|
account, err := p.db.GetAccountByID(ctx, accountID)
|
|
|
|
if err != nil {
|
|
|
|
wrapped := fmt.Errorf("BoostedBy: error fetching account %s: %s", accountID, err)
|
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, gtserror.NewErrorInternalError(wrapped)
|
|
|
|
}
|
|
|
|
return nil, gtserror.NewErrorNotFound(wrapped)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiAccount, err := p.tc.AccountToAPIAccountPublic(ctx, account)
|
2021-06-13 17:42:28 +01:00
|
|
|
if err != nil {
|
2022-11-24 15:12:43 +00:00
|
|
|
err = fmt.Errorf("BoostedBy: error converting account to api model: %s", err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
2021-10-04 14:24:19 +01:00
|
|
|
apiAccounts = append(apiAccounts, apiAccount)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
return apiAccounts, nil
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|