mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 09:32:24 -05:00
[bugfix] fix double firing bun.DB query hooks (#2124)
* improve bun.DB wrapping readability + comments, fix double-firing query hooks * fix incorrect code comment placement * fix linter issues * Update internal/db/basic.go * do as the linter commmands ... --------- Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: Daenney <daenney@users.noreply.github.com>
This commit is contained in:
parent
e70629e856
commit
d5d6ad406f
44 changed files with 645 additions and 535 deletions
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/state"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
|
@ -49,15 +50,11 @@ func initState(ctx context.Context) (*state.State, error) {
|
|||
return &state, nil
|
||||
}
|
||||
|
||||
func stopState(ctx context.Context, state *state.State) error {
|
||||
if err := state.DB.Stop(ctx); err != nil {
|
||||
return fmt.Errorf("error stopping dbConn: %w", err)
|
||||
}
|
||||
|
||||
func stopState(state *state.State) error {
|
||||
err := state.DB.Close()
|
||||
state.Workers.Stop()
|
||||
state.Caches.Stop()
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// Create creates a new account and user
|
||||
|
|
@ -68,6 +65,13 @@ var Create action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -101,17 +105,14 @@ var Create action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if _, err := state.DB.NewSignup(ctx, gtsmodel.NewSignup{
|
||||
_, err = state.DB.NewSignup(ctx, gtsmodel.NewSignup{
|
||||
Username: username,
|
||||
Email: email,
|
||||
Password: password,
|
||||
EmailVerified: true, // Assume cli user wants email marked as verified already.
|
||||
PreApproved: true, // Assume cli user wants account marked as approved already.
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// List returns all existing local accounts.
|
||||
|
|
@ -148,8 +149,7 @@ var List action.GTSAction = func(ctx context.Context) error {
|
|||
for _, u := range users {
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", u.Account.Username, u.AccountID, fmtBool(u.Approved), fmtBool(u.Admin), fmtBool(u.Moderator), fmtDate(u.Account.SuspendedAt), fmtDate(u.ConfirmedAt))
|
||||
}
|
||||
w.Flush()
|
||||
return nil
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
// Confirm sets a user to Approved, sets Email to the current
|
||||
|
|
@ -160,6 +160,13 @@ var Confirm action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -178,14 +185,10 @@ var Confirm action.GTSAction = func(ctx context.Context) error {
|
|||
user.Approved = func() *bool { a := true; return &a }()
|
||||
user.Email = user.UnconfirmedEmail
|
||||
user.ConfirmedAt = time.Now()
|
||||
if err := state.DB.UpdateUser(
|
||||
return state.DB.UpdateUser(
|
||||
ctx, user,
|
||||
"approved", "email", "confirmed_at",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
)
|
||||
}
|
||||
|
||||
// Promote sets admin + moderator flags on a user to true.
|
||||
|
|
@ -195,6 +198,13 @@ var Promote action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -212,14 +222,10 @@ var Promote action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
user.Admin = func() *bool { a := true; return &a }()
|
||||
user.Moderator = func() *bool { a := true; return &a }()
|
||||
if err := state.DB.UpdateUser(
|
||||
return state.DB.UpdateUser(
|
||||
ctx, user,
|
||||
"admin", "moderator",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
)
|
||||
}
|
||||
|
||||
// Demote sets admin + moderator flags on a user to false.
|
||||
|
|
@ -229,6 +235,13 @@ var Demote action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -246,14 +259,10 @@ var Demote action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
user.Admin = func() *bool { a := false; return &a }()
|
||||
user.Moderator = func() *bool { a := false; return &a }()
|
||||
if err := state.DB.UpdateUser(
|
||||
return state.DB.UpdateUser(
|
||||
ctx, user,
|
||||
"admin", "moderator",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
)
|
||||
}
|
||||
|
||||
// Disable sets Disabled to true on a user.
|
||||
|
|
@ -263,6 +272,13 @@ var Disable action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -279,14 +295,10 @@ var Disable action.GTSAction = func(ctx context.Context) error {
|
|||
}
|
||||
|
||||
user.Disabled = func() *bool { d := true; return &d }()
|
||||
if err := state.DB.UpdateUser(
|
||||
return state.DB.UpdateUser(
|
||||
ctx, user,
|
||||
"disabled",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
)
|
||||
}
|
||||
|
||||
// Password sets the password of target account.
|
||||
|
|
@ -296,6 +308,13 @@ var Password action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Ensure state gets stopped on return.
|
||||
if err := stopState(state); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
username := config.GetAdminAccountUsername()
|
||||
if err := validate.Username(username); err != nil {
|
||||
return err
|
||||
|
|
@ -322,12 +341,8 @@ var Password action.GTSAction = func(ctx context.Context) error {
|
|||
}
|
||||
|
||||
user.EncryptedPassword = string(encryptedPassword)
|
||||
if err := state.DB.UpdateUser(
|
||||
return state.DB.UpdateUser(
|
||||
ctx, user,
|
||||
"encrypted_password",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return stopState(ctx, state)
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,12 +95,11 @@ func setupList(ctx context.Context) (*list, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (l *list) shutdown(ctx context.Context) error {
|
||||
func (l *list) shutdown() error {
|
||||
l.out.Flush()
|
||||
err := l.dbService.Stop(ctx)
|
||||
err := l.dbService.Close()
|
||||
l.state.Workers.Stop()
|
||||
l.state.Caches.Stop()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -112,7 +111,7 @@ var ListLocal action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
defer func() {
|
||||
// Ensure lister gets shutdown on exit.
|
||||
if err := list.shutdown(ctx); err != nil {
|
||||
if err := list.shutdown(); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
|
@ -144,7 +143,7 @@ var ListRemote action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
defer func() {
|
||||
// Ensure lister gets shutdown on exit.
|
||||
if err := list.shutdown(ctx); err != nil {
|
||||
if err := list.shutdown(); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ var All action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
defer func() {
|
||||
// Ensure pruner gets shutdown on exit.
|
||||
if err := prune.shutdown(ctx); err != nil {
|
||||
if err := prune.shutdown(); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -74,14 +74,14 @@ func setupPrune(ctx context.Context) (*prune, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (p *prune) shutdown(ctx context.Context) error {
|
||||
func (p *prune) shutdown() error {
|
||||
errs := gtserror.NewMultiError(2)
|
||||
|
||||
if err := p.storage.Close(); err != nil {
|
||||
errs.Appendf("error closing storage backend: %w", err)
|
||||
}
|
||||
|
||||
if err := p.dbService.Stop(ctx); err != nil {
|
||||
if err := p.dbService.Close(); err != nil {
|
||||
errs.Appendf("error stopping database: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ var Orphaned action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
defer func() {
|
||||
// Ensure pruner gets shutdown on exit.
|
||||
if err := prune.shutdown(ctx); err != nil {
|
||||
if err := prune.shutdown(); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ var Remote action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
defer func() {
|
||||
// Ensure pruner gets shutdown on exit.
|
||||
if err := prune.shutdown(ctx); err != nil {
|
||||
if err := prune.shutdown(); err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -52,5 +52,5 @@ var Export action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return dbConn.Stop(ctx)
|
||||
return dbConn.Close()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,5 +52,5 @@ var Import action.GTSAction = func(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return dbConn.Stop(ctx)
|
||||
return dbConn.Close()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue