Merge pull request #14 from Bubuni-Team/feature/reputation-improve

Draft: [reputation] /addrep command
This commit is contained in:
Andriy
2022-04-23 22:06:45 +03:00
committed by GitHub
3 changed files with 44 additions and 18 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ namespace Kruzya.TelegramBot.Core.Service
public Task<double> GetUserReputationAsync(Chat chat, User user);
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<double> IncrementReputationAsync(Chat chat, User user, double diff);
public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0);
}
}
+41 -15
View File
@@ -128,24 +128,26 @@ public class Reputation : CommonHandler
[ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)]
public async Task HandleSetRep(double reputation)
{
if (!_userService.IsUserSuper(From))
{
_logger.LogInformation("{User} attempted to call /setrep in {Chat}", From, Chat.Title);
return;
}
if (RepliedUser == null || RepliedUser.IsBot)
if (!CanModifyUserReputation(RepliedUser))
{
return;
}
reputation = await _reputationService.SetUserReputationAsync(Chat, RepliedUser, reputation);
await Reply(
new HtmlString()
.Code("Пользователю ")
.UserMention(RepliedUser)
.Code($" установлена репутация: {reputation}")
.ToString()
await SendReputationModifyResponse(
await _reputationService.SetUserReputationAsync(Chat, RepliedUser, reputation)
);
}
[ParametrizedCommand(InChat.Public, "addrep", CommandParseMode.Both)]
public async Task HandleAddRep(double reputation)
{
if (!CanModifyUserReputation(RepliedUser))
{
return;
}
await SendReputationModifyResponse(
await _reputationService.IncrementReputationAsync(Chat, RepliedUser, reputation)
);
}
@@ -158,7 +160,9 @@ public class Reputation : CommonHandler
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();
msg.Text($"{i}. ")
.Text($"{user.FirstName} {user.LastName}".Trim().HtmlEncode())
.Text($" ({rep.Value})").Br();
i++;
}
@@ -166,6 +170,28 @@ public class Reputation : CommonHandler
disableNotification: true);
}
private async Task SendReputationModifyResponse(double reputation)
{
await Reply(
new HtmlString()
.Code("Пользователю ")
.UserMention(RepliedUser)
.Code($" установлена репутация: {reputation}")
.ToString()
);
}
private bool CanModifyUserReputation(User? user)
{
if (!_userService.IsUserSuper(From))
{
_logger.LogInformation("{User} attempted to modify someones reputation in {Chat}", From, Chat.Title);
return false;
}
return user is {IsBot: false};
}
private async Task<double> GetUserReputation(User user)
{
return await _reputationService.GetUserReputationAsync(Chat, user);
@@ -37,9 +37,9 @@ public class ReputationService : IReputation
return rep.Value;
}
public async Task IncrementReputationAsync(Chat chat, User user, double diff)
public async Task<double> IncrementReputationAsync(Chat chat, User user, double diff)
{
await SetUserReputationAsync(chat, user, await GetUserReputationAsync(chat, user) + diff);
return await SetUserReputationAsync(chat, user, await GetUserReputationAsync(chat, user) + diff);
}
public async Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver)