[feature] Process incoming Undo Announce properly

This commit is contained in:
tobi 2025-01-24 12:25:39 +01:00
commit 97d635d1dd
2 changed files with 121 additions and 0 deletions

View file

@ -29,6 +29,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/messages"
)
func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) error {
@ -89,6 +90,18 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo)
return err
}
// UNDO ANNOUNCE
case ap.ActivityAnnounce:
if err := f.undoAnnounce(
ctx,
receivingAcct,
requestingAcct,
undo,
asType,
); err != nil {
return err
}
// UNHANDLED
default:
log.Debugf(ctx, "unhandled object type: %s", name)
@ -323,3 +336,72 @@ func (f *federatingDB) undoBlock(
log.Debug(ctx, "Block undone")
return nil
}
func (f *federatingDB) undoAnnounce(
ctx context.Context,
receivingAcct *gtsmodel.Account,
requestingAcct *gtsmodel.Account,
undo vocab.ActivityStreamsUndo,
t vocab.Type,
) error {
asAnnounce, ok := t.(vocab.ActivityStreamsAnnounce)
if !ok {
err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsAnnounce", t)
return gtserror.SetMalformed(err)
}
// Make sure the Undo actor owns the
// Announce they're trying to undo.
if !sameActor(
undo.GetActivityStreamsActor(),
asAnnounce.GetActivityStreamsActor(),
) {
// Ignore this Activity.
return nil
}
// Convert AS Announce to *gtsmodel.Status,
// retrieving origin account + target status.
boost, isNew, err := f.converter.ASAnnounceToStatus(
// Use barebones as we don't
// need to populate attachments
// on boosted status, mentions, etc.
gtscontext.SetBarebones(ctx),
asAnnounce,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error converting AS Announce to boost: %w", err)
return err
}
if boost == nil {
// We were missing origin or
// target(s) for this Announce,
// so we cannot Undo anything.
return nil
}
if isNew {
// We hadn't seen this boost
// before anyway, so there's
// nothing to Undo.
return nil
}
// Ensure requester == announcer.
if boost.AccountID != requestingAcct.ID {
const text = "requestingAcct was not Block origin"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Looks valid. Process side effects asynchronously.
f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{
APObjectType: ap.ActivityAnnounce,
APActivityType: ap.ActivityUndo,
GTSModel: boost,
Receiving: receivingAcct,
Requesting: requestingAcct,
})
return nil
}

View file

@ -189,6 +189,14 @@ func (p *Processor) ProcessFromFediAPI(ctx context.Context, fMsg *messages.FromF
if fMsg.APObjectType == ap.ActorPerson {
return p.fediAPI.MoveAccount(ctx, fMsg)
}
// UNDO SOMETHING
case ap.ActivityUndo:
// UNDO ANNOUNCE
if fMsg.APObjectType == ap.ActivityAnnounce {
return p.fediAPI.UndoAnnounce(ctx, fMsg)
}
}
return gtserror.Newf("unhandled: %s %s", fMsg.APActivityType, fMsg.APObjectType)
@ -1159,3 +1167,34 @@ func (p *fediAPI) RejectAnnounce(ctx context.Context, fMsg *messages.FromFediAPI
return nil
}
func (p *fediAPI) UndoAnnounce(
ctx context.Context,
fMsg *messages.FromFediAPI,
) error {
boost, ok := fMsg.GTSModel.(*gtsmodel.Status)
if !ok {
return gtserror.Newf("%T not parseable as *gtsmodel.Status", fMsg.GTSModel)
}
// Delete the boost wrapper itself.
if err := p.state.DB.DeleteStatusByID(ctx, boost.ID); err != nil {
return gtserror.Newf("db error deleting boost: %w", err)
}
// Update statuses count for the requesting account.
if err := p.utils.decrementStatusesCount(ctx, fMsg.Requesting, boost); err != nil {
log.Errorf(ctx, "error updating account stats: %v", err)
}
// Remove the boost wrapper from all timelines.
if err := p.surface.deleteStatusFromTimelines(ctx, boost.ID); err != nil {
log.Errorf(ctx, "error removing timelined boost: %v", err)
}
// Interaction counts changed on the boosted status;
// uncache the prepared version from all timelines.
p.surface.invalidateStatusFromTimelines(ctx, boost.BoostOfID)
return nil
}