Skip to content

Commit

Permalink
ML auto-ban will trigger on edited messages as well (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer authored Oct 7, 2024
1 parent 92f1666 commit 088533c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/VahterBanBot.Tests/MLBanTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ type MLBanTests(fixture: VahterTestContainers, _unused: MlAwaitFixture) =
let! msgBanned = fixture.MessageIsAutoDeleted msgUpdate.Message
Assert.True msgBanned
}

[<Fact>]
let ``Message which were edited triggers auto-delete`` () = task {
// record a message, which originally was not a spam,
// but it was edited to be a spam
let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0], text = "a", editedText = "2222222")
let! _ = fixture.SendMessage msgUpdate

// assert that the message got auto banned
let! msgBanned = fixture.MessageIsAutoDeleted msgUpdate.Message
Assert.True msgBanned
}

interface IAssemblyFixture<VahterTestContainers>
interface IClassFixture<MlAwaitFixture>
22 changes: 18 additions & 4 deletions src/VahterBanBot.Tests/TgMessageUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,33 @@ type Tg() =
)
)

static member quickMsg (?text: string, ?chat: Chat, ?from: User, ?date: DateTime, ?callback: CallbackQuery, ?caption: string) =
static member quickMsg (?text: string, ?chat: Chat, ?from: User, ?date: DateTime, ?callback: CallbackQuery, ?caption: string, ?editedText: string) =
let updateId = next()
let msgId = next()
Update(
Id = next(),
Id = updateId,
Message =
Message(
MessageId = next(),
MessageId = msgId,
Text = (text |> Option.defaultValue (Guid.NewGuid().ToString())),
Chat = (chat |> Option.defaultValue (Tg.chat())),
From = (from |> Option.defaultValue (Tg.user())),
Date = (date |> Option.defaultValue DateTime.UtcNow),
Caption = (caption |> Option.defaultValue null),
ReplyToMessage = null
)
),
EditedMessage =
if editedText |> Option.isSome then
Message(
MessageId = msgId,
Text = editedText.Value,
Chat = (chat |> Option.defaultValue (Tg.chat())),
From = (from |> Option.defaultValue (Tg.user())),
Date = (date |> Option.defaultValue DateTime.UtcNow),
Caption = (caption |> Option.defaultValue null),
ReplyToMessage = null
)
else null
)

static member replyMsg (msg: Message, ?text: string, ?from: User, ?date: DateTime) =
Expand Down
2 changes: 1 addition & 1 deletion src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -746,5 +746,5 @@ let onUpdate
if update.CallbackQuery <> null then
do! onCallback botClient botConfig logger update.CallbackQuery
else
do! onMessage botUser botClient botConfig logger ml update.Message
do! onMessage botUser botClient botConfig logger ml update.EditedOrMessage
}
7 changes: 7 additions & 0 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ type Telegram.Bot.Types.Message with
msg.Caption
else
msg.Text

type Telegram.Bot.Types.Update with
member msg.EditedOrMessage =
if isNull msg.EditedMessage then
msg.Message
else
msg.EditedMessage

0 comments on commit 088533c

Please sign in to comment.