Fixed [JsonIgnore]

Fixed migration penalty creation
Fixed on migration command execution
Moved out CreatePenalty
Removed ClientId & AdminId since handled by Penalties
This commit is contained in:
Amos 2022-10-13 18:18:06 +01:00 committed by RaidMax
parent 44f22dae3a
commit 778feb8024
3 changed files with 53 additions and 59 deletions

View File

@ -33,7 +33,6 @@ public class MuteManager
var muteState = await ReadPersistentDataV1(client); var muteState = await ReadPersistentDataV1(client);
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {
ClientId = client.ClientId,
Reason = muteState is null ? string.Empty : _translationLookup["PLUGINS_MUTE_MIGRATED"], Reason = muteState is null ? string.Empty : _translationLookup["PLUGINS_MUTE_MIGRATED"],
Expiration = muteState switch Expiration = muteState switch
{ {
@ -41,7 +40,6 @@ public class MuteManager
MuteState.Muted => null, MuteState.Muted => null,
_ => DateTime.UtcNow _ => DateTime.UtcNow
}, },
AdminId = Utilities.IW4MAdminClient().ClientId,
MuteState = muteState ?? MuteState.Unmuted, MuteState = muteState ?? MuteState.Unmuted,
CommandExecuted = true CommandExecuted = true
}; };
@ -49,7 +47,10 @@ public class MuteManager
// Migrate old mute meta, else, client has no state, so set a generic one, but don't write it to database. // Migrate old mute meta, else, client has no state, so set a generic one, but don't write it to database.
if (muteState is not null) if (muteState is not null)
{ {
clientMuteMeta.CommandExecuted = false;
await WritePersistentData(client, clientMuteMeta); await WritePersistentData(client, clientMuteMeta);
await CreatePenalty(muteState.Value, Utilities.IW4MAdminClient(), client, clientMuteMeta.Expiration,
clientMuteMeta.Reason);
} }
else else
{ {
@ -64,25 +65,13 @@ public class MuteManager
var clientMuteMeta = await GetCurrentMuteState(target); var clientMuteMeta = await GetCurrentMuteState(target);
if (clientMuteMeta.MuteState is MuteState.Muted && clientMuteMeta.CommandExecuted) return false; if (clientMuteMeta.MuteState is MuteState.Muted && clientMuteMeta.CommandExecuted) return false;
var newPenalty = new EFPenalty await CreatePenalty(MuteState.Muted, origin, target, dateTime, reason);
{
Type = dateTime is null ? EFPenalty.PenaltyType.Mute : EFPenalty.PenaltyType.TempMute,
Expires = dateTime,
Offender = target,
Offense = reason,
Punisher = origin,
Link = target.AliasLink
};
_logger.LogDebug("Creating new mute for {Target} with reason {Reason}", target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {
ClientId = target.ClientId,
Expiration = dateTime, Expiration = dateTime,
MuteState = MuteState.Muted, MuteState = MuteState.Muted,
Reason = reason, Reason = reason,
AdminId = origin.ClientId,
CommandExecuted = false CommandExecuted = false
}; };
await WritePersistentData(target, clientMuteMeta); await WritePersistentData(target, clientMuteMeta);
@ -100,26 +89,13 @@ public class MuteManager
if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false; if (clientMuteMeta.MuteState is MuteState.Unmuted && clientMuteMeta.CommandExecuted) return false;
if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false; if (!target.IsIngame && clientMuteMeta.MuteState is MuteState.Unmuting) return false;
var newPenalty = new EFPenalty await CreatePenalty(MuteState.Unmuted, origin, target, DateTime.UtcNow, reason);
{
Type = EFPenalty.PenaltyType.Unmute,
Expires = DateTime.Now,
Offender = target,
Offense = reason,
Punisher = origin,
Active = true,
Link = target.AliasLink
};
_logger.LogDebug("Creating new unmute for {Target} with reason {Reason}", target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
clientMuteMeta = new MuteStateMeta clientMuteMeta = new MuteStateMeta
{ {
ClientId = target.ClientId,
Expiration = DateTime.UtcNow, Expiration = DateTime.UtcNow,
MuteState = target.IsIngame ? MuteState.Unmuted : MuteState.Unmuting, MuteState = target.IsIngame ? MuteState.Unmuted : MuteState.Unmuting,
Reason = reason, Reason = reason,
AdminId = origin.ClientId,
CommandExecuted = false CommandExecuted = false
}; };
await WritePersistentData(target, clientMuteMeta); await WritePersistentData(target, clientMuteMeta);
@ -131,6 +107,25 @@ public class MuteManager
return true; return true;
} }
private async Task CreatePenalty(MuteState muteState, EFClient origin, EFClient target, DateTime? dateTime,
string reason)
{
var newPenalty = new EFPenalty
{
Type = muteState is MuteState.Unmuted ? EFPenalty.PenaltyType.Unmute :
dateTime is null ? EFPenalty.PenaltyType.Mute : EFPenalty.PenaltyType.TempMute,
Expires = muteState is MuteState.Unmuted ? DateTime.UtcNow : dateTime,
Offender = target,
Offense = reason,
Punisher = origin,
Active = true,
Link = target.AliasLink
};
_logger.LogDebug("Creating new {MuteState} Penalty for {Target} with reason {Reason}",
nameof(muteState), target.Name, reason);
await newPenalty.TryCreatePenalty(Plugin.Manager.GetPenaltyService(), _logger);
}
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

@ -1,16 +1,13 @@
using Newtonsoft.Json; using System.Text.Json.Serialization;
namespace Mute; namespace Mute;
public class MuteStateMeta public class MuteStateMeta
{ {
public int ClientId { get; set; }
public string Reason { get; set; } = string.Empty; public string Reason { get; set; } = string.Empty;
public DateTime? Expiration { get; set; } public DateTime? Expiration { get; set; }
public int AdminId { get; set; }
public MuteState MuteState { get; set; } public MuteState MuteState { get; set; }
[JsonIgnore] [JsonIgnore] public bool CommandExecuted { get; set; }
public bool CommandExecuted { get; set; }
} }
public enum MuteState public enum MuteState

View File

@ -36,7 +36,6 @@ public class Plugin : IPlugin
{ {
if (!SupportedGames.Contains(server.GameName)) return; if (!SupportedGames.Contains(server.GameName)) return;
switch (gameEvent.Type) switch (gameEvent.Type)
{ {
case GameEvent.EventType.Command: case GameEvent.EventType.Command:
@ -129,13 +128,13 @@ public class Plugin : IPlugin
_interactionRegistration.RegisterInteraction(MuteInteraction, async (targetClientId, game, token) => _interactionRegistration.RegisterInteraction(MuteInteraction, async (targetClientId, game, token) =>
{ {
if (!targetClientId.HasValue || game.HasValue && !SupportedGames.Contains((Server.Game)game.Value)) if (!targetClientId.HasValue || game.HasValue && !SupportedGames.Contains((Server.Game) game.Value))
{ {
return null; return null;
} }
var clientMuteMetaState = var clientMuteMetaState =
(await MuteManager.GetCurrentMuteState(new EFClient { ClientId = targetClientId.Value })) (await MuteManager.GetCurrentMuteState(new EFClient {ClientId = targetClientId.Value}))
.MuteState; .MuteState;
var server = manager.GetServers().First(); var server = manager.GetServers().First();
@ -149,33 +148,34 @@ public class Plugin : IPlugin
return Task.CompletedTask; return Task.CompletedTask;
} }
private InteractionData CreateMuteInteraction(int targetClientId, Server server, Func<Type, string> getCommandNameFunc) private InteractionData CreateMuteInteraction(int targetClientId, Server server,
Func<Type, string> getCommandNameFunc)
{ {
var reasonInput = new var reasonInput = new
{ {
Name = "Reason", Name = "Reason",
Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_REASON"], Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_REASON"],
Type = "text", Type = "text",
Values = (Dictionary<string, string>?)null Values = (Dictionary<string, string>?) null
}; };
var durationInput = new var durationInput = new
{ {
Name = "Duration", Name = "Duration",
Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_DURATION"], Label = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_LABEL_DURATION"],
Type = "select", Type = "select",
Values = (Dictionary<string, string>?)new Dictionary<string, string> Values = (Dictionary<string, string>?) new Dictionary<string, string>
{ {
{ "5m", TimeSpan.FromMinutes(5).HumanizeForCurrentCulture() }, {"5m", TimeSpan.FromMinutes(5).HumanizeForCurrentCulture()},
{ "30m", TimeSpan.FromMinutes(30).HumanizeForCurrentCulture() }, {"30m", TimeSpan.FromMinutes(30).HumanizeForCurrentCulture()},
{ "1h", TimeSpan.FromHours(1).HumanizeForCurrentCulture() }, {"1h", TimeSpan.FromHours(1).HumanizeForCurrentCulture()},
{ "6h", TimeSpan.FromHours(6).HumanizeForCurrentCulture() }, {"6h", TimeSpan.FromHours(6).HumanizeForCurrentCulture()},
{ "1d", TimeSpan.FromDays(1).HumanizeForCurrentCulture() }, {"1d", TimeSpan.FromDays(1).HumanizeForCurrentCulture()},
{ "p", Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_SELECTION_PERMANENT"] } {"p", Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_ACTION_SELECTION_PERMANENT"]}
} }
}; };
var inputs = new[] { reasonInput, durationInput }; var inputs = new[] {reasonInput, durationInput};
var inputsJson = JsonSerializer.Serialize(inputs); var inputsJson = JsonSerializer.Serialize(inputs);
return new InteractionData return new InteractionData
@ -186,8 +186,8 @@ public class Plugin : IPlugin
ActionPath = "DynamicAction", ActionPath = "DynamicAction",
ActionMeta = new() ActionMeta = new()
{ {
{ "InteractionId", MuteInteraction }, {"InteractionId", MuteInteraction},
{ "Inputs", inputsJson }, {"Inputs", inputsJson},
{ {
"ActionButtonLabel", "ActionButtonLabel",
Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"] Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"]
@ -196,7 +196,7 @@ public class Plugin : IPlugin
"Name", "Name",
Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"] Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_MUTE"]
}, },
{ "ShouldRefresh", true.ToString() } {"ShouldRefresh", true.ToString()}
}, },
MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator, MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator,
Source = Name, Source = Name,
@ -230,7 +230,8 @@ public class Plugin : IPlugin
}; };
} }
private InteractionData CreateUnmuteInteraction(int targetClientId, Server server, Func<Type, string> getCommandNameFunc) private InteractionData CreateUnmuteInteraction(int targetClientId, Server server,
Func<Type, string> getCommandNameFunc)
{ {
var reasonInput = new var reasonInput = new
{ {
@ -239,7 +240,7 @@ public class Plugin : IPlugin
Type = "text", Type = "text",
}; };
var inputs = new[] { reasonInput }; var inputs = new[] {reasonInput};
var inputsJson = JsonSerializer.Serialize(inputs); var inputsJson = JsonSerializer.Serialize(inputs);
return new InteractionData return new InteractionData
@ -251,9 +252,9 @@ public class Plugin : IPlugin
ActionPath = "DynamicAction", ActionPath = "DynamicAction",
ActionMeta = new() ActionMeta = new()
{ {
{ "InteractionId", MuteInteraction }, {"InteractionId", MuteInteraction},
{ "Outputs", reasonInput.Name }, {"Outputs", reasonInput.Name},
{ "Inputs", inputsJson }, {"Inputs", inputsJson},
{ {
"ActionButtonLabel", "ActionButtonLabel",
Utilities.CurrentLocalization.LocalizationIndex[ Utilities.CurrentLocalization.LocalizationIndex[
@ -264,7 +265,7 @@ public class Plugin : IPlugin
Utilities.CurrentLocalization.LocalizationIndex[ Utilities.CurrentLocalization.LocalizationIndex[
"WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_UNMUTE"] "WEBFRONT_PROFILE_CONTEXT_MENU_ACTION_UNMUTE"]
}, },
{ "ShouldRefresh", true.ToString() } {"ShouldRefresh", true.ToString()}
}, },
MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator, MinimumPermission = Data.Models.Client.EFClient.Permission.Moderator,
Source = Name, Source = Name,
@ -283,7 +284,8 @@ public class Plugin : IPlugin
} }
var commandResponse = var commandResponse =
await _remoteCommandService.Execute(originId, targetId, getCommandNameFunc(typeof(UnmuteCommand)), args, server); await _remoteCommandService.Execute(originId, targetId, getCommandNameFunc(typeof(UnmuteCommand)),
args, server);
return string.Join(".", commandResponse.Select(result => result.Response)); return string.Join(".", commandResponse.Select(result => result.Response));
} }
}; };