Skip to content

Commit

Permalink
Merge branch 'version-2-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfour committed May 31, 2024
2 parents 09f1e9e + 08ad9d6 commit a9d40e9
Show file tree
Hide file tree
Showing 68 changed files with 2,812 additions and 1,749 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018, 2020-2023 bnfour
Copyright (c) 2018, 2020-2024 bnfour

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
66 changes: 33 additions & 33 deletions WebToTelegramCore/BotCommands/AboutCommand.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
using System;
using System.Reflection;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Resources;
using System.Reflection;
using Bnfour.WebToTelegramCore.Interfaces;
using Bnfour.WebToTelegramCore.Models;
using Bnfour.WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
namespace Bnfour.WebToTelegramCore.BotCommands;

/// <summary>
/// Class that handles /about command that shows general info about the bot.
/// </summary>
public class AboutCommand : BotCommandBase, IBotCommand
{
/// <summary>
/// Class that handles /about command that shows general info about the bot.
/// Command's text.
/// </summary>
public class AboutCommand : BotCommandBase, IBotCommand
{
/// <summary>
/// Command's text.
/// </summary>
public override string Command => "/about";
public override string Command => "/about";

/// <summary>
/// Constructor.
/// </summary>
public AboutCommand() : base() { }
public override string Description => Locale.AboutDescription;

/// <summary>
/// Constructor.
/// </summary>
public AboutCommand() : base() { }

/// <summary>
/// Method to process the command.
/// </summary>
/// <param name="record">Record associated with user who sent the command.
/// Unused here.</param>
/// <returns>Text of message that should be returned to user, with '.' escaped for MarkdownV2</returns>
public override string Process(Record record)
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
// imagine having to escape dot for "markdown"
var prettyVersion = $"{version.Major}\\.{version.Minor}";
#if DEBUG
prettyVersion += " debug";
#endif
return base.Process(record) ?? String.Format(Locale.About, prettyVersion);
}
/// <summary>
/// Method to process the command.
/// </summary>
/// <param name="record">Record associated with user who sent the command.
/// Unused here.</param>
/// <returns>Text of message that should be returned to user, with '.' escaped for MarkdownV2</returns>
public override string Process(Record record)
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
// imagine having to escape dot for "markdown"
var prettyVersion = $"{version.Major}\\.{version.Minor}";
#if DEBUG
prettyVersion += " debug";
#endif
return base.Process(record) ?? string.Format(Locale.About, prettyVersion);
}
}
65 changes: 35 additions & 30 deletions WebToTelegramCore/BotCommands/BotCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Resources;
using Bnfour.WebToTelegramCore.Data;
using Bnfour.WebToTelegramCore.Interfaces;
using Bnfour.WebToTelegramCore.Models;
using Bnfour.WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
namespace Bnfour.WebToTelegramCore.BotCommands;

/// <summary>
/// Base class for commands which are used to anything but to confirm or cancel
/// pending destructive operations.
/// </summary>
public abstract class BotCommandBase : IBotCommand
{
/// <summary>
/// Base class for commands which are used to anything but to confirm or cancel
/// pending destructive operations.
/// Command text; not implemented in abstract classes.
/// </summary>
public abstract class BotCommandBase : IBotCommand
{
/// <summary>
/// Command text; not implemented in abstract classes.
/// </summary>
public abstract string Command { get; }
public abstract string Command { get; }

/// <summary>
/// Short description to be shown alongside the command text in the bot menu
/// within the clients. If empty, the command will not be shown.
/// </summary>
public abstract string Description { get; }

/// <summary>
/// Constructor.
/// </summary>
public BotCommandBase() { }
/// <summary>
/// Constructor.
/// </summary>
public BotCommandBase() { }

/// <summary>
/// Method of abstract base class that filters out users with pending
/// cancellations or deletions of token.
/// </summary>
/// <param name="record">Record to process.</param>
/// <returns>Error message if there is an operation pending,
/// or null otherwise.</returns>
public virtual string Process(Record record)
{
return (!string.IsNullOrEmpty(record.Token) && record.State != RecordState.Normal)
? Locale.ErrorConfirmationPending
: null;
}
/// <summary>
/// Method of abstract base class that filters out users with pending
/// cancellations or deletions of token.
/// </summary>
/// <param name="record">Record to process.</param>
/// <returns>Error message if there is an operation pending,
/// or null otherwise.</returns>
public virtual string Process(Record record)
{
return (!string.IsNullOrEmpty(record.Token) && record.State != RecordState.Normal)
? Locale.ErrorConfirmationPending
: null;
}
}
84 changes: 47 additions & 37 deletions WebToTelegramCore/BotCommands/CancelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Resources;
using Bnfour.WebToTelegramCore.Data;
using Bnfour.WebToTelegramCore.Interfaces;
using Bnfour.WebToTelegramCore.Models;
using Bnfour.WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
namespace Bnfour.WebToTelegramCore.BotCommands;

/// <summary>
/// Class that implements /cancel command to cancel pending destructive
/// operations.
/// </summary>
public class CancelCommand : ConfirmationCommandBase, IBotCommand
{
/// <summary>
/// Class that implements /cancel command to cancel pending destructive
/// operations.
/// Command's text.
/// </summary>
public override string Command => "/cancel";

public override string Description => Locale.CancelDescription;

/// <summary>
/// Database context to use.
/// </summary>
private readonly RecordContext _context;

/// <summary>
/// Constructor.
/// </summary>
public class CancelCommand : ConfirmationCommandBase, IBotCommand
public CancelCommand(RecordContext context) : base()
{
/// <summary>
/// Command's text.
/// </summary>
public override string Command => "/cancel";

/// <summary>
/// Constructor.
/// </summary>
public CancelCommand() : base() { }

/// <summary>
/// Method to process the command. Resets Record's State back to Normal.
/// </summary>
/// <param name="record">Record associated with user who sent the command.</param>
/// <returns>Predefined text if all checks from parent classes passed,
/// corresponding error message otherwise.</returns>
public override string Process(Record record)
_context = context;
}

/// <summary>
/// Method to process the command. Resets Record's State back to Normal.
/// </summary>
/// <param name="record">Record associated with user who sent the command.</param>
/// <returns>Predefined text if all checks from parent classes passed,
/// corresponding error message otherwise.</returns>
public override string Process(Record record)
{
string baseResult = base.Process(record);
if (baseResult != null)
{
string baseResult = base.Process(record);
if (baseResult != null)
{
return baseResult;
}

string reply = record.State == RecordState.PendingDeletion
? Locale.CancelDeletion
: Locale.CancelRegeneration;

record.State = RecordState.Normal;
return reply;
return baseResult;
}

string reply = record.State == RecordState.PendingDeletion
? Locale.CancelDeletion
: Locale.CancelRegeneration;

record.State = RecordState.Normal;
_context.SaveChanges();
return reply;
}
}
Loading

0 comments on commit a9d40e9

Please sign in to comment.