[bugfix] Fix Postgres emoji delete, emoji category change (#2570)

* [bugfix] Fix Postgres emoji delete, emoji category change

* revert trace logging

* caching issue

* update tests
This commit is contained in:
tobi 2024-01-29 15:57:22 +01:00 committed by GitHub
commit aa8bbe6ad2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 500 additions and 233 deletions

View file

@ -132,28 +132,32 @@ func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
}
for _, statusID := range statusIDs {
var emojiIDs []string
status := new(gtsmodel.Status)
// Select statuses with ID.
if _, err := tx.NewSelect().
Table("statuses").
// Select status emoji IDs.
if err := tx.NewSelect().
Model(status).
Column("emojis").
Where("? = ?", bun.Ident("id"), statusID).
Exec(ctx); err != nil &&
Scan(ctx); err != nil &&
err != sql.ErrNoRows {
return err
}
// Delete all instances of this emoji ID from status emojis.
emojiIDs = slices.DeleteFunc(emojiIDs, func(emojiID string) bool {
return emojiID == id
})
// Delete all instances of this
// emoji ID from status emoji IDs.
status.EmojiIDs = slices.DeleteFunc(
status.EmojiIDs,
func(emojiID string) bool {
return emojiID == id
},
)
// Update status emoji IDs.
if _, err := tx.NewUpdate().
Table("statuses").
Model(status).
Where("? = ?", bun.Ident("id"), statusID).
Set("emojis = ?", emojiIDs).
Column("emojis").
Exec(ctx); err != nil &&
err != sql.ErrNoRows {
return err
@ -161,35 +165,39 @@ func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
}
for _, accountID := range accountIDs {
var emojiIDs []string
account := new(gtsmodel.Account)
// Select account with ID.
if _, err := tx.NewSelect().
Table("accounts").
// Select account emoji IDs.
if err := tx.NewSelect().
Model(account).
Column("emojis").
Where("? = ?", bun.Ident("id"), accountID).
Exec(ctx); err != nil &&
Scan(ctx); err != nil &&
err != sql.ErrNoRows {
return err
}
// Delete all instances of this emoji ID from account emojis.
emojiIDs = slices.DeleteFunc(emojiIDs, func(emojiID string) bool {
return emojiID == id
})
// Delete all instances of this
// emoji ID from account emoji IDs.
account.EmojiIDs = slices.DeleteFunc(
account.EmojiIDs,
func(emojiID string) bool {
return emojiID == id
},
)
// Update account emoji IDs.
if _, err := tx.NewUpdate().
Table("accounts").
Model(account).
Where("? = ?", bun.Ident("id"), accountID).
Set("emojis = ?", emojiIDs).
Column("emojis").
Exec(ctx); err != nil &&
err != sql.ErrNoRows {
return err
}
}
// Delete emoji from database.
// Finally, delete emoji from database.
if _, err := tx.NewDelete().
Table("emojis").
Where("? = ?", bun.Ident("id"), id).

View file

@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/testrig"
)
@ -160,6 +161,16 @@ func (suite *EmojiTestSuite) TestGetEmojiCategory() {
suite.NotNil(category)
}
func (suite *EmojiTestSuite) TestUpdateEmojiCategory() {
testEmoji := new(gtsmodel.Emoji)
*testEmoji = *suite.testEmojis["rainbow"]
testEmoji.CategoryID = ""
err := suite.db.UpdateEmoji(context.Background(), testEmoji, "category_id")
suite.NoError(err)
}
func TestEmojiTestSuite(t *testing.T) {
suite.Run(t, new(EmojiTestSuite))
}