-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathusers.go
153 lines (137 loc) · 3.91 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"errors"
"fmt"
"strings"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
"gorm.io/gorm"
)
func SetUserState(user *lnbits.User, bot TipBot, stateKey lnbits.UserStateKey, stateData string) {
user.StateKey = stateKey
user.StateData = stateData
err := UpdateUserRecord(user, bot)
if err != nil {
log.Errorln(err.Error())
return
}
}
func ResetUserState(user *lnbits.User, bot TipBot) {
user.ResetState()
err := UpdateUserRecord(user, bot)
if err != nil {
log.Errorln(err.Error())
return
}
}
var markdownV2Escapes = []string{"_", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
var markdownEscapes = []string{"_", "*", "`", "["}
func MarkdownV2Escape(s string) string {
for _, esc := range markdownV2Escapes {
if strings.Contains(s, esc) {
s = strings.Replace(s, esc, fmt.Sprintf("\\%s", esc), -1)
}
}
return s
}
func MarkdownEscape(s string) string {
for _, esc := range markdownEscapes {
if strings.Contains(s, esc) {
s = strings.Replace(s, esc, fmt.Sprintf("\\%s", esc), -1)
}
}
return s
}
func GetUserStr(user *tb.User) string {
userStr := fmt.Sprintf("@%s", user.Username)
// if user does not have a username
if len(userStr) < 2 && user.FirstName != "" {
userStr = fmt.Sprintf("%s", user.FirstName)
} else if len(userStr) < 2 {
userStr = fmt.Sprintf("%d", user.ID)
}
return userStr
}
func GetUserStrMd(user *tb.User) string {
userStr := fmt.Sprintf("@%s", user.Username)
// if user does not have a username
if len(userStr) < 2 && user.FirstName != "" {
userStr = fmt.Sprintf("[%s](tg://user?id=%d)", user.FirstName, user.ID)
return userStr
} else if len(userStr) < 2 {
userStr = fmt.Sprintf("[%d](tg://user?id=%d)", user.ID, user.ID)
return userStr
} else {
// escape only if user has a username
return MarkdownEscape(userStr)
}
}
func appendUinqueUsersToSlice(slice []*tb.User, i *tb.User) []*tb.User {
for _, ele := range slice {
if ele.ID == i.ID {
return slice
}
}
return append(slice, i)
}
func (bot *TipBot) UserInitializedWallet(user *tb.User) bool {
toUser, err := GetUser(user, *bot)
if err != nil {
return false
}
return toUser.Initialized
}
func (bot *TipBot) GetUserBalance(user *tb.User) (amount int, err error) {
// get user
fromUser, err := GetUser(user, *bot)
if err != nil {
return
}
wallet, err := fromUser.Wallet.Info(*fromUser.Wallet)
if err != nil {
errmsg := fmt.Sprintf("[GetUserBalance] Error: Couldn't fetch user %s's info from LNbits: %s", GetUserStr(user), err)
log.Errorln(errmsg)
return
}
fromUser.Wallet.Balance = wallet.Balance
err = UpdateUserRecord(fromUser, *bot)
if err != nil {
return
}
// msat to sat
amount = int(wallet.Balance) / 1000
log.Infof("[GetUserBalance] %s's balance: %d sat\n", GetUserStr(user), amount)
return
}
// copyLowercaseUser will create a coy user and cast username to lowercase.
func (bot *TipBot) copyLowercaseUser(u *tb.User) *tb.User {
userCopy := *u
userCopy.Username = strings.ToLower(u.Username)
return &userCopy
}
func (bot *TipBot) CreateWalletForTelegramUser(tbUser *tb.User) error {
userCopy := bot.copyLowercaseUser(tbUser)
user := &lnbits.User{Telegram: userCopy}
userStr := GetUserStr(tbUser)
log.Printf("[CreateWalletForTelegramUser] Creating wallet for user %s ... ", userStr)
err := bot.createWallet(user)
if err != nil {
errmsg := fmt.Sprintf("[CreateWalletForTelegramUser] Error: Could not create wallet for user %s", userStr)
log.Errorln(errmsg)
return err
}
tx := bot.database.Save(user)
if tx.Error != nil {
return tx.Error
}
log.Printf("[CreateWalletForTelegramUser] Wallet created for user %s. ", userStr)
return nil
}
func (bot *TipBot) UserExists(user *tb.User) (*lnbits.User, bool) {
lnbitUser, err := GetUser(user, *bot)
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
return nil, false
}
return lnbitUser, true
}