summaryrefslogtreecommitdiff
path: root/internal/processing
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-10-08 13:50:48 +0200
committerLibravatar GitHub <noreply@github.com>2022-10-08 13:50:48 +0200
commitaa07750bdb4dacdb1be39d765114915bba3fc29f (patch)
tree30e9e5052f607f8c8e4f7d518559df8706275e0f /internal/processing
parent[performance] cache domains after max retries in transport (#884) (diff)
downloadgotosocial-aa07750bdb4dacdb1be39d765114915bba3fc29f.tar.xz
[chore] Standardize database queries, use `bun.Ident()` properly (#886)
* use bun.Ident for user queries * use bun.Ident for account queries * use bun.Ident for media queries * add DeleteAccount func * remove CaseInsensitive in Where+use Ident ipv Safe * update admin db * update domain, use ident * update emoji, use ident * update instance queries, use bun.Ident * fix media * update mentions, use bun ident * update relationship + tests * use tableexpr * add test follows to bun db test suite * update notifications * updatebyprimarykey => updatebyid * fix session * prefer explicit ID to pk * fix little fucky wucky * remove workaround * use proper db func for attachment selection * update status db * add m2m entries in test rig * fix up timeline * go fmt * fix status put issue * update GetAccountStatuses
Diffstat (limited to 'internal/processing')
-rw-r--r--internal/processing/admin/createdomainblock.go8
-rw-r--r--internal/processing/admin/deletedomainblock.go4
-rw-r--r--internal/processing/instance.go2
-rw-r--r--internal/processing/media/getfile_test.go6
-rw-r--r--internal/processing/media/unattach.go2
-rw-r--r--internal/processing/media/update.go2
-rw-r--r--internal/processing/status/util.go33
-rw-r--r--internal/processing/user/changepassword.go2
-rw-r--r--internal/processing/user/emailconfirm.go4
-rw-r--r--internal/processing/user/emailconfirm_test.go4
10 files changed, 35 insertions, 32 deletions
diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/createdomainblock.go
index c39a91989..0dd60ea16 100644
--- a/internal/processing/admin/createdomainblock.go
+++ b/internal/processing/admin/createdomainblock.go
@@ -128,15 +128,17 @@ func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account
instance.ContactAccountUsername = ""
instance.ContactAccountID = ""
instance.Version = ""
- if err := p.db.UpdateByPrimaryKey(ctx, instance, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, instance, instance.ID, updatingColumns...); err != nil {
l.Errorf("domainBlockProcessSideEffects: db error updating instance: %s", err)
}
l.Debug("domainBlockProcessSideEffects: instance entry updated")
}
// if we have an instance account for this instance, delete it
- if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "username", Value: block.Domain, CaseInsensitive: true}}, &gtsmodel.Account{}); err != nil {
- l.Errorf("domainBlockProcessSideEffects: db error removing instance account: %s", err)
+ if instanceAccount, err := p.db.GetAccountByUsernameDomain(ctx, block.Domain, block.Domain); err == nil {
+ if err := p.db.DeleteAccount(ctx, instanceAccount.ID); err != nil {
+ l.Errorf("domainBlockProcessSideEffects: db error deleting instance account: %s", err)
+ }
}
// delete accounts through the normal account deletion system (which should also delete media + posts + remove posts from timelines)
diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go
index b65954fe5..8637c173e 100644
--- a/internal/processing/admin/deletedomainblock.go
+++ b/internal/processing/admin/deletedomainblock.go
@@ -55,14 +55,14 @@ func (p *processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Acc
// remove the domain block reference from the instance, if we have an entry for it
i := &gtsmodel.Instance{}
if err := p.db.GetWhere(ctx, []db.Where{
- {Key: "domain", Value: domainBlock.Domain, CaseInsensitive: true},
+ {Key: "domain", Value: domainBlock.Domain},
{Key: "domain_block_id", Value: id},
}, i); err == nil {
updatingColumns := []string{"suspended_at", "domain_block_id", "updated_at"}
i.SuspendedAt = time.Time{}
i.DomainBlockID = ""
i.UpdatedAt = time.Now()
- if err := p.db.UpdateByPrimaryKey(ctx, i, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, i, i.ID, updatingColumns...); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("couldn't update database entry for instance %s: %s", domainBlock.Domain, err))
}
}
diff --git a/internal/processing/instance.go b/internal/processing/instance.go
index 32a4de6f0..2d74fe181 100644
--- a/internal/processing/instance.go
+++ b/internal/processing/instance.go
@@ -224,7 +224,7 @@ func (p *processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSe
}
}
- if err := p.db.UpdateByPrimaryKey(ctx, i, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, i, i.ID, updatingColumns...); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", host, err))
}
diff --git a/internal/processing/media/getfile_test.go b/internal/processing/media/getfile_test.go
index a8a3568e7..6e5271607 100644
--- a/internal/processing/media/getfile_test.go
+++ b/internal/processing/media/getfile_test.go
@@ -69,7 +69,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncached() {
// uncache the file from local
testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
testAttachment.Cached = testrig.FalseBool()
- err := suite.db.UpdateByPrimaryKey(ctx, testAttachment, "cached")
+ err := suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached")
suite.NoError(err)
err = suite.storage.Delete(ctx, testAttachment.File.Path)
suite.NoError(err)
@@ -124,7 +124,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncachedInterrupted() {
// uncache the file from local
testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
testAttachment.Cached = testrig.FalseBool()
- err := suite.db.UpdateByPrimaryKey(ctx, testAttachment, "cached")
+ err := suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached")
suite.NoError(err)
err = suite.storage.Delete(ctx, testAttachment.File.Path)
suite.NoError(err)
@@ -179,7 +179,7 @@ func (suite *GetFileTestSuite) TestGetRemoteFileThumbnailUncached() {
// uncache the file from local
testAttachment.Cached = testrig.FalseBool()
- err = suite.db.UpdateByPrimaryKey(ctx, testAttachment, "cached")
+ err = suite.db.UpdateByID(ctx, testAttachment, testAttachment.ID, "cached")
suite.NoError(err)
err = suite.storage.Delete(ctx, testAttachment.File.Path)
suite.NoError(err)
diff --git a/internal/processing/media/unattach.go b/internal/processing/media/unattach.go
index 5ef8f81f4..d0f34eba1 100644
--- a/internal/processing/media/unattach.go
+++ b/internal/processing/media/unattach.go
@@ -47,7 +47,7 @@ func (p *processor) Unattach(ctx context.Context, account *gtsmodel.Account, med
attachment.UpdatedAt = time.Now()
attachment.StatusID = ""
- if err := p.db.UpdateByPrimaryKey(ctx, attachment, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, attachment, attachment.ID, updatingColumns...); err != nil {
return nil, gtserror.NewErrorNotFound(fmt.Errorf("db error updating attachment: %s", err))
}
diff --git a/internal/processing/media/update.go b/internal/processing/media/update.go
index b8177eeb4..e0833a511 100644
--- a/internal/processing/media/update.go
+++ b/internal/processing/media/update.go
@@ -61,7 +61,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, media
updatingColumns = append(updatingColumns, "focus_x", "focus_y")
}
- if err := p.db.UpdateByPrimaryKey(ctx, attachment, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, attachment, attachment.ID, updatingColumns...); err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error updating media: %s", err))
}
diff --git a/internal/processing/status/util.go b/internal/processing/status/util.go
index 7617894cc..c8b30e2ca 100644
--- a/internal/processing/status/util.go
+++ b/internal/processing/status/util.go
@@ -162,27 +162,28 @@ func (p *processor) ProcessMediaIDs(ctx context.Context, form *apimodel.Advanced
return nil
}
- gtsMediaAttachments := []*gtsmodel.MediaAttachment{}
- attachments := []string{}
+ attachments := []*gtsmodel.MediaAttachment{}
+ attachmentIDs := []string{}
for _, mediaID := range form.MediaIDs {
- // check these attachments exist
- a := &gtsmodel.MediaAttachment{}
- if err := p.db.GetByID(ctx, mediaID, a); err != nil {
- return fmt.Errorf("invalid media type or media not found for media id %s", mediaID)
+ attachment, err := p.db.GetAttachmentByID(ctx, mediaID)
+ if err != nil {
+ return fmt.Errorf("ProcessMediaIDs: invalid media type or media not found for media id %s", mediaID)
}
- // check they belong to the requesting account id
- if a.AccountID != thisAccountID {
- return fmt.Errorf("media with id %s does not belong to account %s", mediaID, thisAccountID)
+
+ if attachment.AccountID != thisAccountID {
+ return fmt.Errorf("ProcessMediaIDs: media with id %s does not belong to account %s", mediaID, thisAccountID)
}
- // check they're not already used in a status
- if a.StatusID != "" || a.ScheduledStatusID != "" {
- return fmt.Errorf("media with id %s is already attached to a status", mediaID)
+
+ if attachment.StatusID != "" || attachment.ScheduledStatusID != "" {
+ return fmt.Errorf("ProcessMediaIDs: media with id %s is already attached to a status", mediaID)
}
- gtsMediaAttachments = append(gtsMediaAttachments, a)
- attachments = append(attachments, a.ID)
+
+ attachments = append(attachments, attachment)
+ attachmentIDs = append(attachmentIDs, attachment.ID)
}
- status.Attachments = gtsMediaAttachments
- status.AttachmentIDs = attachments
+
+ status.Attachments = attachments
+ status.AttachmentIDs = attachmentIDs
return nil
}
diff --git a/internal/processing/user/changepassword.go b/internal/processing/user/changepassword.go
index ddfec6898..856e92bdc 100644
--- a/internal/processing/user/changepassword.go
+++ b/internal/processing/user/changepassword.go
@@ -45,7 +45,7 @@ func (p *processor) ChangePassword(ctx context.Context, user *gtsmodel.User, old
user.EncryptedPassword = string(newPasswordHash)
user.UpdatedAt = time.Now()
- if err := p.db.UpdateByPrimaryKey(ctx, user, "encrypted_password", "updated_at"); err != nil {
+ if err := p.db.UpdateByID(ctx, user, user.ID, "encrypted_password", "updated_at"); err != nil {
return gtserror.NewErrorInternalError(err, "database error")
}
diff --git a/internal/processing/user/emailconfirm.go b/internal/processing/user/emailconfirm.go
index 5a68383b8..82124007c 100644
--- a/internal/processing/user/emailconfirm.go
+++ b/internal/processing/user/emailconfirm.go
@@ -77,7 +77,7 @@ func (p *processor) SendConfirmEmail(ctx context.Context, user *gtsmodel.User, u
user.LastEmailedAt = time.Now()
user.UpdatedAt = time.Now()
- if err := p.db.UpdateByPrimaryKey(ctx, user, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, user, user.ID, updatingColumns...); err != nil {
return fmt.Errorf("SendConfirmEmail: error updating user entry after email sent: %s", err)
}
@@ -126,7 +126,7 @@ func (p *processor) ConfirmEmail(ctx context.Context, token string) (*gtsmodel.U
user.ConfirmationToken = ""
user.UpdatedAt = time.Now()
- if err := p.db.UpdateByPrimaryKey(ctx, user, updatingColumns...); err != nil {
+ if err := p.db.UpdateByID(ctx, user, user.ID, updatingColumns...); err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
diff --git a/internal/processing/user/emailconfirm_test.go b/internal/processing/user/emailconfirm_test.go
index 87aff9756..4e31a3646 100644
--- a/internal/processing/user/emailconfirm_test.go
+++ b/internal/processing/user/emailconfirm_test.go
@@ -74,7 +74,7 @@ func (suite *EmailConfirmTestSuite) TestConfirmEmail() {
user.ConfirmationSentAt = time.Now().Add(-5 * time.Minute)
user.ConfirmationToken = "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6"
- err := suite.db.UpdateByPrimaryKey(ctx, user, updatingColumns...)
+ err := suite.db.UpdateByID(ctx, user, user.ID, updatingColumns...)
suite.NoError(err)
// confirm with the token set above
@@ -102,7 +102,7 @@ func (suite *EmailConfirmTestSuite) TestConfirmEmailOldToken() {
user.ConfirmationSentAt = time.Now().Add(-192 * time.Hour)
user.ConfirmationToken = "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6"
- err := suite.db.UpdateByPrimaryKey(ctx, user, updatingColumns...)
+ err := suite.db.UpdateByID(ctx, user, user.ID, updatingColumns...)
suite.NoError(err)
// confirm with the token set above