Skip to content

Commit

Permalink
Reapply ".NET 8 upgrade (#15)" (#18)
Browse files Browse the repository at this point in the history
This reverts commit bbaae2b.
  • Loading branch information
Szer authored Jun 14, 2024
1 parent 5cfecd6 commit 010c079
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 95 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0.402-jammy as build-env
FROM mcr.microsoft.com/dotnet/sdk:8.0.302-jammy as build-env

### workaround for testcontainers resource reaper issue
ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000"
Expand All @@ -12,7 +12,7 @@ COPY src/VahterBanBot .
COPY global.json .
RUN dotnet publish -c Release -o /publish

FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime
FROM mcr.microsoft.com/dotnet/aspnet:8.0 as runtime
WORKDIR /publish
COPY --from=build-env /publish .
ENTRYPOINT ["dotnet", "VahterBanBot.dll"]
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "7.0.402"
"version": "8.0.302"
}
}
4 changes: 4 additions & 0 deletions src/VahterBanBot.Tests/ContainerTestBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type VahterTestContainers() =
.WithEnvironment("IGNORE_SIDE_EFFECTS", "false")
.WithEnvironment("USE_POLLING", "false")
.WithEnvironment("DATABASE_URL", internalConnectionString)
// .net 8.0 upgrade has a breaking change
// https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/aspnet-port
// Azure default port for containers is 80, se we need explicitly set it
.WithEnvironment("ASPNETCORE_HTTP_PORTS", "80")
.DependsOn(flywayContainer)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80))
.Build()
Expand Down
2 changes: 1 addition & 1 deletion src/VahterBanBot.Tests/VahterBanBot.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
Expand Down
5 changes: 3 additions & 2 deletions src/VahterBanBot/Antispam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ let phrases = [

let countPhrases (wl: string list) =
// premium performance
let rec countPhrase wl totalScore phrase =
let score, p::ps = phrase
let rec countPhrase wl totalScore (score, psx as phrase) =
// List.tail should be safe here as we are passing list of phrases above which is always non-empty
let p, ps = List.head psx, List.tail psx

match wl with
| w :: ws when w = p ->
Expand Down
171 changes: 100 additions & 71 deletions src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,104 @@ let warnSpamDetection
logger.LogInformation logMsg
}

let justMessage
(botClient: ITelegramBotClient)
(botConfig: BotConfiguration)
(logger: ILogger)
(message: Message) = task {
let spamScore = if message.Text <> null then calcSpamScore message.Text else 0

if spamScore >= 100 then
do! warnSpamDetection botClient botConfig message logger spamScore

use _ =
botActivity
.StartActivity("justMessage")
.SetTag("fromUserId", message.From.Id)
.SetTag("fromUsername", message.From.Username)
.SetTag("spamScore", spamScore)
do!
message
|> DbMessage.newMessage
|> DB.insertMessage
|> taskIgnore
}

let adminCommand
(botClient: ITelegramBotClient)
(botConfig: BotConfiguration)
(logger: ILogger)
(message: Message) =

// aux functions to overcome annoying FS3511: This state machine is not statically compilable.
let banOnReplyAux() = task {
let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = Option.ofObj message.ReplyToMessage.From.Username
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
targetUsername
true
if authed then
do! banOnReply botClient botConfig message logger
}
let unbanAux() = task {
let targetUserId = message.Text.Split(" ", StringSplitOptions.RemoveEmptyEntries)[1] |> int64
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
None
false
if authed then
do! unban botClient botConfig message logger targetUserId
}
let softBanOnReplyAux() = task {
let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = Option.ofObj message.ReplyToMessage.From.Username
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
targetUsername
true
if authed then
do! softBanOnReply botClient botConfig message logger
}

task {
use _ = botActivity.StartActivity("adminCommand")
// delete command message
let deleteCmdTask = task {
use _ =
botActivity
.StartActivity("deleteCmdMsg")
.SetTag("msgId", message.MessageId)
.SetTag("chatId", message.Chat.Id)
.SetTag("chatUsername", message.Chat.Username)
do! 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 (un)ban others
if isBanOnReplyCommand message then
do! banOnReplyAux()
elif isUnbanCommand message then
do! unbanAux()
elif isSoftBanOnReplyCommand message then
do! softBanOnReplyAux()
// ping command for testing that bot works and you can talk to it
elif isPingCommand message then
do! ping botClient message
do! deleteCmdTask
}

let onUpdate
(botClient: ITelegramBotClient)
(botConfig: BotConfiguration)
Expand Down Expand Up @@ -435,78 +533,9 @@ let onUpdate

// 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 = task {
use _ =
botActivity
.StartActivity("deleteCmdMsg")
.SetTag("msgId", message.MessageId)
.SetTag("chatId", message.Chat.Id)
.SetTag("chatUsername", message.Chat.Username)
do! 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 (un)ban others
if isBanOnReplyCommand message then
let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = Option.ofObj message.ReplyToMessage.From.Username
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
targetUsername
true
if authed then
do! banOnReply botClient botConfig message logger
elif isUnbanCommand message then
let targetUserId = message.Text.Split(" ", StringSplitOptions.RemoveEmptyEntries)[1] |> int64
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
None
false
if authed then
do! unban botClient botConfig message logger targetUserId
elif isSoftBanOnReplyCommand message then
let targetUserId = message.ReplyToMessage.From.Id
let targetUsername = Option.ofObj message.ReplyToMessage.From.Username
let authed =
isBanAuthorized
botConfig
message
logger
targetUserId
targetUsername
true
if authed then
do! softBanOnReply botClient botConfig message logger
// ping command for testing that bot works and you can talk to it
elif isPingCommand message then
do! ping botClient message
do! deleteCmdTask
do! adminCommand botClient botConfig logger message

// if message is not a command from authorized user, just save it ID to DB
else
let spamScore = if message.Text <> null then calcSpamScore message.Text else 0

if spamScore >= 100 then
do! warnSpamDetection botClient botConfig message logger spamScore

use _ =
botActivity
.StartActivity("justMessage")
.SetTag("fromUserId", message.From.Id)
.SetTag("fromUsername", message.From.Username)
.SetTag("spamScore", spamScore)
do!
message
|> DbMessage.newMessage
|> DB.insertMessage
|> taskIgnore
do! justMessage botClient botConfig logger message
}
4 changes: 3 additions & 1 deletion src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
open System
#nowarn "44" // open telemetry is going crazy with warnings

open System
open System.Collections.Generic
open System.Threading
open System.Threading.Tasks
Expand Down
34 changes: 17 additions & 17 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<WarningsAsErrors>true</WarningsAsErrors>
<TargetFramework>net8.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,21 +18,21 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.7" />
<PackageReference Include="Dapper" Version="2.0.151" />
<PackageReference Include="Dapper.FSharp" Version="4.7.0" />
<PackageReference Include="Giraffe" Version="6.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.11" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Npgsql" Version="7.0.6" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="7.0.6" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.1-beta.1" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Dapper.FSharp" Version="4.8.0" />
<PackageReference Include="Giraffe" Version="6.4.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 010c079

Please sign in to comment.