SS14-17313 Chatfactor: Chat Censorship Systems (#25908)
* SS14-17313 Chat Censorship Systems Adds some systems to manage chat censorship: 1. No-op: does nothing 2. SimpleCensor: a regex-free censor with a variety of rules to use 3. RegexCensor: a censor that uses regex. This exposes a singleton backed by a builder pattern (ChatCensor) that is set up, probably during the code init phase, and then globally available for your censorship needs. * Migrate to Shared * Add a reset function to the builder. * Resolve PJB's feedback; add unit tests
This commit is contained in:
committed by
GitHub
parent
bd58954d4e
commit
bf98a6a8bb
59
Content.Shared/Chat/V2/Moderation/ChatCensor.cs
Normal file
59
Content.Shared/Chat/V2/Moderation/ChatCensor.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace Content.Shared.Chat.V2.Moderation;
|
||||
|
||||
public interface IChatCensor
|
||||
{
|
||||
public bool Censor(string input, out string output, char replaceWith = '*');
|
||||
}
|
||||
|
||||
public sealed class CompoundChatCensor(IEnumerable<IChatCensor> censors) : IChatCensor
|
||||
{
|
||||
public bool Censor(string input, out string output, char replaceWith = '*')
|
||||
{
|
||||
var censored = false;
|
||||
|
||||
foreach (var censor in censors)
|
||||
{
|
||||
if (censor.Censor(input, out output, replaceWith))
|
||||
{
|
||||
censored = true;
|
||||
}
|
||||
}
|
||||
|
||||
output = input;
|
||||
|
||||
return censored;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ChatCensorFactory
|
||||
{
|
||||
private List<IChatCensor> _censors = new();
|
||||
|
||||
public void With(IChatCensor censor)
|
||||
{
|
||||
_censors.Add(censor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a ChatCensor that combines all the censors that have been added to this.
|
||||
/// </summary>
|
||||
public IChatCensor Build()
|
||||
{
|
||||
return new CompoundChatCensor(_censors.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the build state to zero, allowing for different rules to be provided to the next censor(s) built.
|
||||
/// </summary>
|
||||
/// <returns>True if the builder had any setup prior to the reset.</returns>
|
||||
public bool Reset()
|
||||
{
|
||||
var notEmpty = _censors.Count > 0;
|
||||
|
||||
_censors = new List<IChatCensor>();
|
||||
|
||||
return notEmpty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user