Skip to content

Commit

Permalink
fix: admincache and connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Divkix committed Aug 4, 2024
1 parent e8aaa9b commit c7166b7
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 37 deletions.
4 changes: 2 additions & 2 deletions alita/modules/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (m moduleStruct) adminlist(b *gotgbot.Bot, ctx *ext.Context) error {

adminsAvail, admins := cache.GetAdminCacheList(chat.Id)
if !adminsAvail {
admins = cache.LoadAdminCache(b, chat)
admins = cache.LoadAdminCache(b, chat.Id)
cached = false
}

Expand Down Expand Up @@ -594,7 +594,7 @@ func (moduleStruct) adminCache(b *gotgbot.Bot, ctx *ext.Context) error {
return ext.EndGroups
}

cache.LoadAdminCache(b, chat)
cache.LoadAdminCache(b, chat.Id)

k := tr.GetString("strings.CommonStrings.admin_cache.cache_reloaded")
debug_bot.PrettyPrintStruct(k)
Expand Down
2 changes: 1 addition & 1 deletion alita/modules/bot_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func adminCacheAutoUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
adminsAvail, _ := cache.GetAdminCacheList(chat.Id)

if !adminsAvail {
cache.LoadAdminCache(b, chat)
cache.LoadAdminCache(b, chat.Id)
log.Info(fmt.Sprintf("Reloaded admin cache for %d (%s)", chat.Id, chat.Title))
}

Expand Down
5 changes: 4 additions & 1 deletion alita/modules/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ func (m moduleStruct) reconnect(b *gotgbot.Bot, ctx *ext.Context) error {
return err
}

if !chat_status.IsUserInChat(b, gchat.PersonalChat, user.Id) {
// need to convert to chat type
_chat := gchat.ToChat()

if !chat_status.IsUserInChat(b, &_chat, user.Id) {
return ext.EndGroups
}

Expand Down
5 changes: 4 additions & 1 deletion alita/modules/devs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (moduleStruct) chatInfo(b *gotgbot.Bot, ctx *ext.Context) error {
_, _ = msg.Reply(b, err.Error(), nil)
return ext.EndGroups
}
con, _ := chat.PersonalChat.GetMemberCount(b, nil)
// need to convert chat to group chat to use GetMemberCount
_chat := chat.ToChat()
gChat := &_chat
con, _ := gChat.GetMemberCount(b, nil)
replyText = fmt.Sprintf("<b>Name:</b> %s\n<b>Chat ID</b>: %d\n<b>Users Count:</b> %d\n<b>Link:</b> %s", chat.Title, chat.Id, con, chat.InviteLink)
}

Expand Down
3 changes: 2 additions & 1 deletion alita/modules/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ func startHelpPrefixHandler(b *gotgbot.Bot, ctx *ext.Context, user *gotgbot.User
return ext.ContinueGroups
}
}
_, err := helpers.SendNote(b, chatinfo.PersonalChat, ctx, noteData, msg.MessageId)
_chat := chatinfo.ToChat() // need to convert to chat
_, err := helpers.SendNote(b, &_chat, ctx, noteData, msg.MessageId)
if err != nil {
log.Error(err)
return err
Expand Down
2 changes: 1 addition & 1 deletion alita/modules/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (moduleStruct) report(b *gotgbot.Bot, ctx *ext.Context) error {

adminsAvail, admins := cache.GetAdminCacheList(chat.Id)
if !adminsAvail {
admins = cache.LoadAdminCache(b, chat)
admins = cache.LoadAdminCache(b, chat.Id)
}

for i := range admins.UserInfo {
Expand Down
26 changes: 17 additions & 9 deletions alita/utils/cache/adminCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import (
)

// LoadAdminCache loads the admin cache for the chat.
func LoadAdminCache(b *gotgbot.Bot, chat *gotgbot.Chat) AdminCache {
func LoadAdminCache(b *gotgbot.Bot, chatId int64) AdminCache {
if b == nil {
log.Error("LoadAdminCache: bot is nil")
return AdminCache{}
}
if chat == nil {
log.Error("LoadAdminCache: chat is nil")
return AdminCache{}
}

adminList, err := chat.GetAdministrators(b, nil)
adminList, err := b.GetChatAdministrators(chatId, nil)
if err != nil {
log.Error(err)
return AdminCache{}
Expand All @@ -33,10 +29,10 @@ func LoadAdminCache(b *gotgbot.Bot, chat *gotgbot.Chat) AdminCache {
err = Marshal.Set(
Context,
AdminCache{
ChatId: chat.Id,
ChatId: chatId,
},
AdminCache{
ChatId: chat.Id,
ChatId: chatId,
UserInfo: userList,
Cached: true,
},
Expand All @@ -47,7 +43,7 @@ func LoadAdminCache(b *gotgbot.Bot, chat *gotgbot.Chat) AdminCache {
return AdminCache{}
}

_, newAdminList := GetAdminCacheList(chat.Id)
_, newAdminList := GetAdminCacheList(chatId)
return newAdminList
}

Expand All @@ -69,3 +65,15 @@ func GetAdminCacheList(chatId int64) (bool, AdminCache) {
}
return true, *gotAdminlist.(*AdminCache)
}

// GetAdminCacheUser gets the admin cache for the chat.
func GetAdminCacheUser(chatId, userId int64) (bool, gotgbot.MergedChatMember) {
adminList, _ := Marshal.Get(Context, AdminCache{ChatId: chatId}, new(AdminCache))
for i := range adminList.(*AdminCache).UserInfo {
admin := &adminList.(*AdminCache).UserInfo[i]
if admin.User.Id == userId {
return true, *admin
}
}
return false, gotgbot.MergedChatMember{}
}
12 changes: 0 additions & 12 deletions alita/utils/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,3 @@ func InitCache() {
// Initializes marshaler
Marshal = marshaler.New(cacheManager)
}

// GetAdminCacheUser gets the admin cache for the chat.
func GetAdminCacheUser(chatId, userId int64) (bool, gotgbot.MergedChatMember) {
adminList, _ := Marshal.Get(Context, AdminCache{ChatId: chatId}, new(AdminCache))
for i := range adminList.(*AdminCache).UserInfo {
admin := &adminList.(*AdminCache).UserInfo[i]
if admin.User.Id == userId {
return true, *admin
}
}
return false, gotgbot.MergedChatMember{}
}
6 changes: 3 additions & 3 deletions alita/utils/chat_status/chat_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func IsUserAdmin(b *gotgbot.Bot, chatID, userId int64) bool {

adminlist := make([]int64, 0)

adminsAvail, admins := cache.GetAdminCacheList(chat.Id)
adminsAvail, admins := cache.GetAdminCacheList(chatID)
if !adminsAvail {
admins = cache.LoadAdminCache(b, chat.PersonalChat)
admins = cache.LoadAdminCache(b, chatID)
}

if !admins.Cached {
adminList, err := chat.PersonalChat.GetAdministrators(b, nil)
adminList, err := b.GetChatAdministrators(chatID, nil)
if err != nil {
log.Error(err)
return false
Expand Down
3 changes: 2 additions & 1 deletion alita/utils/extraction/extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func ExtractChat(b *gotgbot.Bot, ctx *ext.Context) *gotgbot.Chat {
}
return nil
}
return chat.PersonalChat
_chat := chat.ToChat() // need to convert to Chat type
return &_chat
} else {
chat, err := chat_status.GetChat(b, args[0])
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions alita/utils/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func IsUserConnected(b *gotgbot.Bot, ctx *ext.Context, chatAdmin, botAdmin bool)
log.Error(err)
return nil
}
chat = chatFullInfo.PersonalChat
_chat := chatFullInfo.ToChat() // need to convert to Chat type
chat = &_chat
} else {
_, err := msg.Reply(b,
tr.GetString("strings.Connections.is_user_connected.need_group"),
Expand Down Expand Up @@ -243,7 +244,7 @@ func ConvertButtonV2ToDbButton(buttons []tgmd2html.ButtonV2) (btns []db.Button)
for i, btn := range buttons {
btns[i] = db.Button{
Name: btn.Name,
Url: btn.Text,
Url: btn.Content,
SameLine: btn.SameLine,
}
}
Expand Down Expand Up @@ -283,7 +284,7 @@ func InlineKeyboardMarkupToTgmd2htmlButtonV2(replyMarkup *gotgbot.InlineKeyboard
btns,
tgmd2html.ButtonV2{
Name: button.Text,
Text: button.Url,
Content: button.Url,
SameLine: sameline,
},
)
Expand All @@ -292,7 +293,7 @@ func InlineKeyboardMarkupToTgmd2htmlButtonV2(replyMarkup *gotgbot.InlineKeyboard
btns = append(btns,
tgmd2html.ButtonV2{
Name: inlineKeyboard[0].Text,
Text: inlineKeyboard[0].Url,
Content: inlineKeyboard[0].Url,
SameLine: false,
},
)
Expand Down Expand Up @@ -1295,7 +1296,7 @@ func preFixes(buttons []tgmd2html.ButtonV2, defaultNameButton string, text *stri
buttonUrlPattern, _ := regexp.Compile(`[(htps)?:/w.a-zA-Z\d@%_+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z\d@:%_+.~#?&/=]*)`)
buttons = *_buttons
for i, btn := range *_buttons {
if !buttonUrlPattern.MatchString(btn.Text) {
if !buttonUrlPattern.MatchString(btn.Content) {
buttons = append(buttons[:i], buttons[i+1:]...)
}
}
Expand Down

0 comments on commit c7166b7

Please sign in to comment.