From 4631830931b5e2ed11db14673d7273756825dbe8 Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Tue, 13 Aug 2019 21:59:33 +0300 Subject: [PATCH] Cleanups --- JetHerald/ChatCommandRouter.cs | 18 +++--- JetHerald/Commands/CreateTopicCommand.cs | 1 - JetHerald/Commands/DeleteTopicCommand.cs | 3 +- JetHerald/Commands/ListCommand.cs | 1 - JetHerald/Commands/SubscribeCommand.cs | 3 +- JetHerald/Commands/UnsubscribeCommand.cs | 3 +- JetHerald/Controllers/ReportController.cs | 1 - JetHerald/Db.cs | 74 +++++++++++++---------- JetHerald/TokenHelper.cs | 21 +++++++ 9 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 JetHerald/TokenHelper.cs diff --git a/JetHerald/ChatCommandRouter.cs b/JetHerald/ChatCommandRouter.cs index cd6533c..de0929e 100644 --- a/JetHerald/ChatCommandRouter.cs +++ b/JetHerald/ChatCommandRouter.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Telegram.Bot.Args; @@ -16,10 +15,13 @@ namespace JetHerald string Username { get; } ILogger Log { get; } + Dictionary Commands { get; } + public ChatCommandRouter(string username, ILogger log) { Log = log; Username = username; + Commands = new Dictionary(); } public string Execute(object sender, MessageEventArgs args) @@ -32,12 +34,12 @@ namespace JetHerald Log.LogDebug("Message not directed at us"); return null; } - if (commands.ContainsKey(cmd.Command)) + if (Commands.ContainsKey(cmd.Command)) { try { - Log.LogDebug($"Handling message via {commands[cmd.Command].GetType().Name}"); - return commands[cmd.Command].Execute(cmd, args); + Log.LogDebug($"Handling message via {Commands[cmd.Command].GetType().Name}"); + return Commands[cmd.Command].Execute(cmd, args); } catch (Exception e) { @@ -54,12 +56,10 @@ namespace JetHerald { foreach (var cmd in cmds) { - if (commands.ContainsKey(cmd)) - throw new ArgumentException($"collision for {cmd}, commands {commands[cmd].GetType()} and {c.GetType()}"); - commands[cmd] = c; + if (Commands.ContainsKey(cmd)) + throw new ArgumentException($"collision for {cmd}, commands {Commands[cmd].GetType()} and {c.GetType()}"); + Commands[cmd] = c; } } - - Dictionary commands = new Dictionary(); } } diff --git a/JetHerald/Commands/CreateTopicCommand.cs b/JetHerald/Commands/CreateTopicCommand.cs index fbcc4aa..335b8d2 100644 --- a/JetHerald/Commands/CreateTopicCommand.cs +++ b/JetHerald/Commands/CreateTopicCommand.cs @@ -1,5 +1,4 @@ using System.Linq; -using System.Threading.Tasks; using MySql.Data.MySqlClient; using Telegram.Bot.Args; using Telegram.Bot.Types.Enums; diff --git a/JetHerald/Commands/DeleteTopicCommand.cs b/JetHerald/Commands/DeleteTopicCommand.cs index 62ba7f5..0eef430 100644 --- a/JetHerald/Commands/DeleteTopicCommand.cs +++ b/JetHerald/Commands/DeleteTopicCommand.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using Telegram.Bot.Args; +using Telegram.Bot.Args; using Telegram.Bot.Types.Enums; namespace JetHerald.Commands diff --git a/JetHerald/Commands/ListCommand.cs b/JetHerald/Commands/ListCommand.cs index 433cdf3..455b65d 100644 --- a/JetHerald/Commands/ListCommand.cs +++ b/JetHerald/Commands/ListCommand.cs @@ -1,5 +1,4 @@ using System.Linq; -using System.Threading.Tasks; using Telegram.Bot.Args; namespace JetHerald diff --git a/JetHerald/Commands/SubscribeCommand.cs b/JetHerald/Commands/SubscribeCommand.cs index 290c851..e9d6f33 100644 --- a/JetHerald/Commands/SubscribeCommand.cs +++ b/JetHerald/Commands/SubscribeCommand.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using Telegram.Bot.Args; +using Telegram.Bot.Args; namespace JetHerald { diff --git a/JetHerald/Commands/UnsubscribeCommand.cs b/JetHerald/Commands/UnsubscribeCommand.cs index d9bc3d4..f292b4f 100644 --- a/JetHerald/Commands/UnsubscribeCommand.cs +++ b/JetHerald/Commands/UnsubscribeCommand.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using Telegram.Bot.Args; +using Telegram.Bot.Args; namespace JetHerald { diff --git a/JetHerald/Controllers/ReportController.cs b/JetHerald/Controllers/ReportController.cs index 4d0db7c..4316fbe 100644 --- a/JetHerald/Controllers/ReportController.cs +++ b/JetHerald/Controllers/ReportController.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.Serialization; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace JetHerald.Controllers diff --git a/JetHerald/Db.cs b/JetHerald/Db.cs index 02629f4..5cc5c87 100644 --- a/JetHerald/Db.cs +++ b/JetHerald/Db.cs @@ -1,25 +1,10 @@ using System.Collections.Generic; -using System.Threading.Tasks; +using Microsoft.Extensions.Options; using MySql.Data.MySqlClient; using Dapper; -using Microsoft.Extensions.Options; -using System; -using System.Security.Cryptography; namespace JetHerald { - public static class TokenHelper - { - static RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); - static byte[] buf = new byte[24]; - - public static string GetToken() - { - rng.GetBytes(buf); - return Convert.ToBase64String(buf).Replace('+', '_').Replace('/','_'); - } - } - public class Db { public class Topic @@ -39,23 +24,33 @@ namespace JetHerald { using (var c = GetConnection()) { - return c.Execute("DELETE FROM topic WHERE Name = @name AND AdminToken = @adminToken", new { name, adminToken }); + return c.Execute( + " DELETE" + + " FROM topic" + + " WHERE Name = @name AND AdminToken = @adminToken", + new { name, adminToken }); } } public Topic GetTopic(string name) { using (var c = GetConnection()) - return c.QuerySingleOrDefault("SELECT * FROM topic WHERE Name = @name", new { name }); + return c.QuerySingleOrDefault( + "SELECT *" + + " FROM topic" + + " WHERE Name = @name", + new { name }); } public Topic GetTopic(string token, long chatId) { using (var c = GetConnection()) return c.QuerySingleOrDefault( - "SELECT t.*, tc.ChatId " + - "FROM topic t LEFT JOIN topic_chat tc ON t.TopicId = tc.TopicId AND tc.ChatId = @chatId " + - "WHERE ReadToken = @token", new { token, chatId}); + " SELECT t.*, tc.ChatId " + + " FROM topic t " + + " LEFT JOIN topic_chat tc ON t.TopicId = tc.TopicId AND tc.ChatId = @chatId " + + " WHERE ReadToken = @token", + new { token, chatId}); } public Topic CreateTopic(long userId, string name, string descr) @@ -73,9 +68,9 @@ namespace JetHerald { return c.QuerySingleOrDefault( " INSERT INTO herald.topic " + - " (TopicId, CreatorId, Name, Description, ReadToken, WriteToken, AdminToken) " + + " ( CreatorId, Name, Description, ReadToken, WriteToken, AdminToken) " + " VALUES " + - " (NULL, @CreatorId, @Name, @Description, @ReadToken, @WriteToken, @AdminToken); " + + " (@CreatorId, @Name, @Description, @ReadToken, @WriteToken, @AdminToken); " + " SELECT * FROM topic WHERE TopicId = LAST_INSERT_ID(); ", t); } @@ -83,37 +78,52 @@ namespace JetHerald public IEnumerable GetChatIdsForTopic(uint topicid) { using (var c = GetConnection()) - return c.Query("SELECT ChatId FROM topic_chat WHERE TopicId = @topicid", new { topicid }); + return c.Query( + " SELECT ChatId" + + " FROM topic_chat" + + " WHERE TopicId = @topicid", + new { topicid }); } public IEnumerable GetTopicsForChat(long chatid) { using (var c = GetConnection()) - return c.Query("SELECT t.* FROM topic_chat ct JOIN topic t on t.TopicId = ct.TopicId WHERE ct.ChatId = @chatid", new { chatid }); + return c.Query( + " SELECT t.*" + + " FROM topic_chat ct" + + " JOIN topic t on t.TopicId = ct.TopicId" + + " WHERE ct.ChatId = @chatid", + new { chatid }); } public void CreateSubscription(uint topicId, long chatId) { using (var c = GetConnection()) - c.Execute("INSERT INTO topic_chat (ChatId, TopicId ) VALUES (@chatId, @topicId)", new { topicId, chatId }); + c.Execute( + " INSERT INTO topic_chat" + + " (ChatId, TopicId )" + + " VALUES" + + " (@chatId, @topicId)", + new { topicId, chatId }); } public int RemoveSubscription(string topicName, long chatId) { using (var c = GetConnection()) return c.Execute( - "DELETE tc " + - "FROM topic_chat tc JOIN topic t ON tc.TopicId = t.TopicId " + - "WHERE t.Name = @topicName AND tc.ChatId = @chatId;", + " DELETE tc " + + " FROM topic_chat tc" + + " JOIN topic t ON tc.TopicId = t.TopicId " + + " WHERE t.Name = @topicName AND tc.ChatId = @chatId;", new { topicName, chatId }); } public Db(IOptions cfg) { - Config = cfg; + Config = cfg.Value; } - IOptions Config { get; } - MySqlConnection GetConnection() => new MySqlConnection(Config.Value.DefaultConnection); + Options.ConnectionStrings Config { get; } + MySqlConnection GetConnection() => new MySqlConnection(Config.DefaultConnection); } } diff --git a/JetHerald/TokenHelper.cs b/JetHerald/TokenHelper.cs new file mode 100644 index 0000000..aae69f7 --- /dev/null +++ b/JetHerald/TokenHelper.cs @@ -0,0 +1,21 @@ +using System; +using System.Security.Cryptography; + +namespace JetHerald +{ + public static class TokenHelper + { + static RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); + static byte[] buf = new byte[24]; + static readonly object SyncLock = new object(); + + public static string GetToken() + { + lock (SyncLock) + { + rng.GetBytes(buf); + return Convert.ToBase64String(buf).Replace('+', '_').Replace('/', '_'); + } + } + } +}