diff options
author | 2022-09-21 19:55:52 +0200 | |
---|---|---|
committer | 2022-09-21 19:55:52 +0200 | |
commit | 4cf76a2bfcc2c19bdd34f1bd58d8545d3499481b (patch) | |
tree | 47f558153875675cd7e4d0109e1028d2101ff8da /internal/db | |
parent | [docs] Add --config-path to example CLI commands where needed. (#843) (diff) | |
download | gotosocial-4cf76a2bfcc2c19bdd34f1bd58d8545d3499481b.tar.xz |
[chore] Tidy up status deletion, remove from cache too (#845)
* add func for deleting status from db + cache
* move deletes entirely back to processor
and also only do a delete if the requesting account owns the item being deleted
* tidy up unboost processing
* delete status more efficiently
* fix wrong account id on remote test attachments
* fix federator test
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/status.go | 36 | ||||
-rw-r--r-- | internal/db/bundb/status_test.go | 10 | ||||
-rw-r--r-- | internal/db/status.go | 3 |
3 files changed, 49 insertions, 0 deletions
diff --git a/internal/db/bundb/status.go b/internal/db/bundb/status.go index e247e8940..59417afe4 100644 --- a/internal/db/bundb/status.go +++ b/internal/db/bundb/status.go @@ -227,6 +227,42 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status) (* return status, err } +func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) db.Error { + err := s.conn.RunInTx(ctx, func(tx bun.Tx) error { + // delete links between this status and any emojis it uses + if _, err := tx. + NewDelete(). + Model(>smodel.StatusToEmoji{}). + Where("status_id = ?", bun.Ident(id)). + Exec(ctx); err != nil { + return err + } + + // delete links between this status and any tags it uses + if _, err := tx. + NewDelete(). + Model(>smodel.StatusToTag{}). + Where("status_id = ?", bun.Ident(id)). + Exec(ctx); err != nil { + return err + } + + // delete the status itself + if _, err := tx. + NewDelete(). + Model(>smodel.Status{ID: id}). + WherePK(). + Exec(ctx); err != nil { + return err + } + + s.cache.Invalidate(id) + return nil + }) + + return s.conn.ProcessError(err) +} + func (s *statusDB) GetStatusParents(ctx context.Context, status *gtsmodel.Status, onlyDirect bool) ([]*gtsmodel.Status, db.Error) { parents := []*gtsmodel.Status{} s.statusParent(ctx, status, &parents, onlyDirect) diff --git a/internal/db/bundb/status_test.go b/internal/db/bundb/status_test.go index 36e329806..a796ebdad 100644 --- a/internal/db/bundb/status_test.go +++ b/internal/db/bundb/status_test.go @@ -25,6 +25,7 @@ import ( "time" "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/db" ) type StatusTestSuite struct { @@ -132,6 +133,15 @@ func (suite *StatusTestSuite) TestGetStatusChildren() { } } +func (suite *StatusTestSuite) TestDeleteStatus() { + targetStatus := suite.testStatuses["admin_account_status_1"] + err := suite.db.DeleteStatusByID(context.Background(), targetStatus.ID) + suite.NoError(err) + + _, err = suite.db.GetStatusByID(context.Background(), targetStatus.ID) + suite.ErrorIs(err, db.ErrNoEntries) +} + func TestStatusTestSuite(t *testing.T) { suite.Run(t, new(StatusTestSuite)) } diff --git a/internal/db/status.go b/internal/db/status.go index 307d9ea74..55cec5beb 100644 --- a/internal/db/status.go +++ b/internal/db/status.go @@ -41,6 +41,9 @@ type Status interface { // UpdateStatus updates one status in the database and returns it to the caller. UpdateStatus(ctx context.Context, status *gtsmodel.Status) (*gtsmodel.Status, Error) + // DeleteStatusByID deletes one status from the database. + DeleteStatusByID(ctx context.Context, id string) Error + // CountStatusReplies returns the amount of replies recorded for a status, or an error if something goes wrong CountStatusReplies(ctx context.Context, status *gtsmodel.Status) (int, Error) |