Skip to content

Commit

Permalink
test add user
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Sep 28, 2023
1 parent d27ed92 commit c19a0b0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
40 changes: 40 additions & 0 deletions src/VahterBanBot/DB.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module VahterBanBot.DB

open Npgsql
open VahterBanBot.Types
open Dapper
open VahterBanBot.Utils

let private connString = getEnv "DATABASE_URL"

let upsertUser (user: User) =
task {
use conn = new NpgsqlConnection(connString)

//language=postgresql
let sql =
"""
INSERT INTO "user" (id, username)
VALUES (@id, @username)
ON CONFLICT (id) DO UPDATE
SET username =
CASE
WHEN EXCLUDED.username != "user".username THEN EXCLUDED.username
ELSE "user".username
END,
updated_at =
CASE
WHEN EXCLUDED.username != "user".username THEN timezone('utc'::TEXT, NOW())
ELSE "user".updated_at
END
RETURNING *;
"""

let! insertedUser =
conn.QueryAsync<User>(
sql,
{| id = user.Id; username = user.Username |}
)

return insertedUser |> Seq.head
}
39 changes: 29 additions & 10 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ open Telegram.Bot
open Telegram.Bot.Types
open Giraffe
open Microsoft.Extensions.DependencyInjection
open VahterBanBot
open VahterBanBot.Utils
open VahterBanBot.Bot
open VahterBanBot.Types

type Root = class end

Dapper.FSharp.PostgreSQL.OptionTypes.register()

let botConf =
{ BotToken = getEnv "BOT_TELEGRAM_TOKEN"
Route = getEnvOr "BOT_HOOK_ROUTE" "/bot"
Expand Down Expand Up @@ -55,18 +58,34 @@ match Environment.GetEnvironmentVariable "APPLICATIONINSIGHTS_CONNECTION_STRING"
)

let webApp = choose [
POST >=> route botConf.Route >=> requiresApiKey >=> bindJson<Update> (fun update next ctx -> task {
use scope = ctx.RequestServices.CreateScope()
let telegramClient = scope.ServiceProvider.GetRequiredService<ITelegramBotClient>()
let logger = ctx.GetLogger<Root>()
try
do! onUpdate telegramClient botConf (ctx.GetLogger "VahterBanBot.Bot") update.Message
with e ->
logger.LogError(e, "Unexpected error while processing update")
return! Successful.OK() next ctx
})
// need for Azure health checks on / route
GET >=> route "/" >=> text "OK"

requiresApiKey >=> choose [
POST >=> route botConf.Route >=> bindJson<Update> (fun update next ctx -> task {
use scope = ctx.RequestServices.CreateScope()
let telegramClient = scope.ServiceProvider.GetRequiredService<ITelegramBotClient>()
let logger = ctx.GetLogger<Root>()
try
do! onUpdate telegramClient botConf (ctx.GetLogger "VahterBanBot.Bot") update.Message
with e ->
logger.LogError(e, "Unexpected error while processing update")
return! Successful.OK() next ctx
})

GET >=> routef "/user/%d" (fun id next ctx -> task {
let logger = ctx.GetLogger<Root>()
try
let! upsertedUser =
User.newUser id
|> DB.upsertUser

return! json upsertedUser next ctx
with e ->
logger.LogError(e, "Unexpected error while processing user")
return! ServerErrors.INTERNAL_ERROR() next ctx
})
]
]

let app = builder.Build()
Expand Down
19 changes: 19 additions & 0 deletions src/VahterBanBot/Types.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module VahterBanBot.Types

open System
open System.Collections.Generic

[<CLIMutable>]
Expand All @@ -11,3 +12,21 @@ type BotConfiguration =
ChatsToMonitor: Dictionary<string, int64>
AllowedUsers: Dictionary<string, int64>
ShouldDeleteChannelMessages: bool }

type User =
{ Id: int64
Username: string option
BannedBy: int64 option
BannedAt: DateTime option
BanReason: string option
UpdatedAt: DateTime
CreatedAt: DateTime }

static member newUser(id, ?username: string) =
{ Id = id
Username = username
BannedBy = None
BannedAt = None
BanReason = None
UpdatedAt = DateTime.UtcNow
CreatedAt = DateTime.UtcNow }
2 changes: 2 additions & 0 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<ItemGroup>
<Compile Include="Utils.fs" />
<Compile Include="Types.fs" />
<Compile Include="DB.fs" />
<Compile Include="Bot.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<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" />
Expand Down

0 comments on commit c19a0b0

Please sign in to comment.