Search by user name and ID for chat commands

This commit is contained in:
Jaidyn Ann 2021-08-18 15:15:56 -05:00
parent 03c9cdb249
commit f1464cb01c
2 changed files with 29 additions and 4 deletions

View File

@ -109,24 +109,26 @@ ChatCommand::_ProcessArgs(BString args, BMessage* msg, BString* errorMsg,
{ {
case CMD_ROOM_PARTICIPANT: 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 " errorMsg->SetTo(B_TRANSLATE("%user% isn't a member of this "
"room.")); "room."));
errorMsg->ReplaceAll("%user%", arg); errorMsg->ReplaceAll("%user%", arg);
return false; return false;
} }
msg->AddString("user_id", arg); msg->AddString("user_id", user->GetId());
break; break;
} }
case CMD_KNOWN_USER: 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 " errorMsg->SetTo(B_TRANSLATE("You aren't contacts with and "
"have no chats in common with %user%. Shame.")); "have no chats in common with %user%. Shame."));
errorMsg->ReplaceAll("%user%", arg); errorMsg->ReplaceAll("%user%", arg);
return false; return false;
} }
msg->AddString("user_id", arg); msg->AddString("user_id", user->GetId());
break; break;
} }
case CMD_ANY_USER: 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 bool
ChatCommand::_Send(BMessage* msg, Conversation* chat) ChatCommand::_Send(BMessage* msg, Conversation* chat)
{ {

View File

@ -13,6 +13,9 @@
#include <libsupport/List.h> #include <libsupport/List.h>
class Conversation; class Conversation;
class User;
typedef KeyMap<BString, User*> UserMap;
enum cmd_arg_type enum cmd_arg_type
@ -46,6 +49,8 @@ private:
bool _ProcessArgs(BString args, BMessage* msg, BString* errorMsg, bool _ProcessArgs(BString args, BMessage* msg, BString* errorMsg,
Conversation* chat); Conversation* chat);
User* _FindUser(BString idOrName, UserMap users);
bool _Send(BMessage* msg, Conversation* chat); bool _Send(BMessage* msg, Conversation* chat);
BString fName; BString fName;