Compare commits

..
Author SHA1 Message Date
callebtc 48c4bcb759 diffs 2022-02-26 10:48:20 +03:00
7 changed files with 62 additions and 78 deletions
-7
View File
@@ -17,7 +17,6 @@ var defaultTickerCoolDown = time.Second * 10
type ResettableFunctionTicker struct {
Ticker *time.Ticker
ResetChan chan struct{} // channel used to reset the ticker
StopChan chan struct{} // channel used to stop the ticker
duration time.Duration
Started bool
name string
@@ -45,7 +44,6 @@ func GetTicker(name string, option ...ResettableFunctionTickerOption) *Resettabl
func NewResettableFunctionTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker {
t := &ResettableFunctionTicker{
ResetChan: make(chan struct{}, 1),
StopChan: make(chan struct{}, 1),
name: name,
}
@@ -72,11 +70,6 @@ func (t *ResettableFunctionTicker) Do(f func()) {
case <-t.ResetChan:
// reset signal received. creating new ticker.
t.Ticker = time.NewTicker(t.duration)
case <-t.StopChan:
// reset signal received. creating new ticker.
t.Ticker = nil
tickerMap.Remove(t.name)
return
}
}
}()
-13
View File
@@ -3,10 +3,8 @@ package mutex
import (
"context"
"fmt"
"github.com/LightningTipBot/LightningTipBot/internal/runtime"
"net/http"
"sync"
"time"
"github.com/gorilla/mux"
@@ -120,14 +118,8 @@ func Lock(s string) {
} else {
m := &sync.Mutex{}
m.Lock()
tick := runtime.NewResettableFunctionTicker(s, runtime.WithDuration(time.Hour))
tick.Do(
func() {
Unlock(s)
})
// write into mutex map
mutexMapSync.Lock()
mutexMap.Set(fmt.Sprintf("ticker_%s", s), tick)
mutexMap.Set(s, m)
mutexMapSync.Unlock()
}
@@ -139,11 +131,6 @@ func Unlock(s string) {
mutexMapSync.Lock()
if m, ok := mutexMap.Get(s); ok {
mutexMap.Remove(s)
tickerKey := fmt.Sprintf("ticker_%s", s)
if tick, ok := mutexMap.Get(tickerKey); ok {
tick.(*runtime.ResettableFunctionTicker).StopChan <- struct{}{}
mutexMap.Remove(tickerKey)
}
m.(*sync.Mutex).Unlock()
log.Tracef("[Mutex] Unlocked %s", s)
} else {
+1 -1
View File
@@ -188,7 +188,7 @@ func (bot *TipBot) enterAmountHandler(ctx context.Context, m *tb.Message) (conte
if err != nil {
return ctx, err
}
LnurlWithdrawState := sn.(*LnurlWithdrawState)
LnurlWithdrawState := sn.(LnurlWithdrawState)
LnurlWithdrawState.Amount = amount * 1000 // mSat
// add result to persistent struct
runtime.IgnoreError(LnurlWithdrawState.Set(LnurlWithdrawState, bot.Bunt))
+10 -8
View File
@@ -37,17 +37,19 @@ type LnurlAuthState struct {
// lnurlPayHandler1 is invoked when the first lnurl response was a lnurlpay response
// at this point, the user hans't necessarily entered an amount yet
func (bot *TipBot) lnurlAuthHandler(ctx context.Context, m *tb.Message, authParams *LnurlAuthState) (context.Context, error) {
func (bot *TipBot) lnurlAuthHandler(ctx context.Context, m *tb.Message, authParams LnurlAuthState) (context.Context, error) {
user := LoadUser(ctx)
if user.Wallet == nil {
return ctx, errors.Create(errors.UserNoWalletError)
}
// object that holds all information about the send payment
id := fmt.Sprintf("lnurlauth-%d-%s", m.Sender.ID, RandStringRunes(5))
authParams.Base = storage.New(storage.ID(id))
authParams.From = user
authParams.LanguageCode = ctx.Value("publicLanguageCode").(string)
lnurlAuthState := &LnurlAuthState{
Base: storage.New(storage.ID(id)),
From: user,
LNURLAuthParams: authParams.LNURLAuthParams,
LanguageCode: ctx.Value("publicLanguageCode").(string),
}
// // // create inline buttons
btnAuth = paymentConfirmationMenu.Data(Translate(ctx, "loginButtonMessage"), "confirm_login", id)
btnCancelAuth = paymentConfirmationMenu.Data(Translate(ctx, "cancelButtonMessage"), "cancel_login", id)
@@ -57,15 +59,15 @@ func (bot *TipBot) lnurlAuthHandler(ctx context.Context, m *tb.Message, authPara
btnAuth,
btnCancelAuth),
)
authParams.Message = bot.trySendMessageEditable(m.Chat,
lnurlAuthState.Message = bot.trySendMessageEditable(m.Chat,
fmt.Sprintf(Translate(ctx, "confirmLnurlAuthMessager"),
authParams.LNURLAuthParams.CallbackURL.Host,
lnurlAuthState.LNURLAuthParams.CallbackURL.Host,
),
paymentConfirmationMenu,
)
// save to bunt
runtime.IgnoreError(authParams.Set(authParams, bot.Bunt))
runtime.IgnoreError(lnurlAuthState.Set(lnurlAuthState, bot.Bunt))
return ctx, nil
}
+25 -24
View File
@@ -34,16 +34,19 @@ type LnurlPayState struct {
// lnurlPayHandler1 is invoked when the first lnurl response was a lnurlpay response
// at this point, the user hans't necessarily entered an amount yet
func (bot *TipBot) lnurlPayHandler(ctx context.Context, m *tb.Message, payParams *LnurlPayState) {
func (bot *TipBot) lnurlPayHandler(ctx context.Context, m *tb.Message, payParams LnurlPayState) (context.Context, error) {
user := LoadUser(ctx)
if user.Wallet == nil {
return
return ctx, errors.Create(errors.UserNoWalletError)
}
// object that holds all information about the send payment
id := fmt.Sprintf("lnurlp-%d-%s", m.Sender.ID, RandStringRunes(5))
payParams.Base = storage.New(storage.ID(id))
payParams.From = user
payParams.LanguageCode = ctx.Value("publicLanguageCode").(string)
id := fmt.Sprintf("lnurlp:%d:%s", m.Sender.ID, RandStringRunes(5))
lnurlPayState := &LnurlPayState{
Base: storage.New(storage.ID(id)),
From: user,
LNURLPayParams: payParams.LNURLPayParams,
LanguageCode: ctx.Value("publicLanguageCode").(string),
}
// first we check whether an amount is present in the command
amount, amount_err := decodeAmountFromCommand(m.Text)
@@ -58,51 +61,49 @@ func (bot *TipBot) lnurlPayHandler(ctx context.Context, m *tb.Message, payParams
// check if memo is present in command
memo := GetMemoFromCommand(m.Text, memoStartsAt)
// shorten memo to allowed length
if len(memo) > int(payParams.LNURLPayParams.CommentAllowed) {
memo = memo[:payParams.LNURLPayParams.CommentAllowed]
if len(memo) > int(lnurlPayState.LNURLPayParams.CommentAllowed) {
memo = memo[:lnurlPayState.LNURLPayParams.CommentAllowed]
}
if len(memo) > 0 {
payParams.Comment = memo
lnurlPayState.Comment = memo
}
// amount is already present in the command, i.e., /lnurl <amount> <LNURL>
// amount not in allowed range from LNURL
if amount_err == nil &&
(int64(amount) > (payParams.LNURLPayParams.MaxSendable/1000) || int64(amount) < (payParams.LNURLPayParams.MinSendable/1000)) &&
(payParams.LNURLPayParams.MaxSendable != 0 && payParams.LNURLPayParams.MinSendable != 0) { // only if max and min are set
(int64(amount) > (lnurlPayState.LNURLPayParams.MaxSendable/1000) || int64(amount) < (lnurlPayState.LNURLPayParams.MinSendable/1000)) &&
(lnurlPayState.LNURLPayParams.MaxSendable != 0 && lnurlPayState.LNURLPayParams.MinSendable != 0) { // only if max and min are set
err := fmt.Errorf("amount not in range")
log.Warnf("[lnurlPayHandler] Error: %s", err.Error())
bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "lnurlInvalidAmountRangeMessage"), payParams.LNURLPayParams.MinSendable/1000, payParams.LNURLPayParams.MaxSendable/1000))
bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "lnurlInvalidAmountRangeMessage"), lnurlPayState.LNURLPayParams.MinSendable/1000, lnurlPayState.LNURLPayParams.MaxSendable/1000))
ResetUserState(user, bot)
return
return ctx, err
}
// set also amount in the state of the user
payParams.Amount = amount * 1000 // save as mSat
lnurlPayState.Amount = amount * 1000 // save as mSat
// add result to persistent struct
runtime.IgnoreError(payParams.Set(payParams, bot.Bunt))
runtime.IgnoreError(lnurlPayState.Set(lnurlPayState, bot.Bunt))
// now we actualy check whether the amount was in the command and if not, ask for it
if payParams.LNURLPayParams.MinSendable == payParams.LNURLPayParams.MaxSendable {
amount = payParams.LNURLPayParams.MaxSendable / 1000
payParams.Amount = amount * 1000 // save as mSat
if lnurlPayState.LNURLPayParams.MinSendable == lnurlPayState.LNURLPayParams.MaxSendable {
lnurlPayState.Amount = lnurlPayState.LNURLPayParams.MaxSendable // save as mSat
} else if amount_err != nil || amount < 1 {
// // no amount was entered, set user state and ask for amount
bot.askForAmount(ctx, id, "LnurlPayState", payParams.LNURLPayParams.MinSendable, payParams.LNURLPayParams.MaxSendable, m.Text)
return
bot.askForAmount(ctx, id, "LnurlPayState", lnurlPayState.LNURLPayParams.MinSendable, lnurlPayState.LNURLPayParams.MaxSendable, m.Text)
return ctx, nil
}
// We need to save the pay state in the user state so we can load the payment in the next handler
paramsJson, err := json.Marshal(payParams)
paramsJson, err := json.Marshal(lnurlPayState)
if err != nil {
log.Errorf("[lnurlPayHandler] Error: %s", err.Error())
// bot.trySendMessage(m.Sender, err.Error())
return
return ctx, err
}
SetUserState(user, bot, lnbits.UserHasEnteredAmount, string(paramsJson))
// directly go to confirm
bot.lnurlPayHandlerSend(ctx, m)
return
return bot.lnurlPayHandlerSend(ctx, m)
}
// lnurlPayHandlerSend is invoked when the user has delivered an amount and is ready to pay
+22 -21
View File
@@ -58,17 +58,19 @@ func (bot *TipBot) editSingleButton(ctx context.Context, m *tb.Message, message
// lnurlWithdrawHandler is invoked when the first lnurl response was a lnurl-withdraw response
// at this point, the user hans't necessarily entered an amount yet
func (bot *TipBot) lnurlWithdrawHandler(ctx context.Context, m *tb.Message, withdrawParams *LnurlWithdrawState) {
func (bot *TipBot) lnurlWithdrawHandler(ctx context.Context, m *tb.Message, withdrawParams LnurlWithdrawState) (context.Context, error) {
user := LoadUser(ctx)
if user.Wallet == nil {
return
return ctx, errors.Create(errors.UserNoWalletError)
}
// object that holds all information about the send payment
id := fmt.Sprintf("lnurlw-%d-%s", m.Sender.ID, RandStringRunes(5))
withdrawParams.Base = storage.New(storage.ID(id))
withdrawParams.From = user
withdrawParams.LanguageCode = ctx.Value("publicLanguageCode").(string)
id := fmt.Sprintf("lnurlw:%d:%s", m.Sender.ID, RandStringRunes(5))
LnurlWithdrawState := LnurlWithdrawState{
Base: storage.New(storage.ID(id)),
From: user,
LNURLWithdrawResponse: withdrawParams.LNURLWithdrawResponse,
LanguageCode: ctx.Value("publicLanguageCode").(string),
}
// first we check whether an amount is present in the command
amount, amount_err := decodeAmountFromCommand(m.Text)
@@ -76,45 +78,44 @@ func (bot *TipBot) lnurlWithdrawHandler(ctx context.Context, m *tb.Message, with
// amount is already present in the command, i.e., /lnurl <amount> <LNURL>
// amount not in allowed range from LNURL
if amount_err == nil &&
(int64(amount) > (withdrawParams.LNURLWithdrawResponse.MaxWithdrawable/1000) || int64(amount) < (withdrawParams.LNURLWithdrawResponse.MinWithdrawable/1000)) &&
(withdrawParams.LNURLWithdrawResponse.MaxWithdrawable != 0 && withdrawParams.LNURLWithdrawResponse.MinWithdrawable != 0) { // only if max and min are set
(int64(amount) > (LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable/1000) || int64(amount) < (LnurlWithdrawState.LNURLWithdrawResponse.MinWithdrawable/1000)) &&
(LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable != 0 && LnurlWithdrawState.LNURLWithdrawResponse.MinWithdrawable != 0) { // only if max and min are set
err := fmt.Errorf("amount not in range")
log.Warnf("[lnurlWithdrawHandler] Error: %s", err.Error())
bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "lnurlInvalidAmountRangeMessage"), withdrawParams.LNURLWithdrawResponse.MinWithdrawable/1000, withdrawParams.LNURLWithdrawResponse.MaxWithdrawable/1000))
bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "lnurlInvalidAmountRangeMessage"), LnurlWithdrawState.LNURLWithdrawResponse.MinWithdrawable/1000, LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable/1000))
ResetUserState(user, bot)
return
return ctx, err
}
// if no amount is entered, and if only one amount is possible, we use it
if amount_err != nil && withdrawParams.LNURLWithdrawResponse.MaxWithdrawable == withdrawParams.LNURLWithdrawResponse.MinWithdrawable {
amount = int64(withdrawParams.LNURLWithdrawResponse.MaxWithdrawable / 1000)
if amount_err != nil && LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable == LnurlWithdrawState.LNURLWithdrawResponse.MinWithdrawable {
amount = int64(LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable / 1000)
amount_err = nil
}
// set also amount in the state of the user
withdrawParams.Amount = amount * 1000 // save as mSat
LnurlWithdrawState.Amount = amount * 1000 // save as mSat
// add result to persistent struct
runtime.IgnoreError(withdrawParams.Set(withdrawParams, bot.Bunt))
runtime.IgnoreError(LnurlWithdrawState.Set(LnurlWithdrawState, bot.Bunt))
// now we actualy check whether the amount was in the command and if not, ask for it
if amount_err != nil || amount < 1 {
// // no amount was entered, set user state and ask for amount
bot.askForAmount(ctx, id, "LnurlWithdrawState", withdrawParams.LNURLWithdrawResponse.MinWithdrawable, withdrawParams.LNURLWithdrawResponse.MaxWithdrawable, m.Text)
return
bot.askForAmount(ctx, id, "LnurlWithdrawState", LnurlWithdrawState.LNURLWithdrawResponse.MinWithdrawable, LnurlWithdrawState.LNURLWithdrawResponse.MaxWithdrawable, m.Text)
return ctx, nil
}
// We need to save the pay state in the user state so we can load the payment in the next handler
paramsJson, err := json.Marshal(withdrawParams)
paramsJson, err := json.Marshal(LnurlWithdrawState)
if err != nil {
log.Errorf("[lnurlWithdrawHandler] Error: %s", err.Error())
// bot.trySendMessage(m.Sender, err.Error())
return
return ctx, err
}
SetUserState(user, bot, lnbits.UserHasEnteredAmount, string(paramsJson))
// directly go to confirm
bot.lnurlWithdrawHandlerWithdraw(ctx, m)
return
return bot.lnurlWithdrawHandlerWithdraw(ctx, m)
}
// lnurlWithdrawHandlerWithdraw is invoked when the user has delivered an amount and is ready to pay
+4 -4
View File
@@ -89,7 +89,7 @@ func (bot *TipBot) lnurlHandler(ctx context.Context, m *tb.Message) (context.Con
authParams := &LnurlAuthState{LNURLAuthParams: params.(lnurl.LNURLAuthParams)}
log.Infof("[LNURL-auth] %s", authParams.LNURLAuthParams.Callback)
bot.tryDeleteMessage(statusMsg)
return bot.lnurlAuthHandler(ctx, m, authParams)
return bot.lnurlAuthHandler(ctx, m, *authParams)
case lnurl.LNURLPayParams:
payParams := &LnurlPayState{LNURLPayParams: params.(lnurl.LNURLPayParams)}
@@ -107,13 +107,13 @@ func (bot *TipBot) lnurlHandler(ctx context.Context, m *tb.Message) (context.Con
bot.trySendMessage(m.Sender, fmt.Sprintf("`%s`", payParams.LNURLPayParams.Metadata.Description))
}
// ask whether to make payment
bot.lnurlPayHandler(ctx, m, payParams)
return bot.lnurlPayHandler(ctx, m, *payParams)
case lnurl.LNURLWithdrawResponse:
withdrawParams := &LnurlWithdrawState{LNURLWithdrawResponse: params.(lnurl.LNURLWithdrawResponse)}
withdrawParams := LnurlWithdrawState{LNURLWithdrawResponse: params.(lnurl.LNURLWithdrawResponse)}
log.Infof("[LNURL-w] %s", withdrawParams.LNURLWithdrawResponse.Callback)
bot.tryDeleteMessage(statusMsg)
bot.lnurlWithdrawHandler(ctx, m, withdrawParams)
return bot.lnurlWithdrawHandler(ctx, m, withdrawParams)
default:
if err == nil {
err = fmt.Errorf("invalid LNURL type")