From a4e2b1dc5a6eea4f1f44c38a04fc14e916ea1b27 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Mon, 14 Jun 2021 16:41:25 -0500 Subject: [PATCH] Log receive-dates of messages Fixes #15 --- application/CayaProtocolMessages.h | 7 +++---- application/Conversation.cpp | 14 ++++++++++++-- application/views/CayaRenderView.cpp | 4 ++-- application/views/CayaRenderView.h | 3 ++- application/views/ConversationView.cpp | 7 ++++++- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index 764aa2b..b82b034 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -55,17 +55,16 @@ enum im_what_code { //! Chat message has been sent →Caya // Requires: String "chat_id", String "user_id", String "body" - // Accepts: String "subject" IM_MESSAGE_SENT = 21, //! Chat message received →Caya // Requires: String "chat_id", String "user_id", String "body" - // Accepts: String "subject" IM_MESSAGE_RECEIVED = 22, //! Logs received →Caya - // Requires: String "chat_id", String "user_id", String "body" - // Accepts: String "subject" + // Without "when" (a time_t), the logged message will lack a timestamp + // Requires: Strings "chat_id", Strings "user_id", Strings "body" + // Accepts: in64s "when" IM_LOGS_RECEIVED = 23, //! User started typing →Caya diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 4579942..4b73b53 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -342,11 +342,19 @@ Conversation::_LogChatMessage(BMessage* msg) // Binary logs // TODO: Don't hardcode 21, expose maximum as a setting BStringList users, bodies; + int64 times[21] = { 0 }; + times[0] = (int64)time(NULL); BMessage logMsg; if (_GetChatLogs(&logMsg) == B_OK) { logMsg.FindStrings("body", &bodies); logMsg.FindStrings("user_id", &users); + + int64 found; + for (int i = 0; i < 21; i++) + if (logMsg.FindInt64("when", i, &found) == B_OK) + times[i + 1] = found; + bodies.Remove(21); users.Remove(21); bodies.Add(body, 0); @@ -357,6 +365,9 @@ Conversation::_LogChatMessage(BMessage* msg) newLogMsg.AddInt32("im_what", IM_LOGS_RECEIVED); newLogMsg.AddStrings("body", bodies); newLogMsg.AddStrings("user_id", users); + newLogMsg.AddInt64("when", time(NULL)); + for (int i = 0; i < 21; i++) + newLogMsg.AddInt64("when", times[i]); BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE); WriteAttributeMessage(&logFile, "Caya:logs", &newLogMsg); @@ -382,7 +393,7 @@ Conversation::_GetChatLogs(BMessage* msg) BFile logFile(fCachePath.Path(), B_READ_WRITE | B_CREATE_FILE); - return ReadAttributeMessage(&logFile, "logs", msg); + return ReadAttributeMessage(&logFile, "Caya:logs", msg); } @@ -415,7 +426,6 @@ Conversation::_EnsureCachePath() { if (fCachePath.InitCheck() == B_OK) return; - fCachePath.SetTo(CayaRoomCachePath(fLooper->Protocol()->GetName(), fID.String())); } diff --git a/application/views/CayaRenderView.cpp b/application/views/CayaRenderView.cpp index 0870e10..eb687e3 100644 --- a/application/views/CayaRenderView.cpp +++ b/application/views/CayaRenderView.cpp @@ -27,12 +27,12 @@ CayaRenderView::CayaRenderView(const char *name, const char* smileyConfig) void CayaRenderView::AppendMessage(const char* nick, const char* message, - rgb_color nameColor) + rgb_color nameColor, time_t time) { rgb_color bg = ui_color(B_PANEL_BACKGROUND_COLOR); rgb_color fg = ui_color(B_PANEL_TEXT_COLOR); - Append("<", nameColor, bg, nameColor, time(NULL)); + Append("<", nameColor, bg, nameColor, time); Append(nick, fg, bg, fg); Append("> ", nameColor, bg, nameColor); // AddEmoticText(message, fg, bg); diff --git a/application/views/CayaRenderView.h b/application/views/CayaRenderView.h index c5850d6..ee64ac1 100644 --- a/application/views/CayaRenderView.h +++ b/application/views/CayaRenderView.h @@ -37,7 +37,8 @@ class CayaRenderView : public RunView public: CayaRenderView(const char* name, const char* smileyConfig = NULL); - void AppendMessage(const char* nick, const char* message, rgb_color nameColor); + void AppendMessage(const char* nick, const char* message, + rgb_color nameColor, time_t time = 0); void AppendGenericMessage(const char* message); void AddEmoticText(const char * txt, rgb_color fore, rgb_color bg); diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index 29e40b6..c737358 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -316,6 +316,10 @@ ConversationView::_AppendMessage(BMessage* msg) BString sender_name = users.StringAt(i); BString body = bodies.StringAt(i); rgb_color userColor = ui_color(B_PANEL_TEXT_COLOR); + int64 timeInt; + + if (msg->FindInt64("when", i, &timeInt) != B_OK) + timeInt = (int64)time(NULL); if (sender != NULL) { sender_name = sender->GetName(); @@ -327,7 +331,8 @@ ConversationView::_AppendMessage(BMessage* msg) continue; } - fReceiveView->AppendMessage(sender_name.String(), body.String(), userColor); + fReceiveView->AppendMessage(sender_name.String(), body.String(), + userColor, (time_t)timeInt); } }