From 34436f9a469a113edef7e45ca8b59cf3224980ce Mon Sep 17 00:00:00 2001 From: im-adithya Date: Tue, 2 Jan 2024 17:31:27 +0530 Subject: [PATCH] chore: refactoring --- echo_handlers.go | 91 ++++++++++++++++++++++-------------------------- models.go | 23 ++++-------- service.go | 9 ----- utils.go | 16 --------- 4 files changed, 48 insertions(+), 91 deletions(-) diff --git a/echo_handlers.go b/echo_handlers.go index 0bfec7ec..27ed2671 100644 --- a/echo_handlers.go +++ b/echo_handlers.go @@ -28,7 +28,7 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) { e.Use(middleware.Recover()) e.Use(middleware.RequestID()) e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{ - TokenLookup: "header:X-CSRF-Token,form:_csrf", + TokenLookup: "header:X-CSRF-Token", })) e.Use(session.Middleware(sessions.NewCookieStore([]byte(svc.cfg.CookieSecret)))) e.Use(ddEcho.Middleware(ddEcho.WithServiceName("nostr-wallet-connect"))) @@ -45,14 +45,33 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) { frontend.RegisterHandlers(e) } -func (svc *Service) AboutHandler(c echo.Context) error { +func (svc *Service) CSRFHandler(c echo.Context) error { + csrf, _ := c.Get(middleware.DefaultCSRFConfig.ContextKey).(string) + return c.JSON(http.StatusOK, csrf) +} + +func (svc *Service) InfoHandler(c echo.Context) error { + responseBody := &api.InfoResponse{} + responseBody.BackendType = svc.cfg.LNBackendType + return c.JSON(http.StatusOK, responseBody) +} + +func (svc *Service) UserMeHandler(c echo.Context) error { user, err := svc.GetUser(c) if err != nil { - return err + return c.JSON(http.StatusBadRequest, ErrorResponse{ + Error: true, + Message: fmt.Sprintf("Bad arguments %s", err.Error()), + }) } - return c.Render(http.StatusOK, "about.html", map[string]interface{}{ - "User": user, - }) + if user == nil { + return c.NoContent(http.StatusNotFound) + } + + responseBody := api.User{ + Email: user.Email, + } + return c.JSON(http.StatusOK, responseBody) } func (svc *Service) AppsListHandler(c echo.Context) error { @@ -91,7 +110,6 @@ func (svc *Service) AppsListHandler(c echo.Context) error { } func (svc *Service) AppsShowHandler(c echo.Context) error { - // csrf, _ := c.Get(middleware.DefaultCSRFConfig.ContextKey).(string) user, err := svc.GetUser(c) if err != nil { return c.JSON(http.StatusBadRequest, ErrorResponse{ @@ -115,8 +133,6 @@ func (svc *Service) AppsShowHandler(c echo.Context) error { var lastEvent NostrEvent lastEventResult := svc.db.Where("app_id = ?", app.ID).Order("id desc").Limit(1).Find(&lastEvent) - //var eventsCount int64 - //svc.db.Model(&NostrEvent{}).Where("app_id = ?", app.ID).Count(&eventsCount) paySpecificPermission := AppPermission{} appPermissions := []AppPermission{} @@ -162,9 +178,21 @@ func (svc *Service) AppsShowHandler(c echo.Context) error { return c.JSON(http.StatusOK, response) } -func (svc *Service) CSRFHandler(c echo.Context) error { - csrf, _ := c.Get(middleware.DefaultCSRFConfig.ContextKey).(string) - return c.JSON(http.StatusOK, csrf) +func (svc *Service) AppsDeleteHandler(c echo.Context) error { + user, err := svc.GetUser(c) + if err != nil { + return c.JSON(http.StatusBadRequest, ErrorResponse{ + Error: true, + Message: fmt.Sprintf("Bad arguments %s", err.Error()), + }) + } + if user == nil { + return c.NoContent(http.StatusUnauthorized) + } + app := App{} + svc.db.Where("user_id = ? AND nostr_pubkey = ?", user.ID, c.Param("pubkey")).First(&app) + svc.db.Delete(&app) + return c.NoContent(http.StatusNoContent) } func (svc *Service) AppsCreateHandler(c echo.Context) error { @@ -239,7 +267,7 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error { methodsToCreate := strings.Split(requestMethods, " ") for _, m := range methodsToCreate { //if we don't know this method, we return an error - if _, ok := nip47MethodDescriptions[m]; !ok { + if !validNIP47Methods[m] { return fmt.Errorf("Did not recognize request method: %s", m) } appPermission := AppPermission{ @@ -302,21 +330,6 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error { return c.JSON(http.StatusOK, responseBody) } -func (svc *Service) AppsDeleteHandler(c echo.Context) error { - user, err := svc.GetUser(c) - // TODO: error handling - if err != nil { - return err - } - if user == nil { - return c.NoContent(http.StatusUnauthorized) - } - app := App{} - svc.db.Where("user_id = ? AND nostr_pubkey = ?", user.ID, c.Param("pubkey")).First(&app) - svc.db.Delete(&app) - return c.NoContent(http.StatusNoContent) -} - func (svc *Service) LogoutHandler(c echo.Context) error { sess, _ := session.Get(CookieName, c) sess.Options.MaxAge = -1 @@ -326,25 +339,3 @@ func (svc *Service) LogoutHandler(c echo.Context) error { sess.Save(c.Request(), c.Response()) return c.NoContent(http.StatusNoContent) } - -func (svc *Service) InfoHandler(c echo.Context) error { - responseBody := &api.InfoResponse{} - responseBody.BackendType = svc.cfg.LNBackendType - return c.JSON(http.StatusOK, responseBody) -} - -func (svc *Service) UserMeHandler(c echo.Context) error { - user, err := svc.GetUser(c) - if err != nil { - // TODO: error handling - return err - } - if user == nil { - return c.NoContent(http.StatusNotFound) - } - - responseBody := api.User{ - Email: user.Email, - } - return c.JSON(http.StatusOK, responseBody) -} diff --git a/models.go b/models.go index dd566956..8d045fc4 100644 --- a/models.go +++ b/models.go @@ -37,22 +37,13 @@ const ( NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED = "sent" ) -var nip47MethodDescriptions = map[string]string{ - NIP_47_GET_BALANCE_METHOD: "Read your balance", - NIP_47_GET_INFO_METHOD: "Read your node info", - NIP_47_PAY_INVOICE_METHOD: "Send payments", - NIP_47_MAKE_INVOICE_METHOD: "Create invoices", - NIP_47_LOOKUP_INVOICE_METHOD: "Lookup status of invoices", - NIP_47_LIST_TRANSACTIONS_METHOD: "Read incoming transaction history", -} - -var nip47MethodIcons = map[string]string{ - NIP_47_GET_BALANCE_METHOD: "wallet", - NIP_47_GET_INFO_METHOD: "wallet", - NIP_47_PAY_INVOICE_METHOD: "lightning", - NIP_47_MAKE_INVOICE_METHOD: "invoice", - NIP_47_LOOKUP_INVOICE_METHOD: "search", - NIP_47_LIST_TRANSACTIONS_METHOD: "transactions", +var validNIP47Methods = map[string]bool{ + NIP_47_GET_BALANCE_METHOD: true, + NIP_47_GET_INFO_METHOD: true, + NIP_47_PAY_INVOICE_METHOD: true, + NIP_47_MAKE_INVOICE_METHOD: true, + NIP_47_LOOKUP_INVOICE_METHOD: true, + NIP_47_LIST_TRANSACTIONS_METHOD: true, } // TODO: move to models/Alby diff --git a/service.go b/service.go index f6def421..6b567c3b 100644 --- a/service.go +++ b/service.go @@ -23,15 +23,6 @@ type Service struct { Logger *logrus.Logger } -/*var supportedMethods = map[string]bool{ - NIP_47_PAY_INVOICE_METHOD: true, - NIP_47_GET_BALANCE_METHOD: true, - NIP_47_GET_INFO_METHOD: true, - NIP_47_MAKE_INVOICE_METHOD: true, - NIP_47_LOOKUP_INVOICE_METHOD: true, - NIP_47_LIST_TRANSACTIONS_METHOD: true, -}*/ - func (svc *Service) GetUser(c echo.Context) (user *User, err error) { sess, _ := session.Get(CookieName, c) userID := sess.Values["user_id"] diff --git a/utils.go b/utils.go index 3708afe1..87cb3c5a 100644 --- a/utils.go +++ b/utils.go @@ -25,19 +25,3 @@ func GetStartOfBudget(budget_type string, createdAt time.Time) time.Time { return createdAt } } - -func GetEndOfBudget(budget_type string, createdAt time.Time) time.Time { - start := GetStartOfBudget(budget_type, createdAt) - switch budget_type { - case "daily": - return start.AddDate(0, 0, 1) - case "weekly": - return start.AddDate(0, 0, 7) - case "monthly": - return start.AddDate(0, 1, 0) - case "yearly": - return start.AddDate(1, 0, 0) - default: //"never" - return time.Time{} - } -}