diff options
author | 2022-10-14 17:30:04 +0200 | |
---|---|---|
committer | 2022-10-14 17:30:04 +0200 | |
commit | f7416d6e941df6fe016d66bb5b53d633775c1f6f (patch) | |
tree | 086ee8e0de19ce6f2b8ed8e04ecae3848544b48f /internal/db | |
parent | [feature] Add `/api/v1/admin/custom_emojis/{id}` endpoint for single emoji GE... (diff) | |
download | gotosocial-f7416d6e941df6fe016d66bb5b53d633775c1f6f.tar.xz |
[feature] Add emoji DELETE handler at `/api/v1/admin/custom_emojis` (#913)
* add emoji DELETE handler
* no need to process error (thanks kim)
* don't double check if user is admin
* add missing security annotation
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/bundb_test.go | 2 | ||||
-rw-r--r-- | internal/db/bundb/emoji.go | 37 | ||||
-rw-r--r-- | internal/db/bundb/emoji_test.go | 11 | ||||
-rw-r--r-- | internal/db/emoji.go | 2 |
4 files changed, 52 insertions, 0 deletions
diff --git a/internal/db/bundb/bundb_test.go b/internal/db/bundb/bundb_test.go index 2af6cf122..b05df8b74 100644 --- a/internal/db/bundb/bundb_test.go +++ b/internal/db/bundb/bundb_test.go @@ -41,6 +41,7 @@ type BunDBStandardTestSuite struct { testTags map[string]*gtsmodel.Tag testMentions map[string]*gtsmodel.Mention testFollows map[string]*gtsmodel.Follow + testEmojis map[string]*gtsmodel.Emoji } func (suite *BunDBStandardTestSuite) SetupSuite() { @@ -54,6 +55,7 @@ func (suite *BunDBStandardTestSuite) SetupSuite() { suite.testTags = testrig.NewTestTags() suite.testMentions = testrig.NewTestMentions() suite.testFollows = testrig.NewTestFollows() + suite.testEmojis = testrig.NewTestEmojis() } func (suite *BunDBStandardTestSuite) SetupTest() { diff --git a/internal/db/bundb/emoji.go b/internal/db/bundb/emoji.go index 4fb4f0ce6..51d767a7b 100644 --- a/internal/db/bundb/emoji.go +++ b/internal/db/bundb/emoji.go @@ -68,6 +68,43 @@ func (e *emojiDB) UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, column return emoji, nil } +func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) db.Error { + if err := e.conn.RunInTx(ctx, func(tx bun.Tx) error { + // delete links between this emoji and any statuses that use it + if _, err := tx. + NewDelete(). + TableExpr("? AS ?", bun.Ident("status_to_emojis"), bun.Ident("status_to_emoji")). + Where("? = ?", bun.Ident("status_to_emoji.emoji_id"), id). + Exec(ctx); err != nil { + return err + } + + // delete links between this emoji and any accounts that use it + if _, err := tx. + NewDelete(). + TableExpr("? AS ?", bun.Ident("account_to_emojis"), bun.Ident("account_to_emoji")). + Where("? = ?", bun.Ident("account_to_emoji.emoji_id"), id). + Exec(ctx); err != nil { + return err + } + + if _, err := tx. + NewDelete(). + TableExpr("? AS ?", bun.Ident("emojis"), bun.Ident("emoji")). + Where("? = ?", bun.Ident("emoji.id"), id). + Exec(ctx); err != nil { + return e.conn.ProcessError(err) + } + + return nil + }); err != nil { + return err + } + + e.cache.Invalidate(id) + return nil +} + func (e *emojiDB) GetEmojis(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, db.Error) { emojiIDs := []string{} diff --git a/internal/db/bundb/emoji_test.go b/internal/db/bundb/emoji_test.go index c6577a721..b542f9b67 100644 --- a/internal/db/bundb/emoji_test.go +++ b/internal/db/bundb/emoji_test.go @@ -38,6 +38,17 @@ func (suite *EmojiTestSuite) TestGetUseableEmojis() { suite.Equal("rainbow", emojis[0].Shortcode) } +func (suite *EmojiTestSuite) TestDeleteEmojiByID() { + testEmoji := suite.testEmojis["rainbow"] + + err := suite.db.DeleteEmojiByID(context.Background(), testEmoji.ID) + suite.NoError(err) + + dbEmoji, err := suite.db.GetEmojiByID(context.Background(), testEmoji.ID) + suite.Nil(dbEmoji) + suite.ErrorIs(err, db.ErrNoEntries) +} + func (suite *EmojiTestSuite) TestGetEmojiByStaticURL() { emoji, err := suite.db.GetEmojiByStaticURL(context.Background(), "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png") suite.NoError(err) diff --git a/internal/db/emoji.go b/internal/db/emoji.go index 831629232..d2f66a377 100644 --- a/internal/db/emoji.go +++ b/internal/db/emoji.go @@ -35,6 +35,8 @@ type Emoji interface { // UpdateEmoji updates the given columns of one emoji. // If no columns are specified, every column is updated. UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, columns ...string) (*gtsmodel.Emoji, Error) + // DeleteEmojiByID deletes one emoji by its database ID. + DeleteEmojiByID(ctx context.Context, id string) Error // GetUseableEmojis gets all emojis which are useable by accounts on this instance. GetUseableEmojis(ctx context.Context) ([]*gtsmodel.Emoji, Error) // GetEmojis gets emojis based on given parameters. Useful for admin actions. |