Skip to content

Commit

Permalink
Fixed wrong ID in logs (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer authored Jul 23, 2024
1 parent 2455d50 commit 35ea56a
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ let softBanInChat (botClient: ITelegramBotClient) (chatId: ChatId) targetUserId
}

let unbanInAllChats (botConfig: BotConfiguration) (botClient: ITelegramBotClient) targetUserId = task {
let banTasks =
let unbanTasks =
botConfig.ChatsToMonitor
|> Seq.map (fun (KeyValue(chatUserName, chatId)) -> task {
// unban user in each chat
Expand All @@ -132,7 +132,7 @@ let unbanInAllChats (botConfig: BotConfiguration) (botClient: ITelegramBotClient
with e ->
return Error (chatUserName, chatId, e)
})
return! Task.WhenAll banTasks
return! Task.WhenAll unbanTasks
}

let safeTaskAwait onError (task: Task) =
Expand All @@ -143,23 +143,22 @@ let safeTaskAwait onError (task: Task) =

let aggregateResultInLogMsg
(isBan: bool)
(message: Message)
(targetUserId: int64)
(targetUserName: string option)
(chat: Chat)
(vahter: DbUser)
(user: DbUser)

(logger: ILogger)
(deletedUserMessages: int) // 0 for unban
(results: Result<string * int64, string * int64 * exn> []) =

let resultType = if isBan then "ban" else "unban"
let sanitizedUsername =
targetUserName
|> Option.map prependUsername
|> Option.defaultValue "{NO_USERNAME}"

let vahterUserId = message.From.Id
let vahterUsername = message.From.Username
let chatName = message.Chat.Username
let chatId = message.Chat.Id
let sanitizedUsername = defaultArg user.username null |> prependUsername
let targetUserId = user.id
let vahterUsername = defaultArg vahter.username null |> prependUsername
let vahterUserId = vahter.id

let chatName = chat.Username
let chatId = chat.Id

let logMsgBuilder = StringBuilder()
%logMsgBuilder.Append($"Vahter {prependUsername vahterUsername}({vahterUserId}) {resultType}ned {sanitizedUsername} ({targetUserId}) in {prependUsername chatName}({chatId})")
Expand All @@ -186,19 +185,19 @@ let aggregateResultInLogMsg
) |> ignore
string logMsgBuilder

let aggregateBanResultInLogMsg message =
let aggregateBanResultInLogMsg chat vahter user =
aggregateResultInLogMsg
true
message
message.From.Id
(Some message.From.Username)
chat
vahter
user

let aggregateUnbanResultInLogMsg message targetUserId targetUsername =
let aggregateUnbanResultInLogMsg chat vahter user =
aggregateResultInLogMsg
false
message
targetUserId
targetUsername
chat
vahter
user

let softBanResultInLogMsg (message: Message) (vahter: DbUser) (duration: int) =
let logMsgBuilder = StringBuilder()
Expand Down Expand Up @@ -261,7 +260,7 @@ let totalBan
}

// update user in DB
let updatedUser =
let! updatedUser =
message.From
|> DbUser.newUser
|> DB.upsertUser
Expand Down Expand Up @@ -296,7 +295,7 @@ let totalBan
let! deletedUserMessages = deletedUserMessagesTask

// produce aggregated log message
let logMsg = aggregateBanResultInLogMsg message logger deletedUserMessages banResults
let logMsg = aggregateBanResultInLogMsg message.Chat vahter updatedUser logger deletedUserMessages banResults

// add ban record to DB
do! message
Expand All @@ -306,8 +305,7 @@ let totalBan
// log both to logger and to logs channel
do! botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg) |> taskIgnore
logger.LogInformation logMsg

do! updatedUser.Ignore()

do! deleteMsgTask
}

Expand Down Expand Up @@ -376,29 +374,26 @@ let unban
(botClient: ITelegramBotClient)
(botConfig: BotConfiguration)
(message: Message)
(logger: ILogger)
(targetUserId: int64) = task {
(vahter: DbUser)
(userToUnban: DbUser)
(logger: ILogger) = task {
use banOnReplyActivity = botActivity.StartActivity("unban")
let targetUserId = userToUnban.id
%banOnReplyActivity
.SetTag("vahterId", message.From.Id)
.SetTag("vahterUsername", message.From.Username)
.SetTag("vahterId", vahter.id)
.SetTag("vahterUsername", defaultArg vahter.username null)
.SetTag("targetId", targetUserId)

let! user = DB.getUserById targetUserId
if user.IsSome then
%banOnReplyActivity.SetTag("targetUsername", user.Value.username)

// delete ban record from DB
do! user.Value.id
|> DB.unbanUser

let targetUsername = user |> Option.bind (_.username)
.SetTag("targetUsername", userToUnban.username)

// delete ban record from DB
do! userToUnban.id
|> DB.unbanUser

// try unban user in all monitored chats
let! unbanResults = unbanInAllChats botConfig botClient targetUserId

// produce aggregated log message
let logMsg = aggregateUnbanResultInLogMsg message targetUserId targetUsername logger 0 unbanResults
let logMsg = aggregateUnbanResultInLogMsg message.Chat vahter userToUnban logger 0 unbanResults

// log both to logger and to logs channel
do! botClient.SendTextMessageAsync(ChatId(botConfig.LogsChannelId), logMsg) |> taskIgnore
Expand Down Expand Up @@ -524,9 +519,14 @@ let adminCommand
do! banOnReply botClient botConfig message vahter logger
}
let unbanAux() = task {
let targetUserId = message.Text.Split(" ", StringSplitOptions.RemoveEmptyEntries)[1] |> int64
if isUserVahter botConfig vahter then
do! unban botClient botConfig message logger targetUserId
let targetUserId = message.Text.Split(" ", StringSplitOptions.RemoveEmptyEntries)[1] |> int64
let! userToUnban = DB.getUserById targetUserId
match userToUnban with
| None ->
logger.LogWarning $"User {vahter.username} ({vahter.id}) tried to unban non-existing user {targetUserId}"
| Some userToUnban ->
do! unban botClient botConfig message vahter userToUnban logger
}
let softBanOnReplyAux() = task {
let authed =
Expand All @@ -538,7 +538,7 @@ let adminCommand
if authed then
do! softBanMsg botClient botConfig message.ReplyToMessage vahter logger
}

task {
use _ = botActivity.StartActivity("adminCommand")
// delete command message
Expand Down Expand Up @@ -690,6 +690,7 @@ let onCallback
%onCallbackActivity.SetTag("type", "Spam")
do! vahterMarkedAsSpam botClient botConfig logger vahter msg
do! DB.deleteCallback callbackId
do! botClient.AnswerCallbackQueryAsync(callbackQuery.Id)
}

let onUpdate
Expand Down

0 comments on commit 35ea56a

Please sign in to comment.