From c7166b78ad62a1681907a8dfddea903edab9b478 Mon Sep 17 00:00:00 2001 From: Divanshu Chauhan Date: Sun, 4 Aug 2024 22:21:10 +0530 Subject: [PATCH] fix: admincache and connections --- alita/modules/admin.go | 4 ++-- alita/modules/bot_updates.go | 2 +- alita/modules/connections.go | 5 ++++- alita/modules/devs.go | 5 ++++- alita/modules/helpers.go | 3 ++- alita/modules/reports.go | 2 +- alita/utils/cache/adminCache.go | 26 +++++++++++++++++--------- alita/utils/cache/cache.go | 12 ------------ alita/utils/chat_status/chat_status.go | 6 +++--- alita/utils/extraction/extraction.go | 3 ++- alita/utils/helpers/helpers.go | 11 ++++++----- 11 files changed, 42 insertions(+), 37 deletions(-) diff --git a/alita/modules/admin.go b/alita/modules/admin.go index 25b3542b..000ae9b4 100644 --- a/alita/modules/admin.go +++ b/alita/modules/admin.go @@ -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 } @@ -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) diff --git a/alita/modules/bot_updates.go b/alita/modules/bot_updates.go index fed4830b..8ec202bb 100644 --- a/alita/modules/bot_updates.go +++ b/alita/modules/bot_updates.go @@ -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)) } diff --git a/alita/modules/connections.go b/alita/modules/connections.go index aa4ba847..c7c5ca5c 100644 --- a/alita/modules/connections.go +++ b/alita/modules/connections.go @@ -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 } diff --git a/alita/modules/devs.go b/alita/modules/devs.go index 3542d099..fbaa1059 100644 --- a/alita/modules/devs.go +++ b/alita/modules/devs.go @@ -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("Name: %s\nChat ID: %d\nUsers Count: %d\nLink: %s", chat.Title, chat.Id, con, chat.InviteLink) } diff --git a/alita/modules/helpers.go b/alita/modules/helpers.go index bce5b742..41d7d53a 100644 --- a/alita/modules/helpers.go +++ b/alita/modules/helpers.go @@ -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 diff --git a/alita/modules/reports.go b/alita/modules/reports.go index b131f06e..35bb7dd5 100644 --- a/alita/modules/reports.go +++ b/alita/modules/reports.go @@ -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 { diff --git a/alita/utils/cache/adminCache.go b/alita/utils/cache/adminCache.go index f9dfdeeb..ebcafb51 100644 --- a/alita/utils/cache/adminCache.go +++ b/alita/utils/cache/adminCache.go @@ -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{} @@ -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, }, @@ -47,7 +43,7 @@ func LoadAdminCache(b *gotgbot.Bot, chat *gotgbot.Chat) AdminCache { return AdminCache{} } - _, newAdminList := GetAdminCacheList(chat.Id) + _, newAdminList := GetAdminCacheList(chatId) return newAdminList } @@ -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{} +} diff --git a/alita/utils/cache/cache.go b/alita/utils/cache/cache.go index 165da859..e6758f3a 100644 --- a/alita/utils/cache/cache.go +++ b/alita/utils/cache/cache.go @@ -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{} -} diff --git a/alita/utils/chat_status/chat_status.go b/alita/utils/chat_status/chat_status.go index 9409958a..ac44b762 100644 --- a/alita/utils/chat_status/chat_status.go +++ b/alita/utils/chat_status/chat_status.go @@ -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 diff --git a/alita/utils/extraction/extraction.go b/alita/utils/extraction/extraction.go index 8b318460..1d3f5422 100644 --- a/alita/utils/extraction/extraction.go +++ b/alita/utils/extraction/extraction.go @@ -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 { diff --git a/alita/utils/helpers/helpers.go b/alita/utils/helpers/helpers.go index 825f46b5..4c22a8e4 100644 --- a/alita/utils/helpers/helpers.go +++ b/alita/utils/helpers/helpers.go @@ -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"), @@ -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, } } @@ -283,7 +284,7 @@ func InlineKeyboardMarkupToTgmd2htmlButtonV2(replyMarkup *gotgbot.InlineKeyboard btns, tgmd2html.ButtonV2{ Name: button.Text, - Text: button.Url, + Content: button.Url, SameLine: sameline, }, ) @@ -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, }, ) @@ -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:]...) } }