summaryrefslogtreecommitdiff
path: root/internal/db/bundb/emoji.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/bundb/emoji.go')
-rw-r--r--internal/db/bundb/emoji.go57
1 files changed, 16 insertions, 41 deletions
diff --git a/internal/db/bundb/emoji.go b/internal/db/bundb/emoji.go
index 6e4b5f36b..db9daf0aa 100644
--- a/internal/db/bundb/emoji.go
+++ b/internal/db/bundb/emoji.go
@@ -20,7 +20,6 @@ package bundb
import (
"context"
"database/sql"
- "errors"
"slices"
"strings"
"time"
@@ -70,34 +69,15 @@ func (e *emojiDB) UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, column
func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
var (
+ // Gather necessary fields from
+ // deleted for cache invaliation.
accountIDs []string
statusIDs []string
)
- defer func() {
- // Invalidate cached emoji.
- e.state.Caches.DB.Emoji.Invalidate("ID", id)
+ // Delete the emoji and all related links to it in a singular transaction.
+ if err := e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- // Invalidate cached account and status IDs.
- e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
- e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
- }()
-
- // Load emoji into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := e.GetEmojiByID(
- gtscontext.SetBarebones(ctx),
- id,
- )
- if err != nil && !errors.Is(err, db.ErrNoEntries) {
- // NOTE: even if db.ErrNoEntries is returned, we
- // still run the below transaction to ensure related
- // objects are appropriately deleted.
- return err
- }
-
- return e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete relational links between this emoji
// and any statuses using it, returning the
// status IDs so we can later update them.
@@ -195,7 +175,16 @@ func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
}
return nil
- })
+ }); err != nil {
+ return err
+ }
+
+ // Invalidate emoji, and any effected statuses / accounts.
+ e.state.Caches.DB.Emoji.Invalidate("ID", id)
+ e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
+ e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
+
+ return nil
}
func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) {
@@ -586,15 +575,8 @@ func (e *emojiDB) GetEmojisByIDs(ctx context.Context, ids []string) ([]*gtsmodel
emojis, err := e.state.Caches.DB.Emoji.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Emoji, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached emojis.
- emojis := make([]*gtsmodel.Emoji, 0, count)
+ emojis := make([]*gtsmodel.Emoji, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -657,15 +639,8 @@ func (e *emojiDB) GetEmojiCategoriesByIDs(ctx context.Context, ids []string) ([]
categories, err := e.state.Caches.DB.EmojiCategory.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.EmojiCategory, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached categories.
- categories := make([]*gtsmodel.EmojiCategory, 0, count)
+ categories := make([]*gtsmodel.EmojiCategory, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.