WiP storage for modules

This commit is contained in:
2020-03-08 01:55:50 +04:00
parent 768a0d064f
commit 3fd5042367
11 changed files with 375 additions and 0 deletions
+1
View File
@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AleXr64.BotFramework" Version="0.0.17" /> <PackageReference Include="AleXr64.BotFramework" Version="0.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="3.1.2" /> <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="3.1.2" />
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Kruzya.TelegramBot.Core.Data
{
public class BotUser
{
/// <summary>
/// The unique bot user identifier.
/// </summary>
[Key]
public Guid BotUserId { get; set; }
/// <summary>
/// The chat identifier where this user is related.
/// </summary>
public long ChatId { get; set; }
/// <summary>
/// The Telegram User identifier.
/// </summary>
public int UserId { get; set; }
}
}
+42
View File
@@ -0,0 +1,42 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Kruzya.TelegramBot.Core.Data
{
public class BotUserValue
{
[Key]
public Guid BotUserValueId { get; set; }
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;
data = fmt.Deserialize(memStream);
}
}
[NonSerialized] public object data;
public T GetValue<T>() => (T) data;
public void SetValue<T>(T value) => data = value;
}
}
+19
View File
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
namespace Kruzya.TelegramBot.Core.Data
{
public class CoreContext : DbContext
{
/// <summary>
/// The all known users.
/// </summary>
public DbSet<BotUser> Users { get; set; }
public DbSet<BotUserValue> UserValues { get; set; }
public CoreContext(DbContextOptions<CoreContext> ctx) : base(ctx)
{
Database.Migrate();
}
}
}
+75
View File
@@ -0,0 +1,75 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core
{
public class GeneralHandler : BotEventHandler
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class HandleEverythingAttribute : HandlerAttribute
{
protected override bool CanHandle(HandlerParams param)
{
return true;
}
}
protected readonly CoreContext databaseContext;
public GeneralHandler(CoreContext dbCtx)
{
databaseContext = dbCtx;
}
[HandleEverything]
public async Task Listener()
{
foreach (var message in new Message[] {RawUpdate.Message, RawUpdate.EditedMessage, RawUpdate.ChannelPost, RawUpdate.EditedChannelPost})
{
if (message == null)
{
continue;
}
await VerifyChatPair(message);
}
}
public async Task VerifyChatPair(Message message)
{
await VerifyChatPair(message.Chat, message.From);
}
public async Task VerifyChatPair(Chat chat, User user)
{
await VerifyChatPair(chat.Id, user.Id);
}
public async Task VerifyChatPair(long chat, int user)
{
var hasEntry = await databaseContext.Users.AnyAsync(e => e.ChatId == chat && e.UserId == user);
if (hasEntry)
{
return;
}
await MakeChatPair(chat, user);
}
public async Task MakeChatPair(long chat, int user)
{
await databaseContext.Users.AddAsync(new BotUser()
{
ChatId = chat,
UserId = user,
BotUserId = new Guid()
});
}
}
}
+70
View File
@@ -0,0 +1,70 @@
// <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("20200307215450_Initial")]
partial class Initial
{
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<int>("UserId")
.HasColumnType("int");
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.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
}
}
}
+58
View File
@@ -0,0 +1,58 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Kruzya.TelegramBot.Core.Migrations
{
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
BotUserId = table.Column<Guid>(nullable: false),
ChatId = table.Column<long>(nullable: false),
UserId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.BotUserId);
});
migrationBuilder.CreateTable(
name: "UserValues",
columns: table => new
{
BotUserValueId = table.Column<Guid>(nullable: false),
BotUserId = table.Column<Guid>(nullable: true),
Name = table.Column<string>(nullable: true),
Value = table.Column<byte[]>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_UserValues", x => x.BotUserValueId);
table.ForeignKey(
name: "FK_UserValues_Users_BotUserId",
column: x => x.BotUserId,
principalTable: "Users",
principalColumn: "BotUserId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_UserValues_BotUserId",
table: "UserValues",
column: "BotUserId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "UserValues");
migrationBuilder.DropTable(
name: "Users");
}
}
}
@@ -0,0 +1,68 @@
// <auto-generated />
using System;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kruzya.TelegramBot.Core.Migrations
{
[DbContext(typeof(CoreContext))]
partial class CoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(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<int>("UserId")
.HasColumnType("int");
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.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
}
}
}
+6
View File
@@ -1,7 +1,9 @@
using BotFramework; using BotFramework;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -29,6 +31,10 @@ namespace Kruzya.TelegramBot.Core
services.AddSingleton<Core>(_core); services.AddSingleton<Core>(_core);
foreach (var module in _core.Modules) services.AddSingleton(module.GetType(), module); foreach (var module in _core.Modules) services.AddSingleton(module.GetType(), module);
// Add database context to DI.
services.AddDbContext<CoreContext>(ctx =>
ctx.UseMySql(_core.Configuration.GetConnectionString("DefaultConnection")));
// Trigger module handlers. // Trigger module handlers.
_core.Modules.ConfigureServices(services); _core.Modules.ConfigureServices(services);
} }
+4
View File
@@ -5,5 +5,9 @@
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
},
"ConnectionStrings": {
"DefaultConnection": "server=127.0.0.1;UserId=rider;Password=rider;database=rider;port=8889"
} }
} }
@@ -0,0 +1,7 @@
namespace Kruzya.TelegramBot.UrlLimitations
{
public class UrlLimitationsContext
{
}
}