Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #104 from dmitrydnl/v0.5
Browse files Browse the repository at this point in the history
V0.5
  • Loading branch information
marshtupa authored Nov 23, 2019
2 parents fa87f2f + 8beb37e commit 6ec3810
Show file tree
Hide file tree
Showing 66 changed files with 1,029 additions and 372 deletions.
2 changes: 1 addition & 1 deletion src/StashBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ Global
$4.scope = text/x-json
$0.StandardHeader = $5
$0.VersionControlPolicy = $6
version = 0.4
version = 0.5
EndGlobalSection
EndGlobal
30 changes: 15 additions & 15 deletions src/StashBot/BotResponses.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"welcomeMessage": "Hi, good to see you!",
"mainCommands": "Registration: /reg\nAuthorization: /auth\nInformation about me: /info\nClose chat in any time: /e or /exit",
"information": "This is open source bot for stashing in Telegram Messenger.\nThe code you can find here: https://github.com/dmitrydnl/StashBot",
"registrationWarning": "If you have already registered you will lose all your old data!\nAre you sure? /yes or /no",
"registrationReady": "Input your password or /cancel",
"successRegistration": "Success!\nNow you can auth with password",
"passwordEmpty": "Input password",
"passwordMinLength": "Password min length 12!",
"passwordMaxLength": "Password max length 25!",
"passwordCharacters": "Password can contain only letters, numbers and special characters!",
"authorisationReady": "Input your password or /back",
"successAuthorisation": "Success!",
"failAuthorisation": "WRONG",
"login": "Input message to save it in stash.\nGet messages in stash: /stash\nLogout: /logout",
"logout": "You're logged out"
"MainCommands": "🐾 /SignIn or /SignUp \n\n📘About: /Info\n🛑 Close immediately: /exit",
"Information": "This is open source bot for stashing in Telegram Messenger.\nThe code you can find here: https://github.com/dmitrydnl/StashBot",
"RegistrationWarning": "⚠️ If you have already registered, you'll have lost your data!\nAre you sure? /yes or /no",
"RegistrationReady": "Type password or /cancel",
"PasswordEmpty": "Type password",
"PasswordMinLength": "⚠️ Min password length = 12!",
"PasswordMaxLength": "⚠️ Max password length = 25!",
"PasswordCharacters": "⚠️ Password can contain only letters, numbers and special characters!",
"AuthorisationReady": "Type password or /back",
"Success": "✅ SUCCESS ",
"FailAuthorisation": "❌ WRONG",
"Login": "🛸 Send message to save it.\nGet messages: /Stash\nLogout: /logout",
"Logout": "👀 You've logged out",
"EmptyStash": "🕳 Stash is empty",
"FullStashError": "💯 Stash is full\nDelete your message!"
}
1 change: 1 addition & 0 deletions src/StashBot/BotSettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"chatSessionsClearInterval": 10,
"chatSessionLiveTime": 60,
"stashMessageLimit": 100,
"accountDatabaseType": "sqlite",
"stashDatabaseType": "sqlite"
}
4 changes: 3 additions & 1 deletion src/StashBot/StashBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<ReleaseVersion>0.4</ReleaseVersion>
<ReleaseVersion>0.5</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -42,6 +42,8 @@
<Folder Include="app\Module\Database\Stash\Sqlite\" />
<Folder Include="Migrations\Users\" />
<Folder Include="Migrations\StashMessages\" />
<Folder Include="app\CallbackQueryHandler\" />
<Folder Include="app\Module\Database\Stash\Errors\" />
</ItemGroup>
<ItemGroup>
<None Remove="BotSettings.json" />
Expand Down
8 changes: 4 additions & 4 deletions src/StashBot/app/BotResponses/ResponseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
{
internal enum ResponseType
{
WelcomeMessage,
MainCommands,
Information,
RegistrationWarning,
RegistrationReady,
SuccessRegistration,
PasswordEmpty,
PasswordMinLength,
PasswordMaxLength,
PasswordCharacters,
AuthorisationReady,
SuccessAuthorisation,
Success,
FailAuthorisation,
Login,
Logout
Logout,
EmptyStash,
FullStashError
}
}
31 changes: 13 additions & 18 deletions src/StashBot/app/BotResponses/TextResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

Expand All @@ -12,6 +13,10 @@ internal static class TextResponse

internal static string Get(ResponseType responseType)
{
if (responses == null)
{
SetUpResponses();
}
return responses[responseType];
}

Expand All @@ -23,24 +28,14 @@ internal static void SetUpResponses()
}

responses = new Dictionary<ResponseType, string>();
string text = File.ReadAllText(BOT_RESPONSES_FILE_NAME);
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(text);
string content = File.ReadAllText(BOT_RESPONSES_FILE_NAME);
Dictionary<string, string> jsonResponses = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);

responses.Add(ResponseType.WelcomeMessage, (string)jsonObject.welcomeMessage);
responses.Add(ResponseType.MainCommands, (string)jsonObject.mainCommands);
responses.Add(ResponseType.Information, (string)jsonObject.information);
responses.Add(ResponseType.RegistrationWarning, (string)jsonObject.registrationWarning);
responses.Add(ResponseType.RegistrationReady, (string)jsonObject.registrationReady);
responses.Add(ResponseType.SuccessRegistration, (string)jsonObject.successRegistration);
responses.Add(ResponseType.PasswordEmpty, (string)jsonObject.passwordEmpty);
responses.Add(ResponseType.PasswordMinLength, (string)jsonObject.passwordMinLength);
responses.Add(ResponseType.PasswordMaxLength, (string)jsonObject.passwordMaxLength);
responses.Add(ResponseType.PasswordCharacters, (string)jsonObject.passwordCharacters);
responses.Add(ResponseType.AuthorisationReady, (string)jsonObject.authorisationReady);
responses.Add(ResponseType.SuccessAuthorisation, (string)jsonObject.successAuthorisation);
responses.Add(ResponseType.FailAuthorisation, (string)jsonObject.failAuthorisation);
responses.Add(ResponseType.Login, (string)jsonObject.login);
responses.Add(ResponseType.Logout, (string)jsonObject.logout);
foreach (var response in jsonResponses)
{
Enum.TryParse(response.Key, out ResponseType responseType);
responses.Add(responseType, response.Value);
}
}
}
}
35 changes: 35 additions & 0 deletions src/StashBot/app/BotSettings/StashSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;
using Newtonsoft.Json;

namespace StashBot.BotSettings
{
internal static class StashSettings
{
private const string BOT_SETTINGS_FILE_NAME = "BotSettings.json";

private static bool isSetUp;
private static int stashMessageLimit;

internal static int StashMessageLimit
{
get
{
SetUpSettings();
return stashMessageLimit;
}
}

private static void SetUpSettings()
{
if (isSetUp)
{
return;
}

string text = File.ReadAllText(BOT_SETTINGS_FILE_NAME);
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(text);
stashMessageLimit = (int)jsonObject.stashMessageLimit;
isSetUp = true;
}
}
}
32 changes: 32 additions & 0 deletions src/StashBot/app/CallbackQueryHandler/DeleteMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using StashBot.Module;
using StashBot.Module.Database;
using StashBot.Module.Message;

namespace StashBot.CallbackQueryHandler
{
internal class DeleteMessageHandler : ICallbackQueryHandler
{
private const int MIN_PARAMETERS_COUNT = 3;

public void Handle(string[] queryArray, int messageId)
{
IDatabaseManager databaseManager = ModulesManager.GetDatabaseManager();
IMessageManager messageManager = ModulesManager.GetMessageManager();

if (queryArray.Length < MIN_PARAMETERS_COUNT)
{
return;
}

bool isChatIdParsed = long.TryParse(queryArray[1], out long chatId);
bool isDatabaseMessageIdParsed = long.TryParse(queryArray[2], out long databaseMessageId);
if (!isChatIdParsed || !isDatabaseMessageIdParsed)
{
return;
}

databaseManager.DeleteStashMessage(chatId, databaseMessageId);
messageManager.DeleteMessage(chatId, messageId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StashBot.CallbackQueryHandler
{
internal interface ICallbackQueryHandler
{
void Handle(string[] queryArray, int messageId);
}
}
2 changes: 1 addition & 1 deletion src/StashBot/app/ITelegramUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace StashBot
{
internal interface ITelegramUserMessage
public interface ITelegramUserMessage
{
long ChatId
{
Expand Down
2 changes: 1 addition & 1 deletion src/StashBot/app/ITelegramUserMessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace StashBot
{
internal interface ITelegramUserMessageFactory
public interface ITelegramUserMessageFactory
{
ITelegramUserMessage Create(Message telegramMessage);
}
Expand Down
2 changes: 1 addition & 1 deletion src/StashBot/app/Module/Database/Account/IUser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace StashBot.Module.Database
{
internal interface IUser
public interface IUser
{
long ChatId
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace StashBot.Module.Database.Account.Local
{
internal class DatabaseAccountLocal : IDatabaseAccount
public class DatabaseAccountLocal : IDatabaseAccount
{
private readonly Dictionary<long, IUser> usersDatabase;
private readonly IUserFactory userFactory;

internal DatabaseAccountLocal()
public DatabaseAccountLocal()
{
usersDatabase = new Dictionary<long, IUser>();
userFactory = new UserLocalFactory();
Expand Down
6 changes: 3 additions & 3 deletions src/StashBot/app/Module/Database/Account/Local/UserLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal UserLocal(long chatId, string password)
throw new ArgumentException("Password cannot be null");
}

ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

ChatId = chatId;
IsAuthorized = false;
Expand All @@ -47,7 +47,7 @@ public void Login(string password)
throw new ArgumentException("Password cannot be null");
}

ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

EncryptedPassword = secureManager.EncryptWithAes(password);
IsAuthorized = true;
Expand All @@ -61,7 +61,7 @@ public void Logout()

public bool ValidatePassword(string password)
{
ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

return secureManager.CompareWithHash(password, hashPassword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

namespace StashBot.Module.Database.Account.Sqlite
{
internal class DatabaseAccountSqlite : IDatabaseAccount
public class DatabaseAccountSqlite : IDatabaseAccount
{
private readonly Dictionary<long, IUser> authorizedUsers;
private readonly IUserFactory userFactory;

internal DatabaseAccountSqlite()
public DatabaseAccountSqlite()
{
authorizedUsers = new Dictionary<long, IUser>();
userFactory = new UserSqliteFactory();
}

public void CreateUser(long chatId, string password)
{
ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

if (IsUserExist(chatId))
{
Expand Down
4 changes: 2 additions & 2 deletions src/StashBot/app/Module/Database/Account/Sqlite/UserSqlite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Login(string password)
throw new ArgumentException("Password cannot be null");
}

ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

EncryptedPassword = secureManager.EncryptWithAes(password);
IsAuthorized = true;
Expand All @@ -59,7 +59,7 @@ public void Logout()

public bool ValidatePassword(string password)
{
ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();
ISecureManager secureManager = ModulesManager.GetSecureManager();

return secureManager.CompareWithHash(password, hashPassword);
}
Expand Down
43 changes: 18 additions & 25 deletions src/StashBot/app/Module/Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,19 @@ internal class DatabaseManager : IDatabaseManager

internal DatabaseManager()
{
switch (DatabaseSettings.AccountDatabaseType)
databaseAccount = DatabaseSettings.AccountDatabaseType switch
{
case DatabaseType.Local:
databaseAccount = new DatabaseAccountLocal();
break;
case DatabaseType.Sqlite:
databaseAccount = new DatabaseAccountSqlite();
break;
default:
databaseAccount = new DatabaseAccountLocal();
break;
}
DatabaseType.Local => new DatabaseAccountLocal(),
DatabaseType.Sqlite => new DatabaseAccountSqlite(),
_ => new DatabaseAccountLocal(),
};

switch (DatabaseSettings.StashDatabaseType)
databaseStash = DatabaseSettings.StashDatabaseType switch
{
case DatabaseType.Local:
databaseStash = new DatabaseStashLocal();
break;
case DatabaseType.Sqlite:
databaseStash = new DatabaseStashSqlite();
break;
default:
databaseStash = new DatabaseStashLocal();
break;
}
DatabaseType.Local => new DatabaseStashLocal(),
DatabaseType.Sqlite => new DatabaseStashSqlite(),
_ => new DatabaseStashLocal(),
};
}

public void CreateUser(long chatId, string password)
Expand Down Expand Up @@ -73,16 +61,21 @@ public IStashMessage CreateStashMessage(ITelegramUserMessage telegramMessage)
return databaseStash.CreateStashMessage(telegramMessage);
}

public void SaveMessageToStash(IStashMessage stashMessage)
public IDatabaseError SaveMessageToStash(IStashMessage stashMessage)
{
databaseStash.SaveMessageToStash(stashMessage);
return databaseStash.SaveMessageToStash(stashMessage);
}

public List<IStashMessage> GetMessagesFromStash(long chatId)
public ICollection<IStashMessage> GetMessagesFromStash(long chatId)
{
return databaseStash.GetMessagesFromStash(chatId);
}

public void DeleteStashMessage(long chatId, long databaseMessageId)
{
databaseStash.DeleteStashMessage(chatId, databaseMessageId);
}

public void ClearStash(long chatId)
{
databaseStash.ClearStash(chatId);
Expand Down
Loading

0 comments on commit 6ec3810

Please sign in to comment.