2023-09-12 13:00:35 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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 common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2024-02-09 14:24:49 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
|
2024-05-06 11:49:08 +00:00
|
|
|
statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
|
2023-09-12 13:00:35 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
|
|
|
)
|
|
|
|
|
2024-02-09 14:24:49 +00:00
|
|
|
// GetTargetStatusBy fetches the target status with db load
|
|
|
|
// function, given the authorized (or, nil) requester's
|
|
|
|
// account. This returns an approprate gtserror.WithCode
|
|
|
|
// accounting for not found and visibility to requester.
|
|
|
|
//
|
|
|
|
// window can be used to force refresh of the target if it's
|
|
|
|
// deemed to be stale. Falls back to default window if nil.
|
2023-09-12 13:00:35 +00:00
|
|
|
func (p *Processor) GetTargetStatusBy(
|
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
|
|
|
getTargetFromDB func() (*gtsmodel.Status, error),
|
2024-02-09 14:24:49 +00:00
|
|
|
window *dereferencing.FreshnessWindow,
|
2023-09-12 13:00:35 +00:00
|
|
|
) (
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
visible bool,
|
|
|
|
errWithCode gtserror.WithCode,
|
|
|
|
) {
|
|
|
|
// Fetch the target status from db.
|
|
|
|
target, err := getTargetFromDB()
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, false, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == nil {
|
|
|
|
// DB loader could not find status in database.
|
2023-11-08 14:32:17 +00:00
|
|
|
const text = "target status not found"
|
|
|
|
return nil, false, gtserror.NewErrorNotFound(
|
|
|
|
errors.New(text),
|
|
|
|
text,
|
|
|
|
)
|
2023-09-12 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether target status is visible to requesting account.
|
2024-07-24 11:27:42 +00:00
|
|
|
visible, err = p.visFilter.StatusVisible(ctx, requester, target)
|
2023-09-12 13:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if requester != nil && visible {
|
2024-07-21 12:04:19 +00:00
|
|
|
// Only refresh status if visible to requester,
|
|
|
|
// and there is *authorized* requester to prevent
|
|
|
|
// a possible DOS vector for unauthorized clients.
|
|
|
|
latest, _, err := p.federator.RefreshStatus(ctx,
|
|
|
|
requester.Username,
|
|
|
|
target,
|
|
|
|
nil,
|
|
|
|
window,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "error refreshing target %s: %v", target.URI, err)
|
|
|
|
return target, visible, nil
|
2023-12-15 14:24:39 +00:00
|
|
|
}
|
2024-07-21 12:04:19 +00:00
|
|
|
|
|
|
|
// Set latest.
|
|
|
|
target = latest
|
2023-09-12 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, visible, nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:24:39 +00:00
|
|
|
// GetVisibleTargetStatus calls GetTargetStatusBy(),
|
2023-09-12 13:00:35 +00:00
|
|
|
// but converts a non-visible result to not-found error.
|
2024-02-09 14:24:49 +00:00
|
|
|
//
|
|
|
|
// window can be used to force refresh of the target if it's
|
|
|
|
// deemed to be stale. Falls back to default window if nil.
|
2023-12-15 14:24:39 +00:00
|
|
|
func (p *Processor) GetVisibleTargetStatusBy(
|
2023-09-12 13:00:35 +00:00
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
2023-12-15 14:24:39 +00:00
|
|
|
getTargetFromDB func() (*gtsmodel.Status, error),
|
2024-02-09 14:24:49 +00:00
|
|
|
window *dereferencing.FreshnessWindow,
|
2023-09-12 13:00:35 +00:00
|
|
|
) (
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
errWithCode gtserror.WithCode,
|
|
|
|
) {
|
|
|
|
// Fetch the target status by ID from the database.
|
2023-12-15 14:24:39 +00:00
|
|
|
target, visible, errWithCode := p.GetTargetStatusBy(ctx,
|
2023-09-12 13:00:35 +00:00
|
|
|
requester,
|
2023-12-15 14:24:39 +00:00
|
|
|
getTargetFromDB,
|
2024-02-09 14:24:49 +00:00
|
|
|
window,
|
2023-09-12 13:00:35 +00:00
|
|
|
)
|
|
|
|
if errWithCode != nil {
|
|
|
|
return nil, errWithCode
|
|
|
|
}
|
|
|
|
|
|
|
|
if !visible {
|
|
|
|
// Target should not be seen by requester.
|
2023-11-08 14:32:17 +00:00
|
|
|
const text = "target status not found"
|
|
|
|
return nil, gtserror.NewErrorNotFound(
|
|
|
|
errors.New(text),
|
|
|
|
text,
|
|
|
|
)
|
2023-09-12 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:24:39 +00:00
|
|
|
// GetVisibleTargetStatus calls GetVisibleTargetStatusBy(),
|
|
|
|
// passing in a database function that fetches by status ID.
|
2024-02-09 14:24:49 +00:00
|
|
|
//
|
|
|
|
// window can be used to force refresh of the target if it's
|
|
|
|
// deemed to be stale. Falls back to default window if nil.
|
2023-12-15 14:24:39 +00:00
|
|
|
func (p *Processor) GetVisibleTargetStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
|
|
|
targetID string,
|
2024-02-09 14:24:49 +00:00
|
|
|
window *dereferencing.FreshnessWindow,
|
2023-12-15 14:24:39 +00:00
|
|
|
) (
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
errWithCode gtserror.WithCode,
|
|
|
|
) {
|
|
|
|
return p.GetVisibleTargetStatusBy(ctx, requester, func() (*gtsmodel.Status, error) {
|
|
|
|
return p.state.DB.GetStatusByID(ctx, targetID)
|
2024-02-09 14:24:49 +00:00
|
|
|
}, window)
|
2023-12-15 14:24:39 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 14:27:15 +00:00
|
|
|
// UnwrapIfBoost "unwraps" the given status if
|
|
|
|
// it's a boost wrapper, by returning the boosted
|
|
|
|
// status it targets (pending visibility checks).
|
|
|
|
//
|
|
|
|
// Just returns the input status if it's not a boost.
|
|
|
|
func (p *Processor) UnwrapIfBoost(
|
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
|
|
|
status *gtsmodel.Status,
|
|
|
|
) (*gtsmodel.Status, gtserror.WithCode) {
|
|
|
|
if status.BoostOfID == "" {
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
return p.GetVisibleTargetStatus(ctx,
|
2023-12-15 14:24:39 +00:00
|
|
|
requester,
|
|
|
|
status.BoostOfID,
|
2024-02-09 14:24:49 +00:00
|
|
|
nil,
|
2023-12-01 14:27:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:00:35 +00:00
|
|
|
// GetAPIStatus fetches the appropriate API status model for target.
|
|
|
|
func (p *Processor) GetAPIStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
requester *gtsmodel.Account,
|
|
|
|
target *gtsmodel.Status,
|
|
|
|
) (
|
|
|
|
apiStatus *apimodel.Status,
|
|
|
|
errWithCode gtserror.WithCode,
|
|
|
|
) {
|
2024-06-06 16:38:02 +00:00
|
|
|
apiStatus, err := p.converter.StatusToAPIStatus(ctx, target, requester, statusfilter.FilterContextNone, nil, nil)
|
2023-09-12 13:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
err = gtserror.Newf("error converting status: %w", err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
return apiStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidateTimelinedStatus is a shortcut function for invalidating the cached
|
|
|
|
// representation one status in the home timeline and all list timelines of the
|
|
|
|
// given accountID. It should only be called in cases where a status update
|
|
|
|
// does *not* need to be passed into the processor via the worker queue, since
|
|
|
|
// such invalidation will, in that case, be handled by the processor instead.
|
|
|
|
func (p *Processor) InvalidateTimelinedStatus(ctx context.Context, accountID string, statusID string) error {
|
|
|
|
// Get lists first + bail if this fails.
|
2024-09-16 16:46:09 +00:00
|
|
|
lists, err := p.state.DB.GetListsByAccountID(ctx, accountID)
|
2023-09-12 13:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start new log entry with
|
|
|
|
// the above calling func's name.
|
|
|
|
l := log.
|
|
|
|
WithContext(ctx).
|
|
|
|
WithField("caller", log.Caller(3)).
|
|
|
|
WithField("accountID", accountID).
|
|
|
|
WithField("statusID", statusID)
|
|
|
|
|
|
|
|
// Unprepare item from home + list timelines, just log
|
|
|
|
// if something goes wrong since this is not a showstopper.
|
|
|
|
|
|
|
|
if err := p.state.Timelines.Home.UnprepareItem(ctx, accountID, statusID); err != nil {
|
|
|
|
l.Errorf("error unpreparing item from home timeline: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, list := range lists {
|
|
|
|
if err := p.state.Timelines.List.UnprepareItem(ctx, list.ID, statusID); err != nil {
|
|
|
|
l.Errorf("error unpreparing item from list timeline %s: %v", list.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|