Support moderation (muting, kicking, etc)

Allow kicking/banning/muting/unmuting/deafening and undeafening through
the UserListView right-click menu (if permissions are sufficient).

Support for banning, kicking, and muting were added to the XMPP add-on―
deafening isn't supported, so is left out.
This commit is contained in:
Jaidyn Ann 2021-06-06 16:31:25 -05:00
parent dc3fdd65c8
commit 260b36012e
5 changed files with 130 additions and 18 deletions

View File

@ -225,11 +225,26 @@ enum im_what_code {
//! A user's role has been changed
IM_ROOM_ROLECHANGE = 170,
//! Ban user
IM_ROOM_BAN_PARTICIPANT = 171,
//! Kick user
IM_ROOM_KICK_PARTICIPANT = 172,
IM_ROOM_KICK_PARTICIPANT = 171,
//! Ban user
IM_ROOM_BAN_PARTICIPANT = 172,
//! Unban user
IM_ROOM_UNBAN_PARTICIPANT = 173,
//! Mute user
IM_ROOM_MUTE_PARTICIPANT = 174,
//! Unmute user
IM_ROOM_UNMUTE_PARTICIPANT = 175,
//! Deafen
IM_ROOM_DEAFEN_PARTICIPANT = 176,
//! Allow to read messages
IM_ROOM_UNDEAFEN_PARTICIPANT = 177,
/*

View File

@ -10,19 +10,15 @@
#include <Window.h>
#include "CayaMessages.h"
#include "CayaProtocolMessages.h"
#include "Conversation.h"
#include "ProtocolLooper.h"
#include "Role.h"
#include "User.h"
#include "UserInfoWindow.h"
#include "UserItem.h"
const uint32 kUserInfo = 'ULui';
const uint32 kDeafenUser = 'UMdu';
const uint32 kMuteUser = 'UMmu';
const uint32 kKickUser = 'UMku';
const uint32 kBanUser = 'UMbu';
UserListView::UserListView(const char* name)
: BListView(name),
@ -45,7 +41,24 @@ UserListView::MessageReceived(BMessage* msg)
win->Show();
break;
}
case kKickUser:
_ModerationAction(IM_ROOM_KICK_PARTICIPANT);
break;
case kBanUser:
_ModerationAction(IM_ROOM_BAN_PARTICIPANT);
break;
case kMuteUser:
_ModerationAction(IM_ROOM_MUTE_PARTICIPANT);
break;
case kUnmuteUser:
_ModerationAction(IM_ROOM_UNMUTE_PARTICIPANT);
break;
case kDeafenUser:
_ModerationAction(IM_ROOM_DEAFEN_PARTICIPANT);
break;
case kUndeafenUser:
_ModerationAction(IM_ROOM_UNDEAFEN_PARTICIPANT);
break;
default:
BListView::MessageReceived(msg);
}
@ -80,6 +93,7 @@ UserListView::_UserPopUp()
// Now for the moderation items
Role* role = fChat->GetRole(fChat->OwnUserId());
if (role == NULL) return menu;
int32 perms = role->fPerms;
UserItem* item = (UserItem*)ItemAt(CurrentSelection());
User* selected_user;
@ -87,7 +101,10 @@ UserListView::_UserPopUp()
if (item == NULL || (selected_user = item->GetUser()) == NULL)
return menu;
int32 selected_priority = fChat->GetRole(selected_user->GetId())->fPriority;
Role* selected_role = fChat->GetRole(selected_user->GetId());
if (selected_role == NULL) return menu;
int32 selected_priority = selected_role->fPriority;
int32 selected_perms = selected_role->fPerms;
if (selected_priority > role->fPriority)
return menu;
@ -95,16 +112,22 @@ UserListView::_UserPopUp()
|| (perms & PERM_BAN))
menu->AddSeparatorItem();
if (perms & PERM_DEAFEN)
if ((perms & PERM_DEAFEN) && (selected_perms & PERM_READ))
menu->AddItem(new BMenuItem("Deafen user", new BMessage(kDeafenUser)));
if (perms & PERM_MUTE)
if ((perms & PERM_DEAFEN) && !(selected_perms & PERM_READ))
menu->AddItem(new BMenuItem("Undeafen user", new BMessage(kUndeafenUser)));
if ((perms & PERM_MUTE) && (selected_perms & PERM_WRITE))
menu->AddItem(new BMenuItem("Mute user", new BMessage(kMuteUser)));
if ((perms & PERM_MUTE) && !(selected_perms & PERM_WRITE))
menu->AddItem(new BMenuItem("Unmute user", new BMessage(kUnmuteUser)));
if (perms & PERM_KICK)
menu->AddItem(new BMenuItem("Kick user", new BMessage(kKickUser)));
if (perms & PERM_BAN)
menu->AddItem(new BMenuItem("Ban user", new BMessage(kBanUser)));
menu->SetTargetForItems(this);
return menu;
}
@ -125,3 +148,20 @@ UserListView::_BlankPopUp()
}
void
UserListView::_ModerationAction(int32 im_what)
{
Role* role = fChat->GetRole(fChat->OwnUserId());
int32 perms = role->fPerms;
UserItem* item = (UserItem*)ItemAt(CurrentSelection());
if (item == NULL)
return;
BMessage modMsg(IM_MESSAGE);
modMsg.AddInt32("im_what", im_what);
modMsg.AddString("user_id", item->GetUser()->GetId());
modMsg.AddString("chat_id", fChat->GetId());
fChat->GetProtocolLooper()->PostMessage(&modMsg);
}

View File

@ -11,6 +11,18 @@ class BPopUpMenu;
class Conversation;
enum
{
kUserInfo = 'ULui',
kDeafenUser = 'UMdu',
kUndeafenUser = 'UMud',
kMuteUser = 'UMmu',
kUnmuteUser = 'UMum',
kKickUser = 'UMku',
kBanUser = 'UMbu'
};
class UserListView : public BListView {
public:
UserListView(const char* name);
@ -25,6 +37,8 @@ private:
BPopUpMenu* _UserPopUp();
BPopUpMenu* _BlankPopUp();
void _ModerationAction(int32 im_what);
Conversation* fChat;
};

View File

@ -175,6 +175,14 @@ JabberHandler::Process(BMessage* msg)
break;
}
case IM_ROOM_KICK_PARTICIPANT:
case IM_ROOM_BAN_PARTICIPANT:
case IM_ROOM_UNBAN_PARTICIPANT:
case IM_ROOM_MUTE_PARTICIPANT:
case IM_ROOM_UNMUTE_PARTICIPANT:
_MUCModeration(msg);
break;
default:
return B_ERROR;
}
@ -895,6 +903,37 @@ JabberHandler::_MUCUserId(BString chat_id, const char* nick, BString* id)
}
void
JabberHandler::_MUCModeration(BMessage* msg)
{
BString chat_id = msg->FindString("chat_id");
BString user_id;
BString body = msg->FindString("body");
gloox::MUCRoom* room = fRooms.ValueFor(chat_id);
if (room == NULL || msg->FindString("user_id", &user_id) != B_OK)
return;
std::string nick = gloox::JID(user_id.String()).resource();
switch (msg->FindInt32("im_what"))
{
case IM_ROOM_KICK_PARTICIPANT:
room->kick(nick, body.String());
break;
case IM_ROOM_BAN_PARTICIPANT:
room->ban(nick, body.String());
break;
case IM_ROOM_MUTE_PARTICIPANT:
room->revokeVoice(nick, body.String());
break;
case IM_ROOM_UNMUTE_PARTICIPANT:
room->grantVoice(nick, body.String());
break;
}
}
const char*
JabberHandler::_RoleTitle(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff)
{
@ -922,10 +961,13 @@ JabberHandler::_RolePerms(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff
return 0 | PERM_READ | PERM_NICK;
case gloox::RoleParticipant:
return 0 | PERM_READ | PERM_WRITE | PERM_ROOM_SUBJECT;
case gloox::RoleModerator:
case gloox::RoleModerator: {
int32 perm = 0 | PERM_READ | PERM_WRITE | PERM_ROOM_SUBJECT
| PERM_KICK | PERM_MUTE;
if (aff == gloox::AffiliationOwner)
return PERM_ALL;
return PERM_ALL;
perm = perm | PERM_ROLECHANGE | PERM_BAN;
return perm;
}
}
return 0;
}

View File

@ -133,6 +133,7 @@ private:
BString _MUCChatId(gloox::MUCRoom* room);
bool _MUCUserId(BString chat_id, const char* nick, BString* id);
void _MUCModeration(BMessage* msg);
const char* _RoleTitle(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);
int32 _RolePerms(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);