summaryrefslogtreecommitdiff
path: root/internal/db/bundb/media.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/bundb/media.go')
-rw-r--r--internal/db/bundb/media.go75
1 files changed, 37 insertions, 38 deletions
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
}