-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add get_budget implementation #726
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a724bd1
Add get_budget implementation
jklein24 a647160
Always granted scope
jklein24 fcc2d22
Don't show always_granted in the UI or save it in the DB.
jklein24 52b64ab
chore: get budget feedback
rolznz 634e54d
fix: add omitempty to renews_at in get_budget response
rolznz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/getAlby/hub/db/queries" | ||
"github.com/nbd-wtf/go-nostr" | ||
|
||
"github.com/getAlby/hub/db" | ||
"github.com/getAlby/hub/logger" | ||
"github.com/getAlby/hub/nip47/models" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type getBudgetResponse struct { | ||
UsedBudget uint64 `json:"used_budget"` | ||
TotalBudget uint64 `json:"total_budget"` | ||
RenewsAt *uint64 `json:"renews_at,omitempty"` | ||
RenewalPeriod string `json:"renewal_period"` | ||
} | ||
|
||
func (controller *nip47Controller) HandleGetBudgetEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) { | ||
|
||
logger.Logger.WithFields(logrus.Fields{ | ||
"request_event_id": requestEventId, | ||
}).Debug("Getting budget") | ||
|
||
appPermission := db.AppPermission{} | ||
controller.db.Where("app_id = ? AND scope = ?", app.ID, models.PAY_INVOICE_METHOD).First(&appPermission) | ||
|
||
maxAmount := appPermission.MaxAmountSat | ||
if maxAmount == 0 { | ||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: struct{}{}, | ||
}, nostr.Tags{}) | ||
return | ||
} | ||
|
||
usedBudget := queries.GetBudgetUsageSat(controller.db, &appPermission) | ||
responsePayload := &getBudgetResponse{ | ||
TotalBudget: uint64(maxAmount * 1000), | ||
UsedBudget: usedBudget * 1000, | ||
RenewalPeriod: appPermission.BudgetRenewal, | ||
RenewsAt: queries.GetBudgetRenewsAt(appPermission.BudgetRenewal), | ||
} | ||
|
||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: responsePayload, | ||
}, nostr.Tags{}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually did not catch this in your proposal. I am not sure an empty result makes sense. Maybe the connection should not support this method and return an error instead if the app tries to call this method when a budget is not set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm that's a good alternative too. Maybe we should discuss on the NIP PR. There are some tradeoffs to think through for sure.