From ad1c7b578213406363226f8356bb39d8c85f4544 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 2 Jun 2021 16:53:03 -0500 Subject: [PATCH] Receive lists and changes in room participants from protocols Two new messages were added to the protocol API to do this: M_ROOM_PARTICIPANTS, which can be used when someone joins a room, or on joining a room to send a full list of users, and IM_ROOM_PARTICIPANT_LEFT, for when a user has left the room/disconnected. IM_SET_STATUS no longer assumes received data comes from contacts, but any general user. UserItem was made to reflect changes in the User's name. Chat messages can now be reliably received in a given room. :) --- application/CayaProtocolMessages.h | 6 ++++ application/Conversation.cpp | 9 ++++++ application/Conversation.h | 2 ++ application/MainWindow.cpp | 10 +++--- application/Server.cpp | 50 +++++++++++++++++++++++++++--- application/Server.h | 1 + application/User.cpp | 4 ++- application/views/UserItem.cpp | 18 ++++------- application/views/UserItem.h | 2 -- 9 files changed, 76 insertions(+), 26 deletions(-) diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index 6c39c35..3de5be0 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -89,6 +89,12 @@ enum im_what_code { //! Confirm the room's been joined IM_ROOM_JOINED = 33, + //! Returning a (not necessarily complete) list of room users + IM_ROOM_PARTICIPANTS = 34, + + //! A user left the room + IM_ROOM_PARTICIPANT_LEFT = 35, + /* * Messages related to contact changes. diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 9ca3f13..530dbb3 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -153,10 +153,19 @@ Conversation::AddUser(User* user) { BMessage msg; msg.AddString("user_id", user->GetId()); + msg.AddString("user_name", user->GetName()); _EnsureUser(&msg); } +void +Conversation::RemoveUser(User* user) +{ + fUsers.RemoveItemFor(user->GetId()); + GetView()->UpdateUserList(fUsers); +} + + void Conversation::ShowView(bool typing, bool userAction) { diff --git a/application/Conversation.h b/application/Conversation.h index 9f1426f..ec53224 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -53,7 +53,9 @@ public: UserMap Users(); User* UserById(BString id); + void AddUser(User* user); + void RemoveUser(User* user); private: void _LogChatMessage(BMessage* msg); diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index a76084b..948372d 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -214,11 +214,8 @@ MainWindow::ImMessage(BMessage* msg) } break; } + case IM_ROOM_PARTICIPANTS: case IM_MESSAGE_RECEIVED: - { - _EnsureConversationItem(msg); - break; - } case IM_MESSAGE_SENT: case IM_CHAT_CREATED: { @@ -230,8 +227,9 @@ MainWindow::ImMessage(BMessage* msg) case IM_EXTENDED_CONTACT_INFO: case IM_STATUS_SET: { - if (fRosterWindow != NULL) - fRosterWindow->PostMessage(msg); + if (fServer->ContactById(msg->FindString("user_id")) != NULL) + if (fRosterWindow != NULL) + fRosterWindow->PostMessage(msg); break; } } diff --git a/application/Server.cpp b/application/Server.cpp index 1c74434..afe5251 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "Account.h" @@ -154,14 +155,14 @@ Server::ImMessage(BMessage* msg) if (msg->FindInt32("status", &status) != B_OK) return B_SKIP_MESSAGE; - Contact* contact = _EnsureContact(msg); - if (!contact) + User* user = _EnsureUser(msg); + if (!user) break; - contact->SetNotifyStatus((CayaStatus)status); + user->SetNotifyStatus((CayaStatus)status); BString statusMsg; if (msg->FindString("message", &statusMsg) == B_OK) { - contact->SetNotifyPersonalStatus(statusMsg); + user->SetNotifyPersonalStatus(statusMsg); // contact->GetView()->UpdatePersonalMessage(); } break; @@ -254,6 +255,38 @@ Server::ImMessage(BMessage* msg) { break; } + case IM_ROOM_PARTICIPANTS: + { + Conversation* chat = _EnsureConversation(msg); + BStringList ids; + BStringList name; + + msg->FindStrings("user_name", &name); + if (msg->FindStrings("user_id", &ids) != B_OK) + break; + + ProtocolLooper* protoLooper = _LooperFromMessage(msg); + + for (int i = 0; i < ids.CountStrings(); i++) { + User* user = _EnsureUser(ids.StringAt(i), protoLooper); + + if (name.CountStrings() >= i) { + user->SetNotifyName(name.StringAt(i)); + } + chat->AddUser(user); + } + break; + } + case IM_ROOM_PARTICIPANT_LEFT: + { + Conversation* chat = _EnsureConversation(msg); + User* user = _EnsureUser(msg); + + if (user == NULL || chat == NULL) + break; + chat->RemoveUser(user); + break; + } case IM_SEND_MESSAGE: { // Route this message through the appropriate ProtocolLooper @@ -541,11 +574,18 @@ User* Server::_EnsureUser(BMessage* message) { BString id = message->FindString("user_id"); + return _EnsureUser(id, _LooperFromMessage(message)); +} + + +User* +Server::_EnsureUser(BString id, ProtocolLooper* protoLooper) +{ User* user = UserById(id); if (user == NULL && id.IsEmpty() == false) { user = new User(id, Looper()); - user->SetProtocolLooper(_LooperFromMessage(message)); + user->SetProtocolLooper(protoLooper); AddUser(user); } diff --git a/application/Server.h b/application/Server.h index 65a87a7..0aecb4d 100644 --- a/application/Server.h +++ b/application/Server.h @@ -68,6 +68,7 @@ private: Contact* _EnsureContact(BMessage* message); User* _EnsureUser(BMessage* message); + User* _EnsureUser(BString id, ProtocolLooper* protoLooper); Conversation* _EnsureConversation(BMessage* message); void _ReplicantStatusNotify(CayaStatus status); diff --git a/application/User.cpp b/application/User.cpp index 9efaf70..c1dfb77 100644 --- a/application/User.cpp +++ b/application/User.cpp @@ -142,8 +142,10 @@ User::ProtocolBitmap() const UserItem* User::GetListItem() { - if (fListItem == NULL) + if (fListItem == NULL) { fListItem = new UserItem(fName, this); + RegisterObserver(fListItem); + } return fListItem; } diff --git a/application/views/UserItem.cpp b/application/views/UserItem.cpp index d203536..2787a29 100644 --- a/application/views/UserItem.cpp +++ b/application/views/UserItem.cpp @@ -5,6 +5,7 @@ #include "UserItem.h" +#include "NotifyMessage.h" #include "User.h" @@ -26,18 +27,11 @@ UserItem::GetUser() void UserItem::ObserveString(int32 what, BString str) { -} - - -void -UserItem::ObservePointer(int32 what, void* ptr) -{ -} - - -void -UserItem::ObserveInteger(int32 what, int32 val) -{ + switch (what) { + case STR_CONTACT_NAME: + SetText(str); + break; + } } diff --git a/application/views/UserItem.h b/application/views/UserItem.h index ef5638e..8848968 100644 --- a/application/views/UserItem.h +++ b/application/views/UserItem.h @@ -20,8 +20,6 @@ public: protected: void ObserveString(int32 what, BString str); - void ObservePointer(int32 what, void* ptr); - void ObserveInteger(int32 what, int32 val); private: User* fUser;