diff --git a/Core/Core.csproj b/Core/Core.csproj index 21c37b2..d625025 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -8,6 +8,7 @@ + diff --git a/Core/Data/BotUser.cs b/Core/Data/BotUser.cs new file mode 100644 index 0000000..621c45f --- /dev/null +++ b/Core/Data/BotUser.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Kruzya.TelegramBot.Core.Data +{ + public class BotUser + { + /// + /// The unique bot user identifier. + /// + [Key] + public Guid BotUserId { get; set; } + + /// + /// The chat identifier where this user is related. + /// + public long ChatId { get; set; } + + /// + /// The Telegram User identifier. + /// + public int UserId { get; set; } + } +} \ No newline at end of file diff --git a/Core/Data/BotUserValue.cs b/Core/Data/BotUserValue.cs new file mode 100644 index 0000000..9a95827 --- /dev/null +++ b/Core/Data/BotUserValue.cs @@ -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) data; + public void SetValue(T value) => data = value; + } +} \ No newline at end of file diff --git a/Core/Data/CoreContext.cs b/Core/Data/CoreContext.cs new file mode 100644 index 0000000..6b5f44c --- /dev/null +++ b/Core/Data/CoreContext.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; + +namespace Kruzya.TelegramBot.Core.Data +{ + public class CoreContext : DbContext + { + /// + /// The all known users. + /// + public DbSet Users { get; set; } + + public DbSet UserValues { get; set; } + + public CoreContext(DbContextOptions ctx) : base(ctx) + { + Database.Migrate(); + } + } +} \ No newline at end of file diff --git a/Core/GeneralHandler.cs b/Core/GeneralHandler.cs new file mode 100644 index 0000000..0a8c4cd --- /dev/null +++ b/Core/GeneralHandler.cs @@ -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() + }); + } + } +} \ No newline at end of file diff --git a/Core/Migrations/20200307215450_Initial.Designer.cs b/Core/Migrations/20200307215450_Initial.Designer.cs new file mode 100644 index 0000000..cc79c56 --- /dev/null +++ b/Core/Migrations/20200307215450_Initial.Designer.cs @@ -0,0 +1,70 @@ +// +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("BotUserId") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("ChatId") + .HasColumnType("bigint"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("BotUserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b => + { + b.Property("BotUserValueId") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("BotUserId") + .HasColumnType("char(36)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("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 + } + } +} diff --git a/Core/Migrations/20200307215450_Initial.cs b/Core/Migrations/20200307215450_Initial.cs new file mode 100644 index 0000000..2b8e0a9 --- /dev/null +++ b/Core/Migrations/20200307215450_Initial.cs @@ -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(nullable: false), + ChatId = table.Column(nullable: false), + UserId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.BotUserId); + }); + + migrationBuilder.CreateTable( + name: "UserValues", + columns: table => new + { + BotUserValueId = table.Column(nullable: false), + BotUserId = table.Column(nullable: true), + Name = table.Column(nullable: true), + Value = table.Column(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"); + } + } +} diff --git a/Core/Migrations/CoreContextModelSnapshot.cs b/Core/Migrations/CoreContextModelSnapshot.cs new file mode 100644 index 0000000..827b255 --- /dev/null +++ b/Core/Migrations/CoreContextModelSnapshot.cs @@ -0,0 +1,68 @@ +// +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("BotUserId") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("ChatId") + .HasColumnType("bigint"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("BotUserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b => + { + b.Property("BotUserValueId") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("BotUserId") + .HasColumnType("char(36)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("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 + } + } +} diff --git a/Core/Startup.cs b/Core/Startup.cs index f55e59c..4daa02f 100644 --- a/Core/Startup.cs +++ b/Core/Startup.cs @@ -1,7 +1,9 @@ using BotFramework; +using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -29,6 +31,10 @@ namespace Kruzya.TelegramBot.Core services.AddSingleton(_core); foreach (var module in _core.Modules) services.AddSingleton(module.GetType(), module); + // Add database context to DI. + services.AddDbContext(ctx => + ctx.UseMySql(_core.Configuration.GetConnectionString("DefaultConnection"))); + // Trigger module handlers. _core.Modules.ConfigureServices(services); } diff --git a/Core/appsettings.Development.json b/Core/appsettings.Development.json index 8983e0f..464b352 100644 --- a/Core/appsettings.Development.json +++ b/Core/appsettings.Development.json @@ -5,5 +5,9 @@ "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } + }, + + "ConnectionStrings": { + "DefaultConnection": "server=127.0.0.1;UserId=rider;Password=rider;database=rider;port=8889" } } diff --git a/modules/UrlLimitations/Data/UrlLimitationsContext.cs b/modules/UrlLimitations/Data/UrlLimitationsContext.cs new file mode 100644 index 0000000..de64347 --- /dev/null +++ b/modules/UrlLimitations/Data/UrlLimitationsContext.cs @@ -0,0 +1,7 @@ +namespace Kruzya.TelegramBot.UrlLimitations +{ + public class UrlLimitationsContext + { + + } +} \ No newline at end of file