From e2d801b84b7bf48a004419374f97909c471680fb Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 31 Aug 2021 21:00:29 -0500 Subject: [PATCH] Save user name and color on message received MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- application/views/ConversationView.cpp | 40 ++++++++++++++------------ application/views/RenderView.cpp | 4 +-- application/views/RenderView.h | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index a5b1106..4285e57 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -387,6 +387,23 @@ ConversationView::_InitInterface() bool 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) msg->AddInt64("when", (int64)time(NULL)); @@ -423,31 +440,16 @@ ConversationView::_AppendMessage(BMessage* msg) } // Otherwise, it's message time! - int64 timeInt; - BString user_id; + int64 timeInt = msg->GetInt64("when", time(NULL)); BString user_name = msg->FindString("user_name"); + rgb_color userColor = msg->GetColor("user_color", ui_color(B_PANEL_TEXT_COLOR)); BString body; - rgb_color userColor = ui_color(B_PANEL_TEXT_COLOR); if (msg->FindString("body", &body) != B_OK) 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) { - fReceiveView->AppendGeneric(body); + fReceiveView->AppendGeneric(body, timeInt); return; } @@ -455,7 +457,7 @@ ConversationView::_AppendMessage(BMessage* msg) BString meMsg = "** "; meMsg << user_name.String() << " "; meMsg << body.RemoveFirst("/me "); - fReceiveView->AppendGeneric(meMsg.String()); + fReceiveView->AppendGeneric(meMsg.String(), timeInt); return; } diff --git a/application/views/RenderView.cpp b/application/views/RenderView.cpp index 6b45287..c267826 100644 --- a/application/views/RenderView.cpp +++ b/application/views/RenderView.cpp @@ -18,10 +18,10 @@ RenderView::RenderView(const char* name) void -RenderView::AppendGeneric(const char* message) +RenderView::AppendGeneric(const char* message, int64 when) { if (BString(message).IsEmpty() == true) return; - AppendTimestamp(time(NULL)); + AppendTimestamp(when); Append(message, ui_color(B_PANEL_TEXT_COLOR), B_BOLD_FACE); if (BString(message).EndsWith("\n") == false) Append("\n"); } diff --git a/application/views/RenderView.h b/application/views/RenderView.h index 1cc2d82..7c9d334 100644 --- a/application/views/RenderView.h +++ b/application/views/RenderView.h @@ -12,7 +12,7 @@ class RenderView : public RunView { public: 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 AppendTimestamp(time_t time = 0);