From f1464cb01c81ee484b3957b457e86f84e2ac6f14 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 18 Aug 2021 15:15:56 -0500 Subject: [PATCH] Search by user name and ID for chat commands --- application/ChatCommand.cpp | 28 ++++++++++++++++++++++++---- application/ChatCommand.h | 5 +++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/application/ChatCommand.cpp b/application/ChatCommand.cpp index 363f3e5..7588c0f 100644 --- a/application/ChatCommand.cpp +++ b/application/ChatCommand.cpp @@ -109,24 +109,26 @@ ChatCommand::_ProcessArgs(BString args, BMessage* msg, BString* errorMsg, { case CMD_ROOM_PARTICIPANT: { - if (chat->UserById(arg) == NULL) { + User* user = _FindUser(arg, chat->Users()); + if (user == NULL) { errorMsg->SetTo(B_TRANSLATE("%user% isn't a member of this " "room.")); errorMsg->ReplaceAll("%user%", arg); return false; } - msg->AddString("user_id", arg); + msg->AddString("user_id", user->GetId()); break; } case CMD_KNOWN_USER: { - if (chat->GetProtocolLooper()->UserById(arg) == NULL) { + User* user = _FindUser(arg, chat->GetProtocolLooper()->Users()); + if (user == NULL) { errorMsg->SetTo(B_TRANSLATE("You aren't contacts with and " "have no chats in common with %user%. Shame.")); errorMsg->ReplaceAll("%user%", arg); return false; } - msg->AddString("user_id", arg); + msg->AddString("user_id", user->GetId()); break; } case CMD_ANY_USER: @@ -146,6 +148,24 @@ ChatCommand::_ProcessArgs(BString args, BMessage* msg, BString* errorMsg, } +User* +ChatCommand::_FindUser(BString idOrName, UserMap users) +{ + if (idOrName.IsEmpty() == true) + return NULL; + + bool idFound = false; + User* user = users.ValueFor(idOrName, &idFound); + if (idFound == false) + for (int i = 0; i < users.CountItems(); i++) { + User* check = users.ValueAt(i); + if (check != NULL && check->GetName() == idOrName) + return check; + } + return user; +} + + bool ChatCommand::_Send(BMessage* msg, Conversation* chat) { diff --git a/application/ChatCommand.h b/application/ChatCommand.h index c18b83e..644980a 100644 --- a/application/ChatCommand.h +++ b/application/ChatCommand.h @@ -13,6 +13,9 @@ #include class Conversation; +class User; + +typedef KeyMap UserMap; enum cmd_arg_type @@ -46,6 +49,8 @@ private: bool _ProcessArgs(BString args, BMessage* msg, BString* errorMsg, Conversation* chat); + User* _FindUser(BString idOrName, UserMap users); + bool _Send(BMessage* msg, Conversation* chat); BString fName;