Skip to content

Commit

Permalink
now totally deletes command message, no bamboozle
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Nov 30, 2023
1 parent 67ba7f9 commit 48e2746
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
65 changes: 45 additions & 20 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ let isChannelMessage (message: Message) =
let isPingCommand (message: Message) =
message.Text = "/ban ping"

let isBanOnReplyMessage (message: Message) =
message.Text = "/ban" &&
let isBanCommand (message: Message) =
message.Text = "/ban"

let isBanOnReplyCommand (message: Message) =
isBanCommand message &&
message.ReplyToMessage <> null

let isMessageFromAllowedChats (botConfig: BotConfiguration) (message: Message) =
botConfig.ChatsToMonitor.ContainsValue message.Chat.Id

Expand All @@ -35,8 +38,8 @@ let isBannedPersonAdmin (botConfig: BotConfiguration) (message: Message) =

let isKnownCommand (message: Message) =
isPingCommand message ||
isBanOnReplyMessage message
isBanCommand message

let isBanAuthorized (botConfig: BotConfiguration) (message: Message) (logger: ILogger) =
let fromUserId = message.From.Id
let fromUsername = message.From.Username
Expand Down Expand Up @@ -118,15 +121,37 @@ let aggregateBanResultInLogMsg
)
|> string

let ping
(botClient: ITelegramBotClient)
(message: Message) = task {
use _ = botActivity.StartActivity("ping")
do! botClient.SendTextMessageAsync(ChatId(message.Chat.Id), "pong") |> taskIgnore
}

let deleteChannelMessage
(botClient: ITelegramBotClient)
(message: Message)
(logger: ILogger) = task {
use banOnReplyActivity = botActivity.StartActivity("deleteChannelMessage")
do! botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId)
|> safeTaskAwait (fun e -> logger.LogError ($"Failed to delete message {message.MessageId} from chat {message.Chat.Id}", e))

let probablyChannelName =
if message.SenderChat <> null then
message.SenderChat.Title
else
"[unknown]"
%banOnReplyActivity.SetTag("channelName", probablyChannelName)
logger.LogInformation $"Deleted message from channel {probablyChannelName}"
}

let banOnReply
(botClient: ITelegramBotClient)
(botConfig: BotConfiguration)
(message: Message)
(logger: ILogger) = task {
use banOnReplyActivity = botActivity.StartActivity("banOnReply")
%banOnReplyActivity
.SetTag("chatId", message.Chat.Id)
.SetTag("chatUsername", message.Chat.Username)
.SetTag("vahterId", message.From.Id)
.SetTag("vahterUsername", message.From.Username)

Expand All @@ -141,10 +166,11 @@ let banOnReply
|> DbUser.newUser
|> DbUser.banUser message.From.Id (Option.ofObj message.ReplyToMessage.Text)
|> DB.upsertUser

let deletedUserMessagesTask = task {
let fromUserId = message.ReplyToMessage.From.Id
let! allUserMessages = DB.getUserMessages fromUserId
logger.LogInformation($"Deleting {allUserMessages.Length} messages from user {fromUserId}")

// delete all recorded messages from user in all chats
do!
Expand All @@ -159,7 +185,7 @@ let banOnReply
|> taskIgnore

// delete recorded messages from DB
return! DB.deleteUserMessages fromUserId
return! DB.deleteMsgs allUserMessages
}

// try ban user in all monitored chats
Expand All @@ -182,6 +208,11 @@ let onUpdate
(botConfig: BotConfiguration)
(logger: ILogger)
(message: Message) = task {

use banOnReplyActivity = botActivity.StartActivity("onUpdate")
%banOnReplyActivity
.SetTag("chatId", message.Chat.Id)
.SetTag("chatUsername", message.Chat.Username)

// early return if if we can't process it
if isNull message || isNull message.From then
Expand All @@ -195,34 +226,28 @@ let onUpdate

// check if message comes from channel, we should delete it immediately
if botConfig.ShouldDeleteChannelMessages && isChannelMessage message then
do! botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId)
|> safeTaskAwait (fun e -> logger.LogError ($"Failed to delete message {message.MessageId} from chat {message.Chat.Id}", e))

let probablyChannelName =
if message.SenderChat <> null then
message.SenderChat.Title
else
"[unknown]"
logger.LogInformation $"Deleted message from channel {probablyChannelName}"
do! deleteChannelMessage botClient message logger

// check if message is a known command from authorized user
elif isKnownCommand message && isMessageFromAdmin botConfig message then
use _ = botActivity.StartActivity("adminCommand")
// delete command message
let deleteCmdTask =
botClient.DeleteMessageAsync(ChatId(message.Chat.Id), message.MessageId)
|> safeTaskAwait (fun e -> logger.LogError ($"Failed to delete ping message {message.MessageId} from chat {message.Chat.Id}", e))

// check that user is allowed to ban others
if isBanOnReplyMessage message && isBanAuthorized botConfig message logger then
if isBanOnReplyCommand message && isBanAuthorized botConfig message logger then
do! banOnReply botClient botConfig message logger

// ping command for testing that bot works and you can talk to it
elif isPingCommand message then
do! botClient.SendTextMessageAsync(ChatId(message.Chat.Id), "pong") |> taskIgnore
do! ping botClient message
do! deleteCmdTask

// if message is not a command from authorized user, just save it ID to DB
else
use _ = botActivity.StartActivity("justMessage")
do!
message
|> DbMessage.newMessage
Expand Down
28 changes: 6 additions & 22 deletions src/VahterBanBot/DB.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,18 @@ let getUserMessages (userId: int64): Task<DbMessage array> =
//language=postgresql
let sql = "SELECT * FROM message WHERE user_id = @userId"

let! messages =
conn.QueryAsync<DbMessage>(
sql,
{| userId = userId |}
)
let! messages = conn.QueryAsync<DbMessage>(sql, {| userId = userId |})
return Array.ofSeq messages
}

let deleteUserMessages (userId: int64): Task<int> =
let deleteMsgs (msg: DbMessage[]): Task<int> =
task {
let msgIds = msg |> Array.map (fun m -> m.Message_Id)
use conn = new NpgsqlConnection(connString)

//language=postgresql
let sql = "DELETE FROM message WHERE user_id = @userId"

let! messagesDeleted =
conn.ExecuteAsync(
sql,
{| userId = userId |}
)
return messagesDeleted
let sql = "DELETE FROM message WHERE message_id IN @msgIds"
return! conn.ExecuteAsync(sql, {| msgIds = msgIds |})
}

let cleanupOldMessages (howOld: TimeSpan): Task<int> =
Expand All @@ -105,14 +96,7 @@ let cleanupOldMessages (howOld: TimeSpan): Task<int> =

//language=postgresql
let sql = "DELETE FROM message WHERE created_at < @thatOld"

let! messagesDeleted =
conn.ExecuteAsync(
sql,
{| thatOld = DateTime.UtcNow.Subtract howOld |}
)

return messagesDeleted
return! conn.ExecuteAsync(sql, {| thatOld = DateTime.UtcNow.Subtract howOld |})
}

let getVahterStats(banInterval: TimeSpan option): Task<VahterStats> =
Expand Down
2 changes: 1 addition & 1 deletion src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ let timeSpanAsHumanReadable (ts: TimeSpan) =
type Task<'x> with
member this.Ignore() = task { let! _ = this in () }

let inline taskIgnore (t: Task<'x>) = t.Ignore()
let inline taskIgnore (t: Task<'x>) = t.Ignore()

0 comments on commit 48e2746

Please sign in to comment.