Display message in chat when user changes nick

This commit is contained in:
Jaidyn Ann 2021-08-10 18:03:32 -05:00
parent 35cd5cbc8f
commit 12215266d0
3 changed files with 48 additions and 10 deletions

View File

@ -84,7 +84,8 @@ enum im_what_code {
* Messages related changes in general users. * Messages related changes in general users.
*/ */
/*! User's nick has changed →App */ /*! User's nick has changed →App
Requires: String "user_id", String "user_name" */
IM_USER_NICKNAME_SET = 40, IM_USER_NICKNAME_SET = 40,
/*! Received new status for user →App /*! Received new status for user →App
@ -256,7 +257,7 @@ enum im_what_code {
/*! User has explicitly joined →App /*! User has explicitly joined →App
Requires: String "chat_id", String "user_id" Requires: String "chat_id", String "user_id"
Accepts: String "body" */ Accepts: String "body", String "user_name" */
IM_ROOM_PARTICIPANT_JOINED = 160, IM_ROOM_PARTICIPANT_JOINED = 160,
/*! A user left the room →App /*! A user left the room →App

View File

@ -54,9 +54,8 @@ Conversation::~Conversation()
{ {
((TheApp*)be_app)->GetMainWindow()->RemoveConversation(this); ((TheApp*)be_app)->GetMainWindow()->RemoveConversation(this);
ProtocolLooper* looper = GetProtocolLooper(); if (fLooper != NULL)
if (looper != NULL) fLooper->RemoveConversation(this);
looper->RemoveConversation(this);
delete fChatView; delete fChatView;
delete fConversationItem; delete fConversationItem;
@ -237,6 +236,27 @@ Conversation::ImMessage(BMessage* msg)
GetView()->MessageReceived(msg); GetView()->MessageReceived(msg);
break; break;
} }
case IM_USER_NICKNAME_SET:
{
BString user_id = msg->FindString("user_id");
BString user_name = msg->FindString("user_name");
if (user_id.IsEmpty() == false && user_name.IsEmpty() == false) {
User* user = UserById(user_id);
BString text(B_TRANSLATE("** %old% has changed their nick to %new%."));
text.ReplaceAll("%new%", user_name);
if (user != NULL)
text.ReplaceAll("%old%", user->GetName());
else
text.ReplaceAll("%old%", user_id);
BMessage* notify = new BMessage(IM_MESSAGE);
notify->AddInt32("im_what", IM_MESSAGE_RECEIVED);
notify->AddString("body", text);
GetView()->MessageReceived(notify);
}
break;
}
case IM_LOGS_RECEIVED: case IM_LOGS_RECEIVED:
default: default:
GetView()->MessageReceived(msg); GetView()->MessageReceived(msg);
@ -611,9 +631,8 @@ Conversation::_EnsureUser(BMessage* msg)
NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems()); NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems());
} }
if (name.IsEmpty() == false) { if (name.IsEmpty() == false && user->GetName() != name)
user->SetNotifyName(name); user->SetNotifyName(name);
}
user->RegisterObserver(this); user->RegisterObserver(this);
return user; return user;
} }

View File

@ -288,15 +288,32 @@ Server::ImMessage(BMessage* msg)
} }
case IM_OWN_NICKNAME_SET: case IM_OWN_NICKNAME_SET:
{ {
BString nick = msg->FindString("user_name"); BString nick;
ProtocolLooper* looper = _LooperFromMessage(msg); ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL) if (looper == NULL || msg->FindString("user_name", &nick) != B_OK)
break; break;
Contact* contact = looper->GetOwnContact(); Contact* contact = looper->GetOwnContact();
if (contact != NULL) if (contact != NULL)
contact->SetNotifyName(nick.String()); contact->SetNotifyName(nick.String());
break; break;
} }
case IM_USER_NICKNAME_SET:
{
BString nick;
if (msg->FindString("user_name", &nick) != B_OK)
return B_SKIP_MESSAGE;
User* user = _EnsureUser(msg);
if (user == NULL)
break;
ChatMap conv = user->Conversations();
for (int i = 0; i < conv.CountItems(); i++)
conv.ValueAt(i)->ImMessage(msg);
user->SetNotifyName(nick);
break;
}
case IM_USER_STATUS_SET: case IM_USER_STATUS_SET:
{ {
int32 status; int32 status;
@ -984,7 +1001,8 @@ Server::_EnsureConversation(BMessage* message)
if (item == NULL) { if (item == NULL) {
item = new Conversation(chat_id, Looper()); item = new Conversation(chat_id, Looper());
item->SetProtocolLooper(looper); item->SetProtocolLooper(looper);
item->AddUser(looper->GetOwnContact()); if (looper->GetOwnContact() != NULL)
item->AddUser(looper->GetOwnContact());
looper->AddConversation(item); looper->AddConversation(item);
BMessage meta(IM_MESSAGE); BMessage meta(IM_MESSAGE);