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/ /packages/
.idea .idea
.vs
.DS_Store .DS_Store
appsettings.json appsettings.json
+21 -21
View File
@@ -1,7 +1,10 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Xml.Serialization;
namespace Kruzya.TelegramBot.Core.Data namespace Kruzya.TelegramBot.Core.Data
{ {
@@ -13,30 +16,27 @@ namespace Kruzya.TelegramBot.Core.Data
public BotUser BotUser { get; set; } public BotUser BotUser { get; set; }
public string Name { get; set; } public string Name { get; set; }
public byte[] Value public string ValueType { get; set; }
{
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;
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 void SetValue<T>(T value)
{
public T GetValue<T>() => (T) data; Value = JsonSerializer.SerializeToUtf8Bytes(value, new JsonSerializerOptions { WriteIndented = false,
public void SetValue<T>(T value) => data = value; IgnoreNullValues = true });
ValueType = value.GetType().FullName;
}
} }
} }
+9 -2
View File
@@ -30,8 +30,14 @@ namespace Kruzya.TelegramBot.Core.Extensions
/// </summary> /// </summary>
/// <param name="dbContext"></param> /// <param name="dbContext"></param>
/// <param name="entity">Entity for marking as modified.</param> /// <param name="entity">Entity for marking as modified.</param>
public static void MarkAsModified(this DbContext dbContext, object entity) => public static void MarkAsModified(this DbContext dbContext, object entity)
dbContext.Entry(entity).State = EntityState.Modified; {
var entry = dbContext.Entry(entity);
if (entry.State == EntityState.Added)
return;
entry.State = EntityState.Modified;
}
#endregion #endregion
@@ -97,6 +103,7 @@ namespace Kruzya.TelegramBot.Core.Extensions
if (opt == null) if (opt == null)
{ {
opt = repository.Create(); opt = repository.Create();
opt.Name = option;
opt.BotUser = await repository.GetService<CoreContext>().Users.FindOrCreate(chatId, userId); 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;
using BotFramework.Attributes; using BotFramework.Attributes;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types; using Telegram.Bot.Types;
@@ -28,9 +29,9 @@ namespace Kruzya.TelegramBot.Core
} }
[HandleEverything] [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}) foreach (var message in new Message[] {RawUpdate.Message, RawUpdate.EditedMessage, RawUpdate.ChannelPost, RawUpdate.EditedChannelPost})
{ {
if (message == null) if (message == null)
@@ -38,10 +39,10 @@ namespace Kruzya.TelegramBot.Core
continue; continue;
} }
return new Task(); await VerifyChatPair(message);
} }
throw new ArgumentException(); return true;
} }
public async Task VerifyChatPair(Message message) 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") b.Property<long>("ChatId")
.HasColumnType("bigint"); .HasColumnType("bigint");
b.Property<int>("UserId") b.Property<long>("UserId")
.HasColumnType("int"); .HasColumnType("bigint");
b.HasKey("BotUserId"); b.HasKey("BotUserId");
@@ -49,6 +49,9 @@ namespace Kruzya.TelegramBot.Core.Migrations
b.Property<byte[]>("Value") b.Property<byte[]>("Value")
.HasColumnType("longblob"); .HasColumnType("longblob");
b.Property<string>("ValueType")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("BotUserValueId"); b.HasKey("BotUserValueId");
b.HasIndex("BotUserId"); b.HasIndex("BotUserId");
+1
View File
@@ -1,5 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00 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}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{B41CB31A-641E-4079-87ED-5CE310B2D8C1}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C7821F15-DEDD-474F-A575-A296D4B58F10}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C7821F15-DEDD-474F-A575-A296D4B58F10}"
+2 -2
View File
@@ -1,5 +1,5 @@
# Build a project # 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 WORKDIR /source
# Copy project and perform restoring # 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} rm -f /app/TelegramBot.*.{dll,pdb,json}
# Now reuse image with only runtime # 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 WORKDIR /user
COPY --from=build /app /app COPY --from=build /app /app
ENTRYPOINT ["dotnet", "/app/TelegramBot.dll"] ENTRYPOINT ["dotnet", "/app/TelegramBot.dll"]
+1 -1
View File
@@ -53,7 +53,7 @@ namespace Kruzya.TelegramBot.CombotAntiSpam
if (canKickMembers) if (canKickMembers)
{ {
await Bot.KickChatMemberAsync(Chat, member.Id); await Bot.BanChatMemberAsync(Chat, member.Id);
} }
} }
} }
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<AssemblyName>Kruzya.TelegramBot.D2_WhereIsXur</AssemblyName> <AssemblyName>TelegramBot.D2_WhereIsXur</AssemblyName>
<RootNamespace>Kruzya.TelegramBot.Destiny2.WhereIsXur</RootNamespace> <RootNamespace>Kruzya.TelegramBot.Destiny2.WhereIsXur</RootNamespace>
</PropertyGroup> </PropertyGroup>
+1 -1
View File
@@ -49,7 +49,7 @@ namespace Kruzya.TelegramBot.UrlLimitations
{ {
var targetChat = new ChatId(Chat.Id); var targetChat = new ChatId(Chat.Id);
Task.WaitAll(new Task[] { 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.DeleteMessageAsync(targetChat, message.MessageId),
Bot.SendTextMessageAsync(targetChat, Bot.SendTextMessageAsync(targetChat,
$"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html) $"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html)