From 2466a723be9cd1a6839115fbb09cd13db9bee3e2 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sat, 17 Feb 2018 16:01:05 +0400 Subject: [PATCH] Updating chat name in DataCache --- .../chatterinterface/chatterinterface.go | 3 -- lib/chatter/updaters.go | 43 ------------------- lib/datacache/chats.go | 19 ++++++++ .../datacacheinterface/datacacheinterface.go | 1 + lib/router/group_request.go | 21 +++------ 5 files changed, 26 insertions(+), 61 deletions(-) delete mode 100644 lib/chatter/updaters.go diff --git a/lib/chatter/chatterinterface/chatterinterface.go b/lib/chatter/chatterinterface/chatterinterface.go index f4ff79f..f645f30 100644 --- a/lib/chatter/chatterinterface/chatterinterface.go +++ b/lib/chatter/chatterinterface/chatterinterface.go @@ -15,8 +15,5 @@ type ChatterInterface interface { BanUserFromChat(user *tgbotapi.User, chatRaw *dbmapping.Chat) ProtectChat(update *tgbotapi.Update, playerRaw *dbmapping.Player, chatRaw *dbmapping.Chat) string - UpdateChatTitle(chatRaw *dbmapping.Chat, newTitle string) (*dbmapping.Chat, bool) - UpdateChatTelegramID(update *tgbotapi.Update) (*dbmapping.Chat, bool) - GroupsList(update *tgbotapi.Update) string } diff --git a/lib/chatter/updaters.go b/lib/chatter/updaters.go deleted file mode 100644 index 9620afc..0000000 --- a/lib/chatter/updaters.go +++ /dev/null @@ -1,43 +0,0 @@ -// i2_bot – Instinct PokememBro Bot -// Copyright (c) 2017 Vladimir "fat0troll" Hodakov - -package chatter - -import ( - "github.com/go-telegram-bot-api/telegram-bot-api" - "source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping" -) - -// UpdateChatTitle updates chat title in database -func (ct *Chatter) UpdateChatTitle(chatRaw *dbmapping.Chat, newTitle string) (*dbmapping.Chat, bool) { - chatRaw.Name = newTitle - _, err := c.Db.NamedExec("UPDATE chats SET name=:name WHERE id=:id", &chatRaw) - if err != nil { - c.Log.Error(err) - return chatRaw, false - } - - return chatRaw, true -} - -// UpdateChatTelegramID updates chat's TelegramID when it converts to supergroup -func (ct *Chatter) UpdateChatTelegramID(update *tgbotapi.Update) (*dbmapping.Chat, bool) { - c.Log.Debug("Updating existing Telegram chat ID...") - chatRaw := dbmapping.Chat{} - err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), update.Message.MigrateFromChatID) - if err != nil { - c.Log.Error(err.Error()) - return &chatRaw, false - } - if update.Message.SuperGroupChatCreated { - chatRaw.ChatType = "supergroup" - } - chatRaw.TelegramID = update.Message.MigrateToChatID - _, err = c.Db.NamedExec("UPDATE chats SET chat_type=:chat_type, telegram_id=:telegram_id WHERE id=:id", &chatRaw) - if err != nil { - c.Log.Error(err.Error()) - return &chatRaw, false - } - - return &chatRaw, true -} diff --git a/lib/datacache/chats.go b/lib/datacache/chats.go index beb9396..4493f84 100644 --- a/lib/datacache/chats.go +++ b/lib/datacache/chats.go @@ -168,3 +168,22 @@ func (dc *DataCache) GetOrCreateChat(update *tgbotapi.Update) (*dbmapping.Chat, return &chatRaw, nil } + +// UpdateChatTitle updates chat title with new one +func (dc *DataCache) UpdateChatTitle(chatID int, newTitle string) (*dbmapping.Chat, error) { + chatRaw, err := c.DataCache.GetChatByID(chatID) + if err != nil { + return nil, err + } + chatRaw.Name = newTitle + _, err = c.Db.NamedExec("UPDATE chats SET name=:name WHERE id=:id", &chatRaw) + if err != nil { + return nil, err + } + + dc.chatsMutex.Lock() + dc.chats[chatRaw.ID] = chatRaw + dc.chatsMutex.Unlock() + + return dc.chats[chatRaw.ID], nil +} diff --git a/lib/datacache/datacacheinterface/datacacheinterface.go b/lib/datacache/datacacheinterface/datacacheinterface.go index db42457..aee154b 100644 --- a/lib/datacache/datacacheinterface/datacacheinterface.go +++ b/lib/datacache/datacacheinterface/datacacheinterface.go @@ -18,6 +18,7 @@ type DataCacheInterface interface { GetOrCreateChat(update *tgbotapi.Update) (*dbmapping.Chat, error) GetGroupChatsByIDs(chatIDs []int) []dbmapping.Chat GetLeaguePrivateChats() []dbmapping.Chat + UpdateChatTitle(chatID int, newTitle string) (*dbmapping.Chat, error) AddPlayerToSquad(relation *dbmapping.SquadPlayer) (int, error) GetAllSquadsChats() []dbmapping.Chat diff --git a/lib/router/group_request.go b/lib/router/group_request.go index cd456b0..a1b04ad 100644 --- a/lib/router/group_request.go +++ b/lib/router/group_request.go @@ -5,9 +5,9 @@ package router import ( "github.com/go-telegram-bot-api/telegram-bot-api" - "source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping" "math/rand" "regexp" + "source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping" ) func (r *Router) routeGroupRequest(update *tgbotapi.Update, playerRaw *dbmapping.Player, chatRaw *dbmapping.Chat) string { @@ -33,22 +33,13 @@ func (r *Router) routeGroupRequest(update *tgbotapi.Update, playerRaw *dbmapping } // New chat names if update.Message.NewChatTitle != "" { - _, ok := c.Chatter.UpdateChatTitle(chatRaw, update.Message.NewChatTitle) - if ok { - return "ok" + _, err := c.DataCache.UpdateChatTitle(chatRaw.ID, update.Message.NewChatTitle) + if err != nil { + c.Log.Error(err.Error()) + return "fail" } - return "fail" - } - - // New chat IDs (usually on supergroup creation) - if (update.Message.MigrateToChatID != 0) && (update.Message.MigrateFromChatID != 0) { - _, ok := c.Chatter.UpdateChatTelegramID(update) - if ok { - return "ok" - } - - return "fail" + return "ok" } // easter eggs