Skip to content

Commit

Permalink
[chore] update database caching library (#1040)
Browse files Browse the repository at this point in the history
* convert most of the caches to use result.Cache{}

* add caching of emojis

* fix issues causing failing tests

* update go-cache/v2 instances with v3

* fix getnotification

* add a note about the left-in StatusCreate comment

* update EmojiCategory db access to use new result.Cache{}

* fix possible panic in getstatusparents

* further proof that kim is not stinky
  • Loading branch information
NyaaaWhatsUpDoc authored Nov 15, 2022
1 parent 9ab6013 commit 8598dea
Show file tree
Hide file tree
Showing 55 changed files with 705 additions and 2,269 deletions.
12 changes: 4 additions & 8 deletions cmd/gotosocial/action/admin/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,10 @@ var Promote action.GTSAction = func(ctx context.Context) error {
return err
}

updatingColumns := []string{"admin", "updated_at"}
admin := true
u.Admin = &admin
u.UpdatedAt = time.Now()
if _, err := dbConn.UpdateUser(ctx, u, updatingColumns...); err != nil {
if err := dbConn.UpdateUser(ctx, u); err != nil {
return err
}

Expand Down Expand Up @@ -185,11 +184,10 @@ var Demote action.GTSAction = func(ctx context.Context) error {
return err
}

updatingColumns := []string{"admin", "updated_at"}
admin := false
u.Admin = &admin
u.UpdatedAt = time.Now()
if _, err := dbConn.UpdateUser(ctx, u, updatingColumns...); err != nil {
if err := dbConn.UpdateUser(ctx, u); err != nil {
return err
}

Expand Down Expand Up @@ -221,11 +219,10 @@ var Disable action.GTSAction = func(ctx context.Context) error {
return err
}

updatingColumns := []string{"disabled", "updated_at"}
disabled := true
u.Disabled = &disabled
u.UpdatedAt = time.Now()
if _, err := dbConn.UpdateUser(ctx, u, updatingColumns...); err != nil {
if err := dbConn.UpdateUser(ctx, u); err != nil {
return err
}

Expand Down Expand Up @@ -270,10 +267,9 @@ var Password action.GTSAction = func(ctx context.Context) error {
return fmt.Errorf("error hashing password: %s", err)
}

updatingColumns := []string{"encrypted_password", "updated_at"}
u.EncryptedPassword = string(pw)
u.UpdatedAt = time.Now()
if _, err := dbConn.UpdateUser(ctx, u, updatingColumns...); err != nil {
if err := dbConn.UpdateUser(ctx, u); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.19
require (
codeberg.org/gruf/go-bytesize v1.0.0
codeberg.org/gruf/go-byteutil v1.0.2
codeberg.org/gruf/go-cache/v2 v2.1.4
codeberg.org/gruf/go-cache/v3 v3.1.8
codeberg.org/gruf/go-debug v1.2.0
codeberg.org/gruf/go-errors/v2 v2.0.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ codeberg.org/gruf/go-bytesize v1.0.0/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacp
codeberg.org/gruf/go-byteutil v1.0.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU=
codeberg.org/gruf/go-byteutil v1.0.2 h1:OesVyK5VKWeWdeDR00zRJ+Oy8hjXx1pBhn7WVvcZWVE=
codeberg.org/gruf/go-byteutil v1.0.2/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU=
codeberg.org/gruf/go-cache/v2 v2.1.4 h1:r+6wJiTHZn0qqf+p1VtAjGOgXXJl7s8txhPIwoSMZtI=
codeberg.org/gruf/go-cache/v2 v2.1.4/go.mod h1:j7teiz814lG0PfSfnUs+6HA+2/jcjTAR71Ou3Wbt2Xk=
codeberg.org/gruf/go-cache/v3 v3.1.8 h1:wbUef/QtRstEb7sSpQYHT5CtSFtKkeZr4ZhOTXqOpac=
codeberg.org/gruf/go-cache/v3 v3.1.8/go.mod h1:h6im2UVGdrGtNt4IVKARVeoW4kAdok5ts7CbH15UWXs=
codeberg.org/gruf/go-debug v1.2.0 h1:WBbTMnK1ArFKUmgv04aO2JiC/daTOB8zQGi521qb7OU=
Expand Down
22 changes: 9 additions & 13 deletions internal/api/client/auth/authorize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AuthAuthorizeTestSuite struct {

type authorizeHandlerTestCase struct {
description string
mutateUserAccount func(*gtsmodel.User, *gtsmodel.Account) []string
mutateUserAccount func(*gtsmodel.User, *gtsmodel.Account)
expectedStatusCode int
expectedLocationHeader string
}
Expand All @@ -29,44 +29,40 @@ func (suite *AuthAuthorizeTestSuite) TestAccountAuthorizeHandler() {
tests := []authorizeHandlerTestCase{
{
description: "user has their email unconfirmed",
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) []string {
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) {
// nothing to do, weed_lord420 already has their email unconfirmed
return nil
},
expectedStatusCode: http.StatusSeeOther,
expectedLocationHeader: auth.CheckYourEmailPath,
},
{
description: "user has their email confirmed but is not approved",
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) []string {
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) {
user.ConfirmedAt = time.Now()
user.Email = user.UnconfirmedEmail
return []string{"confirmed_at", "email"}
},
expectedStatusCode: http.StatusSeeOther,
expectedLocationHeader: auth.WaitForApprovalPath,
},
{
description: "user has their email confirmed and is approved, but User entity has been disabled",
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) []string {
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) {
user.ConfirmedAt = time.Now()
user.Email = user.UnconfirmedEmail
user.Approved = testrig.TrueBool()
user.Disabled = testrig.TrueBool()
return []string{"confirmed_at", "email", "approved", "disabled"}
},
expectedStatusCode: http.StatusSeeOther,
expectedLocationHeader: auth.AccountDisabledPath,
},
{
description: "user has their email confirmed and is approved, but Account entity has been suspended",
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) []string {
mutateUserAccount: func(user *gtsmodel.User, account *gtsmodel.Account) {
user.ConfirmedAt = time.Now()
user.Email = user.UnconfirmedEmail
user.Approved = testrig.TrueBool()
user.Disabled = testrig.FalseBool()
account.SuspendedAt = time.Now()
return []string{"confirmed_at", "email", "approved", "disabled"}
},
expectedStatusCode: http.StatusSeeOther,
expectedLocationHeader: auth.AccountDisabledPath,
Expand All @@ -81,6 +77,7 @@ func (suite *AuthAuthorizeTestSuite) TestAccountAuthorizeHandler() {

*user = *suite.testUsers["unconfirmed_account"]
*account = *suite.testAccounts["unconfirmed_account"]
user.SignInCount++ // cannot be 0 or fails NULL constraint

testSession := sessions.Default(ctx)
testSession.Set(sessionUserID, user.ID)
Expand All @@ -89,14 +86,13 @@ func (suite *AuthAuthorizeTestSuite) TestAccountAuthorizeHandler() {
panic(fmt.Errorf("failed on case %s: %w", testCase.description, err))
}

updatingColumns := testCase.mutateUserAccount(user, account)
testCase.mutateUserAccount(user, account)

testCase.description = fmt.Sprintf("%s, %t, %s", user.Email, *user.Disabled, account.SuspendedAt)

updatingColumns = append(updatingColumns, "updated_at")
_, err := suite.db.UpdateUser(context.Background(), user, updatingColumns...)
err := suite.db.UpdateUser(context.Background(), user)
suite.NoError(err)
_, err = suite.db.UpdateAccount(context.Background(), account)
err = suite.db.UpdateAccount(context.Background(), account)
suite.NoError(err)

// call the handler
Expand Down
9 changes: 9 additions & 0 deletions internal/api/client/status/statuscreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func (m *Module) StatusCreatePOSTHandler(c *gin.Context) {
return
}

// DO NOT COMMIT THIS UNCOMMENTED, IT WILL CAUSE MASS CHAOS.
// this is being left in as an ode to kim's shitposting.
//
// user := authed.Account.DisplayName
// if user == "" {
// user = authed.Account.Username
// }
// form.Status += "\n\nsent from " + user + "'s iphone\n"

if err := validateCreateStatus(form); err != nil {
api.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGet)
return
Expand Down
8 changes: 4 additions & 4 deletions internal/api/client/status/statuscreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func (suite *StatusCreateTestSuite) TestPostNewStatusMarkdown() {
// set default post language of account 1 to markdown
testAccount := suite.testAccounts["local_account_1"]
testAccount.StatusFormat = "markdown"
a := testAccount

a, err := suite.db.UpdateAccount(context.Background(), testAccount)
err := suite.db.UpdateAccount(context.Background(), a)
if err != nil {
suite.FailNow(err.Error())
}
Expand Down Expand Up @@ -149,9 +150,8 @@ func (suite *StatusCreateTestSuite) TestPostNewStatusMarkdown() {
func (suite *StatusCreateTestSuite) TestMentionUnknownAccount() {
// first remove remote account 1 from the database so it gets looked up again
remoteAccount := suite.testAccounts["remote_account_1"]
if err := suite.db.DeleteByID(context.Background(), remoteAccount.ID, &gtsmodel.Account{}); err != nil {
panic(err)
}
err := suite.db.DeleteAccount(context.Background(), remoteAccount.ID)
suite.NoError(err)

t := suite.testTokens["local_account_1"]
oauthToken := oauth.DBTokenToToken(t)
Expand Down
171 changes: 0 additions & 171 deletions internal/cache/account.go

This file was deleted.

Loading

0 comments on commit 8598dea

Please sign in to comment.