Skip to content

Commit

Permalink
Replace filter location strings with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
FeTetra committed Dec 10, 2024
1 parent 9672316 commit 3c7a3c7
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task<IActionResult> PostComment(string? username, string? slotType,
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -145,7 +146,7 @@ public async Task<IActionResult> Filter(IMailService mailService)

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -142,7 +143,7 @@ await this.database.SendNotification(user.UserId,
// 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)
{
Expand All @@ -153,7 +154,7 @@ await this.database.SendNotification(user.UserId,
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task<IActionResult> PostReview(int slotId)

// 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -80,7 +81,7 @@ public async Task<IActionResult> UpdateUser()

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -73,7 +74,7 @@ public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] stri
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] stri
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,7 +20,7 @@ public FilterTestPage(DatabaseContext database) : base(database)
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand Down Expand Up @@ -36,14 +37,14 @@ public async Task<IActionResult> OnPost([FromRoute] int slotId, [FromForm] strin

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;

Expand Down Expand Up @@ -57,7 +58,7 @@ [FromForm] string? language

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;
}
Expand Down
9 changes: 4 additions & 5 deletions ProjectLighthouse/Helpers/CensorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -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);
Expand Down Expand Up @@ -50,10 +51,8 @@ public static string FilterMessage(string message, string filterLocation = null,

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;
}
Expand Down
13 changes: 13 additions & 0 deletions ProjectLighthouse/Types/Filter/FilterLocation.cs
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 3c7a3c7

Please sign in to comment.