This commit is contained in:
jetsparrow 2019-08-13 21:59:33 +03:00
parent db6b6ccbc5
commit 4631830931
9 changed files with 75 additions and 50 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Telegram.Bot.Args; using Telegram.Bot.Args;
@ -16,10 +15,13 @@ namespace JetHerald
string Username { get; } string Username { get; }
ILogger Log { get; } ILogger Log { get; }
Dictionary<string, IChatCommand> Commands { get; }
public ChatCommandRouter(string username, ILogger log) public ChatCommandRouter(string username, ILogger log)
{ {
Log = log; Log = log;
Username = username; Username = username;
Commands = new Dictionary<string, IChatCommand>();
} }
public string Execute(object sender, MessageEventArgs args) public string Execute(object sender, MessageEventArgs args)
@ -32,12 +34,12 @@ namespace JetHerald
Log.LogDebug("Message not directed at us"); Log.LogDebug("Message not directed at us");
return null; return null;
} }
if (commands.ContainsKey(cmd.Command)) if (Commands.ContainsKey(cmd.Command))
{ {
try try
{ {
Log.LogDebug($"Handling message via {commands[cmd.Command].GetType().Name}"); Log.LogDebug($"Handling message via {Commands[cmd.Command].GetType().Name}");
return commands[cmd.Command].Execute(cmd, args); return Commands[cmd.Command].Execute(cmd, args);
} }
catch (Exception e) catch (Exception e)
{ {
@ -54,12 +56,10 @@ namespace JetHerald
{ {
foreach (var cmd in cmds) foreach (var cmd in cmds)
{ {
if (commands.ContainsKey(cmd)) if (Commands.ContainsKey(cmd))
throw new ArgumentException($"collision for {cmd}, commands {commands[cmd].GetType()} and {c.GetType()}"); throw new ArgumentException($"collision for {cmd}, commands {Commands[cmd].GetType()} and {c.GetType()}");
commands[cmd] = c; Commands[cmd] = c;
} }
} }
Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
} }
} }

View File

@ -1,5 +1,4 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Telegram.Bot.Args; using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;

View File

@ -1,5 +1,4 @@
using System.Threading.Tasks; using Telegram.Bot.Args;
using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
namespace JetHerald.Commands namespace JetHerald.Commands

View File

@ -1,5 +1,4 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Telegram.Bot.Args; using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald

View File

@ -1,5 +1,4 @@
using System.Threading.Tasks; using Telegram.Bot.Args;
using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald
{ {

View File

@ -1,5 +1,4 @@
using System.Threading.Tasks; using Telegram.Bot.Args;
using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald
{ {

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace JetHerald.Controllers namespace JetHerald.Controllers

View File

@ -1,25 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Dapper; using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Security.Cryptography;
namespace JetHerald 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 Db
{ {
public class Topic public class Topic
@ -39,23 +24,33 @@ namespace JetHerald
{ {
using (var c = GetConnection()) 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) public Topic GetTopic(string name)
{ {
using (var c = GetConnection()) using (var c = GetConnection())
return c.QuerySingleOrDefault<Topic>("SELECT * FROM topic WHERE Name = @name", new { name }); return c.QuerySingleOrDefault<Topic>(
"SELECT *" +
" FROM topic" +
" WHERE Name = @name",
new { name });
} }
public Topic GetTopic(string token, long chatId) public Topic GetTopic(string token, long chatId)
{ {
using (var c = GetConnection()) using (var c = GetConnection())
return c.QuerySingleOrDefault<Topic>( return c.QuerySingleOrDefault<Topic>(
"SELECT t.*, tc.ChatId " + " SELECT t.*, tc.ChatId " +
"FROM topic t LEFT JOIN topic_chat tc ON t.TopicId = tc.TopicId AND tc.ChatId = @chatId " + " FROM topic t " +
"WHERE ReadToken = @token", new { token, chatId}); " 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) public Topic CreateTopic(long userId, string name, string descr)
@ -73,9 +68,9 @@ namespace JetHerald
{ {
return c.QuerySingleOrDefault<Topic>( return c.QuerySingleOrDefault<Topic>(
" INSERT INTO herald.topic " + " INSERT INTO herald.topic " +
" (TopicId, CreatorId, Name, Description, ReadToken, WriteToken, AdminToken) " + " ( CreatorId, Name, Description, ReadToken, WriteToken, AdminToken) " +
" VALUES " + " VALUES " +
" (NULL, @CreatorId, @Name, @Description, @ReadToken, @WriteToken, @AdminToken); " + " (@CreatorId, @Name, @Description, @ReadToken, @WriteToken, @AdminToken); " +
" SELECT * FROM topic WHERE TopicId = LAST_INSERT_ID(); ", " SELECT * FROM topic WHERE TopicId = LAST_INSERT_ID(); ",
t); t);
} }
@ -83,37 +78,52 @@ namespace JetHerald
public IEnumerable<long> GetChatIdsForTopic(uint topicid) public IEnumerable<long> GetChatIdsForTopic(uint topicid)
{ {
using (var c = GetConnection()) using (var c = GetConnection())
return c.Query<long>("SELECT ChatId FROM topic_chat WHERE TopicId = @topicid", new { topicid }); return c.Query<long>(
" SELECT ChatId" +
" FROM topic_chat" +
" WHERE TopicId = @topicid",
new { topicid });
} }
public IEnumerable<Topic> GetTopicsForChat(long chatid) public IEnumerable<Topic> GetTopicsForChat(long chatid)
{ {
using (var c = GetConnection()) using (var c = GetConnection())
return c.Query<Topic>("SELECT t.* FROM topic_chat ct JOIN topic t on t.TopicId = ct.TopicId WHERE ct.ChatId = @chatid", new { chatid }); return c.Query<Topic>(
" 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) public void CreateSubscription(uint topicId, long chatId)
{ {
using (var c = GetConnection()) 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) public int RemoveSubscription(string topicName, long chatId)
{ {
using (var c = GetConnection()) using (var c = GetConnection())
return c.Execute( return c.Execute(
"DELETE tc " + " DELETE tc " +
"FROM topic_chat tc JOIN topic t ON tc.TopicId = t.TopicId " + " FROM topic_chat tc" +
"WHERE t.Name = @topicName AND tc.ChatId = @chatId;", " JOIN topic t ON tc.TopicId = t.TopicId " +
" WHERE t.Name = @topicName AND tc.ChatId = @chatId;",
new { topicName, chatId }); new { topicName, chatId });
} }
public Db(IOptions<Options.ConnectionStrings> cfg) public Db(IOptions<Options.ConnectionStrings> cfg)
{ {
Config = cfg; Config = cfg.Value;
} }
IOptions<Options.ConnectionStrings> Config { get; } Options.ConnectionStrings Config { get; }
MySqlConnection GetConnection() => new MySqlConnection(Config.Value.DefaultConnection); MySqlConnection GetConnection() => new MySqlConnection(Config.DefaultConnection);
} }
} }

21
JetHerald/TokenHelper.cs Normal file
View File

@ -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('/', '_');
}
}
}
}