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.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<string, IChatCommand> Commands { get; }
|
||||
|
||||
public ChatCommandRouter(string username, ILogger log)
|
||||
{
|
||||
Log = log;
|
||||
Username = username;
|
||||
Commands = new Dictionary<string, IChatCommand>();
|
||||
}
|
||||
|
||||
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<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Telegram.Bot.Args;
|
||||
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;
|
||||
|
||||
namespace JetHerald.Commands
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
{
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
{
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace JetHerald.Controllers
|
||||
|
||||
@ -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<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)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.QuerySingleOrDefault<Topic>(
|
||||
"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<Topic>(
|
||||
" 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<long> GetChatIdsForTopic(uint topicid)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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<Options.ConnectionStrings> cfg)
|
||||
{
|
||||
Config = cfg;
|
||||
Config = cfg.Value;
|
||||
}
|
||||
|
||||
IOptions<Options.ConnectionStrings> Config { get; }
|
||||
MySqlConnection GetConnection() => new MySqlConnection(Config.Value.DefaultConnection);
|
||||
Options.ConnectionStrings Config { get; }
|
||||
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