Skip to content

Commit

Permalink
fix: app expires at display
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Dec 26, 2023
1 parent af3fae3 commit 53eac43
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
28 changes: 16 additions & 12 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,20 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
})
}

lastEvent := NostrEvent{}
svc.db.Where("app_id = ?", app.ID).Order("id desc").Limit(1).Find(&lastEvent)
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{}
expiresAt := time.Time{}
var expiresAt int64
svc.db.Where("app_id = ?", app.ID).Find(&appPermissions)

requestMethods := []string{}
for _, appPerm := range appPermissions {
if expiresAt.IsZero() && !appPerm.ExpiresAt.IsZero() {
expiresAt = appPerm.ExpiresAt
if !appPerm.ExpiresAt.IsZero() {
expiresAt = appPerm.ExpiresAt.Unix()
}
if appPerm.RequestMethod == NIP_47_PAY_INVOICE_METHOD {
//find the pay_invoice-specific permissions
Expand All @@ -185,8 +185,6 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
requestMethods = append(requestMethods, appPerm.RequestMethod)
}

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

renewsIn := ""
budgetUsage := int64(0)
maxAmount := paySpecificPermission.MaxAmount
Expand All @@ -196,18 +194,24 @@ func (svc *Service) AppsShowHandler(c echo.Context) error {
renewsIn = getEndOfBudgetString(endOfBudget)
}

return c.JSON(http.StatusOK, ShowAppResponse{
response := ShowAppResponse{
App: app,
PaySpecificPermission: paySpecificPermission,
RequestMethods: requestMethods,
ExpiresAt: expiresAt.Unix(),
ExpiresAtFormatted: expiresAtFormatted,
LastEvent: lastEvent,
EventsCount: eventsCount,
BudgetUsage: budgetUsage,
RenewsIn: renewsIn,
Csrf: csrf,
})
}

if lastEventResult.RowsAffected > 0 {
response.LastEvent = &lastEvent
}
if expiresAt != 0 {
response.ExpiresAt = &expiresAt
}

return c.JSON(http.StatusOK, response)
}

func getEndOfBudgetString(endOfBudget time.Time) (result string) {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/screens/apps/Show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function Show() {
Expires at
</td>
<td className="text-gray-600 dark:text-neutral-400">
{appData.expiresAtFormatted || "never"}
{appData.expiresAt
? new Date(appData.expiresAt * 1000).toLocaleDateString()
: "never"}
</td>
</tr>
</table>
Expand Down
13 changes: 9 additions & 4 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,39 @@ var nip47MethodIcons = map[string]string{
NIP_47_LIST_TRANSACTIONS_METHOD: "transactions",
}

// TODO: move to models/API
type InfoResponse struct {
User *User `json:"user"`
BackendType string `json:"backendType"`
Csrf string `json:"csrf"`
}

// TODO: move to models/API
type CSRFResponse struct {
Csrf string `json:"csrf"`
}

// TODO: move to models/API
type ShowAppResponse struct {
App App `json:"app"`
BudgetUsage int64 `json:"budgetUsage"`
Csrf string `json:"csrf"`
EventsCount int64 `json:"eventsCount"`
ExpiresAt int64 `json:"expiresAt"`
ExpiresAtFormatted string `json:"expiresAtFormatted"`
LastEvent NostrEvent `json:"lastEvent"`
ExpiresAt *int64 `json:"expiresAt"`
LastEvent *NostrEvent `json:"lastEvent"`
PaySpecificPermission AppPermission `json:"paySpecificPermission"`
RenewsIn string `json:"renewsIn"`
RequestMethods []string `json:"requestMethods"`
}

// TODO: move to models/API
type ListAppsResponse struct {
Apps []App `json:"apps"`
Apps []App `json:"apps"`
// TODO: return just the last accessed date instead of the whole event?
LastEvents map[uint]*NostrEvent `json:"lastEvents"`
}

// TODO: move to models/API
type CreateAppResponse struct {
PairingUri string `json:"pairingUri"`
PairingSecret string `json:"pairingSecretKey"`
Expand Down

0 comments on commit 53eac43

Please sign in to comment.