Save user name and color on message received

In Chat-O-Matic, text messages are enqueued by the ConversationView
and appended when the ConversationView becomes attached to the window
(editing a non-attached BTextView doesn't go well).

Previously, only the receive-time was added to the enqueued message― so
that if the view doesn't come into focus for a long while after message
receipt, the timestamp is still accurate. The user's nickname and color
weren't added as well, meaning that if the user left the room before the
message was appended, the ugly user-id and default user color would be
used instead.

This appends user_color and user_name.
This commit is contained in:
Jaidyn Ann 2021-08-31 21:00:29 -05:00
parent 6a160ced88
commit e2d801b84b
3 changed files with 24 additions and 22 deletions

View File

@ -387,6 +387,23 @@ ConversationView::_InitInterface()
bool bool
ConversationView::_AppendOrEnqueueMessage(BMessage* msg) ConversationView::_AppendOrEnqueueMessage(BMessage* msg)
{ {
// Fill the message with user information not provided by protocol
BString user_id = msg->FindString("user_id");
if (msg->FindString("user_id", &user_id) == B_OK) {
User* user = NULL;
if (fConversation != NULL)
user = fConversation->UserById(user_id);
if (user != NULL) {
if (msg->HasString("user_name") == false)
if (user->GetName().IsEmpty() == false)
msg->AddString("user_name", user->GetName());
msg->AddColor("user_color", user->fItemColor);
}
if (msg->HasString("user_name") == false)
msg->AddString("user_name", user_id);
}
// Fill the message with receive time if not provided
if (msg->HasInt64("when") == false) if (msg->HasInt64("when") == false)
msg->AddInt64("when", (int64)time(NULL)); msg->AddInt64("when", (int64)time(NULL));
@ -423,31 +440,16 @@ ConversationView::_AppendMessage(BMessage* msg)
} }
// Otherwise, it's message time! // Otherwise, it's message time!
int64 timeInt; int64 timeInt = msg->GetInt64("when", time(NULL));
BString user_id;
BString user_name = msg->FindString("user_name"); BString user_name = msg->FindString("user_name");
rgb_color userColor = msg->GetColor("user_color", ui_color(B_PANEL_TEXT_COLOR));
BString body; BString body;
rgb_color userColor = ui_color(B_PANEL_TEXT_COLOR);
if (msg->FindString("body", &body) != B_OK) if (msg->FindString("body", &body) != B_OK)
return; return;
if (msg->FindInt64("when", &timeInt) != B_OK)
timeInt = (int64)time(NULL);
if (msg->FindString("user_id", &user_id) == B_OK) {
User* user = NULL;
if (fConversation != NULL
&& (user = fConversation->UserById(user_id)) != NULL) {
user_name = user->GetName();
userColor = user->fItemColor;
}
else if (user_name.IsEmpty() == true)
user_name = user_id;
}
if (user_name.IsEmpty() == true) { if (user_name.IsEmpty() == true) {
fReceiveView->AppendGeneric(body); fReceiveView->AppendGeneric(body, timeInt);
return; return;
} }
@ -455,7 +457,7 @@ ConversationView::_AppendMessage(BMessage* msg)
BString meMsg = "** "; BString meMsg = "** ";
meMsg << user_name.String() << " "; meMsg << user_name.String() << " ";
meMsg << body.RemoveFirst("/me "); meMsg << body.RemoveFirst("/me ");
fReceiveView->AppendGeneric(meMsg.String()); fReceiveView->AppendGeneric(meMsg.String(), timeInt);
return; return;
} }

View File

@ -18,10 +18,10 @@ RenderView::RenderView(const char* name)
void void
RenderView::AppendGeneric(const char* message) RenderView::AppendGeneric(const char* message, int64 when)
{ {
if (BString(message).IsEmpty() == true) return; if (BString(message).IsEmpty() == true) return;
AppendTimestamp(time(NULL)); AppendTimestamp(when);
Append(message, ui_color(B_PANEL_TEXT_COLOR), B_BOLD_FACE); Append(message, ui_color(B_PANEL_TEXT_COLOR), B_BOLD_FACE);
if (BString(message).EndsWith("\n") == false) Append("\n"); if (BString(message).EndsWith("\n") == false) Append("\n");
} }

View File

@ -12,7 +12,7 @@ class RenderView : public RunView {
public: public:
RenderView(const char* name); RenderView(const char* name);
void AppendGeneric(const char* message); void AppendGeneric(const char* message, int64 when);
void AppendUserstamp(const char* nick, rgb_color nameColor); void AppendUserstamp(const char* nick, rgb_color nameColor);
void AppendTimestamp(time_t time = 0); void AppendTimestamp(time_t time = 0);