Skip to content

Commit

Permalink
Merge branch 'feature/refactoring-question-mark'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfour committed May 6, 2023
2 parents 5dc6c9f + a27727c commit af2ac0d
Show file tree
Hide file tree
Showing 55 changed files with 967 additions and 1,305 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vs
.vscode
*/bin
*/obj
*.user
*.user
28 changes: 0 additions & 28 deletions 3rd-party-licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,3 @@ Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.


Markdig -- https://github.com/lunet-io/markdig
==============================================

Copyright (c) 2018, Alexandre Mutel
All rights reserved.

Redistribution and use in source and binary forms, with or without modification
, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 bnfour
Copyright (c) 2018, 2020-2023 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
23 changes: 8 additions & 15 deletions WebToTelegramCore/BotCommands/AboutCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Reflection;

using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Options;
using WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
{
Expand All @@ -11,35 +11,28 @@ namespace WebToTelegramCore.BotCommands
/// </summary>
public class AboutCommand : BotCommandBase, IBotCommand
{
/// <summary>
/// Template to message, {0} is assembly version.
/// </summary>
private const string _template = "**Dotnet Telegram forwarder** v. {0}\n\n" +
"[Open-source!](https://github.com/bnfour/dotnet-telegram-forwarder) " +
"Powered by ASP.NET Core!\n" +
"Written by bnfour, August, October 2018.\n\nN<3";

/// <summary>
/// Command's text.
/// </summary>
public override string Command => "/about";

/// <summary>
/// Constructor that passed localization options to base class.
/// Constructor.
/// </summary>
/// <param name="locale">Localization options to use.</param>
public AboutCommand(LocalizationOptions locale) : base(locale) { }
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.</returns>
/// <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;
return base.Process(record) ?? String.Format(_template, version);
// imagine having to escape dot for "markdown"
var prettyVersion = $"{version.Major}\\.{version.Minor}";
return base.Process(record) ?? String.Format(Locale.About, prettyVersion);
}
}
}
22 changes: 7 additions & 15 deletions WebToTelegramCore/BotCommands/BotCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Options;
using WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
{
Expand All @@ -10,25 +11,15 @@ namespace WebToTelegramCore.BotCommands
/// </summary>
public abstract class BotCommandBase : IBotCommand
{
/// <summary>
/// Text somewhat explaining why processing of this Record
/// was cancelled in this class.
/// </summary>
private readonly string _error;

/// <summary>
/// Command text; not implemented in abstract classes.
/// </summary>
public abstract string Command { get; }

/// <summary>
/// Constructor that sets error message.
/// Constructor.
/// </summary>
/// <param name="locale">Locale options to use.</param>
public BotCommandBase(LocalizationOptions locale)
{
_error = locale.ErrorConfirmationPending;
}
public BotCommandBase() { }

/// <summary>
/// Method of abstract base class that filters out users with pending
Expand All @@ -39,8 +30,9 @@ public BotCommandBase(LocalizationOptions locale)
/// or null otherwise.</returns>
public virtual string Process(Record record)
{
return (record != null && record.State != RecordState.Normal) ?
_error : null;
return (!string.IsNullOrEmpty(record.Token) && record.State != RecordState.Normal)
? Locale.ErrorConfirmationPending
: null;
}
}
}
30 changes: 9 additions & 21 deletions WebToTelegramCore/BotCommands/CancelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Options;
using WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
{
/// <summary>
/// Class that implements /cancel command.
/// Class that implements /cancel command to cancel pending destructive
/// operations.
/// </summary>
public class CancelCommand : ConfirmationCommandBase, IBotCommand
{
/// <summary>
/// Reply text when deletion was cancelled.
/// </summary>
private readonly string _deletionCancel;

/// <summary>
/// Reply text when regeneration was cancelled.
/// </summary>
private readonly string _regenerationCancel;

/// <summary>
/// Command's text.
/// </summary>
public override string Command => "/cancel";

/// <summary>
/// Constructor that sets error message.
/// Constructor.
/// </summary>
/// <param name="locale">Locale options to use.</param>
public CancelCommand(LocalizationOptions locale) : base(locale)
{
_deletionCancel = locale.CancelDeletion;
_regenerationCancel = locale.CancelRegeneration;
}
public CancelCommand() : base() { }

/// <summary>
/// Method to process the command. Resets Record's State back to Normal.
Expand All @@ -48,8 +35,9 @@ public override string Process(Record record)
return baseResult;
}

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

record.State = RecordState.Normal;
return reply;
Expand Down
54 changes: 24 additions & 30 deletions WebToTelegramCore/BotCommands/ConfirmCommand.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
using System;
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Options;
using WebToTelegramCore.Services;
using WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
{
/// <summary>
/// Class that implements /cancel command which either deletes user's token or
/// replaces it with a new one.
/// Class that implements /confirm command which either deletes user's token or
/// replaces it with a new one after a request via previous command.
/// </summary>
public class ConfirmCommand : ConfirmationCommandBase, IBotCommand
{
/// <summary>
/// Message to display when token is deleted.
/// </summary>
private readonly string _deletion;

/// <summary>
/// Format string for message about token regeneration. The only argument {0}
/// is a newly generated token.
/// </summary>
private readonly string _regenration;

/// <summary>
/// Command's text.
/// </summary>
Expand All @@ -38,20 +27,23 @@ public class ConfirmCommand : ConfirmationCommandBase, IBotCommand
/// </summary>
private readonly ITokenGeneratorService _tokenGenerator;

/// <summary>
/// Record manipulation service helper reference.
/// </summary>
private readonly IRecordService _recordService;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="locale">Locale options to use.</param>
/// <param name="context">Database context to use.</param>
/// <param name="generator">Token generator to use.</param>
public ConfirmCommand(LocalizationOptions locale, RecordContext context,
ITokenGeneratorService generator) : base(locale)
/// <param name="recordService">Record helper to use.</param>
public ConfirmCommand(RecordContext context, ITokenGeneratorService generator,
IRecordService recordService) : base()
{
_context = context;
_tokenGenerator = generator;

_deletion = locale.ConfirmDeletion;
_regenration = locale.ConfirmRegeneration;
_recordService = recordService;
}

/// <summary>
Expand Down Expand Up @@ -85,16 +77,18 @@ public override string Process(Record record)
private string Regenerate(Record record)
{
string newToken = _tokenGenerator.Generate();
// so apparently, primary key cannot be changed
Record newRecord = new Record()
{
AccountNumber = record.AccountNumber,
Token = newToken
};
// so apparently, primary key cannot be changed,
// create a new record and transfer all data but token and state (it's pending regeneration right now)
var newRecord = _recordService.Create(newToken, record.AccountNumber);
// consider moving these to Create params with default values?
newRecord.UsageCounter = record.UsageCounter;
newRecord.LastSuccessTimestamp = record.LastSuccessTimestamp;

_context.Remove(record);
_context.Add(newRecord);
_context.SaveChanges();
return String.Format(_regenration, newToken);

return String.Format(Locale.ConfirmRegeneration, newToken);
}

/// <summary>
Expand All @@ -104,9 +98,9 @@ private string Regenerate(Record record)
/// <returns>Message about performed operation.</returns>
private string Delete(Record record)
{
_context.Records.Remove(record);
_context.Remove(record);
_context.SaveChanges();
return _deletion;
return Locale.ConfirmDeletion;
}
}
}
22 changes: 7 additions & 15 deletions WebToTelegramCore/BotCommands/ConfirmationCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using WebToTelegramCore.Data;
using WebToTelegramCore.Interfaces;
using WebToTelegramCore.Models;
using WebToTelegramCore.Options;
using WebToTelegramCore.Resources;

namespace WebToTelegramCore.BotCommands
{
Expand All @@ -11,25 +12,15 @@ namespace WebToTelegramCore.BotCommands
/// </summary>
public abstract class ConfirmationCommandBase : IBotCommand
{
/// <summary>
/// Text somewhat explaining why processing of this Record
/// was cancelled in this class.
/// </summary>
private readonly string _error;

/// <summary>
/// Command text; not implemented in abstract classes.
/// </summary>
public abstract string Command { get; }

/// <summary>
/// Constructor that sets error message.
/// Constructor.
/// </summary>
/// <param name="locale">Locale options to use.</param>
public ConfirmationCommandBase(LocalizationOptions locale)
{
_error = locale.ErrorNoConfirmationPending;
}
public ConfirmationCommandBase() { }

/// <summary>
/// Method of abstract base class that filters out users without pending
Expand All @@ -40,8 +31,9 @@ public ConfirmationCommandBase(LocalizationOptions locale)
/// or null otherwise.</returns>
public virtual string Process(Record record)
{
return (record == null || record.State == RecordState.Normal) ?
_error : null;
return (string.IsNullOrEmpty(record.Token) || record.State == RecordState.Normal)
? Locale.ErrorNoConfirmationPending
: null;
}
}
}
Loading

0 comments on commit af2ac0d

Please sign in to comment.