Store users' names in binary chat logs

This commit is contained in:
Jaidyn Ann 2021-08-11 20:22:42 -05:00
parent fed3b9d9f0
commit 409eaede86
2 changed files with 36 additions and 23 deletions

View File

@ -504,53 +504,61 @@ Conversation::_LogChatMessage(BMessage* msg)
fDateFormatter.Format(date, time(0), B_SHORT_DATE_FORMAT, B_MEDIUM_TIME_FORMAT); fDateFormatter.Format(date, time(0), B_SHORT_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
BString id = msg->FindString("user_id"); BString id = msg->FindString("user_id");
BString name = msg->FindString("user_name");
BString body = msg->FindString("body"); BString body = msg->FindString("body");
if (id.IsEmpty() == true) if (id.IsEmpty() == true)
return; return;
if (name.IsEmpty() == true) {
User* user = UserById(id);
if (user == NULL)
name = id;
else
name = user->GetName();
}
// Binary logs // Binary logs
// TODO: Don't hardcode 21, expose maximum as a setting // TODO: Don't hardcode 31, expose maximum as a setting
BStringList users, bodies; int32 max = 31;
int64 times[21] = { 0 }; BStringList user_ids, user_names, bodies;
int64 times[max] = { 0 };
times[0] = (int64)time(NULL); times[0] = (int64)time(NULL);
BMessage logMsg; BMessage logMsg;
if (_GetChatLogs(&logMsg) == B_OK) { if (_GetChatLogs(&logMsg) == B_OK) {
logMsg.FindStrings("body", &bodies); logMsg.FindStrings("body", &bodies);
logMsg.FindStrings("user_id", &users); logMsg.FindStrings("user_id", &user_ids);
logMsg.FindStrings("user_name", &user_names);
int64 found; int64 found;
for (int i = 0; i < 21; i++) for (int i = 0; i < max; i++)
if (logMsg.FindInt64("when", i, &found) == B_OK) if (logMsg.FindInt64("when", i, &found) == B_OK)
times[i + 1] = found; times[i + 1] = found;
bodies.Remove(21); bodies.Remove(max);
users.Remove(21); user_ids.Remove(max);
user_names.Remove(max);
bodies.Add(body, 0); bodies.Add(body, 0);
users.Add(id, 0); user_ids.Add(id, 0);
user_names.Add(name, 0);
} }
BMessage newLogMsg(IM_MESSAGE); BMessage newLogMsg(IM_MESSAGE);
newLogMsg.AddInt32("im_what", IM_LOGS_RECEIVED); newLogMsg.AddInt32("im_what", IM_LOGS_RECEIVED);
newLogMsg.AddStrings("body", bodies); newLogMsg.AddStrings("body", bodies);
newLogMsg.AddStrings("user_id", users); newLogMsg.AddStrings("user_id", user_ids);
newLogMsg.AddStrings("user_name", user_names);
newLogMsg.AddInt64("when", time(NULL)); newLogMsg.AddInt64("when", time(NULL));
for (int i = 0; i < 21; i++) for (int i = 0; i < max; i++)
newLogMsg.AddInt64("when", times[i]); newLogMsg.AddInt64("when", times[i]);
BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE); BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE);
WriteAttributeMessage(&logFile, "Chat:logs", &newLogMsg); WriteAttributeMessage(&logFile, "Chat:logs", &newLogMsg);
// Plain-text logs // Plain-text logs
BString uname;
if (id.IsEmpty() == false)
uname = UserById(id)->GetName();
else
uname = "You";
BString logLine("["); BString logLine("[");
logLine << date << "] <" << uname << "> " << body << "\n"; logLine << date << "] <" << name << "> " << body << "\n";
logFile.Write(logLine.String(), logLine.Length()); logFile.Write(logLine.String(), logLine.Length());
} }

View File

@ -287,7 +287,7 @@ ConversationView::ObserveString(int32 what, BString str)
{ {
fSubjectTextView->SetText(str); fSubjectTextView->SetText(str);
BString body = B_TRANSLATE("** The subject was changed: %subject%"); BString body = B_TRANSLATE("** The subject is now: %subject%");
body.ReplaceAll("%subject%", str); body.ReplaceAll("%subject%", str);
BMessage topic(IM_MESSAGE); BMessage topic(IM_MESSAGE);
@ -401,16 +401,18 @@ ConversationView::_AppendOrEnqueueMessage(BMessage* msg)
void void
ConversationView::_AppendMessage(BMessage* msg) ConversationView::_AppendMessage(BMessage* msg)
{ {
BStringList users, bodies; BStringList user_ids, user_names, bodies;
if (msg->FindStrings("body", &bodies) != B_OK) if (msg->FindStrings("body", &bodies) != B_OK)
return; return;
msg->FindStrings("user_id", &users); msg->FindStrings("user_id", &user_ids);
msg->FindStrings("user_name", &user_names);
for (int i = bodies.CountStrings(); i >= 0; i--) { for (int i = bodies.CountStrings(); i >= 0; i--) {
User* sender = NULL; User* sender = NULL;
if (fConversation != NULL) if (fConversation != NULL)
sender = fConversation->UserById(users.StringAt(i)); sender = fConversation->UserById(user_ids.StringAt(i));
BString sender_name = users.StringAt(i); BString sender_id = user_ids.StringAt(i);
BString sender_name = user_names.StringAt(i);
BString body = bodies.StringAt(i); BString body = bodies.StringAt(i);
rgb_color userColor = ui_color(B_PANEL_TEXT_COLOR); rgb_color userColor = ui_color(B_PANEL_TEXT_COLOR);
int64 timeInt; int64 timeInt;
@ -423,7 +425,10 @@ ConversationView::_AppendMessage(BMessage* msg)
userColor = sender->fItemColor; userColor = sender->fItemColor;
} }
if (sender_name.IsEmpty() == true) { if (sender_name.IsEmpty() == true && sender_id.IsEmpty() == false)
sender_name = sender_id;
if (sender_id.IsEmpty() == true && sender_name.IsEmpty() == true) {
fReceiveView->AppendGeneric(body.String()); fReceiveView->AppendGeneric(body.String());
continue; continue;
} }