Commit df3896df9c

df3896df9c3e90a187f1552410b372174470d516

parent: 2a66496913

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-18 01:50 UTC
committer: <noreply@github.com>

Add ban/unban/kick room commands (#327)

Layout: unified · split

docs/iamb.1 +6
@@ -161,6 +161,12 @@ Set the room's canonical alias to the one provided, and make the previous one an
161Delete the room's canonical alias. 161Delete the room's canonical alias.
162.It Sy ":room canon show" 162.It Sy ":room canon show"
163Show the room's canonical alias, if any is set. 163Show the room's canonical alias, if any is set.
164.It Sy ":room ban [user] [reason]"
165Ban a user from this room with an optional reason.
166.It Sy ":room unban [user] [reason]"
167Unban a user from this room with an optional reason.
168.It Sy ":room kick [user] [reason]"
169Kick a user from this room with an optional reason.
164.El 170.El
165 171
166.Sh "WINDOW COMMANDS" 172.Sh "WINDOW COMMANDS"
src/base.rs +21
@@ -391,6 +391,24 @@ pub enum RoomField {
391 CanonicalAlias, 391 CanonicalAlias,
392} 392}
393 393
394/// An action that operates on a room member.
395#[derive(Clone, Debug, Eq, PartialEq)]
396pub enum MemberUpdateAction {
397 Ban,
398 Kick,
399 Unban,
400}
401
402impl Display for MemberUpdateAction {
403 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404 match self {
405 MemberUpdateAction::Ban => write!(f, "ban"),
406 MemberUpdateAction::Kick => write!(f, "kick"),
407 MemberUpdateAction::Unban => write!(f, "unban"),
408 }
409 }
410}
411
394/// An action that operates on a focused room. 412/// An action that operates on a focused room.
395#[derive(Clone, Debug, Eq, PartialEq)] 413#[derive(Clone, Debug, Eq, PartialEq)]
396pub enum RoomAction { 414pub enum RoomAction {
@@ -406,6 +424,9 @@ pub enum RoomAction {
406 /// Leave this room. 424 /// Leave this room.
407 Leave(bool), 425 Leave(bool),
408 426
427 /// Update a user's membership in this room.
428 MemberUpdate(MemberUpdateAction, String, Option<String>, bool),
429
409 /// Open the members window. 430 /// Open the members window.
410 Members(Box<CommandContext>), 431 Members(Box<CommandContext>),
411 432
src/commands.rs +75
@@ -20,6 +20,7 @@ use crate::base::{
20 IambAction, 20 IambAction,
21 IambId, 21 IambId,
22 KeysAction, 22 KeysAction,
23 MemberUpdateAction,
23 MessageAction, 24 MessageAction,
24 ProgramCommand, 25 ProgramCommand,
25 ProgramCommands, 26 ProgramCommands,
@@ -411,6 +412,17 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
411 ("dm", "unset", None) => RoomAction::SetDirect(false).into(), 412 ("dm", "unset", None) => RoomAction::SetDirect(false).into(),
412 ("dm", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument), 413 ("dm", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
413 414
415 // :room [kick|ban|unban] <user>
416 ("kick", u, r) => {
417 RoomAction::MemberUpdate(MemberUpdateAction::Kick, u.into(), r, desc.bang).into()
418 },
419 ("ban", u, r) => {
420 RoomAction::MemberUpdate(MemberUpdateAction::Ban, u.into(), r, desc.bang).into()
421 },
422 ("unban", u, r) => {
423 RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into()
424 },
425
414 // :room name set <room-name> 426 // :room name set <room-name>
415 ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(), 427 ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
416 ("name", "set", None) => return Result::Err(CommandError::InvalidArgument), 428 ("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
@@ -1047,6 +1059,69 @@ mod tests {
1047 assert_eq!(res, Err(CommandError::InvalidArgument)); 1059 assert_eq!(res, Err(CommandError::InvalidArgument));
1048 } 1060 }
1049 1061
1062 #[test]
1063 fn test_cmd_room_kick() {
1064 let mut cmds = setup_commands();
1065 let ctx = EditContext::default();
1066
1067 let res = cmds.input_cmd("room kick @user:example.com", ctx.clone()).unwrap();
1068 let act = IambAction::Room(RoomAction::MemberUpdate(
1069 MemberUpdateAction::Kick,
1070 "@user:example.com".into(),
1071 None,
1072 false,
1073 ));
1074 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1075
1076 let res = cmds.input_cmd("room! kick @user:example.com", ctx.clone()).unwrap();
1077 let act = IambAction::Room(RoomAction::MemberUpdate(
1078 MemberUpdateAction::Kick,
1079 "@user:example.com".into(),
1080 None,
1081 true,
1082 ));
1083 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1084
1085 let res = cmds
1086 .input_cmd("room! kick @user:example.com \"reason here\"", ctx.clone())
1087 .unwrap();
1088 let act = IambAction::Room(RoomAction::MemberUpdate(
1089 MemberUpdateAction::Kick,
1090 "@user:example.com".into(),
1091 Some("reason here".into()),
1092 true,
1093 ));
1094 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1095 }
1096
1097 #[test]
1098 fn test_cmd_room_ban_unban() {
1099 let mut cmds = setup_commands();
1100 let ctx = EditContext::default();
1101
1102 let res = cmds
1103 .input_cmd("room! ban @user:example.com \"spam\"", ctx.clone())
1104 .unwrap();
1105 let act = IambAction::Room(RoomAction::MemberUpdate(
1106 MemberUpdateAction::Ban,
1107 "@user:example.com".into(),
1108 Some("spam".into()),
1109 true,
1110 ));
1111 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1112
1113 let res = cmds
1114 .input_cmd("room unban @user:example.com \"reconciled\"", ctx.clone())
1115 .unwrap();
1116 let act = IambAction::Room(RoomAction::MemberUpdate(
1117 MemberUpdateAction::Unban,
1118 "@user:example.com".into(),
1119 Some("reconciled".into()),
1120 false,
1121 ));
1122 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1123 }
1124
1050 #[test] 1125 #[test]
1051 fn test_cmd_redact() { 1126 fn test_cmd_redact() {
1052 let mut cmds = setup_commands(); 1127 let mut cmds = setup_commands();
src/windows/room/mod.rs +43
@@ -22,6 +22,7 @@ use matrix_sdk::{
22 }, 22 },
23 OwnedEventId, 23 OwnedEventId,
24 OwnedRoomAliasId, 24 OwnedRoomAliasId,
25 OwnedUserId,
25 RoomId, 26 RoomId,
26 }, 27 },
27 DisplayName, 28 DisplayName,
@@ -56,6 +57,7 @@ use crate::base::{
56 IambId, 57 IambId,
57 IambInfo, 58 IambInfo,
58 IambResult, 59 IambResult,
60 MemberUpdateAction,
59 MessageAction, 61 MessageAction,
60 ProgramAction, 62 ProgramAction,
61 ProgramContext, 63 ProgramContext,
@@ -269,6 +271,47 @@ impl RoomState {
269 Err(IambError::NotJoined.into()) 271 Err(IambError::NotJoined.into())
270 } 272 }
271 }, 273 },
274 RoomAction::MemberUpdate(mua, user, reason, skip_confirm) => {
275 let Some(room) = store.application.worker.client.get_room(self.id()) else {
276 return Err(IambError::NotJoined.into());
277 };
278
279 let Ok(user_id) = OwnedUserId::try_from(user.as_str()) else {
280 let err = IambError::InvalidUserId(user);
281
282 return Err(err.into());
283 };
284
285 if !skip_confirm {
286 let msg = format!("Do you really want to {mua} {user} from this room?");
287 let act = RoomAction::MemberUpdate(mua, user, reason, true);
288 let act = IambAction::from(act);
289 let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
290 let prompt = Box::new(prompt);
291
292 return Err(UIError::NeedConfirm(prompt));
293 }
294
295 match mua {
296 MemberUpdateAction::Ban => {
297 room.ban_user(&user_id, reason.as_deref())
298 .await
299 .map_err(IambError::from)?;
300 },
301 MemberUpdateAction::Unban => {
302 room.unban_user(&user_id, reason.as_deref())
303 .await
304 .map_err(IambError::from)?;
305 },
306 MemberUpdateAction::Kick => {
307 room.kick_user(&user_id, reason.as_deref())
308 .await
309 .map_err(IambError::from)?;
310 },
311 }
312
313 Ok(vec![])
314 },
272 RoomAction::Members(mut cmd) => { 315 RoomAction::Members(mut cmd) => {
273 let width = Count::Exact(30); 316 let width = Count::Exact(30);
274 let act = 317 let act =