mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 23:31:13 -05:00
Mute Banner for Profile & Prevent Self-Target & Correctly Expire Early Unmutes (#272)
* Fix self-targeting
Remove creation of penalty on mute expiration
* Display mute penalties on profile
Expire mute penalties on unmute
* Resolves issues in code review
Added comment in ClientController.cs
Fixed order of operations in MuteManager.cs
Fixed condition in MuteManager.cs
* Fix self-targeting
Remove creation of penalty on mute expiration
* Display mute penalties on profile
Expire mute penalties on unmute
* Resolves issues in code review
Added comment in ClientController.cs
Fixed order of operations in MuteManager.cs
Fixed condition in MuteManager.cs
* Changed localisation value to be more generic
Fix null reference warning (it should never be null) (34da216
)
This commit is contained in:
@ -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 SharedLibraryCore;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
@ -13,13 +16,15 @@ public class MuteManager
|
||||
private readonly IMetaServiceV2 _metaService;
|
||||
private readonly ITranslationLookup _translationLookup;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IDatabaseContextFactory _databaseContextFactory;
|
||||
private readonly SemaphoreSlim _onMuteAction = new(1, 1);
|
||||
|
||||
public MuteManager(IMetaServiceV2 metaService, ITranslationLookup translationLookup, ILogger logger)
|
||||
public MuteManager(IServiceProvider serviceProvider)
|
||||
{
|
||||
_metaService = metaService;
|
||||
_translationLookup = translationLookup;
|
||||
_logger = logger;
|
||||
_metaService = serviceProvider.GetRequiredService<IMetaServiceV2>();
|
||||
_translationLookup = serviceProvider.GetRequiredService<ITranslationLookup>();
|
||||
_logger = serviceProvider.GetRequiredService<ILogger<MuteManager>>();
|
||||
_databaseContextFactory = serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
||||
}
|
||||
|
||||
public static bool IsExpiredMute(MuteStateMeta muteStateMeta) =>
|
||||
@ -98,11 +103,13 @@ public class MuteManager
|
||||
if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false;
|
||||
if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false;
|
||||
|
||||
if (clientMuteMeta.MuteState is not MuteState.Unmuting)
|
||||
if (clientMuteMeta.MuteState is not MuteState.Unmuting && origin.ClientId != 1)
|
||||
{
|
||||
await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason);
|
||||
}
|
||||
|
||||
await ExpireMutePenalties(target);
|
||||
|
||||
clientMuteMeta = new MuteStateMeta
|
||||
{
|
||||
Expiration = DateTime.UtcNow,
|
||||
@ -130,7 +137,6 @@ public class MuteManager
|
||||
Offender = target,
|
||||
Offense = reason,
|
||||
Punisher = origin,
|
||||
Active = true,
|
||||
Link = target.AliasLink
|
||||
};
|
||||
_logger.LogDebug("Creating new {MuteState} Penalty for {Target} with reason {Reason}",
|
||||
@ -138,6 +144,24 @@ public class MuteManager
|
||||
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)
|
||||
{
|
||||
if (client is null || !client.IsIngame) return;
|
||||
|
Reference in New Issue
Block a user