Global code ovehaul - remove CAS and other, add 15 minute greet check
- Remove CAS, as it no longer works for us - Remove other checks - Implement 15 minute greet timeout check - Fix chat logos - Ditch thirdparty logger, use stdlib log facility wrapper for logging
This commit is contained in:
@@ -1,32 +1,47 @@
|
||||
package moon
|
||||
|
||||
import (
|
||||
"bunny-bot/internal/mylog"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/NicoNex/echotron/v3"
|
||||
)
|
||||
|
||||
// TODO: refactor to return bool, error
|
||||
|
||||
// squash bans given chat member.
|
||||
func squash(chatid int64, userid int64) {
|
||||
banUntilDate := time.Now().Unix() + 10
|
||||
|
||||
user := GetUser(userid)
|
||||
chat := GetChat(chatid)
|
||||
|
||||
// If there is no such chatid, means that no such cache too.
|
||||
if len(ChatList) == 0 || !slices.Contains(ChatList, chatid) {
|
||||
Log.Debugf("Add %s %s (%d) to list of known chats", chat.Type, chat.Title, chat.ID)
|
||||
|
||||
ChatList = append(ChatList, chatid)
|
||||
// squash bans the given chat member and reports what it actually did.
|
||||
//
|
||||
// The three outcomes are deliberately distinct. A debounced call is not a
|
||||
// failure: the user is already banned, which is the outcome we wanted, and the
|
||||
// caller must not report it as an error.
|
||||
//
|
||||
// The user and chat come from the join event that triggered the judgement
|
||||
// rather than from a cache. That event already carries both in full, so a cache
|
||||
// would only ever be a second, staler copy of data we are holding — and a miss
|
||||
// in it would produce a ban logged with blank names.
|
||||
func squash(chat *echotron.Chat, user *echotron.User) banOutcome {
|
||||
// A chat or user with no id is an entity we cannot address, not a ban that
|
||||
// failed. In production the ingress has already refused such an update, so
|
||||
// reaching here means a caller handed us one directly; the guard is the
|
||||
// nil-safety that keeps that from becoming a call against chat 0.
|
||||
if !usableChat(chat) {
|
||||
return banFailed
|
||||
}
|
||||
|
||||
if !usableUser(user) {
|
||||
return banFailed
|
||||
}
|
||||
|
||||
// Telegram treats any ban shorter than 30 seconds (and longer than 1 year)
|
||||
// as permanent, so this deliberately short offset produces a permanent ban.
|
||||
// Do not "fix" it into a longer duration.
|
||||
banUntilDate := time.Now().Unix() + 10
|
||||
|
||||
chatid, userid := chat.ID, user.ID
|
||||
|
||||
key := fmt.Sprintf("%d+%d", chatid, userid)
|
||||
|
||||
if _, exist := SquashedMembers.Get(key); exist {
|
||||
Log.Infof(
|
||||
mylog.Infof(
|
||||
"Skip banning user %s %s (username = %s, id = %d) in %s %s (%d), already banned",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
@@ -37,17 +52,18 @@ func squash(chatid int64, userid int64) {
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
return
|
||||
return banSuppressed
|
||||
}
|
||||
|
||||
if res, err := Tg.BanChatMember(
|
||||
res, err := Tg.BanChatMember(
|
||||
chatid,
|
||||
userid,
|
||||
&echotron.BanOptions{
|
||||
UntilDate: int(banUntilDate),
|
||||
},
|
||||
); err != nil {
|
||||
Log.Errorf(
|
||||
)
|
||||
if err != nil {
|
||||
mylog.Errorf(
|
||||
"Unable to ban user user %s %s (username = %s, id = %d) in %s %s (%d): %s",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
@@ -56,85 +72,53 @@ func squash(chatid int64, userid int64) {
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
err,
|
||||
)
|
||||
|
||||
return banFailed
|
||||
}
|
||||
|
||||
if !res.Ok {
|
||||
mylog.Errorf(
|
||||
"Unable to ban user %s %s (username = %s, id = %d) in %s %s (%d): %s",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
res.Description,
|
||||
)
|
||||
} else {
|
||||
if !res.Ok {
|
||||
Log.Errorf(
|
||||
"Unable to ban user %s %s (username = %s, id = %d) in %s %s (%d): %s",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
res.Description,
|
||||
)
|
||||
} else {
|
||||
Log.Debugf(
|
||||
"Banned user %s %s (username = %s, id = %d) in %s %s (%d).",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
key := fmt.Sprintf("%d+%d", chatid, userid)
|
||||
|
||||
Log.Debugf(
|
||||
"Put user %s %s (username = %s, id = %d) in %s %s (%d) into SquashedMembers to debounce ban.",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
SquashedMembers.Set(key, true, 30*time.Second)
|
||||
|
||||
Log.Debugf(
|
||||
"Remove user %s %s (username = %s, id = %d) in %s %s (%d) from NewMembers.",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
NewMembers.Delete(key)
|
||||
|
||||
Log.Debugf(
|
||||
"Remove user %s %s (username = %s, id = %d) in %s %s (%d) from AppearedMembers.",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
AppearedMembers.Delete(key)
|
||||
|
||||
if err := Cfg.SaveKeyValue(
|
||||
"BannedMembers",
|
||||
fmt.Sprintf("%d", chatid),
|
||||
fmt.Sprintf("%d", userid),
|
||||
fmt.Sprintf("%d", time.Now().Unix()),
|
||||
); err != nil {
|
||||
Log.Error(err)
|
||||
} else {
|
||||
Log.Infof("Info about banned userid %d saved to BannedMembers db", userid)
|
||||
}
|
||||
}
|
||||
return banFailed
|
||||
}
|
||||
|
||||
mylog.Debugf(
|
||||
"Banned user %s %s (username = %s, id = %d) in %s %s (%d).",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
mylog.Debugf(
|
||||
"Put user %s %s (username = %s, id = %d) in %s %s (%d) into SquashedMembers to debounce ban.",
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.Username,
|
||||
user.ID,
|
||||
chat.Type,
|
||||
chat.Title,
|
||||
chat.ID,
|
||||
)
|
||||
|
||||
SquashedMembers.Set(key, true, 30*time.Second)
|
||||
|
||||
return banApplied
|
||||
}
|
||||
|
||||
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||
|
||||
Reference in New Issue
Block a user