2017-10-21 14:14:46 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
2017-11-19 22:16:11 +04:00
|
|
|
|
package chatter
|
2017-10-21 14:14:46 +04:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
|
2017-11-26 08:31:56 +04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"time"
|
2017-10-21 14:14:46 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-11-19 22:16:11 +04:00
|
|
|
|
func (ct *Chatter) getAllGroupChatsWithSquads() ([]dbmapping.ChatSquad, bool) {
|
|
|
|
|
chatsSquads := []dbmapping.ChatSquad{}
|
|
|
|
|
groupChats := []dbmapping.Chat{}
|
|
|
|
|
|
|
|
|
|
err := c.Db.Select(&groupChats, "SELECT * FROM chats WHERE chat_type IN ('group', 'supergroup')")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err)
|
|
|
|
|
return chatsSquads, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range groupChats {
|
|
|
|
|
chatSquad := dbmapping.ChatSquad{}
|
|
|
|
|
squad := dbmapping.Squad{}
|
|
|
|
|
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE chat_id=?"), groupChats[i].ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Debug(err)
|
|
|
|
|
} else {
|
|
|
|
|
chatSquad.ChatRole = "squad"
|
|
|
|
|
}
|
|
|
|
|
err = c.Db.Get(&squad, c.Db.Rebind("SELECT * FROM squads WHERE flood_chat_id=?"), groupChats[i].ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Debug(err)
|
|
|
|
|
} else {
|
|
|
|
|
chatSquad.ChatRole = "flood"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatSquad.Squad = squad
|
|
|
|
|
chatSquad.Chat = groupChats[i]
|
|
|
|
|
|
|
|
|
|
chatsSquads = append(chatsSquads, chatSquad)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chatsSquads, true
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 14:14:46 +04:00
|
|
|
|
// GetChatByID returns dbmapping.Chat instance with given ID.
|
2017-11-19 22:16:11 +04:00
|
|
|
|
func (ct *Chatter) GetChatByID(chatID int64) (dbmapping.Chat, bool) {
|
2017-10-21 14:14:46 +04:00
|
|
|
|
chatRaw := dbmapping.Chat{}
|
|
|
|
|
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE id=?"), chatID)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-21 14:14:46 +04:00
|
|
|
|
return chatRaw, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chatRaw, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetOrCreateChat seeks for chat in database via Telegram update.
|
|
|
|
|
// In case, when there is no chat with such ID, new chat will be created.
|
2017-11-19 22:16:11 +04:00
|
|
|
|
func (ct *Chatter) GetOrCreateChat(telegramUpdate *tgbotapi.Update) (dbmapping.Chat, bool) {
|
2017-10-21 14:14:46 +04:00
|
|
|
|
chatRaw := dbmapping.Chat{}
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("TGID: ", telegramUpdate.Message.Chat.ID)
|
2017-10-21 14:14:46 +04:00
|
|
|
|
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), telegramUpdate.Message.Chat.ID)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Chat stream not found in database.")
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-21 14:14:46 +04:00
|
|
|
|
|
2017-11-03 21:33:02 +04:00
|
|
|
|
nameOfChat := ""
|
|
|
|
|
if telegramUpdate.Message.Chat.FirstName != "" {
|
|
|
|
|
nameOfChat += telegramUpdate.Message.Chat.FirstName
|
|
|
|
|
}
|
|
|
|
|
if telegramUpdate.Message.Chat.LastName != "" {
|
|
|
|
|
nameOfChat += " " + telegramUpdate.Message.Chat.LastName
|
|
|
|
|
}
|
|
|
|
|
if telegramUpdate.Message.Chat.Title != "" {
|
|
|
|
|
if nameOfChat != "" {
|
|
|
|
|
nameOfChat += " [" + telegramUpdate.Message.Chat.Title + "]"
|
|
|
|
|
} else {
|
|
|
|
|
nameOfChat = telegramUpdate.Message.Chat.Title
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatRaw.Name = nameOfChat
|
2017-10-21 14:14:46 +04:00
|
|
|
|
chatRaw.ChatType = telegramUpdate.Message.Chat.Type
|
2017-11-03 21:33:02 +04:00
|
|
|
|
chatRaw.TelegramID = telegramUpdate.Message.Chat.ID
|
2017-10-21 14:14:46 +04:00
|
|
|
|
chatRaw.CreatedAt = time.Now().UTC()
|
|
|
|
|
_, err = c.Db.NamedExec("INSERT INTO chats VALUES(NULL, :name, :chat_type, :telegram_id, :created_at)", &chatRaw)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-21 14:14:46 +04:00
|
|
|
|
return chatRaw, false
|
|
|
|
|
}
|
|
|
|
|
err2 := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=? AND chat_type=?"), chatRaw.TelegramID, chatRaw.ChatType)
|
|
|
|
|
if err2 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err2)
|
2017-10-21 14:14:46 +04:00
|
|
|
|
return chatRaw, false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Info("Chat stream found in database.")
|
2017-10-21 14:14:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chatRaw, true
|
|
|
|
|
}
|
2017-10-22 14:04:14 +04:00
|
|
|
|
|
|
|
|
|
// GetAllPrivateChats returns all private chats
|
2017-11-19 22:16:11 +04:00
|
|
|
|
func (ct *Chatter) GetAllPrivateChats() ([]dbmapping.Chat, bool) {
|
2017-10-22 14:04:14 +04:00
|
|
|
|
privateChats := []dbmapping.Chat{}
|
|
|
|
|
|
|
|
|
|
err := c.Db.Select(&privateChats, "SELECT * FROM chats WHERE chat_type='private'")
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-22 14:04:14 +04:00
|
|
|
|
return privateChats, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return privateChats, true
|
|
|
|
|
}
|
2017-11-03 21:33:02 +04:00
|
|
|
|
|
2017-11-25 03:00:34 +04:00
|
|
|
|
// GetLeaguePrivateChats returns all private chats which profiles are in our league
|
|
|
|
|
func (ct *Chatter) GetLeaguePrivateChats() ([]dbmapping.Chat, bool) {
|
|
|
|
|
privateChats := []dbmapping.Chat{}
|
|
|
|
|
|
|
|
|
|
err := c.Db.Select(&privateChats, "SELECT c.* FROM chats c, players p WHERE c.chat_type='private' AND p.telegram_id = c.telegram_id AND p.league_id = 1")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err)
|
|
|
|
|
return privateChats, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return privateChats, true
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 22:51:50 +04:00
|
|
|
|
// GetAllGroupChats returns all group chats
|
2017-11-19 22:16:11 +04:00
|
|
|
|
func (ct *Chatter) GetAllGroupChats() ([]dbmapping.Chat, bool) {
|
2017-11-13 22:51:50 +04:00
|
|
|
|
groupChats := []dbmapping.Chat{}
|
|
|
|
|
|
|
|
|
|
err := c.Db.Select(&groupChats, "SELECT * FROM chats WHERE chat_type IN ('group', 'supergroup')")
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-11-13 22:51:50 +04:00
|
|
|
|
return groupChats, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupChats, true
|
2017-11-25 03:00:34 +04:00
|
|
|
|
}
|
2017-11-26 08:31:56 +04:00
|
|
|
|
|
|
|
|
|
// GetGroupChatsByIDs returns group chats with selected IDs
|
|
|
|
|
func (ct *Chatter) GetGroupChatsByIDs(chatsIDs string) ([]dbmapping.Chat, bool) {
|
|
|
|
|
groupChats := []dbmapping.Chat{}
|
|
|
|
|
|
|
|
|
|
queryIDs := make([]int, 0)
|
|
|
|
|
queryIDsStr := strings.Split(chatsIDs, ",")
|
|
|
|
|
for i := range queryIDsStr {
|
|
|
|
|
id, _ := strconv.Atoi(queryIDsStr[i])
|
|
|
|
|
if id != 0 {
|
|
|
|
|
queryIDs = append(queryIDs, id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalQueryIDs := ""
|
|
|
|
|
for i := range queryIDs {
|
|
|
|
|
finalQueryIDs += strconv.Itoa(queryIDs[i])
|
|
|
|
|
if i < len(queryIDs)-1 {
|
|
|
|
|
finalQueryIDs += ","
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.Log.Debug("Chat query IDs: " + finalQueryIDs)
|
|
|
|
|
|
|
|
|
|
err := c.Db.Select(&groupChats, "SELECT * FROM chats WHERE chat_type IN ('group', 'supergroup') AND id IN ("+finalQueryIDs+")")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Error(err)
|
|
|
|
|
return groupChats, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupChats, true
|
|
|
|
|
}
|