Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 2, 2024
1 parent 1426f3c commit 34436f9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 91 deletions.
91 changes: 41 additions & 50 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
23 changes: 7 additions & 16 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 0 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
}

0 comments on commit 34436f9

Please sign in to comment.