summaryrefslogtreecommitdiff
path: root/internal/db/bundb
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/bundb')
-rw-r--r--internal/db/bundb/account.go68
-rw-r--r--internal/db/bundb/application.go9
-rw-r--r--internal/db/bundb/conversation.go43
-rw-r--r--internal/db/bundb/emoji.go57
-rw-r--r--internal/db/bundb/filter.go9
-rw-r--r--internal/db/bundb/filterkeyword.go8
-rw-r--r--internal/db/bundb/filterstatus.go9
-rw-r--r--internal/db/bundb/list.go569
-rw-r--r--internal/db/bundb/list_test.go145
-rw-r--r--internal/db/bundb/media.go75
-rw-r--r--internal/db/bundb/mention.go37
-rw-r--r--internal/db/bundb/move.go16
-rw-r--r--internal/db/bundb/notification.go15
-rw-r--r--internal/db/bundb/poll.go226
-rw-r--r--internal/db/bundb/poll_test.go36
-rw-r--r--internal/db/bundb/relationship_block.go136
-rw-r--r--internal/db/bundb/relationship_follow.go214
-rw-r--r--internal/db/bundb/relationship_follow_req.go190
-rw-r--r--internal/db/bundb/relationship_mute.go95
-rw-r--r--internal/db/bundb/relationship_test.go12
-rw-r--r--internal/db/bundb/report.go49
-rw-r--r--internal/db/bundb/report_test.go4
-rw-r--r--internal/db/bundb/sinbinstatus.go19
-rw-r--r--internal/db/bundb/status.go62
-rw-r--r--internal/db/bundb/statusbookmark.go75
-rw-r--r--internal/db/bundb/statusfave.go9
-rw-r--r--internal/db/bundb/tag.go80
-rw-r--r--internal/db/bundb/timeline.go34
-rw-r--r--internal/db/bundb/timeline_test.go4
-rw-r--r--internal/db/bundb/tombstone.go6
-rw-r--r--internal/db/bundb/user.go36
31 files changed, 987 insertions, 1360 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go
index 1569af9cb..16c82c08f 100644
--- a/internal/db/bundb/account.go
+++ b/internal/db/bundb/account.go
@@ -64,15 +64,8 @@ func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsm
accounts, err := a.state.Caches.DB.Account.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Account, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached accounts.
- accounts := make([]*gtsmodel.Account, 0, count)
+ accounts := make([]*gtsmodel.Account, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) account IDs.
@@ -796,20 +789,14 @@ func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account
}
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
- defer a.state.Caches.DB.Account.Invalidate("ID", id)
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Account
+ deleted.ID = id
- // Load account into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := a.GetAccountByID(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
- }
+ // Delete account from database and any related links in a transaction.
+ if err := a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// clear out any emoji links
if _, err := tx.
NewDelete().
@@ -822,44 +809,19 @@ func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
// delete the account
_, err := tx.
NewDelete().
- TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
- Where("? = ?", bun.Ident("account.id"), id).
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?", bun.Ident("uri")).
Exec(ctx)
return err
- })
-}
-
-func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error {
- if *mediaAttachment.Avatar && *mediaAttachment.Header {
- return errors.New("one media attachment cannot be both header and avatar")
- }
-
- var column bun.Ident
- switch {
- case *mediaAttachment.Avatar:
- column = bun.Ident("account.avatar_media_attachment_id")
- case *mediaAttachment.Header:
- column = bun.Ident("account.header_media_attachment_id")
- default:
- return errors.New("given media attachment was neither a header nor an avatar")
- }
-
- // TODO: there are probably more side effects here that need to be handled
- if _, err := a.db.
- NewInsert().
- Model(mediaAttachment).
- Exec(ctx); err != nil {
+ }); err != nil {
return err
}
- if _, err := a.db.
- NewUpdate().
- TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
- Set("? = ?", column, mediaAttachment.ID).
- Where("? = ?", bun.Ident("account.id"), accountID).
- Exec(ctx); err != nil {
- return err
- }
+ // Invalidate cached account by its ID, manually
+ // call invalidate hook in case not cached.
+ a.state.Caches.DB.Account.Invalidate("ID", id)
+ a.state.Caches.OnInvalidateAccount(&deleted)
return nil
}
diff --git a/internal/db/bundb/application.go b/internal/db/bundb/application.go
index 72c4ec206..fda0ba602 100644
--- a/internal/db/bundb/application.go
+++ b/internal/db/bundb/application.go
@@ -147,15 +147,8 @@ func (a *applicationDB) GetAllTokens(ctx context.Context) ([]*gtsmodel.Token, er
tokens, err := a.state.Caches.DB.Token.LoadIDs("ID",
tokenIDs,
func(uncached []string) ([]*gtsmodel.Token, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached tokens.
- tokens := make([]*gtsmodel.Token, 0, count)
+ tokens := make([]*gtsmodel.Token, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) token IDs.
diff --git a/internal/db/bundb/conversation.go b/internal/db/bundb/conversation.go
index 053b23e31..22ff4fd79 100644
--- a/internal/db/bundb/conversation.go
+++ b/internal/db/bundb/conversation.go
@@ -188,15 +188,8 @@ func (c *conversationDB) getConversationsByLastStatusIDs(
accountID,
conversationLastStatusIDs,
func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached conversations.
- conversations := make([]*gtsmodel.Conversation, 0, count)
+ conversations := make([]*gtsmodel.Conversation, 0, len(uncached))
// Perform database query scanning the remaining (uncached) IDs.
if err := c.db.NewSelect().
@@ -267,27 +260,27 @@ func (c *conversationDB) LinkConversationToStatus(ctx context.Context, conversat
}
func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
- // Load conversation into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := c.GetConversationByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Conversation
+ deleted.ID = id
+
+ // Delete conversation from DB.
+ if _, err := c.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?", bun.Ident("account_id")).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached conversation on return after delete.
- defer c.state.Caches.DB.Conversation.Invalidate("ID", id)
+ // Invalidate cached conversation by ID,
+ // manually invalidate hook in case not cached.
+ c.state.Caches.DB.Conversation.Invalidate("ID", id)
+ c.state.Caches.OnInvalidateConversation(&deleted)
- // Finally delete conversation from DB.
- _, err = c.db.NewDelete().
- Model((*gtsmodel.Conversation)(nil)).
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ return nil
}
func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {
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.
diff --git a/internal/db/bundb/filter.go b/internal/db/bundb/filter.go
index b84091cd6..e68a0bcd0 100644
--- a/internal/db/bundb/filter.go
+++ b/internal/db/bundb/filter.go
@@ -83,14 +83,7 @@ func (f *filterDB) GetFiltersForAccountID(ctx context.Context, accountID string)
filters, err := f.state.Caches.DB.Filter.LoadIDs("ID",
filterIDs,
func(uncached []string) ([]*gtsmodel.Filter, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
- filters := make([]*gtsmodel.Filter, 0, count)
+ filters := make([]*gtsmodel.Filter, 0, len(uncached))
if err := f.db.
NewSelect().
Model(&filters).
diff --git a/internal/db/bundb/filterkeyword.go b/internal/db/bundb/filterkeyword.go
index cb9958b81..8a006d10f 100644
--- a/internal/db/bundb/filterkeyword.go
+++ b/internal/db/bundb/filterkeyword.go
@@ -113,14 +113,8 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID",
filterKeywordIDs,
func(uncached []string) ([]*gtsmodel.FilterKeyword, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
+ filterKeywords := make([]*gtsmodel.FilterKeyword, 0, len(uncached))
- filterKeywords := make([]*gtsmodel.FilterKeyword, 0, count)
if err := f.db.
NewSelect().
Model(&filterKeywords).
diff --git a/internal/db/bundb/filterstatus.go b/internal/db/bundb/filterstatus.go
index 8256cd401..95919bd2c 100644
--- a/internal/db/bundb/filterstatus.go
+++ b/internal/db/bundb/filterstatus.go
@@ -100,14 +100,7 @@ func (f *filterDB) getFilterStatuses(ctx context.Context, idColumn string, id st
filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID",
filterStatusIDs,
func(uncached []string) ([]*gtsmodel.FilterStatus, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
- filterStatuses := make([]*gtsmodel.FilterStatus, 0, count)
+ filterStatuses := make([]*gtsmodel.FilterStatus, 0, len(uncached))
if err := f.db.
NewSelect().
Model(&filterStatuses).
diff --git a/internal/db/bundb/list.go b/internal/db/bundb/list.go
index 0ed0f1b15..03dff95e3 100644
--- a/internal/db/bundb/list.go
+++ b/internal/db/bundb/list.go
@@ -29,6 +29,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/paging"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/uptrace/bun"
@@ -85,39 +86,52 @@ func (l *listDB) getList(ctx context.Context, lookup string, dbQuery func(*gtsmo
return list, nil
}
-func (l *listDB) GetListsForAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) {
- // Fetch IDs of all lists owned by this account.
- var listIDs []string
- if err := l.db.
- NewSelect().
- TableExpr("? AS ?", bun.Ident("lists"), bun.Ident("list")).
- Column("list.id").
- Where("? = ?", bun.Ident("list.account_id"), accountID).
- Order("list.id DESC").
- Scan(ctx, &listIDs); err != nil {
+func (l *listDB) GetListsByAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) {
+ listIDs, err := l.getListIDsByAccountID(ctx, accountID)
+ if err != nil {
return nil, err
}
+ return l.GetListsByIDs(ctx, listIDs)
+}
- if len(listIDs) == 0 {
- return nil, nil
- }
+func (l *listDB) CountListsByAccountID(ctx context.Context, accountID string) (int, error) {
+ listIDs, err := l.getListIDsByAccountID(ctx, accountID)
+ return len(listIDs), err
+}
- // Return lists by their IDs.
+func (l *listDB) GetListsContainingFollowID(ctx context.Context, followID string) ([]*gtsmodel.List, error) {
+ listIDs, err := l.getListIDsWithFollowID(ctx, followID)
+ if err != nil {
+ return nil, err
+ }
return l.GetListsByIDs(ctx, listIDs)
}
-func (l *listDB) CountListsForAccountID(ctx context.Context, accountID string) (int, error) {
- return l.db.
- NewSelect().
- Table("lists").
- Where("? = ?", bun.Ident("account_id"), accountID).
- Count(ctx)
+func (l *listDB) GetFollowsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Follow, error) {
+ followIDs, err := l.GetFollowIDsInList(ctx, listID, page)
+ if err != nil {
+ return nil, err
+ }
+ return l.state.DB.GetFollowsByIDs(ctx, followIDs)
+}
+
+func (l *listDB) GetAccountsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Account, error) {
+ accountIDs, err := l.GetAccountIDsInList(ctx, listID, page)
+ if err != nil {
+ return nil, err
+ }
+ return l.state.DB.GetAccountsByIDs(ctx, accountIDs)
+}
+
+func (l *listDB) IsAccountInList(ctx context.Context, listID string, accountID string) (bool, error) {
+ accountIDs, err := l.GetAccountIDsInList(ctx, listID, nil)
+ return slices.Contains(accountIDs, accountID), err
}
func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
var (
err error
- errs = gtserror.NewMultiError(2)
+ errs gtserror.MultiError
)
if list.Account == nil {
@@ -131,22 +145,12 @@ func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
}
}
- if list.ListEntries == nil {
- // List entries are not set, fetch from the database.
- list.ListEntries, err = l.state.DB.GetListEntries(
- gtscontext.SetBarebones(ctx),
- list.ID,
- "", "", "", 0,
- )
- if err != nil {
- errs.Appendf("error populating list entries: %w", err)
- }
- }
-
return errs.Combine()
}
func (l *listDB) PutList(ctx context.Context, list *gtsmodel.List) error {
+ // note that inserting list will call OnInvalidateList()
+ // which will handle clearing caches other than List cache.
return l.state.Caches.DB.List.Store(list, func() error {
_, err := l.db.NewInsert().Model(list).Exec(ctx)
return err
@@ -160,192 +164,146 @@ func (l *listDB) UpdateList(ctx context.Context, list *gtsmodel.List, columns ..
columns = append(columns, "updated_at")
}
- defer func() {
- // Invalidate all entries for this list ID.
- l.state.Caches.DB.ListEntry.Invalidate("ListID", list.ID)
-
- // Invalidate this entire list's timeline.
- if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
- log.Errorf(ctx, "error invalidating list timeline: %q", err)
- }
- }()
-
- return l.state.Caches.DB.List.Store(list, func() error {
+ // Update list in the database, invalidating main list cache.
+ if err := l.state.Caches.DB.List.Store(list, func() error {
_, err := l.db.NewUpdate().
Model(list).
Where("? = ?", bun.Ident("list.id"), list.ID).
Column(columns...).
Exec(ctx)
return err
- })
-}
-
-func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
- // Load list by ID into cache to ensure we can perform
- // all necessary cache invalidation hooks on removal.
- _, err := l.GetListByID(
- // Don't populate the entry;
- // we only want the list ID.
- 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.
+ }); err != nil {
return err
}
- defer func() {
- // Invalidate this list from cache.
- l.state.Caches.DB.List.Invalidate("ID", id)
+ // Invalidate this entire list's timeline.
+ if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
+ log.Errorf(ctx, "error invalidating list timeline: %q", err)
+ }
- // Invalidate this entire list's timeline.
- if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
- log.Errorf(ctx, "error invalidating list timeline: %q", err)
- }
- }()
+ return nil
+}
- return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- // Delete all entries attached to list.
+func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
+ // Acquire list owner ID.
+ var accountID string
+
+ // Gather follow IDs of all
+ // entries contained in list.
+ var followIDs []string
+
+ // Delete all list entries associated with list, and list itself in transaction.
+ if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
if _, err := tx.NewDelete().
Table("list_entries").
Where("? = ?", bun.Ident("list_id"), id).
- Exec(ctx); err != nil {
+ Returning("?", bun.Ident("follow_id")).
+ Exec(ctx, &followIDs); err != nil {
return err
}
- // Delete the list itself.
_, err := tx.NewDelete().
Table("lists").
Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
+ Returning("?", bun.Ident("account_id")).
+ Exec(ctx, &accountID)
return err
- })
-}
+ }); err != nil {
+ return err
+ }
-/*
- LIST ENTRY functions
-*/
+ // Invalidate the main list database cache.
+ l.state.Caches.DB.List.Invalidate("ID", id)
-func (l *listDB) GetListEntryByID(ctx context.Context, id string) (*gtsmodel.ListEntry, error) {
- return l.getListEntry(
- ctx,
- "ID",
- func(listEntry *gtsmodel.ListEntry) error {
- return l.db.NewSelect().
- Model(listEntry).
- Where("? = ?", bun.Ident("list_entry.id"), id).
- Scan(ctx)
- },
- id,
- )
+ // Invalidate cache of list IDs owned by account.
+ l.state.Caches.DB.ListIDs.Invalidate("a" + accountID)
+
+ // Invalidate all related entry caches for this list.
+ l.invalidateEntryCaches(ctx, []string{id}, followIDs)
+
+ return nil
}
-func (l *listDB) getListEntry(ctx context.Context, lookup string, dbQuery func(*gtsmodel.ListEntry) error, keyParts ...any) (*gtsmodel.ListEntry, error) {
- listEntry, err := l.state.Caches.DB.ListEntry.LoadOne(lookup, func() (*gtsmodel.ListEntry, error) {
- var listEntry gtsmodel.ListEntry
+func (l *listDB) getListIDsByAccountID(ctx context.Context, accountID string) ([]string, error) {
+ return l.state.Caches.DB.ListIDs.Load("a"+accountID, func() ([]string, error) {
+ var listIDs []string
- // Not cached! Perform database query.
- if err := dbQuery(&listEntry); err != nil {
+ // List IDs not in cache.
+ // Perform the DB query.
+ if _, err := l.db.NewSelect().
+ Table("lists").
+ Column("id").
+ Where("? = ?", bun.Ident("account_id"), accountID).
+ OrderExpr("? DESC", bun.Ident("created_at")).
+ Exec(ctx, &listIDs); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return nil, err
}
- return &listEntry, nil
- }, keyParts...)
- if err != nil {
- return nil, err // already processed
- }
-
- if gtscontext.Barebones(ctx) {
- // Only a barebones model was requested.
- return listEntry, nil
- }
-
- // Further populate the list entry fields where applicable.
- if err := l.state.DB.PopulateListEntry(ctx, listEntry); err != nil {
- return nil, err
- }
-
- return listEntry, nil
+ return listIDs, nil
+ })
}
-func (l *listDB) GetListEntries(ctx context.Context,
- listID string,
- maxID string,
- sinceID string,
- minID string,
- limit int,
-) ([]*gtsmodel.ListEntry, error) {
- // Ensure reasonable
- if limit < 0 {
- limit = 0
- }
-
- // Make educated guess for slice size
- var (
- entryIDs = make([]string, 0, limit)
- frontToBack = true
- )
-
- q := l.db.
- NewSelect().
- TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")).
- // Select only IDs from table
- Column("entry.id").
- // Select only entries belonging to listID.
- Where("? = ?", bun.Ident("entry.list_id"), listID)
-
- if maxID != "" {
- // return only entries LOWER (ie., older) than maxID
- q = q.Where("? < ?", bun.Ident("entry.id"), maxID)
- }
-
- if sinceID != "" {
- // return only entries HIGHER (ie., newer) than sinceID
- q = q.Where("? > ?", bun.Ident("entry.id"), sinceID)
- }
+func (l *listDB) getListIDsWithFollowID(ctx context.Context, followID string) ([]string, error) {
+ return l.state.Caches.DB.ListIDs.Load("f"+followID, func() ([]string, error) {
+ var listIDs []string
- if minID != "" {
- // return only entries HIGHER (ie., newer) than minID
- q = q.Where("? > ?", bun.Ident("entry.id"), minID)
-
- // page up
- frontToBack = false
- }
+ // List IDs not in cache.
+ // Perform the DB query.
+ if _, err := l.db.NewSelect().
+ Table("list_entries").
+ Column("list_id").
+ Where("? = ?", bun.Ident("follow_id"), followID).
+ OrderExpr("? DESC", bun.Ident("created_at")).
+ Exec(ctx, &listIDs); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
+ return nil, err
+ }
- if limit > 0 {
- // limit amount of entries returned
- q = q.Limit(limit)
- }
+ return listIDs, nil
+ })
+}
- if frontToBack {
- // Page down.
- q = q.Order("entry.id DESC")
- } else {
- // Page up.
- q = q.Order("entry.id ASC")
- }
+func (l *listDB) GetFollowIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
+ return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "f"+listID, page, func() ([]string, error) {
+ var followIDs []string
- if err := q.Scan(ctx, &entryIDs); err != nil {
- return nil, err
- }
+ // Follow IDs not in cache.
+ // Perform the DB query.
+ _, err := l.db.NewSelect().
+ Table("list_entries").
+ Column("follow_id").
+ Where("? = ?", bun.Ident("list_id"), listID).
+ OrderExpr("? DESC", bun.Ident("created_at")).
+ Exec(ctx, &followIDs)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return nil, err
+ }
- if len(entryIDs) == 0 {
- return nil, nil
- }
+ return followIDs, nil
+ })
+}
- // If we're paging up, we still want entries
- // to be sorted by ID desc, so reverse ids slice.
- // https://zchee.github.io/golang-wiki/SliceTricks/#reversing
- if !frontToBack {
- for l, r := 0, len(entryIDs)-1; l < r; l, r = l+1, r-1 {
- entryIDs[l], entryIDs[r] = entryIDs[r], entryIDs[l]
+func (l *listDB) GetAccountIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
+ return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "a"+listID, page, func() ([]string, error) {
+ var accountIDs []string
+
+ // Account IDs not in cache.
+ // Perform the DB query.
+ _, err := l.db.NewSelect().
+ Table("follows").
+ Column("follows.target_account_id").
+ Join("INNER JOIN ?", bun.Ident("list_entries")).
+ JoinOn("? = ?", bun.Ident("follows.id"), bun.Ident("list_entries.follow_id")).
+ Where("? = ?", bun.Ident("list_entries.list_id"), listID).
+ OrderExpr("? DESC", bun.Ident("list_entries.id")).
+ Exec(ctx, &accountIDs)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return nil, err
}
- }
- // Return list entries by their IDs.
- return l.GetListEntriesByIDs(ctx, entryIDs)
+ return accountIDs, nil
+ })
}
func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) {
@@ -353,15 +311,8 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
lists, err := l.state.Caches.DB.List.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.List, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached lists.
- lists := make([]*gtsmodel.List, 0, count)
+ lists := make([]*gtsmodel.List, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -402,82 +353,6 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
return lists, nil
}
-func (l *listDB) GetListEntriesByIDs(ctx context.Context, ids []string) ([]*gtsmodel.ListEntry, error) {
- // Load all entry IDs via cache loader callbacks.
- entries, err := l.state.Caches.DB.ListEntry.LoadIDs("ID",
- ids,
- func(uncached []string) ([]*gtsmodel.ListEntry, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
- // Preallocate expected length of uncached entries.
- entries := make([]*gtsmodel.ListEntry, 0, count)
-
- // Perform database query scanning
- // the remaining (uncached) IDs.
- if err := l.db.NewSelect().
- Model(&entries).
- Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
- Scan(ctx); err != nil {
- return nil, err
- }
-
- return entries, nil
- },
- )
- if err != nil {
- return nil, err
- }
-
- // Reorder the entries by their
- // IDs to ensure in correct order.
- getID := func(e *gtsmodel.ListEntry) string { return e.ID }
- util.OrderBy(entries, ids, getID)
-
- if gtscontext.Barebones(ctx) {
- // no need to fully populate.
- return entries, nil
- }
-
- // Populate all loaded entries, removing those we fail to
- // populate (removes needing so many nil checks everywhere).
- entries = slices.DeleteFunc(entries, func(entry *gtsmodel.ListEntry) bool {
- if err := l.PopulateListEntry(ctx, entry); err != nil {
- log.Errorf(ctx, "error populating entry %s: %v", entry.ID, err)
- return true
- }
- return false
- })
-
- return entries, nil
-}
-
-func (l *listDB) GetListEntriesForFollowID(ctx context.Context, followID string) ([]*gtsmodel.ListEntry, error) {
- var entryIDs []string
-
- if err := l.db.
- NewSelect().
- TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")).
- // Select only IDs from table
- Column("entry.id").
- // Select only entries belonging with given followID.
- Where("? = ?", bun.Ident("entry.follow_id"), followID).
- Scan(ctx, &entryIDs); err != nil {
- return nil, err
- }
-
- if len(entryIDs) == 0 {
- return nil, nil
- }
-
- // Return list entries by their IDs.
- return l.GetListEntriesByIDs(ctx, entryIDs)
-}
-
func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.ListEntry) error {
var err error
@@ -496,109 +371,111 @@ func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.List
}
func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEntry) error {
- defer func() {
- // Collect unique list IDs from the provided entries.
- listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
- return e.ListID
- })
-
- for _, id := range listIDs {
- // Invalidate the timeline for the list this entry belongs to.
- if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
- log.Errorf(ctx, "error invalidating list timeline: %q", err)
- }
- }
- }()
-
- // Finally, insert each list entry into the database.
- return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ // Insert all entries into the database in a single transaction (all or nothing!).
+ if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
for _, entry := range entries {
- entry := entry // rescope
- if err := l.state.Caches.DB.ListEntry.Store(entry, func() error {
- _, err := tx.
- NewInsert().
- Model(entry).
- Exec(ctx)
- return err
- }); err != nil {
+ if _, err := tx.
+ NewInsert().
+ Model(entry).
+ Exec(ctx); err != nil {
return err
}
}
return nil
+ }); err != nil {
+ return err
+ }
+
+ // Collect unique list IDs from the provided list entries.
+ listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
+ return e.ListID
+ })
+
+ // Collect unique follow IDs from the provided list entries.
+ followIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
+ return e.FollowID
})
+
+ // Invalidate all related list entry caches.
+ l.invalidateEntryCaches(ctx, listIDs, followIDs)
+
+ return nil
}
-func (l *listDB) DeleteListEntry(ctx context.Context, id string) error {
- // Load list entry into cache to ensure we can perform
- // all necessary cache invalidation hooks on removal.
- entry, err := l.GetListEntryByID(
- // Don't populate the entry;
- // we only want the list ID.
- gtscontext.SetBarebones(ctx),
- id,
- )
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // Already gone.
- return nil
- }
+func (l *listDB) DeleteListEntry(ctx context.Context, listID string, followID string) error {
+ // Delete list entry with given
+ // ID, returning its list ID.
+ if _, err := l.db.NewDelete().
+ Table("list_entries").
+ Where("? = ?", bun.Ident("list_id"), listID).
+ Where("? = ?", bun.Ident("follow_id"), followID).
+ Exec(ctx, &listID); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- defer func() {
- // Invalidate this list entry upon delete.
- l.state.Caches.DB.ListEntry.Invalidate("ID", id)
+ // Invalidate all related list entry caches.
+ l.invalidateEntryCaches(ctx, []string{listID},
+ []string{followID})
- // Invalidate the timeline for the list this entry belongs to.
- if err := l.state.Timelines.List.RemoveTimeline(ctx, entry.ListID); err != nil {
- log.Errorf(ctx, "error invalidating list timeline: %q", err)
- }
- }()
-
- // Finally delete the list entry.
- _, err = l.db.NewDelete().
- Table("list_entries").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ return nil
}
-func (l *listDB) DeleteListEntriesForFollowID(ctx context.Context, followID string) error {
- var entryIDs []string
+func (l *listDB) DeleteAllListEntriesByFollows(ctx context.Context, followIDs ...string) error {
+ var listIDs []string
+
+ // Check for empty list.
+ if len(followIDs) == 0 {
+ return nil
+ }
- // Fetch entry IDs for follow ID.
- if err := l.db.
- NewSelect().
+ // Delete all entries with follow
+ // ID, returning IDs and list IDs.
+ if _, err := l.db.NewDelete().
Table("list_entries").
- Column("id").
- Where("? = ?", bun.Ident("follow_id"), followID).
- Order("id DESC").
- Scan(ctx, &entryIDs); err != nil {
+ Where("? IN (?)", bun.Ident("follow_id"), bun.In(followIDs)).
+ Returning("?", bun.Ident("list_id")).
+ Exec(ctx, &listIDs); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- for _, id := range entryIDs {
- // Delete each separately to trigger cache invalidations.
- if err := l.DeleteListEntry(ctx, id); err != nil {
- return err
- }
- }
+ // Deduplicate IDs before invalidate.
+ listIDs = util.Deduplicate(listIDs)
+
+ // Invalidate all related list entry caches.
+ l.invalidateEntryCaches(ctx, listIDs, followIDs)
return nil
}
-func (l *listDB) ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) {
- exists, err := l.db.
- NewSelect().
- TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("list_entry")).
- Join(
- "JOIN ? AS ? ON ? = ?",
- bun.Ident("follows"), bun.Ident("follow"),
- bun.Ident("list_entry.follow_id"), bun.Ident("follow.id"),
- ).
- Where("? = ?", bun.Ident("list_entry.list_id"), listID).
- Where("? = ?", bun.Ident("follow.target_account_id"), accountID).
- Exists(ctx)
-
- return exists, err
+// invalidateEntryCaches will invalidate all related ListEntry caches for given list IDs and follow IDs, including timelines.
+func (l *listDB) invalidateEntryCaches(ctx context.Context, listIDs, followIDs []string) {
+ var keys []string
+
+ // Generate ListedID keys to invalidate.
+ keys = slices.Grow(keys[:0], 2*len(listIDs))
+ for _, listID := range listIDs {
+ keys = append(keys,
+ "a"+listID,
+ "f"+listID,
+ )
+
+ // Invalidate the timeline for the list this entry belongs to.
+ if err := l.state.Timelines.List.RemoveTimeline(ctx, listID); err != nil {
+ log.Errorf(ctx, "error invalidating list timeline: %q", err)
+ }
+ }
+
+ // Invalidate ListedID slice cache entries.
+ l.state.Caches.DB.ListedIDs.Invalidate(keys...)
+
+ // Generate ListID keys to invalidate.
+ keys = slices.Grow(keys[:0], len(followIDs))
+ for _, followID := range followIDs {
+ keys = append(keys, "f"+followID)
+ }
+
+ // Invalidate ListID slice cache entries.
+ l.state.Caches.DB.ListIDs.Invalidate(keys...)
}
diff --git a/internal/db/bundb/list_test.go b/internal/db/bundb/list_test.go
index 9c5fb2c76..3952a87c0 100644
--- a/internal/db/bundb/list_test.go
+++ b/internal/db/bundb/list_test.go
@@ -24,7 +24,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
- "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@@ -32,7 +31,7 @@ type ListTestSuite struct {
BunDBStandardTestSuite
}
-func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
+func (suite *ListTestSuite) testStructs() (*gtsmodel.List, []*gtsmodel.ListEntry, *gtsmodel.Account) {
testList := &gtsmodel.List{}
*testList = *suite.testLists["local_account_1_list_1"]
@@ -55,12 +54,10 @@ func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
}
})
- testList.ListEntries = entries
-
testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
- return testList, testAccount
+ return testList, entries, testAccount
}
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
@@ -103,7 +100,7 @@ func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, act
}
func (suite *ListTestSuite) TestGetListByID() {
- testList, _ := suite.testStructs()
+ testList, _, _ := suite.testStructs()
dbList, err := suite.db.GetListByID(context.Background(), testList.ID)
if err != nil {
@@ -111,13 +108,12 @@ func (suite *ListTestSuite) TestGetListByID() {
}
suite.checkList(testList, dbList)
- suite.checkListEntries(testList.ListEntries, dbList.ListEntries)
}
func (suite *ListTestSuite) TestGetListsForAccountID() {
- testList, testAccount := suite.testStructs()
+ testList, _, testAccount := suite.testStructs()
- dbLists, err := suite.db.GetListsForAccountID(context.Background(), testAccount.ID)
+ dbLists, err := suite.db.GetListsByAccountID(context.Background(), testAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
@@ -129,20 +125,9 @@ func (suite *ListTestSuite) TestGetListsForAccountID() {
suite.checkList(testList, dbLists[0])
}
-func (suite *ListTestSuite) TestGetListEntries() {
- testList, _ := suite.testStructs()
-
- dbListEntries, err := suite.db.GetListEntries(context.Background(), testList.ID, "", "", "", 0)
- if err != nil {
- suite.FailNow(err.Error())
- }
-
- suite.checkListEntries(testList.ListEntries, dbListEntries)
-}
-
func (suite *ListTestSuite) TestPutList() {
ctx := context.Background()
- _, testAccount := suite.testStructs()
+ _, _, testAccount := suite.testStructs()
testList := &gtsmodel.List{
ID: "01H0J2PMYM54618VCV8Y8QYAT4",
@@ -166,7 +151,7 @@ func (suite *ListTestSuite) TestPutList() {
func (suite *ListTestSuite) TestUpdateList() {
ctx := context.Background()
- testList, _ := suite.testStructs()
+ testList, _, _ := suite.testStructs()
// Get List in the cache first.
dbList, err := suite.db.GetListByID(ctx, testList.ID)
@@ -192,7 +177,7 @@ func (suite *ListTestSuite) TestUpdateList() {
func (suite *ListTestSuite) TestDeleteList() {
ctx := context.Background()
- testList, _ := suite.testStructs()
+ testList, _, _ := suite.testStructs()
// Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
@@ -209,18 +194,19 @@ func (suite *ListTestSuite) TestDeleteList() {
_, err := suite.db.GetListByID(ctx, testList.ID)
suite.ErrorIs(err, db.ErrNoEntries)
- // All entries belonging to this
- // list should now be deleted.
- listEntries, err := suite.db.GetListEntries(ctx, testList.ID, "", "", "", 0)
- if err != nil {
- suite.FailNow(err.Error())
- }
- suite.Empty(listEntries)
+ // All accounts / follows attached to this
+ // list should now be return empty values.
+ listAccounts, err1 := suite.db.GetAccountsInList(ctx, testList.ID, nil)
+ listFollows, err2 := suite.db.GetFollowsInList(ctx, testList.ID, nil)
+ suite.NoError(err1)
+ suite.NoError(err2)
+ suite.Empty(listAccounts)
+ suite.Empty(listFollows)
}
func (suite *ListTestSuite) TestPutListEntries() {
ctx := context.Background()
- testList, _ := suite.testStructs()
+ testList, testEntries, _ := suite.testStructs()
listEntries := []*gtsmodel.ListEntry{
{
@@ -244,91 +230,58 @@ func (suite *ListTestSuite) TestPutListEntries() {
suite.FailNow(err.Error())
}
- // Add these entries to the test list, sort it again
- // to reflect what we'd expect to get from the db.
- testList.ListEntries = append(testList.ListEntries, listEntries...)
- slices.SortFunc(testList.ListEntries, func(a, b *gtsmodel.ListEntry) int {
- const k = -1
- switch {
- case a.ID > b.ID:
- return +k
- case a.ID < b.ID:
- return -k
- default:
- return 0
- }
- })
-
- // Now get all list entries from the db.
- // Use barebones for this because the ones
- // we just added will fail if we try to get
- // the nonexistent follows.
- dbListEntries, err := suite.db.GetListEntries(
- gtscontext.SetBarebones(ctx),
- testList.ID,
- "", "", "", 0)
- if err != nil {
- suite.FailNow(err.Error())
- }
-
- suite.checkListEntries(testList.ListEntries, dbListEntries)
+ // Get all follows stored under this list ID, to ensure
+ // the newly added list entry follows are among these.
+ followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
+ suite.NoError(err)
+ suite.Len(followIDs, len(testEntries)+len(listEntries))
+ suite.Contains(followIDs, "01H0MKNFRFZS8R9WV6DBX31Y03")
+ suite.Contains(followIDs, "01H0MKP6RR8VEHN3GVWFBP2H30")
+ suite.Contains(followIDs, "01H0MKQ0KA29C6NFJ27GTZD16J")
}
func (suite *ListTestSuite) TestDeleteListEntry() {
ctx := context.Background()
- testList, _ := suite.testStructs()
-
- // Get List in the cache first.
- if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
- suite.FailNow(err.Error())
- }
+ testList, testEntries, _ := suite.testStructs()
// Delete the first entry.
- if err := suite.db.DeleteListEntry(ctx, testList.ListEntries[0].ID); err != nil {
- suite.FailNow(err.Error())
- }
-
- // Get list from the db again.
- dbList, err := suite.db.GetListByID(ctx, testList.ID)
- if err != nil {
+ if err := suite.db.DeleteListEntry(ctx,
+ testEntries[0].ListID,
+ testEntries[0].FollowID,
+ ); err != nil {
suite.FailNow(err.Error())
}
- // Bodge the testlist as though
- // we'd removed the first entry.
- testList.ListEntries = testList.ListEntries[1:]
- suite.checkList(testList, dbList)
+ // Get all follows stored under this list ID, to ensure
+ // the newly removed list entry follow is now missing.
+ followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
+ suite.NoError(err)
+ suite.Len(followIDs, len(testEntries)-1)
+ suite.NotContains(followIDs, testEntries[0].FollowID)
}
-func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() {
+func (suite *ListTestSuite) TestDeleteAllListEntriesByFollows() {
ctx := context.Background()
- testList, _ := suite.testStructs()
-
- // Get List in the cache first.
- if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
- suite.FailNow(err.Error())
- }
+ testList, testEntries, _ := suite.testStructs()
// Delete the first entry.
- if err := suite.db.DeleteListEntriesForFollowID(ctx, testList.ListEntries[0].FollowID); err != nil {
+ if err := suite.db.DeleteAllListEntriesByFollows(ctx,
+ testEntries[0].FollowID,
+ ); err != nil {
suite.FailNow(err.Error())
}
- // Get list from the db again.
- dbList, err := suite.db.GetListByID(ctx, testList.ID)
- if err != nil {
- suite.FailNow(err.Error())
- }
-
- // Bodge the testlist as though
- // we'd removed the first entry.
- testList.ListEntries = testList.ListEntries[1:]
- suite.checkList(testList, dbList)
+ // Get all follows stored under this list ID, to ensure
+ // the newly removed list entry follow is now missing.
+ followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
+ suite.NoError(err)
+ suite.Len(followIDs, len(testEntries)-1)
+ suite.NotContains(followIDs, testEntries[0].FollowID)
}
func (suite *ListTestSuite) TestListIncludesAccount() {
ctx := context.Background()
- testList, _ := suite.testStructs()
+ testList, _, _ := suite.testStructs()
for accountID, expected := range map[string]bool{
suite.testAccounts["admin_account"].ID: true,
@@ -336,7 +289,7 @@ func (suite *ListTestSuite) TestListIncludesAccount() {
suite.testAccounts["local_account_2"].ID: true,
"01H7074GEZJ56J5C86PFB0V2CT": false,
} {
- includes, err := suite.db.ListIncludesAccount(ctx, testList.ID, accountID)
+ includes, err := suite.db.IsAccountInList(ctx, testList.ID, accountID)
if err != nil {
suite.FailNow(err.Error())
}
diff --git a/internal/db/bundb/media.go b/internal/db/bundb/media.go
index 65348733c..de980a16a 100644
--- a/internal/db/bundb/media.go
+++ b/internal/db/bundb/media.go
@@ -24,7 +24,6 @@ import (
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
- "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/paging"
@@ -57,15 +56,8 @@ func (m *mediaDB) GetAttachmentsByIDs(ctx context.Context, ids []string) ([]*gts
media, err := m.state.Caches.DB.Media.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.MediaAttachment, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached media attachments.
- media := make([]*gtsmodel.MediaAttachment, 0, count)
+ media := make([]*gtsmodel.MediaAttachment, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -129,30 +121,38 @@ func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAtt
}
func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
- // Load media into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- media, err := m.GetAttachmentByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
- return err
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.MediaAttachment
+ deleted.ID = id
- // On return, ensure that media with ID is invalidated.
- defer m.state.Caches.DB.Media.Invalidate("ID", id)
+ // Delete media attachment and update related models in new transaction.
+ err := m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- // Delete media attachment in new transaction.
- err = m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- if media.AccountID != "" {
+ // Initially, delete the media model,
+ // returning the required fields we need.
+ if _, err := tx.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?, ?, ?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("status_id"),
+ bun.Ident("avatar"),
+ bun.Ident("header"),
+ ).
+ Exec(ctx); err != nil {
+ return gtserror.Newf("error deleting media: %w", err)
+ }
+
+ // If media was attached to account,
+ // we need to remove link from account.
+ if deleted.AccountID != "" {
var account gtsmodel.Account
// Get related account model.
if _, err := tx.NewSelect().
Model(&account).
- Where("? = ?", bun.Ident("id"), media.AccountID).
+ Where("? = ?", bun.Ident("id"), deleted.AccountID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting account: %w", err)
}
@@ -160,11 +160,11 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
var set func(*bun.UpdateQuery) *bun.UpdateQuery
switch {
- case *media.Avatar && account.AvatarMediaAttachmentID == id:
+ case *deleted.Avatar && account.AvatarMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id"))
}
- case *media.Header && account.HeaderMediaAttachmentID == id:
+ case *deleted.Header && account.HeaderMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("header_media_attachment_id"))
}
@@ -183,13 +183,15 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
}
}
- if media.StatusID != "" {
+ // If media was attached to a status,
+ // we need to remove link from status.
+ if deleted.StatusID != "" {
var status gtsmodel.Status
// Get related status model.
if _, err := tx.NewSelect().
Model(&status).
- Where("? = ?", bun.Ident("id"), media.StatusID).
+ Where("? = ?", bun.Ident("id"), deleted.StatusID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting status: %w", err)
}
@@ -213,17 +215,14 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
}
}
- // Finally delete this media.
- if _, err := tx.NewDelete().
- Table("media_attachments").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx); err != nil {
- return gtserror.Newf("error deleting media: %w", err)
- }
-
return nil
})
+ // Invalidate cached media with ID, manually
+ // call invalidate hook in case not in cache.
+ m.state.Caches.DB.Media.Invalidate("ID", id)
+ m.state.Caches.OnInvalidateMedia(&deleted)
+
return err
}
diff --git a/internal/db/bundb/mention.go b/internal/db/bundb/mention.go
index e56300367..ba8c0ba11 100644
--- a/internal/db/bundb/mention.go
+++ b/internal/db/bundb/mention.go
@@ -69,15 +69,8 @@ func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.
mentions, err := m.state.Caches.DB.Mention.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Mention, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached mentions.
- mentions := make([]*gtsmodel.Mention, 0, count)
+ mentions := make([]*gtsmodel.Mention, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -166,24 +159,18 @@ func (m *mentionDB) PutMention(ctx context.Context, mention *gtsmodel.Mention) e
}
func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error {
- defer m.state.Caches.DB.Mention.Invalidate("ID", id)
-
- // Load mention into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := m.GetMention(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Delete mention with given ID,
+ // returning the deleted models.
+ if _, err := m.db.NewDelete().
+ Table("mentions").
+ Where("? = ?", bun.Ident("id"), id).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Finally delete mention from DB.
- _, err = m.db.NewDelete().
- Table("mentions").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ // Invalidate the cached mention with ID.
+ m.state.Caches.DB.Mention.Invalidate("ID", id)
+
+ return nil
}
diff --git a/internal/db/bundb/move.go b/internal/db/bundb/move.go
index cccef5872..23e5c6d27 100644
--- a/internal/db/bundb/move.go
+++ b/internal/db/bundb/move.go
@@ -234,13 +234,17 @@ func (m *moveDB) UpdateMove(ctx context.Context, move *gtsmodel.Move, columns ..
}
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
- defer m.state.Caches.DB.Move.Invalidate("ID", id)
-
- _, err := m.db.
- NewDelete().
+ // Delete move with given ID.
+ if _, err := m.db.NewDelete().
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
Where("? = ?", bun.Ident("move.id"), id).
- Exec(ctx)
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
+ return nil
+ }
- return err
+ // Invalidate the cached move model with ID.
+ m.state.Caches.DB.Move.Invalidate("ID", id)
+
+ return nil
}
diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go
index 9959b160e..770e84c5c 100644
--- a/internal/db/bundb/notification.go
+++ b/internal/db/bundb/notification.go
@@ -22,6 +22,7 @@ import (
"errors"
"slices"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -107,15 +108,8 @@ func (n *notificationDB) GetNotificationsByIDs(ctx context.Context, ids []string
notifs, err := n.state.Caches.DB.Notification.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Notification, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached notifications.
- notifs := make([]*gtsmodel.Notification, 0, count)
+ notifs := make([]*gtsmodel.Notification, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -299,7 +293,8 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
NewDelete().
Table("notifications").
Where("? = ?", bun.Ident("id"), id).
- Exec(ctx); err != nil {
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
@@ -310,7 +305,7 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" {
- return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set")
+ return gtserror.New("one of targetAccountID or originAccountID must be set")
}
q := n.db.
diff --git a/internal/db/bundb/poll.go b/internal/db/bundb/poll.go
index 5c1d9c6dd..f5c33ce9b 100644
--- a/internal/db/bundb/poll.go
+++ b/internal/db/bundb/poll.go
@@ -177,17 +177,36 @@ func (p *pollDB) UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...st
}
func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
- // Delete poll by ID from database.
- if _, err := p.db.NewDelete().
- Table("polls").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx); err != nil {
+ // Delete poll vote with ID, and its associated votes from the database.
+ if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+
+ // Delete poll from database.
+ if _, err := tx.NewDelete().
+ Table("polls").
+ Where("? = ?", bun.Ident("id"), id).
+ Exec(ctx); err != nil {
+ return err
+ }
+
+ // Delete the poll votes.
+ _, err := tx.NewDelete().
+ Table("poll_votes").
+ Where("? = ?", bun.Ident("poll_id"), id).
+ Exec(ctx)
+ return err
+ }); err != nil {
return err
}
- // Invalidate poll by ID from cache.
+ // Wrap provided ID in a poll
+ // model for calling cache hook.
+ var deleted gtsmodel.Poll
+ deleted.ID = id
+
+ // Invalidate cached poll with ID, manually
+ // call invalidate hook in case not cached.
p.state.Caches.DB.Poll.Invalidate("ID", id)
- p.state.Caches.DB.PollVoteIDs.Invalidate(id)
+ p.state.Caches.OnInvalidatePoll(&deleted)
return nil
}
@@ -274,15 +293,8 @@ func (p *pollDB) GetPollVotes(ctx context.Context, pollID string) ([]*gtsmodel.P
votes, err := p.state.Caches.DB.PollVote.LoadIDs("ID",
voteIDs,
func(uncached []string) ([]*gtsmodel.PollVote, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached votes.
- votes := make([]*gtsmodel.PollVote, 0, count)
+ votes := make([]*gtsmodel.PollVote, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -391,148 +403,44 @@ func (p *pollDB) PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
})
}
-func (p *pollDB) DeletePollVotes(ctx context.Context, pollID string) error {
- err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- // Delete all votes in poll.
- res, err := tx.NewDelete().
- Table("poll_votes").
- Where("? = ?", bun.Ident("poll_id"), pollID).
- Exec(ctx)
- if err != nil {
- // irrecoverable
- return err
- }
-
- ra, err := res.RowsAffected()
- if err != nil {
- // irrecoverable
- return err
- }
-
- if ra == 0 {
- // No poll votes deleted,
- // nothing to update.
- return nil
- }
-
- // Select current poll counts from DB,
- // taking minimal columns needed to
- // increment/decrement votes.
- var poll gtsmodel.Poll
- switch err := tx.NewSelect().
- Model(&poll).
- Column("options", "votes", "voters").
- Where("? = ?", bun.Ident("id"), pollID).
- Scan(ctx); {
-
- case err == nil:
- // no issue.
-
- case errors.Is(err, db.ErrNoEntries):
- // no votes found,
- // return here.
- return nil
-
- default:
- // irrecoverable.
- return err
- }
-
- // Zero all counts.
- poll.ResetVotes()
-
- // Finally, update the poll entry.
- _, err = tx.NewUpdate().
- Model(&poll).
- Column("votes", "voters").
- Where("? = ?", bun.Ident("id"), pollID).
- Exec(ctx)
- return err
- })
-
- if err != nil {
- return err
- }
-
- // Invalidate poll vote and poll entry from caches.
- p.state.Caches.DB.Poll.Invalidate("ID", pollID)
- p.state.Caches.DB.PollVote.Invalidate("PollID", pollID)
- p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
-
- return nil
-}
-
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
- err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
- // Slice should only ever be of length
- // 0 or 1; it's a slice of slices only
- // because we can't LIMIT deletes to 1.
- var choicesSlice [][]int
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.PollVote
+ deleted.AccountID = accountID
+ deleted.PollID = pollID
+
+ // Delete the poll vote with given poll and account IDs, and update vote counts.
+ if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete vote in poll by account,
- // returning the ID + choices of the vote.
- if err := tx.NewDelete().
- Table("poll_votes").
+ // returning deleted model info.
+ switch _, err := tx.NewDelete().
+ Model(&deleted).
Where("? = ?", bun.Ident("poll_id"), pollID).
Where("? = ?", bun.Ident("account_id"), accountID).
Returning("?", bun.Ident("choices")).
- Scan(ctx, &choicesSlice); err != nil {
- // irrecoverable.
- return err
- }
-
- if len(choicesSlice) != 1 {
- // No poll votes by this
- // acct on this poll.
- return nil
- }
-
- // Extract the *actual* choices.
- choices := choicesSlice[0]
-
- // Select current poll counts from DB,
- // taking minimal columns needed to
- // increment/decrement votes.
- var poll gtsmodel.Poll
- switch err := tx.NewSelect().
- Model(&poll).
- Column("options", "votes", "voters").
- Where("? = ?", bun.Ident("id"), pollID).
- Scan(ctx); {
+ Exec(ctx); {
case err == nil:
- // no issue.
-
+ // no issue
case errors.Is(err, db.ErrNoEntries):
- // no poll found,
- // return here.
return nil
-
default:
- // irrecoverable.
return err
}
- // Decrement votes for choices.
- poll.DecrementVotes(choices)
-
- // Finally, update the poll entry.
- _, err := tx.NewUpdate().
- Model(&poll).
- Column("votes", "voters").
- Where("? = ?", bun.Ident("id"), pollID).
- Exec(ctx)
+ // Update the votes for this deleted poll.
+ err := updatePollCounts(ctx, tx, &deleted)
return err
- })
-
- if err != nil {
+ }); err != nil {
return err
}
- // Invalidate poll vote and poll entry from caches.
- p.state.Caches.DB.Poll.Invalidate("ID", pollID)
+ // Invalidate the poll vote cache by given poll + account IDs, also
+ // manually call invalidation hook in case not actually stored in cache.
p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
- p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
+ p.state.Caches.OnInvalidatePollVote(&deleted)
return nil
}
@@ -562,6 +470,48 @@ func (p *pollDB) DeletePollVotesByAccountID(ctx context.Context, accountID strin
return nil
}
+// updatePollCounts updates the vote counts on a poll for the given deleted PollVote model.
+func updatePollCounts(ctx context.Context, tx bun.Tx, deleted *gtsmodel.PollVote) error {
+
+ // Select current poll counts from DB,
+ // taking minimal columns needed to
+ // increment/decrement votes.
+ var poll gtsmodel.Poll
+ switch err := tx.NewSelect().
+ Model(&poll).
+ Column("options", "votes", "voters").
+ Where("? = ?", bun.Ident("id"), deleted.PollID).
+ Scan(ctx); {
+
+ case err == nil:
+ // no issue.
+
+ case errors.Is(err, db.ErrNoEntries):
+ // no poll found,
+ // return here.
+ return nil
+
+ default:
+ // irrecoverable.
+ return err
+ }
+
+ // Decrement votes for these choices.
+ poll.DecrementVotes(deleted.Choices)
+
+ // Finally, update the poll entry.
+ if _, err := tx.NewUpdate().
+ Model(&poll).
+ Column("votes", "voters").
+ Where("? = ?", bun.Ident("id"), deleted.PollID).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
+ return err
+ }
+
+ return nil
+}
+
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
return db.NewSelect().
diff --git a/internal/db/bundb/poll_test.go b/internal/db/bundb/poll_test.go
index 6bdbdb983..8af9295d9 100644
--- a/internal/db/bundb/poll_test.go
+++ b/internal/db/bundb/poll_test.go
@@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
- "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
@@ -286,41 +285,6 @@ func (suite *PollTestSuite) TestDeletePoll() {
}
}
-func (suite *PollTestSuite) TestDeletePollVotes() {
- // Create a new context for this test.
- ctx, cncl := context.WithCancel(context.Background())
- defer cncl()
-
- for _, poll := range suite.testPolls {
- // Delete votes associated with poll from database.
- err := suite.db.DeletePollVotes(ctx, poll.ID)
- suite.NoError(err)
-
- // Fetch latest version of poll from database.
- poll, err = suite.db.GetPollByID(
- gtscontext.SetBarebones(ctx),
- poll.ID,
- )
- suite.NoError(err)
-
- // Check that poll counts are all zero.
- suite.Equal(*poll.Voters, 0)
- suite.Equal(make([]int, len(poll.Options)), poll.Votes)
- }
-}
-
-func (suite *PollTestSuite) TestDeletePollVotesNoPoll() {
- // Create a new context for this test.
- ctx, cncl := context.WithCancel(context.Background())
- defer cncl()
-
- // Try to delete votes of nonexistent poll.
- nonPollID := "01HF6V4XWTSZWJ80JNPPDTD4DB"
-
- err := suite.db.DeletePollVotes(ctx, nonPollID)
- suite.NoError(err)
-}
-
func (suite *PollTestSuite) TestDeletePollVotesBy() {
ctx, cncl := context.WithCancel(context.Background())
defer cncl()
diff --git a/internal/db/bundb/relationship_block.go b/internal/db/bundb/relationship_block.go
index 4093bad07..9738970e5 100644
--- a/internal/db/bundb/relationship_block.go
+++ b/internal/db/bundb/relationship_block.go
@@ -105,15 +105,8 @@ func (r *relationshipDB) GetBlocksByIDs(ctx context.Context, ids []string) ([]*g
blocks, err := r.state.Caches.DB.Block.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Block, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached blocks.
- blocks := make([]*gtsmodel.Block, 0, count)
+ blocks := make([]*gtsmodel.Block, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -222,94 +215,93 @@ func (r *relationshipDB) PutBlock(ctx context.Context, block *gtsmodel.Block) er
}
func (r *relationshipDB) DeleteBlockByID(ctx context.Context, id string) error {
- // Load block into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetBlockByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Block
+
+ // Delete block with given ID,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached block on return after delete.
- defer r.state.Caches.DB.Block.Invalidate("ID", id)
+ // Invalidate cached block with ID, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.Block.Invalidate("ID", id)
+ r.state.Caches.OnInvalidateBlock(&deleted)
- // Finally delete block from DB.
- _, err = r.db.NewDelete().
- Table("blocks").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteBlockByURI(ctx context.Context, uri string) error {
- // Load block into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetBlockByURI(gtscontext.SetBarebones(ctx), uri)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Block
+
+ // Delete block with given URI,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("uri"), uri).
+ Returning("?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached block on return after delete.
- defer r.state.Caches.DB.Block.Invalidate("URI", uri)
+ // Invalidate cached block with URI, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.Block.Invalidate("URI", uri)
+ r.state.Caches.OnInvalidateBlock(&deleted)
- // Finally delete block from DB.
- _, err = r.db.NewDelete().
- Table("blocks").
- Where("? = ?", bun.Ident("uri"), uri).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID string) error {
- var blockIDs []string
-
- // Get full list of IDs.
- if err := r.db.NewSelect().
- Column("id").
- Table("blocks").
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted []*gtsmodel.Block
+
+ // Delete all blocks either from
+ // account, or targeting account,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
- Scan(ctx, &blockIDs); err != nil {
+ Returning("?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- if len(blockIDs) == 0 {
- // Nothing
- // to delete.
- return nil
- }
-
- defer func() {
- // Invalidate all account's incoming / outoing blocks on return.
- r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
- r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
- }()
+ // Invalidate all account's incoming / outoing blocks.
+ r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
+ r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
- // Load all blocks into cache, this *really* isn't great
- // but it is the only way we can ensure we invalidate all
- // related caches correctly (e.g. visibility).
- _, err := r.GetAccountBlocks(ctx, accountID, nil)
- if err != nil && !errors.Is(err, db.ErrNoEntries) {
- return err
+ // In case not all blocks were in
+ // cache, manually call invalidate hooks.
+ for _, block := range deleted {
+ r.state.Caches.OnInvalidateBlock(block)
}
- // Finally delete all from DB.
- _, err = r.db.NewDelete().
- Table("blocks").
- Where("? IN (?)", bun.Ident("id"), bun.In(blockIDs)).
- Exec(ctx)
- return err
+ return nil
}
diff --git a/internal/db/bundb/relationship_follow.go b/internal/db/bundb/relationship_follow.go
index 413f3a2af..042d12f37 100644
--- a/internal/db/bundb/relationship_follow.go
+++ b/internal/db/bundb/relationship_follow.go
@@ -20,7 +20,6 @@ package bundb
import (
"context"
"errors"
- "fmt"
"slices"
"time"
@@ -82,15 +81,8 @@ func (r *relationshipDB) GetFollowsByIDs(ctx context.Context, ids []string) ([]*
follows, err := r.state.Caches.DB.Follow.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Follow, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached follows.
- follows := make([]*gtsmodel.Follow, 0, count)
+ follows := make([]*gtsmodel.Follow, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -252,139 +244,155 @@ func (r *relationshipDB) UpdateFollow(ctx context.Context, follow *gtsmodel.Foll
})
}
-func (r *relationshipDB) deleteFollow(ctx context.Context, id string) error {
- // Delete the follow itself using the given ID.
+func (r *relationshipDB) DeleteFollow(
+ ctx context.Context,
+ sourceAccountID string,
+ targetAccountID string,
+) error {
+
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Follow
+ deleted.AccountID = sourceAccountID
+ deleted.TargetAccountID = targetAccountID
+
+ // Delete follow from origin
+ // account, to targeting account,
+ // returning the deleted models.
if _, err := r.db.NewDelete().
- Table("follows").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx); err != nil {
+ Model(&deleted).
+ Where("? = ?", bun.Ident("account_id"), sourceAccountID).
+ Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
+ Returning("?", bun.Ident("id")).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Delete every list entry that used this followID.
- if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
- return fmt.Errorf("deleteFollow: error deleting list entries: %w", err)
+ // Invalidate cached follow with source / target account IDs,
+ // manually calling invalidate hook in case it isn't cached.
+ r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID",
+ sourceAccountID, targetAccountID)
+ r.state.Caches.OnInvalidateFollow(&deleted)
+
+ // Delete every list entry that was created targetting this follow ID.
+ if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
+ return gtserror.Newf("error deleting list entries: %w", err)
}
return nil
}
-func (r *relationshipDB) DeleteFollow(ctx context.Context, sourceAccountID string, targetAccountID string) error {
- // Load follow into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- follow, err := r.GetFollow(
- gtscontext.SetBarebones(ctx),
- sourceAccountID,
- targetAccountID,
- )
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // Already gone.
- return nil
- }
+func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Follow
+ deleted.ID = id
+
+ // Delete follow with given ID,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached follow on return after delete.
- defer r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
-
- // Finally delete follow from DB.
- return r.deleteFollow(ctx, follow.ID)
-}
+ // Invalidate cached follow with ID, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.Follow.Invalidate("ID", id)
+ r.state.Caches.OnInvalidateFollow(&deleted)
-func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {
- // Load follow into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- follow, err := r.GetFollowByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // Already gone.
- return nil
- }
- return err
+ // Delete every list entry that was created targetting this follow ID.
+ if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, id); err != nil {
+ return gtserror.Newf("error deleting list entries: %w", err)
}
- // Drop this now-cached follow on return after delete.
- defer r.state.Caches.DB.Follow.Invalidate("ID", id)
-
- // Finally delete follow from DB.
- return r.deleteFollow(ctx, follow.ID)
+ return nil
}
func (r *relationshipDB) DeleteFollowByURI(ctx context.Context, uri string) error {
- // Load follow into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- follow, err := r.GetFollowByURI(gtscontext.SetBarebones(ctx), uri)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // Already gone.
- return nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Follow
+
+ // Delete follow with given URI,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("uri"), uri).
+ Returning("?, ?, ?",
+ bun.Ident("id"),
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached follow on return after delete.
- defer r.state.Caches.DB.Follow.Invalidate("URI", uri)
+ // Invalidate cached follow with URI, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.Follow.Invalidate("URI", uri)
+ r.state.Caches.OnInvalidateFollow(&deleted)
- // Finally delete follow from DB.
- return r.deleteFollow(ctx, follow.ID)
+ // Delete every list entry that was created targetting this follow ID.
+ if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
+ return gtserror.Newf("error deleting list entries: %w", err)
+ }
+
+ return nil
}
func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID string) error {
- var followIDs []string
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted []*gtsmodel.Follow
- // Get full list of IDs.
- if _, err := r.db.
- NewSelect().
- Column("id").
- Table("follows").
+ // Delete all follows either from
+ // account, or targeting account,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
- Exec(ctx, &followIDs); err != nil {
+ Returning("?, ?, ?",
+ bun.Ident("id"),
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- if len(followIDs) == 0 {
- // Nothing
- // to delete.
- return nil
- }
-
- defer func() {
- // Invalidate all account's incoming / outoing follows on return.
- r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
- r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
- }()
+ // Gather the follow IDs that were deleted for removing related list entries.
+ followIDs := util.Gather(nil, deleted, func(follow *gtsmodel.Follow) string {
+ return follow.ID
+ })
- // Load all follows into cache, this *really* isn't great
- // but it is the only way we can ensure we invalidate all
- // related caches correctly (e.g. visibility).
- _, err := r.GetAccountFollows(ctx, accountID, nil)
- if err != nil && !errors.Is(err, db.ErrNoEntries) {
- return err
+ // Delete every list entry that was created targetting any of these follow IDs.
+ if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, followIDs...); err != nil {
+ return gtserror.Newf("error deleting list entries: %w", err)
}
- // Delete all follows from DB.
- _, err = r.db.NewDelete().
- Table("follows").
- Where("? IN (?)", bun.Ident("id"), bun.In(followIDs)).
- Exec(ctx)
- if err != nil {
- return err
- }
+ // Invalidate all account's incoming / outoing follows.
+ r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
+ r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
- for _, id := range followIDs {
- // Finally, delete all list entries associated with each follow ID.
- if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
- return err
- }
+ // In case not all follow were in
+ // cache, manually call invalidate hooks.
+ for _, follow := range deleted {
+ r.state.Caches.OnInvalidateFollow(follow)
}
return nil
diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go
index 2e058fbbb..fc0ca5c0a 100644
--- a/internal/db/bundb/relationship_follow_req.go
+++ b/internal/db/bundb/relationship_follow_req.go
@@ -81,15 +81,8 @@ func (r *relationshipDB) GetFollowRequestsByIDs(ctx context.Context, ids []strin
follows, err := r.state.Caches.DB.FollowRequest.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.FollowRequest, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached followReqs.
- follows := make([]*gtsmodel.FollowRequest, 0, count)
+ follows := make([]*gtsmodel.FollowRequest, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -293,124 +286,131 @@ func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountI
}, targetAccountID, sourceAccountID)
}
-func (r *relationshipDB) DeleteFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) error {
- // Load followreq into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- follow, err := r.GetFollowRequest(
- gtscontext.SetBarebones(ctx),
- sourceAccountID,
- targetAccountID,
- )
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // Already gone.
- return nil
- }
+func (r *relationshipDB) DeleteFollowRequest(
+ ctx context.Context,
+ sourceAccountID string,
+ targetAccountID string,
+) error {
+
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.FollowRequest
+ deleted.AccountID = sourceAccountID
+ deleted.TargetAccountID = targetAccountID
+
+ // Delete all follow reqs either
+ // from account, or targeting account,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("account_id"), sourceAccountID).
+ Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
+ Returning("?", bun.Ident("id")).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached follow request on return after delete.
- defer r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
+ // Invalidate cached follow with source / target account IDs,
+ // manually calling invalidate hook in case it isn't cached.
+ r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID",
+ sourceAccountID, targetAccountID)
+ r.state.Caches.OnInvalidateFollowRequest(&deleted)
- // Finally delete followreq from DB.
- _, err = r.db.NewDelete().
- Table("follow_requests").
- Where("? = ?", bun.Ident("id"), follow.ID).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error {
- // Load followreq into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetFollowRequestByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.FollowRequest
+ deleted.ID = id
+
+ // Delete follow with given URI,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached follow request on return after delete.
- defer r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
+ // Invalidate cached follow with URI, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
+ r.state.Caches.OnInvalidateFollowRequest(&deleted)
- // Finally delete followreq from DB.
- _, err = r.db.NewDelete().
- Table("follow_requests").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteFollowRequestByURI(ctx context.Context, uri string) error {
- // Load followreq into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetFollowRequestByURI(gtscontext.SetBarebones(ctx), uri)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.FollowRequest
+
+ // Delete follow with given URI,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("uri"), uri).
+ Returning("?, ?, ?",
+ bun.Ident("id"),
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached follow request on return after delete.
- defer r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
+ // Invalidate cached follow with URI, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
+ r.state.Caches.OnInvalidateFollowRequest(&deleted)
- // Finally delete followreq from DB.
- _, err = r.db.NewDelete().
- Table("follow_requests").
- Where("? = ?", bun.Ident("uri"), uri).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accountID string) error {
- var followReqIDs []string
-
- // Get full list of IDs.
- if _, err := r.db.
- NewSelect().
- Column("id").
- Table("follow_requests").
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted []*gtsmodel.FollowRequest
+
+ // Delete all follows either from
+ // account, or targeting account,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
- Exec(ctx, &followReqIDs); err != nil {
+ Returning("?, ?, ?",
+ bun.Ident("id"),
+ bun.Ident("account_id"),
+ bun.Ident("target_account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- if len(followReqIDs) == 0 {
- // Nothing
- // to delete.
- return nil
- }
+ // Invalidate all account's incoming / outoing follows requests.
+ r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
+ r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
- defer func() {
- // Invalidate all account's incoming / outoing follow requests on return.
- r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
- r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
- }()
-
- // Load all followreqs into cache, this *really* isn't
- // great but it is the only way we can ensure we invalidate
- // all related caches correctly (e.g. visibility).
- _, err := r.GetAccountFollowRequests(ctx, accountID, nil)
- if err != nil && !errors.Is(err, db.ErrNoEntries) {
- return err
+ // In case not all follow were in
+ // cache, manually call invalidate hooks.
+ for _, followReq := range deleted {
+ r.state.Caches.OnInvalidateFollowRequest(followReq)
}
- // Finally delete all from DB.
- _, err = r.db.NewDelete().
- Table("follow_requests").
- Where("? IN (?)", bun.Ident("id"), bun.In(followReqIDs)).
- Exec(ctx)
- return err
+ return nil
}
diff --git a/internal/db/bundb/relationship_mute.go b/internal/db/bundb/relationship_mute.go
index 61b89d323..37d97a64f 100644
--- a/internal/db/bundb/relationship_mute.go
+++ b/internal/db/bundb/relationship_mute.go
@@ -87,15 +87,8 @@ func (r *relationshipDB) getMutesByIDs(ctx context.Context, ids []string) ([]*gt
mutes, err := r.state.Caches.DB.UserMute.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.UserMute, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached mutes.
- mutes := make([]*gtsmodel.UserMute, 0, count)
+ mutes := make([]*gtsmodel.UserMute, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -209,72 +202,64 @@ func (r *relationshipDB) PutMute(ctx context.Context, mute *gtsmodel.UserMute) e
}
func (r *relationshipDB) DeleteMuteByID(ctx context.Context, id string) error {
- // Load mute into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetMuteByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.UserMute
+
+ // Delete mute with given ID,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?", bun.Ident("account_id")).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Drop this now-cached mute on return after delete.
- defer r.state.Caches.DB.UserMute.Invalidate("ID", id)
+ // Invalidate cached mute with ID, manually
+ // call invalidate hook in case not cached.
+ r.state.Caches.DB.UserMute.Invalidate("ID", id)
+ r.state.Caches.OnInvalidateUserMute(&deleted)
- // Finally delete mute from DB.
- _, err = r.db.NewDelete().
- Table("user_mutes").
- Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID string) error {
- var muteIDs []string
-
- // Get full list of IDs.
- if err := r.db.NewSelect().
- Column("id").
- Table("user_mutes").
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted []*gtsmodel.UserMute
+
+ // Delete all mutes either from
+ // account, or targeting account,
+ // returning the deleted models.
+ if _, err := r.db.NewDelete().
+ Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
- Scan(ctx, &muteIDs); err != nil {
+ Returning("?",
+ bun.Ident("account_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- if len(muteIDs) == 0 {
- // Nothing
- // to delete.
- return nil
- }
-
- defer func() {
- // Invalidate all account's incoming / outoing mutes on return.
- r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
- r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
- }()
+ // Invalidate all account's incoming / outoing user mutes.
+ r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
+ r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
- // Load all mutes into cache, this *really* isn't great
- // but it is the only way we can ensure we invalidate all
- // related caches correctly (e.g. visibility).
- _, err := r.GetAccountMutes(ctx, accountID, nil)
- if err != nil && !errors.Is(err, db.ErrNoEntries) {
- return err
+ // In case not all user mutes were in
+ // cache, manually call invalidate hooks.
+ for _, block := range deleted {
+ r.state.Caches.OnInvalidateUserMute(block)
}
- // Finally delete all from DB.
- _, err = r.db.NewDelete().
- Table("user_mutes").
- Where("? IN (?)", bun.Ident("id"), bun.In(muteIDs)).
- Exec(ctx)
- return err
+ return nil
}
func (r *relationshipDB) GetAccountMutes(
diff --git a/internal/db/bundb/relationship_test.go b/internal/db/bundb/relationship_test.go
index 46a4f1f25..7aa749c90 100644
--- a/internal/db/bundb/relationship_test.go
+++ b/internal/db/bundb/relationship_test.go
@@ -826,10 +826,10 @@ func (suite *RelationshipTestSuite) TestUnfollowExisting() {
suite.NotNil(follow)
followID := follow.ID
- // We should have list entries for this follow.
- listEntries, err := suite.db.GetListEntriesForFollowID(context.Background(), followID)
+ // We should have lists that this follow is a part of.
+ lists, err := suite.db.GetListsContainingFollowID(context.Background(), followID)
suite.NoError(err)
- suite.NotEmpty(listEntries)
+ suite.NotEmpty(lists)
err = suite.db.DeleteFollowByID(context.Background(), followID)
suite.NoError(err)
@@ -838,10 +838,10 @@ func (suite *RelationshipTestSuite) TestUnfollowExisting() {
suite.EqualError(err, db.ErrNoEntries.Error())
suite.Nil(follow)
- // ListEntries pertaining to this follow should be deleted too.
- listEntries, err = suite.db.GetListEntriesForFollowID(context.Background(), followID)
+ // Lists containing this follow should return empty too.
+ lists, err = suite.db.GetListsContainingFollowID(context.Background(), followID)
suite.NoError(err)
- suite.Empty(listEntries)
+ suite.Empty(lists)
}
func (suite *RelationshipTestSuite) TestGetFollowNotExisting() {
diff --git a/internal/db/bundb/report.go b/internal/db/bundb/report.go
index d2096a78a..582584988 100644
--- a/internal/db/bundb/report.go
+++ b/internal/db/bundb/report.go
@@ -248,45 +248,36 @@ func (r *reportDB) PutReport(ctx context.Context, report *gtsmodel.Report) error
})
}
-func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) {
+func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error {
// Update the report's last-updated
report.UpdatedAt = time.Now()
if len(columns) != 0 {
columns = append(columns, "updated_at")
}
- if _, err := r.db.
- NewUpdate().
- Model(report).
- Where("? = ?", bun.Ident("report.id"), report.ID).
- Column(columns...).
- Exec(ctx); err != nil {
- return nil, err
- }
-
- r.state.Caches.DB.Report.Invalidate("ID", report.ID)
- return report, nil
+ return r.state.Caches.DB.Report.Store(report, func() error {
+ _, err := r.db.
+ NewUpdate().
+ Model(report).
+ Where("? = ?", bun.Ident("report.id"), report.ID).
+ Column(columns...).
+ Exec(ctx)
+ return err
+ })
}
func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error {
- defer r.state.Caches.DB.Report.Invalidate("ID", id)
-
- // Load status into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := r.GetReportByID(gtscontext.SetBarebones(ctx), id)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Delete the report from DB.
+ if _, err := r.db.NewDelete().
+ TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
+ Where("? = ?", bun.Ident("report.id"), id).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- // Finally delete report from DB.
- _, err = r.db.NewDelete().
- TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
- Where("? = ?", bun.Ident("report.id"), id).
- Exec(ctx)
- return err
+ // Invalidate any cached report model by ID.
+ r.state.Caches.DB.Report.Invalidate("ID", id)
+
+ return nil
}
diff --git a/internal/db/bundb/report_test.go b/internal/db/bundb/report_test.go
index 1a488c729..57828890d 100644
--- a/internal/db/bundb/report_test.go
+++ b/internal/db/bundb/report_test.go
@@ -202,7 +202,7 @@ func (suite *ReportTestSuite) TestUpdateReport() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
- if _, err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
+ if err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
suite.FailNow(err.Error())
}
@@ -228,7 +228,7 @@ func (suite *ReportTestSuite) TestUpdateReportAllColumns() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
- if _, err := suite.db.UpdateReport(ctx, report); err != nil {
+ if err := suite.db.UpdateReport(ctx, report); err != nil {
suite.FailNow(err.Error())
}
diff --git a/internal/db/bundb/sinbinstatus.go b/internal/db/bundb/sinbinstatus.go
index 5fc368022..dd2c17f67 100644
--- a/internal/db/bundb/sinbinstatus.go
+++ b/internal/db/bundb/sinbinstatus.go
@@ -19,8 +19,10 @@ package bundb
import (
"context"
+ "errors"
"time"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/uptrace/bun"
@@ -110,13 +112,18 @@ func (s *sinBinStatusDB) UpdateSinBinStatus(
}
func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error {
- // On return ensure status invalidated from cache.
- defer s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
-
- _, err := s.db.
+ // Delete the status from DB.
+ if _, err := s.db.
NewDelete().
TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")).
Where("? = ?", bun.Ident("sin_bin_status.id"), id).
- Exec(ctx)
- return err
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
+ return err
+ }
+
+ // Invalidate any cached sinbin status model by ID.
+ s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
+
+ return nil
}
diff --git a/internal/db/bundb/status.go b/internal/db/bundb/status.go
index d0befd52f..5340b63cd 100644
--- a/internal/db/bundb/status.go
+++ b/internal/db/bundb/status.go
@@ -54,15 +54,8 @@ func (s *statusDB) GetStatusesByIDs(ctx context.Context, ids []string) ([]*gtsmo
statuses, err := s.state.Caches.DB.Status.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Status, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached statuses.
- statuses := make([]*gtsmodel.Status, 0, count)
+ statuses := make([]*gtsmodel.Status, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) status IDs.
@@ -486,24 +479,13 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
}
func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
- // Load status into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := s.GetStatusByID(
- 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
- }
-
- // On return ensure status invalidated from cache.
- defer s.state.Caches.DB.Status.Invalidate("ID", id)
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.Status
+ deleted.ID = id
- return s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ // Delete status from database and any related links in a transaction.
+ if err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// delete links between this status and any emojis it uses
if _, err := tx.
NewDelete().
@@ -524,26 +506,42 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
// Delete links between this status
// and any threads it was a part of.
- _, err = tx.
+ if _, err := tx.
NewDelete().
TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")).
Where("? = ?", bun.Ident("thread_to_status.status_id"), id).
- Exec(ctx)
- if err != nil {
+ Exec(ctx); err != nil {
return err
}
// delete the status itself
if _, err := tx.
NewDelete().
- TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
- Where("? = ?", bun.Ident("status.id"), id).
- Exec(ctx); err != nil {
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), id).
+ Returning("?, ?, ?, ?, ?",
+ bun.Ident("account_id"),
+ bun.Ident("boost_of_id"),
+ bun.Ident("in_reply_to_id"),
+ bun.Ident("attachments"),
+ bun.Ident("poll_id"),
+ ).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
return nil
- })
+ }); err != nil {
+ return err
+ }
+
+ // Invalidate cached status by its ID, manually
+ // call the invalidate hook in case not cached.
+ s.state.Caches.DB.Status.Invalidate("ID", id)
+ s.state.Caches.OnInvalidateStatus(&deleted)
+
+ return nil
}
func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) {
diff --git a/internal/db/bundb/statusbookmark.go b/internal/db/bundb/statusbookmark.go
index 87fb17351..1534050da 100644
--- a/internal/db/bundb/statusbookmark.go
+++ b/internal/db/bundb/statusbookmark.go
@@ -73,15 +73,8 @@ func (s *statusBookmarkDB) GetStatusBookmarksByIDs(ctx context.Context, ids []st
bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.StatusBookmark, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached bookmarks.
- bookmarks := make([]*gtsmodel.StatusBookmark, 0, count)
+ bookmarks := make([]*gtsmodel.StatusBookmark, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) bookmarks.
@@ -264,60 +257,86 @@ func (s *statusBookmarkDB) PutStatusBookmark(ctx context.Context, bookmark *gtsm
}
func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error {
- _, err := s.db.
- NewDelete().
- Table("status_bookmarks").
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.StatusBookmark
+ deleted.ID = id
+
+ // Delete block with given URI,
+ // returning the deleted models.
+ if _, err := s.db.NewDelete().
+ Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
- Exec(ctx)
- if err != nil {
+ Returning("?", bun.Ident("status_id")).
+ Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
+
+ // Invalidate cached status bookmark by its ID,
+ // manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("ID", id)
+ s.state.Caches.OnInvalidateStatusBookmark(&deleted)
+
return nil
}
func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" {
- return errors.New("DeleteBookmarks: one of targetAccountID or originAccountID must be set")
+ return gtserror.New("one of targetAccountID or originAccountID must be set")
}
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted []*gtsmodel.StatusBookmark
+
q := s.db.
NewDelete().
- TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark"))
+ Model(&deleted).
+ Returning("?", bun.Ident("status_id"))
if targetAccountID != "" {
- q = q.Where("? = ?", bun.Ident("status_bookmark.target_account_id"), targetAccountID)
- defer s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
+ q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID)
}
if originAccountID != "" {
- q = q.Where("? = ?", bun.Ident("status_bookmark.account_id"), originAccountID)
- defer s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
+ q = q.Where("? = ?", bun.Ident("account_id"), originAccountID)
}
- if _, err := q.Exec(ctx); err != nil {
+ if _, err := q.Exec(ctx); err != nil &&
+ !errors.Is(err, db.ErrNoEntries) {
return err
}
- if targetAccountID != "" {
- s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
- }
-
- if originAccountID != "" {
- s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
+ for _, deleted := range deleted {
+ // Invalidate cached status bookmark by status ID,
+ // manually call invalidate hook in case not cached.
+ s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", deleted.StatusID)
+ s.state.Caches.OnInvalidateStatusBookmark(deleted)
}
return nil
}
func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error {
- q := s.db.
- NewDelete().
+ // Delete status bookmarks
+ // from database by status ID.
+ q := s.db.NewDelete().
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")).
Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID)
if _, err := q.Exec(ctx); err != nil {
return err
}
+
+ // Wrap provided ID in a bookmark
+ // model for calling cache hook.
+ var deleted gtsmodel.StatusBookmark
+ deleted.StatusID = statusID
+
+ // Invalidate cached status bookmark by status ID,
+ // manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID)
+ s.state.Caches.OnInvalidateStatusBookmark(&deleted)
+
return nil
}
diff --git a/internal/db/bundb/statusfave.go b/internal/db/bundb/statusfave.go
index eb372c24b..cf20fbba3 100644
--- a/internal/db/bundb/statusfave.go
+++ b/internal/db/bundb/statusfave.go
@@ -133,15 +133,8 @@ func (s *statusFaveDB) GetStatusFaves(ctx context.Context, statusID string) ([]*
faves, err := s.state.Caches.DB.StatusFave.LoadIDs("ID",
faveIDs,
func(uncached []string) ([]*gtsmodel.StatusFave, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached faves.
- faves := make([]*gtsmodel.StatusFave, 0, count)
+ faves := make([]*gtsmodel.StatusFave, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) fave IDs.
diff --git a/internal/db/bundb/tag.go b/internal/db/bundb/tag.go
index e6a14c97e..6c3d870f6 100644
--- a/internal/db/bundb/tag.go
+++ b/internal/db/bundb/tag.go
@@ -20,6 +20,7 @@ package bundb
import (
"context"
"errors"
+ "slices"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -79,15 +80,8 @@ func (t *tagDB) GetTags(ctx context.Context, ids []string) ([]*gtsmodel.Tag, err
tags, err := t.state.Caches.DB.Tag.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Tag, error) {
- // Avoid querying
- // if none uncached.
- count := len(uncached)
- if count == 0 {
- return nil, nil
- }
-
// Preallocate expected length of uncached tags.
- tags := make([]*gtsmodel.Tag, 0, count)
+ tags := make([]*gtsmodel.Tag, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@@ -148,17 +142,11 @@ func (t *tagDB) GetFollowedTags(ctx context.Context, accountID string, page *pag
if err != nil {
return nil, err
}
-
- tags, err := t.GetTags(ctx, tagIDs)
- if err != nil {
- return nil, err
- }
-
- return tags, nil
+ return t.GetTags(ctx, tagIDs)
}
func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string, page *paging.Page) ([]string, error) {
- return loadPagedIDs(&t.state.Caches.DB.TagIDsFollowedByAccount, accountID, page, func() ([]string, error) {
+ return loadPagedIDs(&t.state.Caches.DB.FollowingTagIDs, ">"+accountID, page, func() ([]string, error) {
var tagIDs []string
// Tag IDs not in cache. Perform DB query.
@@ -178,7 +166,7 @@ func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string
}
func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]string, error) {
- return loadPagedIDs(&t.state.Caches.DB.AccountIDsFollowingTag, tagID, nil, func() ([]string, error) {
+ return loadPagedIDs(&t.state.Caches.DB.FollowingTagIDs, "<"+tagID, nil, func() ([]string, error) {
var accountIDs []string
// Account IDs not in cache. Perform DB query.
@@ -198,18 +186,11 @@ func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]
}
func (t *tagDB) IsAccountFollowingTag(ctx context.Context, accountID string, tagID string) (bool, error) {
- accountTagIDs, err := t.getTagIDsFollowedByAccount(ctx, accountID, nil)
+ followingTagIDs, err := t.getTagIDsFollowedByAccount(ctx, accountID, nil)
if err != nil {
return false, err
}
-
- for _, accountTagID := range accountTagIDs {
- if accountTagID == tagID {
- return true, nil
- }
- }
-
- return false, nil
+ return slices.Contains(followingTagIDs, tagID), nil
}
func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID string) error {
@@ -234,9 +215,15 @@ func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID stri
return nil
}
- // Otherwise, this is a new followed tag, so we invalidate caches related to it.
- t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID)
- t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
+ // We updated something, invalidate caches.
+ t.state.Caches.DB.FollowingTagIDs.Invalidate(
+
+ // tag IDs followed by account
+ ">"+accountID,
+
+ // account IDs following tag
+ "<"+tagID,
+ )
return nil
}
@@ -259,9 +246,15 @@ func (t *tagDB) DeleteFollowedTag(ctx context.Context, accountID string, tagID s
return nil
}
- // If we deleted anything, invalidate caches related to it.
- t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID)
- t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
+ // We deleted something, invalidate caches.
+ t.state.Caches.DB.FollowingTagIDs.Invalidate(
+
+ // tag IDs followed by account
+ ">"+accountID,
+
+ // account IDs following tag
+ "<"+tagID,
+ )
return err
}
@@ -278,16 +271,26 @@ func (t *tagDB) DeleteFollowedTagsByAccountID(ctx context.Context, accountID str
return gtserror.Newf("error deleting followed tags for account %s: %w", accountID, err)
}
- // Invalidate account ID caches for the account and those tags.
- t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
- t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagIDs...)
+ // Convert tag IDs to the keys
+ // we use for caching tag follow
+ // and following IDs.
+ keys := tagIDs
+ for i := range keys {
+ keys[i] = "<" + keys[i]
+ }
+ keys = append(keys, ">"+accountID)
+
+ // If we deleted anything, invalidate caches with keys.
+ t.state.Caches.DB.FollowingTagIDs.Invalidate(keys...)
return nil
}
func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []string) ([]string, error) {
- // Accounts might be following multiple tags in this list, but we only want to return each account once.
- accountIDs := []string{}
+ // Make conservative estimate for no. accounts.
+ accountIDs := make([]string, 0, len(tagIDs))
+
+ // Gather all accounts following tags.
for _, tagID := range tagIDs {
tagAccountIDs, err := t.getAccountIDsFollowingTag(ctx, tagID)
if err != nil {
@@ -295,5 +298,8 @@ func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []strin
}
accountIDs = append(accountIDs, tagAccountIDs...)
}
+
+ // Accounts might be following multiple tags in list,
+ // but we only want to return each account once.
return util.Deduplicate(accountIDs), nil
}
diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go
index b2af5583f..bcb7953d4 100644
--- a/internal/db/bundb/timeline.go
+++ b/internal/db/bundb/timeline.go
@@ -70,7 +70,7 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
// To take account of exclusive lists, get all of
// this account's lists, so we can filter out follows
// that are in contained in exclusive lists.
- lists, err := t.state.DB.GetListsForAccountID(ctx, accountID)
+ lists, err := t.state.DB.GetListsByAccountID(ctx, accountID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
}
@@ -84,9 +84,15 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
continue
}
+ // Fetch all follow IDs of the entries ccontained in this list.
+ listFollowIDs, err := t.state.DB.GetFollowIDsInList(ctx, list.ID, nil)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return nil, gtserror.Newf("db error getting list entry follow ids: %w", err)
+ }
+
// Exclusive list, index all its follow IDs.
- for _, listEntry := range list.ListEntries {
- ignoreFollowIDs[listEntry.FollowID] = struct{}{}
+ for _, followID := range listFollowIDs {
+ ignoreFollowIDs[followID] = struct{}{}
}
}
@@ -370,30 +376,20 @@ func (t *timelineDB) GetListTimeline(
frontToBack = true
)
- // Fetch all listEntries entries from the database.
- listEntries, err := t.state.DB.GetListEntries(
- // Don't need actual follows
- // for this, just the IDs.
- gtscontext.SetBarebones(ctx),
- listID,
- "", "", "", 0,
+ // Fetch all follow IDs contained in list from DB.
+ followIDs, err := t.state.DB.GetFollowIDsInList(
+ ctx, listID, nil,
)
if err != nil {
- return nil, fmt.Errorf("error getting entries for list %s: %w", listID, err)
+ return nil, fmt.Errorf("error getting follows in list: %w", err)
}
- // If there's no list entries we can't
+ // If there's no list follows we can't
// possibly return anything for this list.
- if len(listEntries) == 0 {
+ if len(followIDs) == 0 {
return make([]*gtsmodel.Status, 0), nil
}
- // Extract just the IDs of each follow.
- followIDs := make([]string, 0, len(listEntries))
- for _, listEntry := range listEntries {
- followIDs = append(followIDs, listEntry.FollowID)
- }
-
// Select target account IDs from follows.
subQ := t.db.
NewSelect().
diff --git a/internal/db/bundb/timeline_test.go b/internal/db/bundb/timeline_test.go
index 4874c2b35..50747b50d 100644
--- a/internal/db/bundb/timeline_test.go
+++ b/internal/db/bundb/timeline_test.go
@@ -184,8 +184,8 @@ func (suite *TimelineTestSuite) TestGetHomeTimelineIgnoreExclusive() {
suite.checkStatuses(s, id.Highest, id.Lowest, 8)
// Remove admin account from the exclusive list.
- listEntryID := suite.testListEntries["local_account_1_list_1_entry_2"].ID
- if err := suite.db.DeleteListEntry(ctx, listEntryID); err != nil {
+ listEntry := suite.testListEntries["local_account_1_list_1_entry_2"]
+ if err := suite.db.DeleteListEntry(ctx, listEntry.ListID, listEntry.FollowID); err != nil {
suite.FailNow(err.Error())
}
diff --git a/internal/db/bundb/tombstone.go b/internal/db/bundb/tombstone.go
index bff4ad839..773702323 100644
--- a/internal/db/bundb/tombstone.go
+++ b/internal/db/bundb/tombstone.go
@@ -67,12 +67,14 @@ func (t *tombstoneDB) PutTombstone(ctx context.Context, tombstone *gtsmodel.Tomb
}
func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error {
- defer t.state.Caches.DB.Tombstone.Invalidate("ID", id)
-
// Delete tombstone from DB.
_, err := t.db.NewDelete().
TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")).
Where("? = ?", bun.Ident("tombstone.id"), id).
Exec(ctx)
+
+ // Invalidate any cached tombstone by given ID.
+ t.state.Caches.DB.Tombstone.Invalidate("ID", id)
+
return err
}
diff --git a/internal/db/bundb/user.go b/internal/db/bundb/user.go
index 1ca65f016..fc8effa91 100644
--- a/internal/db/bundb/user.go
+++ b/internal/db/bundb/user.go
@@ -19,10 +19,8 @@ package bundb
import (
"context"
- "errors"
"time"
- "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -209,26 +207,26 @@ func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ..
}
func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error {
- defer u.state.Caches.DB.User.Invalidate("ID", userID)
-
- // Load user into cache before attempting a delete,
- // as we need it cached in order to trigger the invalidate
- // callback. This in turn invalidates others.
- _, err := u.GetUserByID(gtscontext.SetBarebones(ctx), userID)
- if err != nil {
- if errors.Is(err, db.ErrNoEntries) {
- // not an issue.
- err = nil
- }
+ // Gather necessary fields from
+ // deleted for cache invaliation.
+ var deleted gtsmodel.User
+ deleted.ID = userID
+
+ // Delete user from DB.
+ if _, err := u.db.NewDelete().
+ Model(&deleted).
+ Where("? = ?", bun.Ident("id"), userID).
+ Returning("?", bun.Ident("account_id")).
+ Exec(ctx); err != nil {
return err
}
- // Finally delete user from DB.
- _, err = u.db.NewDelete().
- TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
- Where("? = ?", bun.Ident("user.id"), userID).
- Exec(ctx)
- return err
+ // Invalidate cached user by ID, manually
+ // call invalidate hook in case not cached.
+ u.state.Caches.DB.User.Invalidate("ID", userID)
+ u.state.Caches.OnInvalidateUser(&deleted)
+
+ return nil
}
func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error {