Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Docs/BotFramework.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@startuml Modules

package Framework {
package BF.Abstractions
package BF.Analyzer
package BF.Core
}

package Plugins {
package BF.Providers.Telegram
package BF.Providers.Discord
}

BF.Analyzer --> BF.Abstractions
BF.Core --> BF.Abstractions
BF.Core --> BF.Analyzer

BF.Providers.Telegram --> BF.Abstractions
BF.Providers.Discord --> BF.Abstractions

package Bot

Bot --> BF.Core
Bot --> BF.Providers.Telegram

@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

public interface IDialogContext
{
ISenderInfo SenderInfo { get; }

int State { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kysect.BotFramework.Abstractions.Contexts;

public interface IDiscordSenderInfo : ISenderInfo
{
ulong GuildId { get; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Abstractions.Visitors;

namespace Kysect.BotFramework.Abstractions.Contexts;

public interface ISenderInfo
{
long ChatId { get; }
long UserSenderId { get; }
string UserSenderUsername { get; }
bool IsAdmin { get; }

TContext Accept<TContext>(ISenderInfoVisitor<TContext> visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Kysect.BotFramework.Abstractions.Contexts;

public interface ITelegramSenderInfo : ISenderInfo
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Kysect.BotFramework.Abstactions.Services;

public interface IDialogContextProvider
{
public IDialogContext GetDialogContext(ISenderInfo senderInfo);
IDialogContext GetDialogContext(ISenderInfo senderInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Kysect.BotFramework.Abstractions.Contexts;

namespace Kysect.BotFramework.Abstractions.Visitors;

public interface ISenderInfoVisitor<out TContext>
{
TContext Visit(IDiscordSenderInfo senderInfo);

TContext Visit(ITelegramSenderInfo senderInfo);
}
64 changes: 30 additions & 34 deletions Kysect.BotFramework/ApiProviders/Discord/DiscordSenderInfo.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
using Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Core.Contexts;
using Kysect.BotFramework.Data;
using Kysect.BotFramework.Data.Entities;
using System;
using Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Abstractions.Visitors;

namespace Kysect.BotFramework.ApiProviders.Discord
namespace Kysect.BotFramework.ApiProviders.Discord;

public class DiscordSenderInfo : IDiscordSenderInfo
{
public class DiscordSenderInfo : SenderInfo
public DiscordSenderInfo(long chatId, long userSenderId, string userSenderUsername, bool isAdmin, ulong guildId)
{
GuildId = guildId;
ChatId = chatId;
UserSenderId = userSenderId;
UserSenderUsername = userSenderUsername;
IsAdmin = isAdmin;
}

public long ChatId { get; }

public long UserSenderId { get; }

public string UserSenderUsername { get; }

public bool IsAdmin { get; }

public ulong GuildId { get; }

public TContext Accept<TContext>(ISenderInfoVisitor<TContext> visitor)
{
public ulong GuildId { get; }

internal override ContextType ContextType => ContextType.Discord;

public DiscordSenderInfo(long chatId, long userSenderId, string userSenderUsername, bool isAdmin, ulong guildId)
: base(chatId, userSenderId, userSenderUsername, isAdmin)
{
GuildId = guildId;
}

private DiscordSenderInfoEntity ToEntity()
{
var entity = new DiscordSenderInfoEntity
{
GuildId = GuildId,
ChatId = ChatId,
UserSenderId = UserSenderId
};
return entity;
}

internal override IDialogContext GetOrCreateDialogContext(BotFrameworkDbContext dbContext)
{
var contextSenderInfo = DiscordSenderInfoEntity.GetOrCreate(this, dbContext);
var contextModel = DialogContextEntity.GetOrCreate(contextSenderInfo, ContextType, dbContext);

return new DialogContext(contextModel.State, contextModel.SenderInfoId, this, ContextType, dbContext);
}
if (visitor is null)
throw new ArgumentNullException(nameof(visitor));

return visitor.Visit(this);
}
}
50 changes: 24 additions & 26 deletions Kysect.BotFramework/ApiProviders/Telegram/TelegramSenderInfo.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
using Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Core.Contexts;
using Kysect.BotFramework.Data;
using Kysect.BotFramework.Data.Entities;
using Kysect.BotFramework.Abstractions.Visitors;
using System;

namespace Kysect.BotFramework.ApiProviders.Telegram
namespace Kysect.BotFramework.ApiProviders.Telegram;

public class TelegramSenderInfo : ITelegramSenderInfo
{
public class TelegramSenderInfo : SenderInfo
public TelegramSenderInfo(long chatId, long userSenderId, string userSenderUsername, bool isAdmin)
{
internal override ContextType ContextType => ContextType.Telegram;
ChatId = chatId;
UserSenderId = userSenderId;
UserSenderUsername = userSenderUsername;
IsAdmin = isAdmin;
}

public TelegramSenderInfo(long chatId, long userSenderId, string userSenderUsername, bool isAdmin)
: base(chatId, userSenderId, userSenderUsername, isAdmin)
{ }

private TelegramSenderInfoEntity ToEntity()
{
var entity = new TelegramSenderInfoEntity()
{
ChatId = ChatId,
UserSenderId = UserSenderId
};
return entity;
}
public long ChatId { get; }

public long UserSenderId { get; }

internal override IDialogContext GetOrCreateDialogContext(BotFrameworkDbContext dbContext)
{
var contextSenderInfo = TelegramSenderInfoEntity.GetOrCreate(this, dbContext);
var contextModel = DialogContextEntity.GetOrCreate(contextSenderInfo, ContextType, dbContext);

return new DialogContext(contextModel.State, contextModel.SenderInfoId, this, ContextType, dbContext);
}
public string UserSenderUsername { get; }

public bool IsAdmin { get; }

public TContext Accept<TContext>(ISenderInfoVisitor<TContext> visitor)
{
if (visitor is null)
throw new ArgumentNullException(nameof(visitor));

return visitor.Visit(this);
}
}
6 changes: 5 additions & 1 deletion Kysect.BotFramework/Core/BotManagerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using FluentScanning;
using Kysect.BotFramework.Abstactions.Services;
using Kysect.BotFramework.Abstractions.Commands;
using Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Abstractions.Visitors;
using Kysect.BotFramework.ApiProviders;
using Kysect.BotFramework.Attributes;
using Kysect.BotFramework.Core.Contexts.Providers;
using Kysect.BotFramework.Core.Exceptions;
using Kysect.BotFramework.Core.Tools;
using Kysect.BotFramework.Core.Tools.Extensions;
using Kysect.BotFramework.Core.Tools.Loggers;
using Kysect.BotFramework.Core.Visitors;
using Kysect.BotFramework.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -95,7 +98,8 @@ public BotManagerBuilder SetDatabaseOptions(Action<DbContextOptionsBuilder> opti
{
ServiceCollection
.AddDbContext<BotFrameworkDbContext>(optionsAction)
.AddScoped<IDialogContextProvider, StorageDialogContextProvider>();
.AddScoped<IDialogContextProvider, StorageDialogContextProvider>()
.AddScoped<ISenderInfoVisitor<IDialogContext>, DialogSenderInfoVisitor>();

return this;
}
Expand Down
73 changes: 34 additions & 39 deletions Kysect.BotFramework/Core/Contexts/DialogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,48 @@
using Kysect.BotFramework.Data;
using Kysect.BotFramework.Data.Entities;

namespace Kysect.BotFramework.Core.Contexts
namespace Kysect.BotFramework.Core.Contexts;

public class DialogContext : IDialogContext
{
public class DialogContext : IDialogContext
{
private readonly BotFrameworkDbContext _dbContext;
private readonly BotFrameworkDbContext _dbContext;

private readonly long _senderInfoId;
private readonly ContextType _contextType;

private int _state;
private readonly long _senderInfoId;
private readonly ContextType _contextType;

public ISenderInfo SenderInfo { get; }
private int _state;

public int State
public int State
{
get => _state;
set
{
get => _state;
set
{
_state = value;
SaveChanges();
}
_state = value;
SaveChanges();
}
}

public DialogContext(
int state,
long senderInfoId,
SenderInfo senderInfo,
ContextType contextType,
BotFrameworkDbContext dbContext)
{
SenderInfo = senderInfo;
_senderInfoId = senderInfoId;
_state = state;
_contextType = contextType;
_dbContext = dbContext;
}
public DialogContext(
int state,
long senderInfoId,
ContextType contextType,
BotFrameworkDbContext dbContext)
{
_senderInfoId = senderInfoId;
_state = state;
_contextType = contextType;
_dbContext = dbContext;
}

private void SaveChanges()
{
DialogContextEntity context = _dbContext.DialogContexts.FirstOrDefault(
x =>
x.SenderInfoId == _senderInfoId
&& x.ContextType == _contextType);
private void SaveChanges()
{
DialogContextEntity context = _dbContext.DialogContexts.FirstOrDefault(
x =>
x.SenderInfoId == _senderInfoId
&& x.ContextType == _contextType);

context!.State = State;
_dbContext.DialogContexts.Update(context);
_dbContext.SaveChanges();
}
context!.State = State;
_dbContext.DialogContexts.Update(context);
_dbContext.SaveChanges();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
using Kysect.BotFramework.Abstactions.Services;
using System;
using Kysect.BotFramework.Abstactions.Services;
using Kysect.BotFramework.Abstractions.Contexts;
using Kysect.BotFramework.Data;
using Kysect.BotFramework.Abstractions.Visitors;

namespace Kysect.BotFramework.Core.Contexts.Providers;

public class StorageDialogContextProvider : IDialogContextProvider
{
private readonly BotFrameworkDbContext _dbContext;
private readonly ISenderInfoVisitor<IDialogContext> _dialogSenderInfoVisitor;

public StorageDialogContextProvider(BotFrameworkDbContext dbContext)
=> _dbContext = dbContext;
public StorageDialogContextProvider(ISenderInfoVisitor<IDialogContext> dialogSenderInfoVisitor)
{
_dialogSenderInfoVisitor = dialogSenderInfoVisitor;
}

public IDialogContext GetDialogContext(ISenderInfo senderInfo)
=> (senderInfo as SenderInfo).GetOrCreateDialogContext(_dbContext);
{
if (senderInfo is null)
throw new ArgumentNullException(nameof(senderInfo));

return senderInfo.Accept(_dialogSenderInfoVisitor);
}
}
25 changes: 0 additions & 25 deletions Kysect.BotFramework/Core/Contexts/SenderInfo.cs

This file was deleted.

Loading