1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-12 08:08:06 -05:00

Display mute penalties on profile

Expire mute penalties on unmute
This commit is contained in:
Amos
2022-10-21 21:36:10 +01:00
committed by RaidMax
parent fe08463e2c
commit 34da216f95
6 changed files with 50 additions and 19 deletions

View File

@ -1,4 +1,7 @@
using Data.Models; using Data.Abstractions;
using Data.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Database.Models; using SharedLibraryCore.Database.Models;
@ -13,13 +16,15 @@ public class MuteManager
private readonly IMetaServiceV2 _metaService; private readonly IMetaServiceV2 _metaService;
private readonly ITranslationLookup _translationLookup; private readonly ITranslationLookup _translationLookup;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IDatabaseContextFactory _databaseContextFactory;
private readonly SemaphoreSlim _onMuteAction = new(1, 1); private readonly SemaphoreSlim _onMuteAction = new(1, 1);
public MuteManager(IMetaServiceV2 metaService, ITranslationLookup translationLookup, ILogger logger) public MuteManager(IServiceProvider serviceProvider)
{ {
_metaService = metaService; _metaService = serviceProvider.GetRequiredService<IMetaServiceV2>();
_translationLookup = translationLookup; _translationLookup = serviceProvider.GetRequiredService<ITranslationLookup>();
_logger = logger; _logger = serviceProvider.GetRequiredService<ILogger<MuteManager>>();
_databaseContextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
} }
public static bool IsExpiredMute(MuteStateMeta muteStateMeta) => public static bool IsExpiredMute(MuteStateMeta muteStateMeta) =>
@ -103,6 +108,8 @@ public class MuteManager
await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason); await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason);
} }
await ExpireMutePenalties(target);
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {
Expiration = DateTime.UtcNow, Expiration = DateTime.UtcNow,
@ -130,7 +137,6 @@ public class MuteManager
Offender = target, Offender = target,
Offense = reason, Offense = reason,
Punisher = origin, Punisher = origin,
Active = true,
Link = target.AliasLink Link = target.AliasLink
}; };
_logger.LogDebug("Creating new {MuteState} Penalty for {Target} with reason {Reason}", _logger.LogDebug("Creating new {MuteState} Penalty for {Target} with reason {Reason}",
@ -138,6 +144,23 @@ public class MuteManager
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger); await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
} }
private async Task ExpireMutePenalties(EFClient client)
{
await using var context = _databaseContextFactory.CreateContext();
var mutePenalties = await context.Penalties
.Where(penalty => penalty.OffenderId == client.ClientId &&
penalty.Type == EFPenalty.PenaltyType.Mute || penalty.Type == EFPenalty.PenaltyType.TempMute &&
penalty.Expires == null || penalty.Expires > DateTime.UtcNow)
.ToListAsync();
foreach (var mutePenalty in mutePenalties)
{
mutePenalty.Expires = DateTime.UtcNow;
}
await context.SaveChangesAsync();
}
public static async Task PerformGameCommand(Server server, EFClient? client, MuteStateMeta muteStateMeta) public static async Task PerformGameCommand(Server server, EFClient? client, MuteStateMeta muteStateMeta)
{ {
if (client is null || !client.IsIngame) return; if (client is null || !client.IsIngame) return;

View File

@ -4,7 +4,7 @@ namespace Mute;
public class MuteStateMeta public class MuteStateMeta
{ {
public string Reason { get; set; } = string.Empty; public string? Reason { get; set; }
public DateTime? Expiration { get; set; } public DateTime? Expiration { get; set; }
public MuteState MuteState { get; set; } public MuteState MuteState { get; set; }
[JsonIgnore] public bool CommandExecuted { get; set; } [JsonIgnore] public bool CommandExecuted { get; set; }

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Data.Abstractions;
using Microsoft.Extensions.Logging;
using Mute.Commands; using Mute.Commands;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Commands; using SharedLibraryCore.Commands;
@ -24,12 +25,12 @@ public class Plugin : IPlugin
private readonly IRemoteCommandService _remoteCommandService; private readonly IRemoteCommandService _remoteCommandService;
private static readonly string MuteInteraction = "Webfront::Profile::Mute"; private static readonly string MuteInteraction = "Webfront::Profile::Mute";
public Plugin(ILogger<Plugin> logger, IMetaServiceV2 metaService, IInteractionRegistration interactionRegistration, public Plugin(ILogger<Plugin> logger, IInteractionRegistration interactionRegistration,
ITranslationLookup translationLookup, IRemoteCommandService remoteCommandService) IRemoteCommandService remoteCommandService, IServiceProvider serviceProvider)
{ {
_interactionRegistration = interactionRegistration; _interactionRegistration = interactionRegistration;
_remoteCommandService = remoteCommandService; _remoteCommandService = remoteCommandService;
MuteManager = new MuteManager(metaService, translationLookup, logger); MuteManager = new MuteManager(serviceProvider);
} }
public async Task OnEventAsync(GameEvent gameEvent, Server server) public async Task OnEventAsync(GameEvent gameEvent, Server server)

View File

@ -131,7 +131,7 @@ namespace SharedLibraryCore.Services
} }
private static readonly EFPenalty.PenaltyType[] LinkedPenalties = private static readonly EFPenalty.PenaltyType[] LinkedPenalties =
{ EFPenalty.PenaltyType.Ban, EFPenalty.PenaltyType.Flag, EFPenalty.PenaltyType.TempBan }; { EFPenalty.PenaltyType.Ban, EFPenalty.PenaltyType.Flag, EFPenalty.PenaltyType.TempBan, EFPenalty.PenaltyType.TempMute, EFPenalty.PenaltyType.Mute };
private static readonly Expression<Func<EFPenalty, bool>> Filter = p => private static readonly Expression<Func<EFPenalty, bool>> Filter = p =>
LinkedPenalties.Contains(p.Type) && p.Active && (p.Expires == null || p.Expires > DateTime.UtcNow); LinkedPenalties.Contains(p.Type) && p.Active && (p.Expires == null || p.Expires > DateTime.UtcNow);

View File

@ -162,7 +162,12 @@ namespace WebfrontCore.Controllers
}); });
} }
clientDto.ActivePenalty = activePenalties.OrderByDescending(_penalty => _penalty.Type).FirstOrDefault(); clientDto.ActivePenalty = activePenalties.MaxBy(penalty => penalty.Type switch
{
EFPenalty.PenaltyType.TempMute => 0,
EFPenalty.PenaltyType.Mute => 1,
_ => (int)penalty.Type
});
clientDto.Meta.AddRange(Authorized ? meta : meta.Where(m => !m.IsSensitive)); clientDto.Meta.AddRange(Authorized ? meta : meta.Where(m => !m.IsSensitive));
var strippedName = clientDto.Name.StripColors(); var strippedName = clientDto.Name.StripColors();

View File

@ -21,6 +21,8 @@
EFPenalty.PenaltyType.Ban => "alert-danger", EFPenalty.PenaltyType.Ban => "alert-danger",
EFPenalty.PenaltyType.Flag => "alert-secondary", EFPenalty.PenaltyType.Flag => "alert-secondary",
EFPenalty.PenaltyType.TempBan => "alert-secondary", EFPenalty.PenaltyType.TempBan => "alert-secondary",
EFPenalty.PenaltyType.TempMute => "alert-secondary",
EFPenalty.PenaltyType.Mute => "alert-secondary",
_ => "alert" _ => "alert"
}; };
} }
@ -367,7 +369,7 @@
}); });
} }
if ((Model.LevelInt < (int)ViewBag.User.Level && !Model.HasActivePenalty || isTempBanned) && ViewBag.Authorized) if ((Model.LevelInt < (int)ViewBag.User.Level && !isPermBanned || isTempBanned) && ViewBag.Authorized)
{ {
menuItems.Items.Add(new SideContextMenuItem menuItems.Items.Add(new SideContextMenuItem
{ {
@ -379,7 +381,7 @@
}); });
} }
if ((Model.LevelInt < (int)ViewBag.User.Level && Model.HasActivePenalty || isTempBanned) && ViewBag.Authorized) if ((Model.LevelInt < (int)ViewBag.User.Level && isPermBanned || isTempBanned) && ViewBag.Authorized)
{ {
menuItems.Items.Add(new SideContextMenuItem menuItems.Items.Add(new SideContextMenuItem
{ {