gotosocial/internal/federation/federatingdb/undo.go

326 lines
8.3 KiB
Go
Raw Permalink Normal View History

// 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 federatingdb
import (
"context"
"errors"
"fmt"
2021-11-13 16:29:43 +00:00
"github.com/superseriousbusiness/activity/streams/vocab"
2021-08-31 13:59:12 +00:00
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
[chore] use our own logging implementation (#716) * first commit Signed-off-by: kim <grufwub@gmail.com> * replace logging with our own log library Signed-off-by: kim <grufwub@gmail.com> * fix imports Signed-off-by: kim <grufwub@gmail.com> * fix log imports Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com> * fix package import cycle between config and log package Signed-off-by: kim <grufwub@gmail.com> * fix empty kv.Fields{} being passed to WithFields() Signed-off-by: kim <grufwub@gmail.com> * fix uses of log.WithFields() with whitespace issues and empty slices Signed-off-by: kim <grufwub@gmail.com> * *linter related grumbling* Signed-off-by: kim <grufwub@gmail.com> * gofmt the codebase! also fix more log.WithFields() formatting issues Signed-off-by: kim <grufwub@gmail.com> * update testrig code to match new changes Signed-off-by: kim <grufwub@gmail.com> * fix error wrapping in non fmt.Errorf function Signed-off-by: kim <grufwub@gmail.com> * add benchmarking of log.Caller() vs non-cached Signed-off-by: kim <grufwub@gmail.com> * fix syslog tests, add standard build tags to test runner to ensure consistency Signed-off-by: kim <grufwub@gmail.com> * make syslog tests more robust Signed-off-by: kim <grufwub@gmail.com> * fix caller depth arithmatic (is that how you spell it?) Signed-off-by: kim <grufwub@gmail.com> * update to use unkeyed fields in kv.Field{} instances Signed-off-by: kim <grufwub@gmail.com> * update go-kv library Signed-off-by: kim <grufwub@gmail.com> * update libraries list Signed-off-by: kim <grufwub@gmail.com> * fuck you linter get nerfed Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
2022-07-19 08:47:55 +00:00
"github.com/superseriousbusiness/gotosocial/internal/log"
)
func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) error {
log.DebugKV(ctx, "undo", serialize{undo})
activityContext := getActivityContext(ctx)
if activityContext.internal {
return nil // Already processed.
}
requestingAcct := activityContext.requestingAcct
receivingAcct := activityContext.receivingAcct
for _, object := range ap.ExtractObjects(undo) {
// Try to get object as vocab.Type,
// else skip handling (likely) IRI.
asType := object.GetType()
if asType == nil {
continue
}
// Check and handle any vocab.Type objects.
switch name := asType.GetTypeName(); name {
// UNDO FOLLOW
2021-09-03 08:30:40 +00:00
case ap.ActivityFollow:
if err := f.undoFollow(
ctx,
receivingAcct,
requestingAcct,
undo,
asType,
); err != nil {
return err
}
// UNDO LIKE
2021-09-03 08:30:40 +00:00
case ap.ActivityLike:
if err := f.undoLike(
ctx,
receivingAcct,
requestingAcct,
undo,
asType,
); err != nil {
return err
}
// UNDO BLOCK
2021-09-03 08:30:40 +00:00
case ap.ActivityBlock:
if err := f.undoBlock(
ctx,
receivingAcct,
requestingAcct,
undo,
asType,
); err != nil {
return err
}
// UNHANDLED
default:
log.Debugf(ctx, "unhandled object type: %s", name)
}
}
return nil
}
func (f *federatingDB) undoFollow(
ctx context.Context,
receivingAcct *gtsmodel.Account,
requestingAcct *gtsmodel.Account,
undo vocab.ActivityStreamsUndo,
t vocab.Type,
) error {
asFollow, ok := t.(vocab.ActivityStreamsFollow)
if !ok {
err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsFollow", t)
return gtserror.SetMalformed(err)
}
// Make sure the Undo
// actor owns the target.
if !sameActor(
undo.GetActivityStreamsActor(),
asFollow.GetActivityStreamsActor(),
) {
// Ignore this Activity.
return nil
}
// Convert AS Follow to barebones *gtsmodel.Follow,
// retrieving origin + target accts from the db.
follow, err := f.converter.ASFollowToFollow(
gtscontext.SetBarebones(ctx),
asFollow,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error converting AS Follow to follow: %w", err)
return err
}
// We were missing origin or
// target for this Follow, so
// we cannot Undo anything.
if follow == nil {
return nil
}
// Ensure addressee is follow target.
if follow.TargetAccountID != receivingAcct.ID {
const text = "receivingAcct was not Follow target"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Ensure requester is follow origin.
if follow.AccountID != requestingAcct.ID {
const text = "requestingAcct was not Follow origin"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Delete any existing follow with this URI.
err = f.state.DB.DeleteFollowByURI(ctx, follow.URI)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error deleting follow: %w", err)
return err
}
// Delete any existing follow request with this URI.
err = f.state.DB.DeleteFollowRequestByURI(ctx, follow.URI)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error deleting follow request: %w", err)
return err
}
log.Debug(ctx, "Follow undone")
return nil
}
func (f *federatingDB) undoLike(
ctx context.Context,
receivingAcct *gtsmodel.Account,
requestingAcct *gtsmodel.Account,
undo vocab.ActivityStreamsUndo,
t vocab.Type,
) error {
asLike, ok := t.(vocab.ActivityStreamsLike)
if !ok {
err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsLike", t)
return gtserror.SetMalformed(err)
}
// Make sure the Undo
// actor owns the target.
if !sameActor(
undo.GetActivityStreamsActor(),
asLike.GetActivityStreamsActor(),
) {
// Ignore this Activity.
return nil
}
// Convert AS Like to barebones *gtsmodel.StatusFave,
// retrieving liking acct and target status from the DB.
fave, err := f.converter.ASLikeToFave(
gtscontext.SetBarebones(ctx),
asLike,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error converting AS Like to fave: %w", err)
return err
}
// We were missing status, account,
// or other for this Like, so we
// cannot Undo anything.
if fave == nil {
return nil
}
// Ensure addressee is fave target.
if fave.TargetAccountID != receivingAcct.ID {
const text = "receivingAcct was not Fave target"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Ensure requester is fave origin.
if fave.AccountID != requestingAcct.ID {
const text = "requestingAcct was not Fave origin"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Fetch fave from the DB so we know the ID to delete it.
//
// Ignore URI on Likes, since we often get multiple Likes
// with the same target and account ID, but differing URIs.
// Instead, we'll select using account and target status.
//
// Regardless of the URI, we can read an Undo Like to mean
// "I don't want to fave this post anymore".
fave, err = f.state.DB.GetStatusFave(
gtscontext.SetBarebones(ctx),
fave.AccountID,
fave.StatusID,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf(
"db error getting fave from %s targeting %s: %w",
fave.AccountID, fave.StatusID, err,
)
return err
}
if fave == nil {
// We didn't have this fave
// stored anyway, so we can't
// Undo it, just ignore.
return nil
}
// Delete the fave.
if err := f.state.DB.DeleteStatusFaveByID(ctx, fave.ID); err != nil {
err := gtserror.Newf("db error deleting fave %s: %w", fave.ID, err)
return err
}
log.Debug(ctx, "Like undone")
return nil
}
func (f *federatingDB) undoBlock(
ctx context.Context,
receivingAcct *gtsmodel.Account,
requestingAcct *gtsmodel.Account,
undo vocab.ActivityStreamsUndo,
t vocab.Type,
) error {
asBlock, ok := t.(vocab.ActivityStreamsBlock)
if !ok {
err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsBlock", t)
return gtserror.SetMalformed(err)
}
// Make sure the Undo
// actor owns the target.
if !sameActor(
undo.GetActivityStreamsActor(),
asBlock.GetActivityStreamsActor(),
) {
// Ignore this Activity.
return nil
}
// Convert AS Block to barebones *gtsmodel.Block,
// retrieving origin + target accts from the DB.
block, err := f.converter.ASBlockToBlock(
gtscontext.SetBarebones(ctx),
asBlock,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error converting AS Block to block: %w", err)
return err
}
// We were missing origin or
// target for this Block, so
// we cannot Undo anything.
if block == nil {
return nil
}
// Ensure addressee is block target.
if block.TargetAccountID != receivingAcct.ID {
const text = "receivingAcct was not Block target"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Ensure requester is block origin.
if block.AccountID != requestingAcct.ID {
const text = "requestingAcct was not Block origin"
return gtserror.NewErrorForbidden(errors.New(text), text)
}
// Delete any existing block.
err = f.state.DB.DeleteBlockByURI(ctx, block.URI)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error deleting block: %w", err)
return err
}
log.Debug(ctx, "Block undone")
return nil
}