Skip to content

Commit

Permalink
fix: app expiresAt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Nov 3, 2023
1 parent 53b7182 commit 6a0c869
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
requestMethods = append(requestMethods, nip47MethodDescriptions[appPerm.RequestMethod])
}

expiresAtFormatted := expiresAt.Format("January 2, 2006 03:04 PM")

renewsIn := ""
budgetUsage := int64(0)
maxAmount := paySpecificPermission.MaxAmount
Expand All @@ -201,6 +203,7 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
"PaySpecificPermission": paySpecificPermission,
"RequestMethods": requestMethods,
"ExpiresAt": expiresAt,
"ExpiresAtFormatted": expiresAtFormatted,
"User": user,
"LastEvent": lastEvent,
"EventsCount": eventsCount,
Expand Down Expand Up @@ -244,8 +247,11 @@ 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) // FIXME: should display like "January 2, 2006 03:04 PM" in the UI
expiresAt = time.Unix(int64(expiresAtTimestamp), 0).Format(time.RFC3339)
}
expiresAtISO, _ := time.Parse(time.RFC3339, expiresAt)
expiresAtFormatted := expiresAtISO.Format("January 2, 2006 03:04 PM")

requestMethods := c.QueryParam("request_methods")
customRequestMethods := requestMethods
if requestMethods == "" {
Expand Down Expand Up @@ -305,6 +311,7 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
"MaxAmount": maxAmount,
"BudgetRenewal": budgetRenewal,
"ExpiresAt": expiresAt,
"ExpiresAtFormatted": expiresAtFormatted,
"RequestMethods": requestMethods,
"CustomRequestMethods": customRequestMethods,
"RequestMethodHelper": requestMethodHelper,
Expand Down Expand Up @@ -339,7 +346,11 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
app := App{Name: name, NostrPubkey: pairingPublicKey}
maxAmount, _ := strconv.Atoi(c.FormValue("MaxAmount"))
budgetRenewal := c.FormValue("BudgetRenewal")
expiresAt, _ := time.Parse("2006-01-02", c.FormValue("ExpiresAt"))
expiresAt, err := time.Parse(time.RFC3339, c.FormValue("ExpiresAt"))
if (err != nil) {
return fmt.Errorf("Invalid ExpiresAt: %v", err)
}

if !expiresAt.IsZero() {
expiresAt = time.Date(expiresAt.Year(), expiresAt.Month(), expiresAt.Day(), 23, 59, 59, 0, expiresAt.Location())
}
Expand Down
6 changes: 3 additions & 3 deletions views/apps/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ <h2 class="font-bold text-2xl font-headline mb-4 dark:text-white">
<div data-days="7" class="cursor-pointer rounded border-2 border-gray-200 dark:border-gray-400 text-center py-4">1 week</div>
<div data-days="30" class="cursor-pointer rounded border-2 border-gray-200 dark:border-gray-400 text-center py-4">1 month</div>
<div data-days="365" class="cursor-pointer rounded border-2 border-gray-200 dark:border-gray-400 text-center py-4">1 year</div>
<div data-days="0" class="cursor-pointer rounded border-2 border-gray-200 dark:border-gray-400 text-center py-4 border-purple-700 dark:border-purple-300 text-purple-700 dark:text-white bg-purple-100 dark:bg-purple-900">Never</div>
<div data-days="0" class="cursor-pointer rounded border-2 text-center py-4 border-purple-700 dark:border-purple-300 text-purple-700 dark:text-white bg-purple-100 dark:bg-purple-900">Never</div>
</div>
</div>
{{else}}
<p class="text-lg font-medium mb-2">Connection expiry time</p>
<p class="text-gray-600 dark:text-gray-300 text-sm">{{ .ExpiresAt }}</p>
<p class="text-gray-600 dark:text-gray-300 text-sm">{{ .ExpiresAtFormatted }}</p>
{{end}}

{{if .User.Email}}
Expand Down Expand Up @@ -216,7 +216,7 @@ <h2 class="font-bold text-2xl font-headline mb-4 dark:text-white">
expiresAt.value = "";
} else {
let expiryDate = new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
expiresAt.value = expiryDate.toISOString().split('T')[0];
expiresAt.value = expiryDate.toISOString();
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions views/apps/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ <h2 class="font-bold text-2xl font-headline dark:text-white">{{.App.Name}}</h2>
<tr>
<td class="align-top font-medium dark:text-white">Expires at</td>
<td class="text-gray-600 dark:text-neutral-400">
{{ if not .ExpiresAt.IsZero}}
{{.ExpiresAt}}
{{ if not .ExpiresAt.IsZero}}
{{.ExpiresAtFormatted}}
{{else}}
never
{{end}}
Expand Down

0 comments on commit 6a0c869

Please sign in to comment.