hdkv
/
i2_bot
Archived
1
Fork 0

Updating chat name in DataCache

master
Vladimir Hodakov 2018-02-17 16:01:05 +04:00
parent bf7eec791a
commit 2466a723be
5 changed files with 26 additions and 61 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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