mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
Revert 41013783a4
This commit is contained in:
parent
404a6e5623
commit
a6f282e80c
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
@ -7,7 +8,7 @@ namespace JetHerald
|
||||
{
|
||||
public interface IChatCommand
|
||||
{
|
||||
string Execute(CommandString cmd, MessageEventArgs messageEventArgs);
|
||||
Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs);
|
||||
}
|
||||
|
||||
public class ChatCommandRouter
|
||||
@ -24,7 +25,7 @@ namespace JetHerald
|
||||
Commands = new Dictionary<string, IChatCommand>();
|
||||
}
|
||||
|
||||
public string Execute(object sender, MessageEventArgs args)
|
||||
public async Task<string> Execute(object sender, MessageEventArgs args)
|
||||
{
|
||||
var text = args.Message.Text;
|
||||
if (CommandString.TryParse(text, out var cmd))
|
||||
@ -39,7 +40,7 @@ namespace JetHerald
|
||||
try
|
||||
{
|
||||
Log.LogDebug($"Handling message via {Commands[cmd.Command].GetType().Name}");
|
||||
return Commands[cmd.Command].Execute(cmd, args);
|
||||
return await Commands[cmd.Command].Execute(cmd, args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
@ -14,7 +15,7 @@ namespace JetHerald.Commands
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public string Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
{
|
||||
if (cmd.Parameters.Length < 1)
|
||||
return null;
|
||||
@ -31,7 +32,7 @@ namespace JetHerald.Commands
|
||||
|
||||
try
|
||||
{
|
||||
var topic = db.CreateTopic(msg.From.Id, name, descr);
|
||||
var topic = await db.CreateTopic(msg.From.Id, name, descr);
|
||||
return $"created {topic.Name}\n" +
|
||||
$"readToken\n{topic.ReadToken}\n" +
|
||||
$"writeToken\n{topic.WriteToken}\n" +
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Telegram.Bot.Args;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace JetHerald.Commands
|
||||
@ -12,7 +13,7 @@ namespace JetHerald.Commands
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public string Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
{
|
||||
if (cmd.Parameters.Length < 2)
|
||||
return null;
|
||||
@ -25,7 +26,7 @@ namespace JetHerald.Commands
|
||||
string name = cmd.Parameters[0];
|
||||
string adminToken = cmd.Parameters[1];
|
||||
|
||||
var topic = db.DeleteTopic(name, adminToken);
|
||||
var topic = await db.DeleteTopic(name, adminToken);
|
||||
return $"deleted {name} and all its subscriptions";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
@ -12,11 +13,11 @@ namespace JetHerald
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public string Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
{
|
||||
var msg = messageEventArgs.Message;
|
||||
var chatid = msg.Chat.Id;
|
||||
var topics = db.GetTopicsForChat(chatid);
|
||||
var topics = await db.GetTopicsForChat(chatid);
|
||||
|
||||
return topics.Any()
|
||||
? "Topics:\n" + string.Join("\n", topics.Select(GetTopicListing))
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Telegram.Bot.Args;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
{
|
||||
@ -11,7 +12,7 @@ namespace JetHerald
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public string Execute(CommandString cmd, MessageEventArgs args)
|
||||
public async Task<string> Execute(CommandString cmd, MessageEventArgs args)
|
||||
{
|
||||
if (cmd.Parameters.Length < 1)
|
||||
return null;
|
||||
@ -19,7 +20,7 @@ namespace JetHerald
|
||||
var chatid = args.Message.Chat.Id;
|
||||
var token = cmd.Parameters[0];
|
||||
|
||||
var topic = db.GetTopic(token, chatid);
|
||||
var topic = await db.GetTopic(token, chatid);
|
||||
|
||||
if (topic == null)
|
||||
return "topic not found";
|
||||
@ -29,7 +30,7 @@ namespace JetHerald
|
||||
return "token mismatch";
|
||||
else
|
||||
{
|
||||
db.CreateSubscription(topic.TopicId, chatid);
|
||||
await db.CreateSubscription(topic.TopicId, chatid);
|
||||
return $"subscribed to {topic.Name}";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Telegram.Bot.Args;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Args;
|
||||
|
||||
namespace JetHerald
|
||||
{
|
||||
@ -11,7 +12,7 @@ namespace JetHerald
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public string Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
|
||||
{
|
||||
if (cmd.Parameters.Length < 1)
|
||||
return null;
|
||||
@ -19,7 +20,7 @@ namespace JetHerald
|
||||
var msg = messageEventArgs.Message;
|
||||
var chatid = msg.Chat.Id;
|
||||
var topicName = cmd.Parameters[0];
|
||||
int affected = db.RemoveSubscription(topicName, chatid);
|
||||
int affected = await db.RemoveSubscription(topicName, chatid);
|
||||
if (affected >= 1)
|
||||
return $"unsubscribed from {topicName}";
|
||||
else
|
||||
|
||||
@ -8,9 +8,9 @@ namespace JetHerald.Controllers
|
||||
public class EchoController : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public IActionResult Get(string msg)
|
||||
public async Task<IActionResult> Get(string msg)
|
||||
{
|
||||
Task.Delay(100);
|
||||
await Task.Delay(100);
|
||||
return Ok(msg);
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace JetHerald.Controllers
|
||||
@ -18,15 +19,15 @@ namespace JetHerald.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Post([FromBody] ReportArgs args)
|
||||
public async Task<IActionResult> Post([FromBody] ReportArgs args)
|
||||
{
|
||||
var t = Db.GetTopic(args.Topic);
|
||||
var t = await Db.GetTopic(args.Topic);
|
||||
if (t == null)
|
||||
return new NotFoundResult();
|
||||
else if (!t.WriteToken.Equals(args.WriteToken, StringComparison.OrdinalIgnoreCase))
|
||||
return StatusCode(403);
|
||||
|
||||
Herald.PublishMessage(t, args.Message);
|
||||
await Herald.PublishMessage(t, args.Message);
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Dapper;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JetHerald
|
||||
{
|
||||
@ -20,11 +21,11 @@ namespace JetHerald
|
||||
public long? ChatId { get; set; }
|
||||
}
|
||||
|
||||
public int DeleteTopic(string name, string adminToken)
|
||||
public async Task<int> DeleteTopic(string name, string adminToken)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
{
|
||||
return c.Execute(
|
||||
return await c.ExecuteAsync(
|
||||
" DELETE" +
|
||||
" FROM topic" +
|
||||
" WHERE Name = @name AND AdminToken = @adminToken",
|
||||
@ -32,20 +33,20 @@ namespace JetHerald
|
||||
}
|
||||
}
|
||||
|
||||
public Topic GetTopic(string name)
|
||||
public async Task<Topic> GetTopic(string name)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.QuerySingleOrDefault<Topic>(
|
||||
return await c.QuerySingleOrDefaultAsync<Topic>(
|
||||
"SELECT *" +
|
||||
" FROM topic" +
|
||||
" WHERE Name = @name",
|
||||
new { name });
|
||||
}
|
||||
|
||||
public Topic GetTopic(string token, long chatId)
|
||||
public async Task<Topic> GetTopic(string token, long chatId)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.QuerySingleOrDefault<Topic>(
|
||||
return await c.QuerySingleOrDefaultAsync<Topic>(
|
||||
" SELECT t.*, tc.ChatId " +
|
||||
" FROM topic t " +
|
||||
" LEFT JOIN topic_chat tc ON t.TopicId = tc.TopicId AND tc.ChatId = @chatId " +
|
||||
@ -53,7 +54,7 @@ namespace JetHerald
|
||||
new { token, chatId});
|
||||
}
|
||||
|
||||
public Topic CreateTopic(long userId, string name, string descr)
|
||||
public async Task<Topic> CreateTopic(long userId, string name, string descr)
|
||||
{
|
||||
var t = new Topic
|
||||
{
|
||||
@ -66,7 +67,7 @@ namespace JetHerald
|
||||
};
|
||||
using (var c = GetConnection())
|
||||
{
|
||||
return c.QuerySingleOrDefault<Topic>(
|
||||
return await c.QuerySingleOrDefaultAsync<Topic>(
|
||||
" INSERT INTO herald.topic " +
|
||||
" ( CreatorId, Name, Description, ReadToken, WriteToken, AdminToken) " +
|
||||
" VALUES " +
|
||||
@ -75,20 +76,20 @@ namespace JetHerald
|
||||
t);
|
||||
}
|
||||
}
|
||||
public IEnumerable<long> GetChatIdsForTopic(uint topicid)
|
||||
public async Task<IEnumerable<long>> GetChatIdsForTopic(uint topicid)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.Query<long>(
|
||||
return await c.QueryAsync<long>(
|
||||
" SELECT ChatId" +
|
||||
" FROM topic_chat" +
|
||||
" WHERE TopicId = @topicid",
|
||||
new { topicid });
|
||||
}
|
||||
|
||||
public IEnumerable<Topic> GetTopicsForChat(long chatid)
|
||||
public async Task<IEnumerable<Topic>> GetTopicsForChat(long chatid)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.Query<Topic>(
|
||||
return await c.QueryAsync<Topic>(
|
||||
" SELECT t.*" +
|
||||
" FROM topic_chat ct" +
|
||||
" JOIN topic t on t.TopicId = ct.TopicId" +
|
||||
@ -96,10 +97,10 @@ namespace JetHerald
|
||||
new { chatid });
|
||||
}
|
||||
|
||||
public void CreateSubscription(uint topicId, long chatId)
|
||||
public async Task CreateSubscription(uint topicId, long chatId)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
c.Execute(
|
||||
await c.ExecuteAsync(
|
||||
" INSERT INTO topic_chat" +
|
||||
" (ChatId, TopicId )" +
|
||||
" VALUES" +
|
||||
@ -107,10 +108,10 @@ namespace JetHerald
|
||||
new { topicId, chatId });
|
||||
}
|
||||
|
||||
public int RemoveSubscription(string topicName, long chatId)
|
||||
public async Task<int> RemoveSubscription(string topicName, long chatId)
|
||||
{
|
||||
using (var c = GetConnection())
|
||||
return c.Execute(
|
||||
return await c.ExecuteAsync(
|
||||
" DELETE tc " +
|
||||
" FROM topic_chat tc" +
|
||||
" JOIN topic t ON tc.TopicId = t.TopicId " +
|
||||
|
||||
@ -53,15 +53,15 @@ namespace JetHerald
|
||||
Client.StartReceiving();
|
||||
}
|
||||
|
||||
public void PublishMessage(Db.Topic topic, string message)
|
||||
public async Task PublishMessage(Db.Topic topic, string message)
|
||||
{
|
||||
var chatIds = Db.GetChatIdsForTopic(topic.TopicId);
|
||||
var chatIds = await Db.GetChatIdsForTopic(topic.TopicId);
|
||||
var formatted = $"|{topic.Description}|:\n{message}";
|
||||
foreach (var c in chatIds)
|
||||
_ = Client.SendTextMessageAsync(c, formatted);
|
||||
await Client.SendTextMessageAsync(c, formatted);
|
||||
}
|
||||
|
||||
void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
|
||||
async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
|
||||
{
|
||||
var msg = messageEventArgs.Message;
|
||||
if (msg == null || msg.Type != MessageType.Text)
|
||||
@ -69,9 +69,9 @@ namespace JetHerald
|
||||
|
||||
try
|
||||
{
|
||||
var reply = Commands.Execute(sender, messageEventArgs);
|
||||
var reply = await Commands.Execute(sender, messageEventArgs);
|
||||
if (reply != null)
|
||||
_ = Client.SendTextMessageAsync(
|
||||
await Client.SendTextMessageAsync(
|
||||
chatId: msg.Chat.Id,
|
||||
text: reply,
|
||||
replyToMessageId: msg.MessageId);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user