mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
Cleanups
This commit is contained in:
parent
41013783a4
commit
3ba9265b5c
@ -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>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using Telegram.Bot.Args;
|
||||||
using Telegram.Bot.Args;
|
|
||||||
|
|
||||||
namespace JetHerald
|
namespace JetHerald
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using Telegram.Bot.Args;
|
||||||
using Telegram.Bot.Args;
|
|
||||||
|
|
||||||
namespace JetHerald
|
namespace JetHerald
|
||||||
{
|
{
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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,14 +24,22 @@ 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)
|
||||||
@ -54,8 +47,10 @@ namespace JetHerald
|
|||||||
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,19 +78,33 @@ 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)
|
||||||
@ -103,17 +112,18 @@ namespace JetHerald
|
|||||||
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" +
|
||||||
|
" JOIN topic t ON tc.TopicId = t.TopicId " +
|
||||||
" WHERE t.Name = @topicName AND tc.ChatId = @chatId;",
|
" 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
21
JetHerald/TokenHelper.cs
Normal 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('/', '_');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user