[reputation] Add rating.

This commit is contained in:
West14
2022-02-14 14:39:55 +02:00
parent f80b5d0da8
commit e885613c58
6 changed files with 95 additions and 11 deletions
+8
View File
@@ -0,0 +1,8 @@
namespace Kruzya.TelegramBot.Core.Data;
public interface IReputationEntity
{
public long UserId { get; set; }
public long ChatId { get; set; }
public double Value { get; set; }
}
+4 -1
View File
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kruzya.TelegramBot.Core.Data;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Service
@@ -9,5 +11,6 @@ namespace Kruzya.TelegramBot.Core.Service
public Task<double> SetUserReputationAsync(Chat chat, User user, double value);
public Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver);
public Task IncrementReputationAsync(Chat chat, User user, double diff);
public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0);
}
}
+49 -2
View File
@@ -3,7 +3,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BotFramework.Abstractions;
using Kruzya.TelegramBot.Core.Cache;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Service
@@ -11,10 +16,17 @@ namespace Kruzya.TelegramBot.Core.Service
public class UserService
{
private readonly List<long> _superUsers;
private readonly Dictionary<long, string> _statusMap;
private readonly ICacheStorage<long, User> _userCache;
private readonly ITelegramBotClient _bot;
private readonly ILogger<UserService> _logger;
public UserService(IConfiguration configuration)
public UserService(
IConfiguration configuration,
IBotInstance bot,
ICacheStorage<long, User> userCache,
ILogger<UserService> logger)
{
_superUsers = configuration.GetSection("SuperUsers")
.GetChildren()
@@ -24,6 +36,41 @@ namespace Kruzya.TelegramBot.Core.Service
_statusMap = configuration.GetSection("CustomMemberStatus")
.GetChildren()
.ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value);
_userCache = userCache;
_bot = bot.BotClient;
_logger = logger;
}
public async Task<User> GetUser(long userId, long chatId, bool bypassCache = false)
{
if (bypassCache || !_userCache.TryGetValue(userId, out var user))
{
if (bypassCache)
{
_logger.LogDebug("Bypassing cache.");
}
else
{
_logger.LogDebug("User {UserId} is not found in cache. Fetching from Telegram API.", userId);
}
user = await GetUserByChat(chatId, userId);
CacheUser(user);
}
return user;
}
private void CacheUser(User user)
{
// TODO: config entry for TTL
_userCache.SetValue(user.Id, user, Convert.ToInt32(TimeSpan.FromHours(1).TotalSeconds));
}
private async Task<User> GetUserByChat(long chatId, long userId)
{
return (await _bot.GetChatMemberAsync(chatId, userId)).User;
}
public bool IsUserSuper(User user)
+4 -4
View File
@@ -1,10 +1,10 @@
namespace West.TelegramBot.Reputation.Data;
using Kruzya.TelegramBot.Core.Data;
public class UserReputation
namespace West.TelegramBot.Reputation.Data;
public class UserReputation : IReputationEntity
{
public long UserId { get; set; }
public long ChatId { get; set; }
public double Value { get; set; }
}
+18 -3
View File
@@ -17,7 +17,7 @@ public class Reputation : CommonHandler
private readonly UserService _userService;
private readonly ILogger _logger;
private readonly IReputation _reputationService;
private static readonly List<string> RepUpChars = new()
{
"+",
@@ -43,8 +43,7 @@ public class Reputation : CommonHandler
CoreContext db,
UserService userService,
IReputation reputationService,
ILogger<Reputation> logger
) : base(db)
ILogger<Reputation> logger) : base(db)
{
_reputationService = reputationService;
_userService = userService;
@@ -129,6 +128,22 @@ public class Reputation : CommonHandler
);
}
[Command(InChat.Public, "top", CommandParseMode.Both)]
public async Task HandleTop()
{
var msg = new HtmlString();
msg.TextBr("Топ репутации чата: ");
var i = 1;
foreach (var rep in await _reputationService.GetChatRatingAsync(Chat))
{
var user = await _userService.GetUser(rep.UserId, rep.ChatId);
msg.Text($"{i}. ").UserMention(user).Text($" ({rep.Value})").Br();
i++;
}
await Bot.SendHtmlStringAsync(Chat, msg);
}
private async Task<double> GetUserReputation(User user)
{
return await _reputationService.GetUserReputationAsync(Chat, user);
@@ -1,4 +1,5 @@
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
@@ -54,6 +55,16 @@ public class ReputationService : IReputation
return Math.Round(senderValue == 0 ? 1 : Math.Sqrt(senderValue), 2);
}
public async Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0)
{
return await _reputationContext.UserReputation
.Where(r => r.ChatId == chat.Id && r.Value != 0D)
.OrderByDescending(r => r.Value)
.Skip(offset)
.Take(limit)
.ToListAsync();
}
private async Task<UserReputation?> FindUserReputation(Chat chat, User user)
{
return await _reputationContext.UserReputation.SingleOrDefaultAsync(