Skip to content

Commit

Permalink
extracted the commands attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed Aug 17, 2024
1 parent 696ae00 commit 89b483b
Show file tree
Hide file tree
Showing 22 changed files with 34 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<ProjectReference Include="..\..\OneShelf.Telegram\OneShelf.Telegram\OneShelf.Telegram.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model;

Check warning on line 6 in OneShelf.OneDog/OneShelf.OneDog.Processor/Services/ChannelActions.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'OneShelf.Telegram.Model' appeared previously in this namespace
using Telegram.BotAPI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Database.Model.Enums;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using OneShelf.Billing.Api.Client;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.OneDog.Processor.Services.Commands.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Reflection;
using Microsoft.Extensions.Logging;
using OneShelf.OneDog.Processor.Model;
using OneShelf.OneDog.Processor.Services.Commands;
using OneShelf.OneDog.Processor.Services.Commands.Admin;
using OneShelf.OneDog.Processor.Services.Commands.DomainAdmin;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.IoMemories;

namespace OneShelf.OneDog.Processor.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Options;
using OneShelf.OneDog.Database;
using OneShelf.OneDog.Database.Model;
using OneShelf.OneDog.Processor.Model;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace OneShelf.Telegram.Processor.Model.CommandAttributes;
using OneShelf.Telegram.Model;

namespace OneShelf.Telegram.Processor.Model.CommandAttributes;

public class AdminCommandAttribute : CommandAttribute
{
public AdminCommandAttribute(string alias, string buttonDescription, string? helpDescription = null)
: base(alias, false, true, buttonDescription, helpDescription ?? buttonDescription, true, false)
: base(alias, false, true, buttonDescription, helpDescription ?? buttonDescription, Role.Admin)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace OneShelf.Telegram.Processor.Model.CommandAttributes;
using OneShelf.Telegram.Model;

namespace OneShelf.Telegram.Processor.Model.CommandAttributes;

public class BothCommandAttribute : CommandAttribute
{
public BothCommandAttribute(string alias, string buttonDescription, string? helpDescription = null)
: base(alias, false, true, buttonDescription, helpDescription ?? buttonDescription, true, true)
: base(alias, false, true, buttonDescription, helpDescription ?? buttonDescription, Role.Regular)
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public async Task UpdateCommands(List<CommandAttribute> commands)
Scope = new BotCommandScopeChat(_telegramOptions.AdminId),
});

await api.SetMyCommandsAsync(new SetMyCommandsArgs(commands.Where(x => x.SupportsNoParameters).Where(x => x.AppliesToRegular).Select(x => new BotCommand(x.Alias, x.ButtonDescription)))
await api.SetMyCommandsAsync(new SetMyCommandsArgs(commands.Where(x => x.SupportsNoParameters).Where(x => x.Role == Role.Regular).Select(x => new BotCommand(x.Alias, x.ButtonDescription)))
{
Scope = new BotCommandScopeDefault(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.Telegram.Processor.Model;
using OneShelf.Telegram.Processor.Model.CommandAttributes;
using OneShelf.Telegram.Processor.Services.Commands.Base;
using TelegramOptions = OneShelf.Telegram.Processor.Model.TelegramOptions;

namespace OneShelf.Telegram.Processor.Services.Commands.Admin;

Expand Down Expand Up @@ -32,7 +33,7 @@ private async Task Update()
{
await _channelActions.UpdateCommands(
_dialogHandlerMemory
.GetCommands(null)
.GetCommands(Role.Regular)
.Select(x => x.attribute)
.ToList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OneShelf.Telegram.Helpers;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.Ios;
using OneShelf.Telegram.Processor.Helpers;
using OneShelf.Telegram.Processor.Model;
using OneShelf.Telegram.Processor.Model.CommandAttributes;
using OneShelf.Telegram.Processor.Services.Commands.Base;
using TelegramOptions = OneShelf.Telegram.Processor.Model.TelegramOptions;

namespace OneShelf.Telegram.Processor.Services.Commands;

Expand Down Expand Up @@ -33,7 +34,7 @@ protected override async Task ExecuteQuickly()
Io.WriteLine();
Io.WriteLine("Помимо этого, вот чем я могу помочь:");
Io.WriteLine();
foreach (var command in _dialogHandlerMemory.GetCommands(Io.IsAdmin).Where(x => x.attribute.SupportsNoParameters))
foreach (var command in _dialogHandlerMemory.GetCommands(Io.IsAdmin ? Role.Admin : Role.Regular).Where(x => x.attribute.SupportsNoParameters))
{
Io.WriteLine($"/{command.attribute.Alias}: {command.attribute.HelpDescription}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Microsoft.Extensions.Logging;
using OneShelf.Telegram.Model;
using OneShelf.Telegram.Model.IoMemories;
using OneShelf.Telegram.Processor.Model.CommandAttributes;
using OneShelf.Telegram.Processor.Services.Commands;
Expand Down Expand Up @@ -159,23 +160,18 @@ public DialogHandlerMemory(ILogger<DialogHandlerMemory> logger)
_commands = commands.Select(x => x.Select(x => (x, attribute: x.GetCustomAttribute<CommandAttribute>())).ToList()).ToList();
}

public IEnumerable<(Type commandType, CommandAttribute attribute)> GetCommands(bool? admin) => _commands
.SelectMany(x => x).Where(x => admin switch
{
null => true,
true => x.attribute.AppliesToAdmins,
false => x.attribute.AppliesToRegular
});
public IEnumerable<(Type commandType, CommandAttribute attribute)> GetCommands(Role availableTo) => _commands
.SelectMany(x => x).Where(x => availableTo >= x.attribute.Role);

public IEnumerable<IEnumerable<CommandAttribute>> GetCommandsGrid(bool isAdmin) => _commands
.Select(x => x.Where(x => x.attribute.SupportsNoParameters).Where(x => isAdmin ? x.attribute.AppliesToAdmins : x.attribute.AppliesToRegular)
public IEnumerable<IEnumerable<CommandAttribute>> GetCommandsGrid(Role availableTo) => _commands
.Select(x => x.Where(x => x.attribute.SupportsNoParameters).Where(x => availableTo >= x.attribute.Role)
.Select(x => x.attribute).ToList())
.Where(x => x.Any())
.ToList();


public (Type commandType, CommandAttribute attribute) Help => GetCommands(true).Single(x => x.commandType == typeof(Help));
public (Type commandType, CommandAttribute attribute) Search => GetCommands(true).Single(x => x.commandType == typeof(Search));
public (Type commandType, CommandAttribute attribute) Help => GetCommands(Role.Regular).Single(x => x.commandType == typeof(Help));
public (Type commandType, CommandAttribute attribute) Search => GetCommands(Role.Regular).Single(x => x.commandType == typeof(Search));

public IoMemory? Get(long userId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override async Task<bool> HandleSync(Update update)
{
memory.NewInput(text);
_ioFactory.InitDialog(memory);
command = _dialogHandlerMemory.GetCommands(isAdmin).Single(x =>
command = _dialogHandlerMemory.GetCommands(isAdmin ? Role.Admin : Role.Regular).Single(x =>
x.attribute.Alias == memory.Alias && (memory.Parameters == null
? x.attribute.SupportsNoParameters
: x.attribute.SupportsParameters)).commandType;
Expand All @@ -124,7 +124,7 @@ protected override async Task<bool> HandleSync(Update update)
}
else // new command
{
command = _dialogHandlerMemory.GetCommands(isAdmin).SingleOrDefault(x =>
command = _dialogHandlerMemory.GetCommands(isAdmin ? Role.Admin : Role.Regular).SingleOrDefault(x =>
x.attribute.Alias == alias &&
(parameters != null ? x.attribute.SupportsParameters : x.attribute.SupportsNoParameters)).commandType;
if (command == null)
Expand Down Expand Up @@ -229,7 +229,7 @@ await api.SendMessageAsync(new(userId, finish.ReplyMessageBody.ToString())

if (completed)
{
finish.ReplyMessageMarkup = new ReplyKeyboardMarkup(_dialogHandlerMemory.GetCommandsGrid(isAdmin)
finish.ReplyMessageMarkup = new ReplyKeyboardMarkup(_dialogHandlerMemory.GetCommandsGrid(isAdmin ? Role.Admin : Role.Regular)
.Select(x => x
.Where(x => x.SupportsNoParameters)
.Select(x => new KeyboardButton($"/{x.Alias}: {x.ButtonDescription}"))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OneShelf.OneDog.Processor.Model;
namespace OneShelf.Telegram.Model;

public class CommandAttribute : Attribute
{
Expand All @@ -18,8 +18,8 @@ public CommandAttribute(string alias, bool supportsParameters, bool supportsNoPa
}

public string Alias { get; }
public bool SupportsParameters { get; }
public bool SupportsNoParameters { get; }
public bool SupportsParameters { get; init; }
public bool SupportsNoParameters { get; init; }
public string ButtonDescription { get; }
public string HelpDescription { get; }
public Role Role { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OneShelf.OneDog.Processor.Model;
namespace OneShelf.Telegram.Model;

public enum Role
{
Expand Down

0 comments on commit 89b483b

Please sign in to comment.