Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use pubkey url in apps show handler and add 404 page #110

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) {
templates["apps/create.html"] = template.Must(template.ParseFS(embeddedViews, "views/apps/create.html", "views/layout.html"))
templates["alby/index.html"] = template.Must(template.ParseFS(embeddedViews, "views/backends/alby/index.html", "views/layout.html"))
templates["about.html"] = template.Must(template.ParseFS(embeddedViews, "views/about.html", "views/layout.html"))
templates["404.html"] = template.Must(template.ParseFS(embeddedViews, "views/404.html", "views/layout.html"))
templates["lnd/index.html"] = template.Must(template.ParseFS(embeddedViews, "views/backends/lnd/index.html", "views/layout.html"))
e.Renderer = &TemplateRegistry{
templates: templates,
Expand All @@ -65,7 +66,7 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) {
e.Use(middleware.Recover())
e.Use(middleware.RequestID())
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
TokenLookup: "form:_csrf",
TokenLookup: "form:_csrf",
}))
e.Use(session.Middleware(sessions.NewCookieStore([]byte(svc.cfg.CookieSecret))))
e.Use(ddEcho.Middleware(ddEcho.WithServiceName("nostr-wallet-connect")))
Expand All @@ -75,9 +76,9 @@ func (svc *Service) RegisterSharedRoutes(e *echo.Echo) {
e.GET("/public/*", echo.WrapHandler(http.StripPrefix("/public/", assetHandler)))
e.GET("/apps", svc.AppsListHandler)
e.GET("/apps/new", svc.AppsNewHandler)
e.GET("/apps/:id", svc.AppsShowHandler)
e.GET("/apps/:pubkey", svc.AppsShowHandler)
e.POST("/apps", svc.AppsCreateHandler)
e.POST("/apps/delete/:id", svc.AppsDeleteHandler)
e.POST("/apps/delete/:pubkey", svc.AppsDeleteHandler)
e.GET("/logout", svc.LogoutHandler)
e.GET("/about", svc.AboutHandler)
e.GET("/", svc.IndexHandler)
Expand Down Expand Up @@ -157,7 +158,14 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
}

app := App{}
svc.db.Where("user_id = ?", user.ID).First(&app, c.Param("id"))
svc.db.Where("user_id = ? AND nostr_pubkey = ?", user.ID, c.Param("pubkey")).First(&app)

if app.NostrPubkey == "" {
return c.Render(http.StatusNotFound, "404.html", map[string]interface{}{
"User": user,
})
}

lastEvent := NostrEvent{}
svc.db.Where("app_id = ?", app.ID).Order("id desc").Limit(1).Find(&lastEvent)
var eventsCount int64
Expand All @@ -173,7 +181,6 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
budgetUsage = svc.GetBudgetUsage(&appPermission)
endOfBudget := GetEndOfBudget(appPermission.BudgetRenewal, app.CreatedAt)
renewsIn = getEndOfBudgetString(endOfBudget)

}

return c.Render(http.StatusOK, "apps/show.html", map[string]interface{}{
Expand Down Expand Up @@ -222,7 +229,7 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
budgetRenewal := strings.ToLower(c.QueryParam("budget_renewal"))
expiresAt := c.QueryParam("expires_at") // YYYY-MM-DD or MM/DD/YYYY or timestamp in seconds
if expiresAtTimestamp, err := strconv.Atoi(expiresAt); err == nil {
expiresAt = time.Unix(int64(expiresAtTimestamp), 0).Format(time.RFC3339)
expiresAt = time.Unix(int64(expiresAtTimestamp), 0).Format(time.RFC3339)
}
disabled := c.QueryParam("editable") == "false"
budgetEnabled := maxAmount != "" || budgetRenewal != ""
Expand Down Expand Up @@ -360,7 +367,7 @@ func (svc *Service) AppsDeleteHandler(c echo.Context) error {
return c.Redirect(302, "/")
}
app := App{}
svc.db.Where("user_id = ?", user.ID).First(&app, c.Param("id"))
svc.db.Where("user_id = ? AND nostr_pubkey = ?", user.ID, c.Param("pubkey")).First(&app)
svc.db.Delete(&app)
return c.Redirect(302, "/apps")
}
Expand Down
13 changes: 13 additions & 0 deletions views/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{define "body"}}

<div
class="w-full lg:w-8/12 mx-auto bg-white rounded-md shadow px-4 lg:px-12 py-4 lg:py-12 mb-10 dark:bg-surface-02dp"
>
<h3 class="font-bold text-2xl font-headline mb-4 dark:text-white">404 Page Not Found</h3>
<p class="leading-relaxed dark:text-neutral-400">
The page you are looking for does not exist.
</p>
</div>

{{end}}
</div>
2 changes: 1 addition & 1 deletion views/apps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h2 class="font-bold text-2xl font-headline py-2 dark:text-white">Your app conne
</tr>
{{else}}
{{range .Apps}}
<tr class="bg-white border-t dark:bg-surface-02dp dark:border-white/10 cursor-pointer hover:bg-purple-50 dark:hover:bg-surface-16dp" onclick="window.location='/apps/{{.ID}}'">
<tr class="bg-white border-t dark:bg-surface-02dp dark:border-white/10 cursor-pointer hover:bg-purple-50 dark:hover:bg-surface-16dp" onclick="window.location='/apps/{{.NostrPubkey}}'">
<td class="px-6 py-4 text-gray-500 dark:text-white">
{{.Name}}
</td>
Expand Down
2 changes: 1 addition & 1 deletion views/apps/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <h3 class="text-xl font-headline mb-2 dark:text-white">Danger zone</h3>
</div>
</div>

<form method="post" action="/apps/delete/{{.App.ID}}">
<form method="post" action="/apps/delete/{{.App.NostrPubkey}}">
<input type="hidden" name="_csrf" value="{{.Csrf}}">
<button type="submit"
class="inline-flex bg-white border border-red-400 cursor-pointer dark:bg-surface-02dp dark:hover:bg-surface-16dp duration-150 focus-visible:ring-2 focus-visible:ring-offset-2 focus:outline-none font-medium hover:bg-gray-50 items-center justify-center px-5 py-3 rounded-md shadow text-gray-700 dark:text-neutral-300 transition w-full sm:w-[250px] sm:mr-8 mt-8 sm:mt-0 order-last sm:order-first">Disconnect</button>
Expand Down