From effd010b466ed7b266a8745871ddb9efd1b16e03 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Fri, 4 Jun 2021 16:32:18 -0500 Subject: [PATCH] Allow leaving/getting booted from rooms Now the user can leave or be forced to leave a chat. For this, two protocol API messages were added: IM_LEAVE_ROOM, and IM_ROOM_LEFT. Aside from that, IM_ROOM_BAN_PARTICIPANT and IM_ROOM_KICK_PARTICIPANT were added, though they aren't used yet. --- application/CayaProtocolMessages.h | 23 +++++++++++++--- application/MainWindow.cpp | 31 +++++++++++++++++++++- application/MainWindow.h | 5 +++- application/Server.cpp | 8 ++++++ application/Server.h | 1 + application/views/ConversationListView.cpp | 17 ++++++++++++ application/views/ConversationView.cpp | 6 +++++ 7 files changed, 86 insertions(+), 5 deletions(-) diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index 44aca5b..c0dadca 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -191,14 +191,20 @@ enum im_what_code { //! Confirm the room's been joined IM_ROOM_JOINED = 153, + //! User left the room + IM_LEAVE_ROOM = 154, + + //! User left the room + IM_ROOM_LEFT = 155, + //! Quietly add a user(s) to the chat - IM_ROOM_PARTICIPANTS = 154, + IM_ROOM_PARTICIPANTS = 156, //! User has newly and explicitly joined - IM_ROOM_PARTICIPANT_JOINED = 155, + IM_ROOM_PARTICIPANT_JOINED = 157, //! A user left the room - IM_ROOM_PARTICIPANT_LEFT = 156, + IM_ROOM_PARTICIPANT_LEFT = 158, /* @@ -212,6 +218,17 @@ enum im_what_code { IM_ROOM_SUBJECT = 161, + /* + * Room moderation + */ + + //! Ban user + IM_ROOM_BAN_PARTICIPANT = 170, + + //! Kick user + IM_ROOM_KICK_PARTICIPANT = 171, + + /* * Special messages */ diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 948372d..8dbfe46 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -176,7 +176,6 @@ MainWindow::MessageReceived(BMessage* message) message->AddString("body", fSendView->Text()); fChatView->MessageReceived(message); fSendView->SetText(""); - break; } @@ -214,6 +213,7 @@ MainWindow::ImMessage(BMessage* msg) } break; } + case IM_ROOM_JOINED: case IM_ROOM_PARTICIPANTS: case IM_MESSAGE_RECEIVED: case IM_MESSAGE_SENT: @@ -222,6 +222,16 @@ MainWindow::ImMessage(BMessage* msg) _EnsureConversationItem(msg); break; } + case IM_ROOM_LEFT: + { + ConversationItem* item = _EnsureConversationItem(msg); + if (item == NULL) + break; + + _RemoveListItem(item); + item->GetConversation()->GetView()->MessageReceived(msg); + break; + } case IM_AVATAR_SET: case IM_CONTACT_INFO: case IM_EXTENDED_CONTACT_INFO: @@ -422,3 +432,22 @@ MainWindow::_UpdateListItem(ConversationItem* item) } +void +MainWindow::_RemoveListItem(ConversationItem* item) +{ + int32 index = fListView->IndexOf(item); + if (index > 0) + index--; + + fListView->RemoveItem(item); + fServer->RemoveConversation(item->GetConversation()); + + if (fListView->CountItems() == 0) { + fChatView = new ConversationView(); + SetConversation(NULL); + } + else + fListView->Select(index); +} + + diff --git a/application/MainWindow.h b/application/MainWindow.h index 12bdc2d..6ab7ea4 100644 --- a/application/MainWindow.h +++ b/application/MainWindow.h @@ -48,8 +48,11 @@ private: void _InitInterface(); BMenuBar* _CreateMenuBar(); - ConversationItem* _EnsureConversationItem(BMessage* msg); + ConversationItem* + _EnsureConversationItem(BMessage* msg); void _UpdateListItem(ConversationItem* item); + void _RemoveListItem(ConversationItem* item); + Server* fServer; RosterWindow* fRosterWindow; diff --git a/application/Server.cpp b/application/Server.cpp index 46799f0..708187e 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -251,6 +251,7 @@ Server::ImMessage(BMessage* msg) } case IM_ROOM_JOINED: { + _EnsureConversation(msg); break; } case IM_ROOM_PARTICIPANTS: @@ -535,6 +536,13 @@ Server::AddConversation(Conversation* chat) } +void +Server::RemoveConversation(Conversation* chat) +{ + fChatMap.RemoveItemFor(chat->GetId()); +} + + BString Server::GetOwnContact() { diff --git a/application/Server.h b/application/Server.h index 0aecb4d..318b1c8 100644 --- a/application/Server.h +++ b/application/Server.h @@ -59,6 +59,7 @@ public: ChatMap Conversations() const; Conversation* ConversationById(BString id); void AddConversation(Conversation* chat); + void RemoveConversation(Conversation* chat); // TODO: there should be a contact for each account. BString GetOwnContact(); diff --git a/application/views/ConversationListView.cpp b/application/views/ConversationListView.cpp index fa4fb05..fc222a8 100644 --- a/application/views/ConversationListView.cpp +++ b/application/views/ConversationListView.cpp @@ -10,8 +10,10 @@ #include #include "CayaMessages.h" +#include "CayaProtocolMessages.h" #include "Conversation.h" #include "ConversationItem.h" +#include "ProtocolLooper.h" const uint32 kOpenSelectedChat = 'CVos'; @@ -38,6 +40,21 @@ ConversationListView::MessageReceived(BMessage* msg) break; } + case kLeaveSelectedChat: + { + ConversationItem* item; + int32 selIndex = CurrentSelection(); + + if ((item = (ConversationItem*)ItemAt(selIndex)) == NULL) + break; + + BMessage leave(IM_MESSAGE); + leave.AddInt32("im_what", IM_LEAVE_ROOM); + leave.AddString("chat_id", item->GetConversation()->GetId()); + item->GetConversation()->GetProtocolLooper()->MessageReceived(&leave); + break; + } + default: BListView::MessageReceived(msg); } diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index 4b2a1eb..deb7b1a 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -100,6 +100,12 @@ ConversationView::ImMessage(BMessage* msg) int32 im_what = msg->FindInt32("im_what"); switch (im_what) { + case IM_ROOM_LEFT: + { + delete fConversation; + delete this; + break; + } case IM_MESSAGE_RECEIVED: { BString message = msg->FindString("body");