diff options
Diffstat (limited to 'internal/db/bundb/statusfave.go')
-rw-r--r-- | internal/db/bundb/statusfave.go | 86 |
1 files changed, 64 insertions, 22 deletions
diff --git a/internal/db/bundb/statusfave.go b/internal/db/bundb/statusfave.go index 0f7e5df74..497262530 100644 --- a/internal/db/bundb/statusfave.go +++ b/internal/db/bundb/statusfave.go @@ -156,16 +156,26 @@ func (s *statusFaveDB) PutStatusFave(ctx context.Context, fave *gtsmodel.StatusF } func (s *statusFaveDB) DeleteStatusFaveByID(ctx context.Context, id string) db.Error { - if _, err := s.conn. - NewDelete(). - Table("status_faves"). - Where("? = ?", bun.Ident("id"), id). - Exec(ctx); err != nil { - return s.conn.ProcessError(err) + defer s.state.Caches.GTS.StatusFave().Invalidate("ID", id) + + // Load fave 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.GetStatusFaveByID(gtscontext.SetBarebones(ctx), id) + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + // not an issue. + err = nil + } + return err } - s.state.Caches.GTS.StatusFave().Invalidate("ID", id) - return nil + // Finally delete fave from DB. + _, err = s.conn.NewDelete(). + Table("status_faves"). + Where("? = ?", bun.Ident("id"), id). + Exec(ctx) + return s.conn.ProcessError(err) } func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID string, originAccountID string) db.Error { @@ -173,13 +183,12 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st return errors.New("DeleteStatusFaves: one of targetAccountID or originAccountID must be set") } - // Capture fave IDs in a RETURNING statement. var faveIDs []string q := s.conn. - NewDelete(). - Table("status_faves"). - Returning("?", bun.Ident("id")) + NewSelect(). + Column("id"). + Table("status_faves") if targetAccountID != "" { q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID) @@ -193,12 +202,29 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st return s.conn.ProcessError(err) } + defer func() { + // Invalidate all IDs on return. + for _, id := range faveIDs { + s.state.Caches.GTS.StatusFave().Invalidate("ID", id) + } + }() + + // Load all faves 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). for _, id := range faveIDs { - // Invalidate each of the returned status fave IDs. - s.state.Caches.GTS.StatusFave().Invalidate("ID", id) + _, err := s.GetStatusFaveByID(ctx, id) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return err + } } - return nil + // Finally delete all from DB. + _, err := s.conn.NewDelete(). + Table("status_faves"). + Where("? IN (?)", bun.Ident("id"), bun.In(faveIDs)). + Exec(ctx) + return s.conn.ProcessError(err) } func (s *statusFaveDB) DeleteStatusFavesForStatus(ctx context.Context, statusID string) db.Error { @@ -206,19 +232,35 @@ func (s *statusFaveDB) DeleteStatusFavesForStatus(ctx context.Context, statusID var faveIDs []string q := s.conn. - NewDelete(). + NewSelect(). + Column("id"). Table("status_faves"). - Where("? = ?", bun.Ident("status_id"), statusID). - Returning("?", bun.Ident("id")) - + Where("? = ?", bun.Ident("status_id"), statusID) if _, err := q.Exec(ctx, &faveIDs); err != nil { return s.conn.ProcessError(err) } + defer func() { + // Invalidate all IDs on return. + for _, id := range faveIDs { + s.state.Caches.GTS.StatusFave().Invalidate("ID", id) + } + }() + + // Load all faves 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). for _, id := range faveIDs { - // Invalidate each of the returned status fave IDs. - s.state.Caches.GTS.StatusFave().Invalidate("ID", id) + _, err := s.GetStatusFaveByID(ctx, id) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return err + } } - return nil + // Finally delete all from DB. + _, err := s.conn.NewDelete(). + Table("status_faves"). + Where("? IN (?)", bun.Ident("id"), bun.In(faveIDs)). + Exec(ctx) + return s.conn.ProcessError(err) } |