Cache info about users, cleanup for module

This commit is contained in:
2022-01-03 23:25:20 +03:00
parent 64a4a1fabb
commit cf34fcd89a
2 changed files with 26 additions and 5 deletions
+5
View File
@@ -1,4 +1,5 @@
using BotFramework; using BotFramework;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@@ -7,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core namespace Kruzya.TelegramBot.Core
{ {
@@ -36,6 +38,9 @@ namespace Kruzya.TelegramBot.Core
services.AddDbContext<CoreContext>(ctx => services.AddDbContext<CoreContext>(ctx =>
ctx.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))); ctx.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
// Cache for users.
services.AddSingleton<ICacheStorage<long, User>, MemoryCache<long, User>>();
// Trigger module handlers. // Trigger module handlers.
_core.Modules.ConfigureServices(services); _core.Modules.ConfigureServices(services);
} }
+21 -5
View File
@@ -8,9 +8,11 @@ using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using BotFramework.Utils; using BotFramework.Utils;
using Castle.Core.Internal; using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
namespace UserGroupTag namespace UserGroupTag
@@ -18,16 +20,17 @@ namespace UserGroupTag
public class Handler : BotEventHandler public class Handler : BotEventHandler
{ {
private readonly CoreContext _db; private readonly CoreContext _db;
private readonly ICacheStorage<long, User> _userCache;
public Handler(CoreContext db)
public Handler(CoreContext db, ICacheStorage<long, User> userCache)
{ {
_db = db; _db = db;
_userCache = userCache;
} }
[ParametrizedCommand(InChat.Public, "add_group", CommandParseMode.Both)] [ParametrizedCommand(InChat.Public, "add_group", CommandParseMode.Both)]
public async Task HandleAddGroup(string groupName) public async Task HandleAddGroup(string groupName)
{ {
Console.WriteLine("add_group");
// var groupName = group.Text; // var groupName = group.Text;
var repliedMessage = RawUpdate.Message!.ReplyToMessage; var repliedMessage = RawUpdate.Message!.ReplyToMessage;
@@ -56,6 +59,7 @@ namespace UserGroupTag
userGroupOption.SetValue(tagDictionary); userGroupOption.SetValue(tagDictionary);
_db.MarkAsModified(userGroupOption); _db.MarkAsModified(userGroupOption);
_userCache.SetValue(repliedMessage.From.Id, repliedMessage.From, Int32.MaxValue);
await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} добавлен в группу {groupName}", await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} добавлен в группу {groupName}",
ParseMode.Html); ParseMode.Html);
} }
@@ -79,13 +83,25 @@ namespace UserGroupTag
var msg = new HtmlString() var msg = new HtmlString()
.Text("Пользователь ") .Text("Пользователь ")
.UserMention(From) .UserMention(From)
.Text(" охуел и хочет доебаться к: "); .Text($" призывает группу пользователей {groupName}: ");
foreach (var userId in userList) foreach (var userId in userList)
{ {
msg.User(userId, userId.ToString()).Text(", "); msg.UserMention(await GetUser(userId, Chat)).Text(", ");
} }
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString().TrimEnd(',', ' '), ParseMode.Html); await Bot.SendTextMessageAsync(Chat.Id, msg.ToString().TrimEnd(',', ' '), ParseMode.Html);
} }
protected async Task<User> GetUser(long userId, Chat chat)
{
User user;
if (!_userCache.TryGetValue(userId, out user))
{
user = (await Bot.GetChatMemberAsync(Chat, userId)).User;
_userCache.SetValue(userId, user, Int32.MaxValue);
}
return user;
}
} }
} }