Re-enable logging, make appending messages to ConversationView safer

Now all instances of sending messages (bar status changes) to a
conversation's CayaRenderView only happen when attached to the window.

Messages sent by user appear after the protocol confirms it's been sent
(IM_MESSAGE_SENT), rather than appearing right after the user sends it
to the protocol (IM_SEND_MESSAGE).

Logs are now re-enabled (since they can safely be appended under any
circumstance).
This commit is contained in:
Jaidyn Ann 2021-05-30 19:07:50 -05:00
parent 3e202e3609
commit cd3eb3cbf3
4 changed files with 79 additions and 26 deletions

View File

@ -57,12 +57,18 @@ Conversation::ImMessage(BMessage* msg)
GetView()->MessageReceived(msg); GetView()->MessageReceived(msg);
break; break;
} }
case IM_SEND_MESSAGE: case IM_MESSAGE_SENT:
{ {
_LogChatMessage(msg); _LogChatMessage(msg);
GetView()->MessageReceived(msg);
break;
}
case IM_SEND_MESSAGE:
{
fMessenger.SendMessage(msg); fMessenger.SendMessage(msg);
break; break;
} }
case IM_LOGS_RECEIVED:
default: default:
GetView()->MessageReceived(msg); GetView()->MessageReceived(msg);
} }
@ -169,8 +175,20 @@ Conversation::AddUser(User* user)
ConversationView* ConversationView*
Conversation::GetView() Conversation::GetView()
{ {
if (fChatView == NULL) if (fChatView != NULL)
fChatView = new ConversationView(this); return fChatView;
fChatView = new ConversationView(this);
if (fLooper->Protocol()->SaveLogs() == false)
return fChatView;
BStringList logs = _GetChatLogs();
BMessage logMsg(IM_MESSAGE);
logMsg.AddInt32("im_what", IM_LOGS_RECEIVED);
logMsg.AddStrings("body", logs);
fChatView->MessageReceived(&logMsg);
return fChatView; return fChatView;
} }

View File

@ -347,6 +347,7 @@ Server::ImMessage(BMessage* msg)
conversation->GetProtocolLooper()->PostMessage(msg); conversation->GetProtocolLooper()->PostMessage(msg);
break; break;
} }
case IM_MESSAGE_SENT:
case IM_MESSAGE_RECEIVED: case IM_MESSAGE_RECEIVED:
{ {
Conversation* item = _EnsureConversation(msg); Conversation* item = _EnsureConversation(msg);

View File

@ -123,8 +123,6 @@ ConversationView::MessageReceived(BMessage* message)
if (text == "") if (text == "")
return; return;
fReceiveView->AppendOwnMessage(text.String());
BMessage msg(IM_MESSAGE); BMessage msg(IM_MESSAGE);
msg.AddInt32("im_what", IM_SEND_MESSAGE); msg.AddInt32("im_what", IM_SEND_MESSAGE);
msg.AddString("chat_id", fConversation->GetId()); msg.AddString("chat_id", fConversation->GetId());
@ -192,33 +190,17 @@ ConversationView::ImMessage(BMessage* msg)
break; break;
} }
// If not attached to the chat window, then re-handle this message _AppendOrEnqueueMessage(msg);
// later [AttachedToWindow()], since that's required to append to
// fReceiveview.
if (Window() == NULL) {
fMessageQueue.AddItem(new BMessage(*msg));
return;
}
// Now we're free to append!
Window()->LockLooper();
fReceiveView->AppendOtherMessage(uname.String(), message.String());
Window()->UnlockLooper();
// Message received, clear status anyway // Message received, clear status anyway
fStatus->SetText(""); // fStatus->SetText("");
break; break;
} }
case IM_MESSAGE_SENT:
case IM_LOGS_RECEIVED: case IM_LOGS_RECEIVED:
{ {
BStringList logs; _AppendOrEnqueueMessage(msg);
if (msg->FindStrings("log", &logs) != B_OK)
return;
for (int i = logs.CountStrings(); i >= 0; i--)
fReceiveView->AppendGenericMessage(logs.StringAt(i).String());
break; break;
} }
case IM_CONTACT_STARTED_TYPING: case IM_CONTACT_STARTED_TYPING:
@ -309,3 +291,52 @@ ConversationView::AppendStatus(CayaStatus status)
} }
bool
ConversationView::_AppendOrEnqueueMessage(BMessage* msg)
{
// If not attached to the chat window, then re-handle this message
// later [AttachedToWindow()], since you can't edit an unattached
// RenderView.
if (Window() == NULL) {
fMessageQueue.AddItem(new BMessage(*msg));
return false;
}
// Alright, we're good to append!
_AppendMessage(msg);
return true;
}
void
ConversationView::_AppendMessage(BMessage* msg)
{
BString message = msg->FindString("body");
BString id = msg->FindString("user_id");
BString uname = "";
if (id.IsEmpty() == false) {
User* sender = fConversation->UserById(id);
if (sender == NULL || (uname = sender->GetName()) == NULL)
uname = id;
}
if (msg->FindInt32("im_what") == IM_MESSAGE_SENT)
fReceiveView->AppendOwnMessage(message.String());
else if (uname.IsEmpty() == false)
fReceiveView->AppendOtherMessage(uname.String(), message.String());
else {
BStringList bodies;
if (msg->FindStrings("body", &bodies) != B_OK)
return;
for (int i = bodies.CountStrings(); i >= 0; i--)
fReceiveView->AppendGenericMessage(bodies.StringAt(i).String());
}
}

View File

@ -46,6 +46,9 @@ public:
void AvoidFocus(bool avoid); void AvoidFocus(bool avoid);
private: private:
bool _AppendOrEnqueueMessage(BMessage* msg);
void _AppendMessage(BMessage* msg);
Conversation* fConversation; Conversation* fConversation;
Contact* fContact; Contact* fContact;
CayaRenderView* fReceiveView; CayaRenderView* fReceiveView;