Replace filter location strings with enum

This commit is contained in:
FeTetra 2024-12-10 09:48:37 -05:00
commit 3c7a3c7bf5
12 changed files with 37 additions and 17 deletions

View file

@ -142,7 +142,7 @@ public class CommentController : ControllerBase
targetId = await this.database.UserIdFromUsername(username!);
}
string filteredText = CensorHelper.FilterMessage(comment.Message, "in-game comment", username);
string filteredText = CensorHelper.FilterMessage(comment.Message, Location.ChatMessage, username);
bool success = await this.database.PostComment(token.UserId, targetId, type, filteredText);
if (success) return this.Ok();

View file

@ -10,6 +10,7 @@ using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Entities.Notifications;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Mail;
using LBPUnion.ProjectLighthouse.Types.Serialization;
@ -145,7 +146,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
if (ServerConfiguration.Instance.LogChatMessages) Logger.Info($"{username}: \"{message}\"", LogArea.Filter);
message = CensorHelper.FilterMessage(message, "in-game message", username);
message = CensorHelper.FilterMessage(message, Location.ChatMessage, username);
return this.Ok(message);
}

View file

@ -10,6 +10,7 @@ using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Resources;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
@ -142,7 +143,7 @@ public class PublishController : ControllerBase
// Yes Rider, this isn't null
Debug.Assert(slot.Resources != null, "slot.ResourceList != null");
slot.Name = CensorHelper.FilterMessage(slot.Name, "slot name", user.Username);
slot.Name = CensorHelper.FilterMessage(slot.Name, Location.SlotName, user.Username);
if (slot.Name.Length > 64)
{
@ -153,7 +154,7 @@ public class PublishController : ControllerBase
return this.BadRequest();
}
slot.Description = CensorHelper.FilterMessage(slot.Description, "slot description", user.Username);
slot.Description = CensorHelper.FilterMessage(slot.Description, Location.SlotDescription, user.Username);
if (slot.Description.Length > 512)
{

View file

@ -101,7 +101,7 @@ public class ReviewController : ControllerBase
// Temporary fix until this can be refactored to use a UserEntity properly
string username = await this.database.UsernameFromGameToken(token);
newReview.Text = CensorHelper.FilterMessage(newReview.Text, "slot review", username);
newReview.Text = CensorHelper.FilterMessage(newReview.Text, Location.SlotReview, username);
if (newReview.Text.Length > 512) return this.BadRequest();

View file

@ -12,6 +12,7 @@ using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Authorization;
@ -80,7 +81,7 @@ public class UserController : ControllerBase
if (update.Biography.Length > 512) return this.BadRequest();
string filteredBio = CensorHelper.FilterMessage(update.Biography, "user biography", user.Username);
string filteredBio = CensorHelper.FilterMessage(update.Biography, Location.UserBiography, user.Username);
user.Biography = filteredBio;
}

View file

@ -6,6 +6,7 @@ using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -73,7 +74,7 @@ public class SlotPageController : ControllerBase
}
string username = await this.database.UsernameFromWebToken(token);
string filteredText = CensorHelper.FilterMessage(msg, "slot comment", username);
string filteredText = CensorHelper.FilterMessage(msg, Location.SlotReview, username);
bool success = await this.database.PostComment(token.UserId, id, CommentType.Level, filteredText);
if (success)

View file

@ -5,6 +5,7 @@ using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -49,7 +50,7 @@ public class UserPageController : ControllerBase
}
string username = await this.database.UsernameFromWebToken(token);
string filteredText = CensorHelper.FilterMessage(msg, "user comment", username);
string filteredText = CensorHelper.FilterMessage(msg, Location.UserComment, username);
bool success = await this.database.PostComment(token.UserId, id, CommentType.Profile, filteredText);
if (success)

View file

@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Helpers;
#endif
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using Microsoft.AspNetCore.Mvc;
@ -19,7 +20,7 @@ public class FilterTestPage : BaseLayout
public IActionResult OnGet(string? text = null)
{
#if DEBUG
if (text != null) this.FilteredText = CensorHelper.FilterMessage(text, "test filter");
if (text != null) this.FilteredText = CensorHelper.FilterMessage(text, Location.Test);
this.Text = text;
return this.Page();

View file

@ -5,6 +5,7 @@ using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Filter;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -36,14 +37,14 @@ public class SlotSettingsPage : BaseLayout
if (name != null)
{
name = CensorHelper.FilterMessage(name, "slot name", this.User.Username);
name = CensorHelper.FilterMessage(name, Location.SlotName, this.User.Username);
if (this.Slot.Name != name && name.Length <= 64)
this.Slot.Name = name;
}
if (description != null)
{
description = CensorHelper.FilterMessage(description, "slot description", this.User.Username);
description = CensorHelper.FilterMessage(description, Location.SlotDescription, this.User.Username);
if (this.Slot.Description != description && description.Length <= 512)
this.Slot.Description = description;
}

View file

@ -7,6 +7,7 @@ using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Localization;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Filter;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -57,7 +58,7 @@ public class UserSettingsPage : BaseLayout
if (this.ProfileUser.Biography != biography && biography.Length <= 512)
{
string filteredBio = CensorHelper.FilterMessage(biography, "user biography", this.ProfileUser.Username);
string filteredBio = CensorHelper.FilterMessage(biography, Location.UserBiography, this.ProfileUser.Username);
this.ProfileUser.Biography = filteredBio;
}

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
namespace LBPUnion.ProjectLighthouse.Helpers;
@ -19,7 +20,7 @@ public static class CensorHelper
"UwU", "OwO", "uwu", "owo", "o3o", ">.>", "*pounces on you*", "*boops*", "*baps*", ":P", "x3", "O_O", "xD", ":3", ";3", "^w^",
};
public static string FilterMessage(string message, string filterLocation = null, string username = null)
public static string FilterMessage(string message, Location filterLocation = Location.None, string username = null)
{
if (CensorConfiguration.Instance.UserInputFilterMode == FilterMode.None) return message;
StringBuilder stringBuilder = new(message);
@ -50,10 +51,8 @@ public static class CensorHelper
if (ServerConfiguration.Instance.LogChatFiltering && filteredMessage != message)
Logger.Info(
$"Comment sent {(username != null ? $"by {username} " : "")}" +
$"{(filterLocation != null ? $"from {filterLocation}" : "")}" +
$"\"{message}\" => \"{filteredMessage}\"",
LogArea.Filter);
$"Comment sent {(username != null ? $"by {username} " : "")}" + $"from {filterLocation}" +
$"\"{message}\" => \"{filteredMessage}\"", LogArea.Filter);
return filteredMessage;
}

View file

@ -0,0 +1,13 @@
namespace LBPUnion.ProjectLighthouse.Types.Filter;
public enum Location
{
SlotName = 0,
SlotDescription = 1,
SlotReview = 2,
UserBiography = 3,
UserComment = 4,
ChatMessage = 5,
Test = 6,
None = 7,
}