Skip to content

Commit

Permalink
Merge pull request #2 from Liminiens/latest-update
Browse files Browse the repository at this point in the history
Push current master
  • Loading branch information
Szer authored Dec 15, 2020
2 parents dd4a323 + 5158f17 commit 590eb0d
Show file tree
Hide file tree
Showing 22 changed files with 269 additions and 139 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/azure-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Deploy docker image
on:
push:
branches: [ master ]
paths-ignore:
- "**.md"

jobs:
build:
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/dotnet-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build & Test

on:
push:
branches: [ master ]
paths-ignore:
- "**.md"
pull_request:
branches: [ master ]
paths-ignore:
- "**.md"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build & Test
run: docker build .
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
out/

# Visual Studio 2015 cache/options directory
.vs/
Expand Down
10 changes: 10 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<PropertyGroup>
<TargetFramework>net5</TargetFramework>
<ServerGarbageCollection>false</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-dotnet
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.sln ./
Expand All @@ -7,16 +7,21 @@ COPY src/Grinder.Common/Grinder.Common.fsproj ./src/Grinder.Common/Grinder.Commo
COPY src/Grinder.DataAccess/Grinder.DataAccess.csproj ./src/Grinder.DataAccess/Grinder.DataAccess.csproj
COPY tests/Grinder.Tests/Grinder.Tests.fsproj ./tests/Grinder.Tests/Grinder.Tests.fsproj
COPY src/Grinder.ExportTool/Grinder.ExportTool.fsproj ./src/Grinder.ExportTool/Grinder.ExportTool.fsproj
RUN dotnet restore
COPY Directory.Build.props ./Directory.Build.props
RUN dotnet tool restore
RUN dotnet restore -r linux-x64
# Then build app
COPY src/Grinder/. ./src/Grinder
COPY src/Grinder.Common/. ./src/Grinder.Common
COPY src/Grinder.DataAccess/. ./src/Grinder.DataAccess
WORKDIR /app/src/Grinder
# Build&Test
RUN dotnet test -c Release -r linux-x64 -o out --no-restore --verbosity normal
# Publish
RUN dotnet publish -r linux-x64 -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/runtime:3.1
FROM mcr.microsoft.com/dotnet/runtime:5.0

EXPOSE 80

Expand Down
2 changes: 0 additions & 2 deletions Grinder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{25990364-33CE-
ProjectSection(SolutionItems) = preProject
docker-compose.yml = docker-compose.yml
Dockerfile = Dockerfile
azure-pipelines.yml = azure-pipelines.yml
x86.Dockerfile = x86.Dockerfile
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{3AB38C70-1DF4-48D9-992B-B7BFBFDDFD7A}"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ Telegram bot for chats administration.

## Configuration and build

Just open project with Rider/VS 2019 and run build
- Install Net5 SDK

or use dotnet cli
- Open project with Rider/VS 2019 and run build

- or use dotnet cli

```bash
dotnet build
Expand Down
35 changes: 0 additions & 35 deletions azure-pipelines.yml

This file was deleted.

2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.300"
"version": "5.0.101"
}
}
8 changes: 1 addition & 7 deletions src/Grinder.Common/Grinder.Common.fsproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Include="Library.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.6.2" />
<PackageReference Include="FSharp.Core" Version="5.0.0" />
<PackageReference Include="TaskBuilder.fs" Version="2.1.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Grinder.DataAccess/Grinder.DataAccess.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5</TargetFramework>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

Expand Down
5 changes: 1 addition & 4 deletions src/Grinder.ExportTool/Grinder.ExportTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,7 +10,7 @@

<ItemGroup>
<PackageReference Include="FSharp.Control.AsyncSeq" Version="2.0.21" />
<PackageReference Include="FSharp.Core" Version="4.6.2" />
<PackageReference Include="FSharp.Core" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="TDLib" Version="1.3.0" />
<PackageReference Include="tdlib.native" Version="1.3.0" />
Expand Down
8 changes: 4 additions & 4 deletions src/Grinder.ExportTool/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ let updateAuthorizationState (dialer: Dialer) (authLock: AutoResetEvent) (state:
|> Task.Ignore

| :? TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber ->
printfn "Enter phone"
Console.WriteLine "Enter phone"
let phone = Console.ReadLine()
do! dialer.ExecuteAsync(new TdApi.SetAuthenticationPhoneNumber(PhoneNumber = phone))
|> Task.Ignore

| :? TdApi.AuthorizationState.AuthorizationStateWaitCode ->
printfn "Enter code"
Console.WriteLine "Enter code"
let code = Console.ReadLine()
do! dialer.ExecuteAsync(new TdApi.CheckAuthenticationCode(Code = code))
|> Task.Ignore
Expand Down Expand Up @@ -143,7 +143,7 @@ let main argv =
dialer.ExecuteAsync(new TdApi.GetChats(Limit = 200, OffsetOrder = Int64.MaxValue))
|> Async.AwaitTask

printfn "Starting export of usernames"
Console.WriteLine "Starting export of usernames"
let! users =
chats.ChatIds
|> AsyncSeq.ofSeq
Expand Down Expand Up @@ -175,6 +175,6 @@ let main argv =
|> JsonConvert.SerializeObject
|> stream.Write

printfn "Finished export of usernames"
Console.WriteLine "Finished export of usernames"
} |> Async.RunSynchronously
0 // return an integer exit code
74 changes: 59 additions & 15 deletions src/Grinder/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,16 @@ module Processing =
Usernames: UserUsername seq
}

type PingContext = {
ChatId: TelegramChatId
}

type Command =
| BanCommand of BanCommandContext
| BanOnReplyCommand of ActionOnReplyCommandContext
| UnbanCommand of UnbanCommandContext
| UnbanOnReplyCommand of ActionOnReplyCommandContext
| PingCommand
| PingCommand of PingContext
| DoNothingCommand

type CommandError =
Expand Down Expand Up @@ -407,7 +411,7 @@ module Processing =
}
UnbanCommand context
| BotCommand(Ping) ->
PingCommand
PingCommand { ChatId = %context.Message.Chat.Id }
| InvalidCommand _ ->
DoNothingCommand

Expand All @@ -424,6 +428,12 @@ module Processing =
botSettings.AllowedUsers.Set
|> Set.contains username
|> not

let logErrors =
Seq.iter (function
| ApiError str
| AdminBanNotAllowedError str -> logErr str
)

match command with
| BanCommand context ->
Expand All @@ -434,8 +444,12 @@ module Processing =
if userCanBeBanned user then
for chat in botSettings.ChatsToMonitor.Set do
yield botApi.BanUserByUsername chat user context.Until.Value
|> Async.Map ^ fun result ->
Result.mapError ApiError result
|> Async.Map ^ Result.mapError
(sprintf "Error on baning user %A in chat %A until %A. %s"
user
chat
context.Until.Value
>> ApiError)
else
yield sprintf "Cannot ban admin @%s" %user
|> createCommandError AdminBanNotAllowedError
Expand All @@ -445,6 +459,8 @@ module Processing =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Expand Down Expand Up @@ -472,9 +488,14 @@ module Processing =
let requests =
if userCanBeBanned username then
[for chat in botSettings.ChatsToMonitor.Set do
yield botApi.BanUserByUserId chat context.UserId (DateTime.UtcNow.AddMonths(13))
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
let until = DateTime.UtcNow.AddMonths(13)
yield botApi.BanUserByUserId chat context.UserId until
|> Async.Map ^ Result.mapError
(sprintf "Error on baning userId %A in chat %A until %A. %s"
context.UserId
chat
until
>> ApiError)]
else
[sprintf "Cannot ban admin %s" %username
|> createCommandError AdminBanNotAllowedError
Expand All @@ -484,6 +505,8 @@ module Processing =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Expand All @@ -499,16 +522,24 @@ module Processing =
[for user in context.Usernames do
for chat in botSettings.ChatsToMonitor.Set do
yield botApi.UnbanUser chat user
|> Async.Map ^ fun result ->
Result.mapError ApiError result
|> Async.Map ^ Result.mapError
(sprintf "Error on unbaning user %A in chat %A. %s"
user
chat
>> ApiError)
yield botApi.UnrestrictUser chat user
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
|> Async.Map ^ Result.mapError
(sprintf "Error on unrestricting user %A in chat %A. %s"
user
chat
>> ApiError)]
let! errors =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Usernames = context.Usernames
Expand All @@ -533,24 +564,32 @@ module Processing =
let requests =
[for chat in botSettings.ChatsToMonitor.Set do
yield botApi.UnbanUser chat username
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
|> Async.Map ^ Result.mapError
(sprintf "Error on unban user %A in chat %A. %s"
username
chat
>> ApiError)]

let! errors =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Usernames = Set.empty.Add username
}

return Some <| UnbanOnReplyMessage(context.From, message, errors)
| PingCommand ->
do! botApi.SendTextToChannel "pong"
| PingCommand context ->
sprintf "Sending PONG to %A" context.ChatId
|> logInfo
do! botApi.SendTextMessage context.ChatId "pong"
return None
| DoNothingCommand ->
logDbg "Do Nothing Command has been successfully processed and we haven't done anything remotely useful"
return None
}

Expand All @@ -569,7 +608,12 @@ module Processing =
do! Datastore.upsertUsers users
do! "Updated user database"
|> ApiExt.sendMessage botSettings.ChannelId config
sprintf "Successfully processed admin command for fileId %s" fileId
|> logInfo

| Error e ->
sprintf "Error on processing admin command with fileId: %s. Error: %s" fileId e
|> logErr
do! ApiExt.sendMessage botSettings.ChannelId config e
}

Expand Down
Loading

0 comments on commit 590eb0d

Please sign in to comment.