[Optimization] Handle channel name filter checks in memory (#2767)

* Handle channel name filter checks in memory

With this commit, name filters are loaded into memory when the server loads and new channels are compared against these memory values VS hitting the database each time.

* Minor formatting tweaks
This commit is contained in:
Vayle 2023-01-19 22:23:11 -05:00 committed by GitHub
parent 3424ae2dde
commit ba5b901c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 6 deletions

View File

@ -803,9 +803,19 @@ bool ChatChannelList::IsOnChannelBlockList(const std::string& channel_name) {
}
// Check if channel_name is already in the BlockedChannelNames vector
return Strings::Contains(ChatChannelList::GetBlockedChannelNames(), channel_name);
return Strings::Contains(GetBlockedChannelNames(), channel_name);
}
bool ChatChannelList::IsOnFilteredNameList(const std::string& name) {
if (name.empty()) {
return false;
}
// Check if name is already in the filtered name vector
return Strings::Contains(GetFilteredNames(), name);
}
void ChatChannelList::AddToChannelBlockList(const std::string& channel_name) {
if (channel_name.empty()) {
return;
@ -822,6 +832,19 @@ void ChatChannelList::AddToChannelBlockList(const std::string& channel_name) {
}
}
void ChatChannelList::AddToFilteredNames(const std::string& name) {
if (name.empty()) {
return;
}
// Add name to the filtered names vector if it is not already present
if (!Strings::Contains(ChatChannelList::GetFilteredNames(), name)) {
auto filtered_names = GetFilteredNames(); // Get current filter name list
filtered_names.push_back(name); // Add new name to local filtered names list
SetFilteredNameList(filtered_names); // Set filtered names list to match local filtered names list
}
}
void ServerToClient45SayLink(std::string& clientSayLink, const std::string& serverSayLink) {
if (serverSayLink.find('\x12') == std::string::npos) {
clientSayLink = serverSayLink;

View File

@ -84,14 +84,20 @@ public:
void SendAllChannels(Client *c);
void Process();
static inline std::vector<std::string> GetBlockedChannelNames() { return m_blocked_channel_names; }
static inline std::vector<std::string> GetFilteredNames() { return m_filtered_names; }
static inline void ClearChannelBlockList() { m_blocked_channel_names.clear(); };
static inline void ClearFilteredNameList() { m_filtered_names.clear(); };
static void AddToChannelBlockList(const std::string& channel_name);
static void AddToFilteredNames(const std::string& name);
static bool IsOnChannelBlockList(const std::string& channel_name);
static bool IsOnFilteredNameList(const std::string& channel_name);
static inline void SetChannelBlockList(std::vector<std::string> new_list) { m_blocked_channel_names = new_list; }
static inline void SetFilteredNameList(std::vector<std::string> new_list) { m_filtered_names = new_list; }
private:
LinkedList<ChatChannel*> ChatChannels;
static inline std::vector<std::string> m_blocked_channel_names;
static inline std::vector<std::string> m_filtered_names;
};

View File

@ -264,6 +264,22 @@ void UCSDatabase::LoadReservedNamesFromDB()
LogInfo("Loaded [{}] reserved channel name(s)", channels.size());
}
void UCSDatabase::LoadFilteredNamesFromDB()
{
ChatChannelList::ClearFilteredNameList();
auto names = NameFilterRepository::All(*this);
if (names.empty()) {
LogDebug("No filtered names exist in the database...");
}
for (const auto& e : names) {
ChatChannelList::AddToFilteredNames(e.name);
}
LogInfo("Loaded [{}] filtered channel name(s)", names.size());
}
bool UCSDatabase::IsChatChannelInDB(const std::string& channel_name)
{
auto r = ChatchannelsRepository::Count(
@ -362,11 +378,9 @@ bool UCSDatabase::CheckChannelNameFilter(const std::string& channel_name)
{
LogDebug("Checking if [{}] is on the name filter", channel_name);
// TODO: This should potentially just be pulled into memory at some other point
// This if fine for now
for (auto &e: NameFilterRepository::All(*this)) {
if (Strings::Contains(Strings::ToLower(channel_name), Strings::ToLower(e.name))) {
LogInfo("Failed to pass name filter check for [{}] against word [{}]", channel_name, e.name);
for (const auto &e: ChatChannelList::GetFilteredNames()) {
if (Strings::Contains(Strings::ToLower(channel_name), Strings::ToLower(e))) {
LogInfo("Failed to pass name filter check for [{}] against word [{}]", channel_name, e);
return false;
}
}

View File

@ -45,6 +45,7 @@ public:
bool GetVariable(const char* varname, char* varvalue, uint16 varvalue_len);
bool LoadChatChannels();
void LoadReservedNamesFromDB();
void LoadFilteredNamesFromDB();
bool IsChatChannelInDB(const std::string& channel_name);
bool CheckChannelNameFilter(const std::string& channel_name);
void SaveChatChannel(const std::string& channel_name, const std::string& channel_owner, const std::string& channel_password, const uint16& min_status);