diff options
author | 2023-09-04 15:55:17 +0200 | |
---|---|---|
committer | 2023-09-04 14:55:17 +0100 | |
commit | 3ed1ca68e52527f74103e1a57ae48ae533508c3a (patch) | |
tree | d6113d71d6f88a3d99bbd2215ead6ca1d4fa6153 /internal/processing/admin/account.go | |
parent | [chore]: Bump golang.org/x/image from 0.11.0 to 0.12.0 (#2178) (diff) | |
download | gotosocial-3ed1ca68e52527f74103e1a57ae48ae533508c3a.tar.xz |
[feature] Store admin actions in the db, prevent conflicting actions (#2167)
Diffstat (limited to 'internal/processing/admin/account.go')
-rw-r--r-- | internal/processing/admin/account.go | 86 |
1 files changed, 62 insertions, 24 deletions
diff --git a/internal/processing/admin/account.go b/internal/processing/admin/account.go index 93e324441..155d8c0b4 100644 --- a/internal/processing/admin/account.go +++ b/internal/processing/admin/account.go @@ -29,36 +29,74 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/messages" ) -func (p *Processor) AccountAction(ctx context.Context, account *gtsmodel.Account, form *apimodel.AdminAccountActionRequest) gtserror.WithCode { - targetAccount, err := p.state.DB.GetAccountByID(ctx, form.TargetAccountID) +func (p *Processor) AccountAction( + ctx context.Context, + adminAcct *gtsmodel.Account, + request *apimodel.AdminActionRequest, +) (string, gtserror.WithCode) { + targetAcct, err := p.state.DB.GetAccountByID(ctx, request.TargetID) if err != nil { - return gtserror.NewErrorInternalError(err) + err := gtserror.Newf("db error getting target account: %w", err) + return "", gtserror.NewErrorInternalError(err) } - adminAction := >smodel.AdminAccountAction{ - ID: id.NewULID(), - AccountID: account.ID, - TargetAccountID: targetAccount.ID, - Text: form.Text, - } + switch gtsmodel.NewAdminActionType(request.Type) { + case gtsmodel.AdminActionSuspend: + return p.accountActionSuspend(ctx, adminAcct, targetAcct, request.Text) - switch form.Type { - case string(gtsmodel.AdminActionSuspend): - adminAction.Type = gtsmodel.AdminActionSuspend - // pass the account delete through the client api channel for processing - p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{ - APObjectType: ap.ActorPerson, - APActivityType: ap.ActivityDelete, - OriginAccount: account, - TargetAccount: targetAccount, - }) default: - return gtserror.NewErrorBadRequest(fmt.Errorf("admin action type %s is not supported for this endpoint", form.Type)) - } + // TODO: add more types to this slice when adding + // more types to the switch statement above. + supportedTypes := []string{ + gtsmodel.AdminActionSuspend.String(), + } - if err := p.state.DB.Put(ctx, adminAction); err != nil { - return gtserror.NewErrorInternalError(err) + err := fmt.Errorf( + "admin action type %s is not supported for this endpoint, "+ + "currently supported types are: %q", + request.Type, supportedTypes) + + return "", gtserror.NewErrorBadRequest(err, err.Error()) } +} + +func (p *Processor) accountActionSuspend( + ctx context.Context, + adminAcct *gtsmodel.Account, + targetAcct *gtsmodel.Account, + text string, +) (string, gtserror.WithCode) { + actionID := id.NewULID() + + errWithCode := p.actions.Run( + ctx, + >smodel.AdminAction{ + ID: actionID, + TargetCategory: gtsmodel.AdminActionCategoryAccount, + TargetID: targetAcct.ID, + Target: targetAcct, + Type: gtsmodel.AdminActionSuspend, + AccountID: adminAcct.ID, + Text: text, + }, + func(ctx context.Context) gtserror.MultiError { + if err := p.state.Workers.ProcessFromClientAPI( + ctx, + messages.FromClientAPI{ + APObjectType: ap.ActorPerson, + APActivityType: ap.ActivityDelete, + OriginAccount: adminAcct, + TargetAccount: targetAcct, + }, + ); err != nil { + errs := gtserror.NewMultiError(1) + errs.Append(err) + return errs + } + + return nil + }, + ) - return nil + return actionID, errWithCode } |