Compare commits
19
Commits
api
...
fix_create_wallet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61c2b898fa | ||
|
|
8d28a3de35 | ||
|
|
e8e5ddb0b3 | ||
|
|
70cb0a1251 | ||
|
|
303e6d8c41 | ||
|
|
46f7942022 | ||
|
|
c7adbc7689 | ||
|
|
c702aa2268 | ||
|
|
31d29df021 | ||
|
|
1599fc9704 | ||
|
|
0ecbb8d58c | ||
|
|
2642965052 | ||
|
|
e46470b127 | ||
|
|
7a8cf08f47 | ||
|
|
c03e68666d | ||
|
|
95aa2a0c92 | ||
|
|
77c0652e8a | ||
|
|
f6bb0ca47f | ||
|
|
4b4e518609 |
@@ -1,6 +1,9 @@
|
||||
package lnbits
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tidwall/gjson"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req"
|
||||
@@ -40,8 +43,8 @@ func (c *Client) GetUser(userId string) (user User, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// CreateUserWithInitialWallet creates new user with initial wallet
|
||||
func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId string, email string) (wal User, err error) {
|
||||
// CreateUserWithWallet creates new user with wallet
|
||||
func (c *Client) CreateUserWithWallet(userName, walletName, adminId string, email string) (user User, err error) {
|
||||
resp, err := req.Post(c.url+"/usermanager/api/v1/users", c.header, req.BodyJSON(struct {
|
||||
WalletName string `json:"wallet_name"`
|
||||
AdminId string `json:"admin_id"`
|
||||
@@ -58,7 +61,14 @@ func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId strin
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
err = resp.ToJSON(&wal)
|
||||
err = resp.ToJSON(&user)
|
||||
res := gjson.Get(resp.String(), "wallets.0")
|
||||
if res.Index == 0 {
|
||||
err = fmt.Errorf("could not create initial wallet")
|
||||
return
|
||||
}
|
||||
user.Wallet = &Wallet{}
|
||||
err = json.Unmarshal([]byte(res.Raw), user.Wallet)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ type Client struct {
|
||||
InvoiceKey string
|
||||
}
|
||||
|
||||
// User is a bot user containing lnbits and telegram information.
|
||||
// This struct is used for orm persistence.
|
||||
// Users are created, by talking to the bot or by receiving funds (tips, receive, faucet)
|
||||
// The initialized flag is set true, if user already talked to the bot.
|
||||
// This is used for the '<- talk to this bot' message when sending tips to new users.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name" gorm:"primaryKey"`
|
||||
|
||||
@@ -74,9 +74,9 @@ func newTelegramBot() *tb.Bot {
|
||||
// todo -- may want to derive user wallets from this specific bot wallet (master wallet), since lnbits usermanager extension is able to do that.
|
||||
func (bot TipBot) initBotWallet() error {
|
||||
botWalletInitialisation.Do(func() {
|
||||
_, err := bot.initWallet(bot.Telegram.Me)
|
||||
_, err := bot.userWithInitWallet(bot.Telegram.Me)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initBotWallet] Could not initialize bot wallet: %s", err.Error()))
|
||||
log.Errorln(fmt.Sprintf("[initBotWallet] ❌ 🧨 ❌ Could not initialize bot wallet: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
+40
-60
@@ -4,20 +4,16 @@ import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/errors"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/errors"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/str"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
||||
log "github.com/sirupsen/logrus"
|
||||
tb "gopkg.in/lightningtipbot/telebot.v3"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error) {
|
||||
@@ -29,7 +25,7 @@ func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
// bot.helpHandler(m)
|
||||
log.Printf("[⭐️ /start] New user: %s (%d)\n", GetUserStr(ctx.Sender()), ctx.Sender().ID)
|
||||
walletCreationMsg := bot.trySendMessageEditable(ctx.Sender(), Translate(ctx, "startSettingWalletMessage"))
|
||||
user, err := bot.initWallet(ctx.Sender())
|
||||
user, err := bot.userWithInitWallet(ctx.Sender())
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[startHandler] Error with initWallet: %s", err.Error()))
|
||||
bot.tryEditMessage(walletCreationMsg, Translate(ctx, "startWalletErrorMessage"))
|
||||
@@ -48,74 +44,58 @@ func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (bot TipBot) initWallet(tguser *tb.User) (*lnbits.User, error) {
|
||||
// userWithInitWallet will ensure that lnbits.User is initialized.
|
||||
// Initialized users have already talked to the bot,
|
||||
// therefor they are able to receive messages from the bot.
|
||||
func (bot TipBot) userWithInitWallet(tguser *tb.User) (*lnbits.User, error) {
|
||||
user, err := GetUser(tguser, bot)
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user = &lnbits.User{Telegram: tguser}
|
||||
err = bot.createWallet(user)
|
||||
if err != nil {
|
||||
return user, err
|
||||
if err != nil {
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user, err = bot.createWallet(tguser)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
return user, fmt.Errorf("[initWallet] error updating user: %v", err)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
// set user initialized
|
||||
user, err := GetUser(tguser, bot)
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initWallet] error updating user: %s", err.Error()))
|
||||
return user, err
|
||||
}
|
||||
} else if !user.Initialized {
|
||||
return nil, err
|
||||
}
|
||||
if !user.Initialized {
|
||||
// update all tip tooltips (with the "initialize me" message) that this user might have received before
|
||||
tipTooltipInitializedHandler(user.Telegram, bot)
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initWallet] error updating user: %s", err.Error()))
|
||||
return user, err
|
||||
return user, fmt.Errorf("[initWallet] error updating user: %v", err)
|
||||
}
|
||||
} else if user.Initialized {
|
||||
// wallet is already initialized
|
||||
return user, nil
|
||||
} else {
|
||||
err = fmt.Errorf("could not initialize wallet")
|
||||
return user, err
|
||||
}
|
||||
// wallet is already initialized
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (bot TipBot) createWallet(user *lnbits.User) error {
|
||||
UserStr := GetUserStr(user.Telegram)
|
||||
u, err := bot.Client.CreateUserWithInitialWallet(strconv.FormatInt(user.Telegram.ID, 10),
|
||||
fmt.Sprintf("%d (%s)", user.Telegram.ID, UserStr),
|
||||
// createWallet will create a wallet for any telegram user.
|
||||
func (bot TipBot) createWallet(u *tb.User) (*lnbits.User, error) {
|
||||
userStr := GetUserStr(u)
|
||||
user, err := bot.Client.CreateUserWithWallet(strconv.FormatInt(u.ID, 10),
|
||||
fmt.Sprintf("%d (%s)", u.ID, userStr),
|
||||
internal.Configuration.Lnbits.AdminId,
|
||||
UserStr)
|
||||
userStr)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Create wallet error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
return nil, fmt.Errorf("[createWallet] Create wallet error: %v", err)
|
||||
}
|
||||
user.Wallet = &lnbits.Wallet{}
|
||||
user.ID = u.ID
|
||||
user.Name = u.Name
|
||||
wallet, err := bot.Client.Wallets(*user)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Get wallet error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
}
|
||||
user.Wallet = &wallet[0]
|
||||
|
||||
user.Telegram = u
|
||||
user.AnonID = fmt.Sprint(str.Int32Hash(user.ID))
|
||||
user.AnonIDSha256 = str.AnonIdSha256(user)
|
||||
user.UUID = str.UUIDSha256(user)
|
||||
user.AnonIDSha256 = str.AnonIdSha256(&user)
|
||||
user.UUID = str.UUIDSha256(&user)
|
||||
|
||||
user.Initialized = false
|
||||
user.CreatedAt = time.Now()
|
||||
err = UpdateUserRecord(user, bot)
|
||||
err = UpdateUserRecord(&user, bot)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Update user record error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
return nil, fmt.Errorf("[createWallet] Update user record error: %v", err)
|
||||
}
|
||||
return nil
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
@@ -100,20 +100,14 @@ func (bot *TipBot) CreateWalletForTelegramUser(tbUser *tb.User) (*lnbits.User, e
|
||||
if _, exists := bot.UserExists(tbUser); exists {
|
||||
return nil, fmt.Errorf("user already exists")
|
||||
}
|
||||
user := &lnbits.User{Telegram: tbUser}
|
||||
userStr := GetUserStr(tbUser)
|
||||
log.Printf("[CreateWalletForTelegramUser] Creating wallet for user %s ... ", userStr)
|
||||
err := bot.createWallet(user)
|
||||
user, err := bot.createWallet(tbUser)
|
||||
if err != nil {
|
||||
errmsg := fmt.Sprintf("[CreateWalletForTelegramUser] Error: Could not create wallet for user %s", userStr)
|
||||
log.Errorln(errmsg)
|
||||
return user, err
|
||||
}
|
||||
// todo: remove this. we're doing this already in bot.createWallet().
|
||||
err = UpdateUserRecord(user, *bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("[CreateWalletForTelegramUser] Wallet created for user %s. ", userStr)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user