From d9e1a759a1f1ff58e611e8af79657ff85d26df63 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 18 Jul 2023 15:31:36 +0300 Subject: [PATCH] make use of the after hook finalizer --- apis/admin.go | 56 +++++++++++++++++---------------------------- apis/collection.go | 32 ++++++++++---------------- apis/file.go | 10 ++++---- apis/realtime.go | 15 ++++++------ apis/record_auth.go | 56 +++++++++++++++++---------------------------- apis/record_crud.go | 24 ++++++++----------- apis/settings.go | 16 ++++++------- 7 files changed, 82 insertions(+), 127 deletions(-) diff --git a/apis/admin.go b/apis/admin.go index 0e64afed..d3ba5cd6 100644 --- a/apis/admin.go +++ b/apis/admin.go @@ -69,11 +69,9 @@ func (api *adminApi) authRefresh(c echo.Context) error { event.Admin = admin return api.app.OnAdminBeforeAuthRefreshRequest().Trigger(event, func(e *core.AdminAuthRefreshEvent) error { - if err := api.app.OnAdminAfterAuthRefreshRequest().Trigger(event); err != nil { - return err - } - - return api.authResponse(e.HttpContext, e.Admin) + return api.app.OnAdminAfterAuthRefreshRequest().Trigger(event, func(e *core.AdminAuthRefreshEvent) error { + return api.authResponse(e.HttpContext, e.Admin) + }) }) } @@ -97,11 +95,9 @@ func (api *adminApi) authWithPassword(c echo.Context) error { return NewBadRequestError("Failed to authenticate.", err) } - if err := api.app.OnAdminAfterAuthWithPasswordRequest().Trigger(event); err != nil { - return err - } - - return api.authResponse(e.HttpContext, e.Admin) + return api.app.OnAdminAfterAuthWithPasswordRequest().Trigger(event, func(e *core.AdminAuthWithPasswordEvent) error { + return api.authResponse(e.HttpContext, e.Admin) + }) }) } }) @@ -135,11 +131,9 @@ func (api *adminApi) requestPasswordReset(c echo.Context) error { } }) - if err := api.app.OnAdminAfterRequestPasswordResetRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnAdminAfterRequestPasswordResetRequest().Trigger(event, func(e *core.AdminRequestPasswordResetEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -171,11 +165,9 @@ func (api *adminApi) confirmPasswordReset(c echo.Context) error { return NewBadRequestError("Failed to set new password.", err) } - if err := api.app.OnAdminAfterConfirmPasswordResetRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnAdminAfterConfirmPasswordResetRequest().Trigger(event, func(e *core.AdminConfirmPasswordResetEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -252,11 +244,9 @@ func (api *adminApi) create(c echo.Context) error { return NewBadRequestError("Failed to create admin.", err) } - if err := api.app.OnAdminAfterCreateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Admin) + return api.app.OnAdminAfterCreateRequest().Trigger(event, func(e *core.AdminCreateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Admin) + }) }) } }) @@ -296,11 +286,9 @@ func (api *adminApi) update(c echo.Context) error { return NewBadRequestError("Failed to update admin.", err) } - if err := api.app.OnAdminAfterUpdateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Admin) + return api.app.OnAdminAfterUpdateRequest().Trigger(event, func(e *core.AdminUpdateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Admin) + }) }) } }) @@ -328,10 +316,8 @@ func (api *adminApi) delete(c echo.Context) error { return NewBadRequestError("Failed to delete admin.", err) } - if err := api.app.OnAdminAfterDeleteRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnAdminAfterDeleteRequest().Trigger(event, func(e *core.AdminDeleteEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } diff --git a/apis/collection.go b/apis/collection.go index bf7a652e..6e7d4179 100644 --- a/apis/collection.go +++ b/apis/collection.go @@ -91,11 +91,9 @@ func (api *collectionApi) create(c echo.Context) error { return NewBadRequestError("Failed to create the collection.", err) } - if err := api.app.OnCollectionAfterCreateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Collection) + return api.app.OnCollectionAfterCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Collection) + }) }) } }) @@ -128,11 +126,9 @@ func (api *collectionApi) update(c echo.Context) error { return NewBadRequestError("Failed to update the collection.", err) } - if err := api.app.OnCollectionAfterUpdateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Collection) + return api.app.OnCollectionAfterUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Collection) + }) }) } }) @@ -153,11 +149,9 @@ func (api *collectionApi) delete(c echo.Context) error { return NewBadRequestError("Failed to delete collection due to existing dependency.", err) } - if err := api.app.OnCollectionAfterDeleteRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnCollectionAfterDeleteRequest().Trigger(event, func(e *core.CollectionDeleteEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } @@ -183,11 +177,9 @@ func (api *collectionApi) bulkImport(c echo.Context) error { return NewBadRequestError("Failed to import the submitted collections.", err) } - if err := api.app.OnCollectionsAfterImportRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnCollectionsAfterImportRequest().Trigger(event, func(e *core.CollectionsImportEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) diff --git a/apis/file.go b/apis/file.go index c994566a..25dcf2ef 100644 --- a/apis/file.go +++ b/apis/file.go @@ -50,12 +50,10 @@ func (api *fileApi) fileToken(c echo.Context) error { return NewBadRequestError("Failed to generate file token.", nil) } - if err := api.app.OnFileAfterTokenRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, map[string]string{ - "token": e.Token, + return api.app.OnFileAfterTokenRequest().Trigger(event, func(e *core.FileTokenEvent) error { + return e.HttpContext.JSON(http.StatusOK, map[string]string{ + "token": e.Token, + }) }) }) } diff --git a/apis/realtime.go b/apis/realtime.go index 9523d2c6..6d9c86d5 100644 --- a/apis/realtime.go +++ b/apis/realtime.go @@ -142,8 +142,11 @@ func (api *realtimeApi) connect(c echo.Context) error { return nil } - if err := api.app.OnRealtimeAfterMessageSend().Trigger(msgEvent); err != nil && api.app.IsDebug() { - log.Println("OnRealtimeAfterMessageSend error:", err) + if err := api.app.OnRealtimeAfterMessageSend().Trigger(msgEvent); err != nil { + if api.app.IsDebug() { + log.Println("Realtime connection closed (OnRealtimeAfterMessageSend error):", client.Id(), err) + } + return nil } idleTimer.Stop() @@ -202,11 +205,9 @@ func (api *realtimeApi) setSubscriptions(c echo.Context) error { // subscribe to the new subscriptions e.Client.Subscribe(e.Subscriptions...) - if err := api.app.OnRealtimeAfterSubscribeRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRealtimeAfterSubscribeRequest().Trigger(event, func(e *core.RealtimeSubscribeEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } diff --git a/apis/record_auth.go b/apis/record_auth.go index 9cfe73a6..a1b75338 100644 --- a/apis/record_auth.go +++ b/apis/record_auth.go @@ -332,11 +332,9 @@ func (api *recordAuthApi) requestPasswordReset(c echo.Context) error { } }) - if err := api.app.OnRecordAfterRequestPasswordResetRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterRequestPasswordResetRequest().Trigger(event, func(e *core.RecordRequestPasswordResetEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -374,11 +372,9 @@ func (api *recordAuthApi) confirmPasswordReset(c echo.Context) error { return NewBadRequestError("Failed to set new password.", err) } - if err := api.app.OnRecordAfterConfirmPasswordResetRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterConfirmPasswordResetRequest().Trigger(event, func(e *core.RecordConfirmPasswordResetEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -417,11 +413,9 @@ func (api *recordAuthApi) requestVerification(c echo.Context) error { } }) - if err := api.app.OnRecordAfterRequestVerificationRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterRequestVerificationRequest().Trigger(event, func(e *core.RecordRequestVerificationEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -459,11 +453,9 @@ func (api *recordAuthApi) confirmVerification(c echo.Context) error { return NewBadRequestError("An error occurred while submitting the form.", err) } - if err := api.app.OnRecordAfterConfirmVerificationRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterConfirmVerificationRequest().Trigger(event, func(e *core.RecordConfirmVerificationEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -499,11 +491,9 @@ func (api *recordAuthApi) requestEmailChange(c echo.Context) error { return NewBadRequestError("Failed to request email change.", err) } - if err := api.app.OnRecordAfterRequestEmailChangeRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterRequestEmailChangeRequest().Trigger(event, func(e *core.RecordRequestEmailChangeEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -533,11 +523,9 @@ func (api *recordAuthApi) confirmEmailChange(c echo.Context) error { return NewBadRequestError("Failed to confirm email change.", err) } - if err := api.app.OnRecordAfterConfirmEmailChangeRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterConfirmEmailChangeRequest().Trigger(event, func(e *core.RecordConfirmEmailChangeEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } }) @@ -610,11 +598,9 @@ func (api *recordAuthApi) unlinkExternalAuth(c echo.Context) error { return NewBadRequestError("Cannot unlink the external auth provider.", err) } - if err := api.app.OnRecordAfterUnlinkExternalAuthRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterUnlinkExternalAuthRequest().Trigger(event, func(e *core.RecordUnlinkExternalAuthEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } diff --git a/apis/record_crud.go b/apis/record_crud.go index 5a4ace23..424839e5 100644 --- a/apis/record_crud.go +++ b/apis/record_crud.go @@ -238,11 +238,9 @@ func (api *recordApi) create(c echo.Context) error { log.Println(err) } - if err := api.app.OnRecordAfterCreateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Record) + return api.app.OnRecordAfterCreateRequest().Trigger(event, func(e *core.RecordCreateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Record) + }) }) } }) @@ -323,11 +321,9 @@ func (api *recordApi) update(c echo.Context) error { log.Println(err) } - if err := api.app.OnRecordAfterUpdateRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.JSON(http.StatusOK, e.Record) + return api.app.OnRecordAfterUpdateRequest().Trigger(event, func(e *core.RecordUpdateEvent) error { + return e.HttpContext.JSON(http.StatusOK, e.Record) + }) }) } }) @@ -380,11 +376,9 @@ func (api *recordApi) delete(c echo.Context) error { return NewBadRequestError("Failed to delete record. Make sure that the record is not part of a required relation reference.", err) } - if err := api.app.OnRecordAfterDeleteRequest().Trigger(event); err != nil { - return err - } - - return e.HttpContext.NoContent(http.StatusNoContent) + return api.app.OnRecordAfterDeleteRequest().Trigger(event, func(e *core.RecordDeleteEvent) error { + return e.HttpContext.NoContent(http.StatusNoContent) + }) }) } diff --git a/apis/settings.go b/apis/settings.go index 2284ff74..8aa9d681 100644 --- a/apis/settings.go +++ b/apis/settings.go @@ -63,16 +63,14 @@ func (api *settingsApi) set(c echo.Context) error { return NewBadRequestError("An error occurred while submitting the form.", err) } - if err := api.app.OnSettingsAfterUpdateRequest().Trigger(event); err != nil { - return err - } + return api.app.OnSettingsAfterUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error { + redactedSettings, err := api.app.Settings().RedactClone() + if err != nil { + return NewBadRequestError("", err) + } - redactedSettings, err := api.app.Settings().RedactClone() - if err != nil { - return NewBadRequestError("", err) - } - - return e.HttpContext.JSON(http.StatusOK, redactedSettings) + return e.HttpContext.JSON(http.StatusOK, redactedSettings) + }) }) } })