Merge remote-tracking branch 'origin/feature/new-dotnet-5' into feature/warns-net5

This commit is contained in:
West14
2021-12-26 13:15:15 +02:00
12 changed files with 157 additions and 35 deletions
+1
View File
@@ -3,6 +3,7 @@ obj/
/packages/
.idea
.vs
.DS_Store
appsettings.json
+21 -21
View File
@@ -1,7 +1,10 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Xml.Serialization;
namespace Kruzya.TelegramBot.Core.Data
{
@@ -13,30 +16,27 @@ namespace Kruzya.TelegramBot.Core.Data
public BotUser BotUser { get; set; }
public string Name { get; set; }
public byte[] Value
{
get
{
var fmt = new BinaryFormatter();
using var memStream = new MemoryStream();
fmt.Serialize(memStream, data);
return memStream.ToArray();
}
set
{
var fmt = new BinaryFormatter();
using var memStream = new MemoryStream();
memStream.Write(value);
memStream.Position = 0;
public string ValueType { get; set; }
data = fmt.Deserialize(memStream);
public byte[] Value { get; set; }
public T GetValue<T>(T defVal = default)
{
try
{
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(Value));
}
catch (Exception)
{
return defVal;
}
}
[NonSerialized] public object data;
public T GetValue<T>() => (T) data;
public void SetValue<T>(T value) => data = value;
public void SetValue<T>(T value)
{
Value = JsonSerializer.SerializeToUtf8Bytes(value, new JsonSerializerOptions { WriteIndented = false,
IgnoreNullValues = true });
ValueType = value.GetType().FullName;
}
}
}
+9 -2
View File
@@ -30,8 +30,14 @@ namespace Kruzya.TelegramBot.Core.Extensions
/// </summary>
/// <param name="dbContext"></param>
/// <param name="entity">Entity for marking as modified.</param>
public static void MarkAsModified(this DbContext dbContext, object entity) =>
dbContext.Entry(entity).State = EntityState.Modified;
public static void MarkAsModified(this DbContext dbContext, object entity)
{
var entry = dbContext.Entry(entity);
if (entry.State == EntityState.Added)
return;
entry.State = EntityState.Modified;
}
#endregion
@@ -97,6 +103,7 @@ namespace Kruzya.TelegramBot.Core.Extensions
if (opt == null)
{
opt = repository.Create();
opt.Name = option;
opt.BotUser = await repository.GetService<CoreContext>().Users.FindOrCreate(chatId, userId);
}
+6 -5
View File
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
@@ -28,9 +29,9 @@ namespace Kruzya.TelegramBot.Core
}
[HandleEverything]
public async Task Listener()
[Priority(-10)]
public async Task<bool> Listener()
{
Console.WriteLine("handle");
foreach (var message in new Message[] {RawUpdate.Message, RawUpdate.EditedMessage, RawUpdate.ChannelPost, RawUpdate.EditedChannelPost})
{
if (message == null)
@@ -38,10 +39,10 @@ namespace Kruzya.TelegramBot.Core
continue;
}
return new Task();
await VerifyChatPair(message);
}
throw new ArgumentException();
return true;
}
public async Task VerifyChatPair(Message message)
+73
View File
@@ -0,0 +1,73 @@
// <auto-generated />
using System;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kruzya.TelegramBot.Core.Migrations
{
[DbContext(typeof(CoreContext))]
[Migration("20211225192324_AddValueType")]
partial class AddValueType
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUser", b =>
{
b.Property<Guid>("BotUserId")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("ChatId")
.HasColumnType("bigint");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("BotUserId");
b.ToTable("Users");
});
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b =>
{
b.Property<Guid>("BotUserValueId")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<Guid?>("BotUserId")
.HasColumnType("char(36)");
b.Property<string>("Name")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<byte[]>("Value")
.HasColumnType("longblob");
b.Property<string>("ValueType")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("BotUserValueId");
b.HasIndex("BotUserId");
b.ToTable("UserValues");
});
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b =>
{
b.HasOne("Kruzya.TelegramBot.Core.Data.BotUser", "BotUser")
.WithMany()
.HasForeignKey("BotUserId");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace Kruzya.TelegramBot.Core.Migrations
{
public partial class AddValueType : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ValueType",
table: "UserValues",
nullable: true);
migrationBuilder.AlterColumn<long>(
name: "UserId",
table: "Users",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ValueType",
table: "UserValues");
migrationBuilder.AlterColumn<int>(
name: "UserId",
table: "Users",
type: "int",
nullable: false,
oldClrType: typeof(long));
}
}
}
+5 -2
View File
@@ -26,8 +26,8 @@ namespace Kruzya.TelegramBot.Core.Migrations
b.Property<long>("ChatId")
.HasColumnType("bigint");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("BotUserId");
@@ -49,6 +49,9 @@ namespace Kruzya.TelegramBot.Core.Migrations
b.Property<byte[]>("Value")
.HasColumnType("longblob");
b.Property<string>("ValueType")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("BotUserValueId");
b.HasIndex("BotUserId");
+1
View File
@@ -1,5 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{B41CB31A-641E-4079-87ED-5CE310B2D8C1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C7821F15-DEDD-474F-A575-A296D4B58F10}"
+2 -2
View File
@@ -1,5 +1,5 @@
# Build a project
FROM mcr.microsoft.com/dotnet/sdk:3.1.416-bullseye-arm64v8 AS build
FROM mcr.microsoft.com/dotnet/sdk:5.0.404-buster-slim-arm64v8 AS build
WORKDIR /source
# Copy project and perform restoring
@@ -20,7 +20,7 @@ RUN dotnet publish -c release -o /app --no-restore Kruzya.TelegramBot.sln && \
rm -f /app/TelegramBot.*.{dll,pdb,json}
# Now reuse image with only runtime
FROM mcr.microsoft.com/dotnet/aspnet:3.1.22-alpine3.14-arm64v8
FROM mcr.microsoft.com/dotnet/aspnet:5.0.13-alpine3.14-arm64v8
WORKDIR /user
COPY --from=build /app /app
ENTRYPOINT ["dotnet", "/app/TelegramBot.dll"]
+1 -1
View File
@@ -53,7 +53,7 @@ namespace Kruzya.TelegramBot.CombotAntiSpam
if (canKickMembers)
{
await Bot.KickChatMemberAsync(Chat, member.Id);
await Bot.BanChatMemberAsync(Chat, member.Id);
}
}
}
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>Kruzya.TelegramBot.D2_WhereIsXur</AssemblyName>
<AssemblyName>TelegramBot.D2_WhereIsXur</AssemblyName>
<RootNamespace>Kruzya.TelegramBot.Destiny2.WhereIsXur</RootNamespace>
</PropertyGroup>
+1 -1
View File
@@ -49,7 +49,7 @@ namespace Kruzya.TelegramBot.UrlLimitations
{
var targetChat = new ChatId(Chat.Id);
Task.WaitAll(new Task[] {
Bot.KickChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
Bot.DeleteMessageAsync(targetChat, message.MessageId),
Bot.SendTextMessageAsync(targetChat,
$"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html)