Commit 992459575f
Unregistered key
Layout: unified · split
docs/iamb.1 +26 −4
| @@ -156,14 +156,36 @@ Send an invitation to a user to join the currently focused room with an optional | ||
| 156 | 156 | Leave the currently focused room. |
| 157 | 157 | .It Sy ":members" |
| 158 | 158 | View a list of members of the currently focused room. |
| 159 | .It Sy ":room name set [name]" | |
| 160 | Set the name of the currently focused room. | |
| 161 | .It Sy ":room name unset" | |
| 162 | Unset the name of the currently focused room. | |
| 159 | .It Sy ":room access set [rule]" | |
| 160 | Set the join rules for the room to control who can access it. | |
| 161 | Possible values for the | |
| 162 | .Sy rule | |
| 163 | argument are | |
| 164 | .Dq invite , | |
| 165 | .Dq knock , | |
| 166 | .Dq knock-restricted , | |
| 167 | .Dq public , | |
| 168 | and | |
| 169 | .Dq restricted . | |
| 170 | The | |
| 171 | .Dq knock-restricted | |
| 172 | and | |
| 173 | .Dq restricted | |
| 174 | values allow additional | |
| 175 | .Sy ++members=!room:example.org | |
| 176 | arguments to specify rooms (and spaces) whose members are allowed to join without an invite or knocking. | |
| 177 | .It Sy ":room access unset" | |
| 178 | Set the room to being invite-only so that those not already members cannot join. | |
| 179 | .It Sy ":room access show" | |
| 180 | Show the current join rules for the room. | |
| 163 | 181 | .It Sy ":room dm set" |
| 164 | 182 | Mark the currently focused room as a direct message. |
| 165 | 183 | .It Sy ":room dm unset" |
| 166 | 184 | Mark the currently focused room as a normal room. |
| 185 | .It Sy ":room name set [name]" | |
| 186 | Set the name of the currently focused room. | |
| 187 | .It Sy ":room name unset" | |
| 188 | Unset the name of the currently focused room. | |
| 167 | 189 | .It Sy ":room unread set" |
| 168 | 190 | Mark the currently focused room as unread. |
| 169 | 191 | .It Sy ":room unread unset" |
src/base.rs +13
| @@ -37,6 +37,7 @@ use matrix_sdk::{ | ||
| 37 | 37 | EventId, |
| 38 | 38 | OwnedEventId, |
| 39 | 39 | OwnedMxcUri, |
| 40 | OwnedRoomAliasId, | |
| 40 | 41 | OwnedRoomId, |
| 41 | 42 | OwnedRoomOrAliasId, |
| 42 | 43 | OwnedTransactionId, |
| @@ -67,6 +68,7 @@ use matrix_sdk::{ | ||
| 67 | 68 | }, |
| 68 | 69 | presence::PresenceState, |
| 69 | 70 | profile::{ProfileFieldName, ProfileFieldValue}, |
| 71 | room::JoinRule, | |
| 70 | 72 | }, |
| 71 | 73 | }; |
| 72 | 74 | |
| @@ -409,6 +411,9 @@ impl Visitor<'_> for SortUserVisitor { | ||
| 409 | 411 | /// A room property. |
| 410 | 412 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 411 | 413 | pub enum RoomField { |
| 414 | /// The room's join rules, aka who can access this room. | |
| 415 | Access, | |
| 416 | ||
| 412 | 417 | /// The room's history visibility. |
| 413 | 418 | History, |
| 414 | 419 | |
| @@ -498,6 +503,9 @@ pub enum RoomAction { | ||
| 498 | 503 | /// Set whether a room is a direct message. |
| 499 | 504 | SetDirect(bool), |
| 500 | 505 | |
| 506 | /// Set the join rules for a room to control who can access it and how. | |
| 507 | SetAccess(JoinRule), | |
| 508 | ||
| 501 | 509 | /// Set a room property. |
| 502 | 510 | Set(RoomField, String), |
| 503 | 511 | |
| @@ -1909,6 +1917,11 @@ impl ChatStore { | ||
| 1909 | 1917 | } |
| 1910 | 1918 | } |
| 1911 | 1919 | |
| 1920 | /// Get the alias for a room if it has one (and the client knows it). | |
| 1921 | pub fn get_joined_room_alias(&self, room_id: &RoomId) -> Option<OwnedRoomAliasId> { | |
| 1922 | self.worker.client.get_room(room_id).and_then(|r| r.canonical_alias()) | |
| 1923 | } | |
| 1924 | ||
| 1912 | 1925 | /// Get the title for a room. |
| 1913 | 1926 | pub fn get_room_title(&self, room_id: &RoomId) -> String { |
| 1914 | 1927 | self.rooms |
src/commands.rs +76 −5
| @@ -6,11 +6,13 @@ use std::{convert::TryFrom, str::FromStr as _}; | ||
| 6 | 6 | |
| 7 | 7 | use matrix_sdk::ruma::{ |
| 8 | 8 | OwnedMxcUri, |
| 9 | OwnedRoomId, | |
| 9 | 10 | OwnedRoomOrAliasId, |
| 10 | 11 | OwnedUserId, |
| 11 | 12 | RoomVersionId, |
| 12 | 13 | events::tag::TagName, |
| 13 | 14 | profile::{ProfileFieldName, ProfileFieldValue}, |
| 15 | room::{AllowRule, JoinRule, Restricted as JoinRestrictions}, | |
| 14 | 16 | }; |
| 15 | 17 | |
| 16 | 18 | use modalkit::{ |
| @@ -628,16 +630,63 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 628 | 630 | let arg = iter.next(); |
| 629 | 631 | let trailing = iter.collect::<Vec<_>>(); |
| 630 | 632 | |
| 631 | match (field.as_str(), action.as_str()) { | |
| 633 | match (field.as_str(), action.as_str(), arg.as_deref()) { | |
| 632 | 634 | // Skip check for commands that takes a variable number of arguments: |
| 633 | ("version", "upgrade") => (), | |
| 635 | ("access", "set", Some("restricted" | "knock-restricted")) => (), | |
| 636 | ("version", "upgrade", _) => (), | |
| 634 | 637 | |
| 635 | 638 | // Reject if we have any trailing arguments: |
| 636 | (_, _) if !trailing.is_empty() => return Result::Err(CommandError::InvalidArgument), | |
| 637 | (_, _) => (), | |
| 639 | (_, _, _) if !trailing.is_empty() => return Result::Err(CommandError::InvalidArgument), | |
| 640 | (_, _, _) => (), | |
| 638 | 641 | } |
| 639 | 642 | |
| 640 | 643 | let act: IambAction = match (field.as_str(), action.as_str(), arg) { |
| 644 | // :room access set | |
| 645 | ("access", "set", Some(rule)) => { | |
| 646 | let allow = trailing | |
| 647 | .iter() | |
| 648 | .map(|a| { | |
| 649 | let (flag, v) = match OptionType::from_str(a)? { | |
| 650 | OptionType::Positional(_) => return Err(CommandError::InvalidArgument), | |
| 651 | OptionType::Flag(_, None) => return Err(CommandError::InvalidArgument), | |
| 652 | OptionType::Flag(f, Some(v)) => (f, v), | |
| 653 | }; | |
| 654 | ||
| 655 | match flag.as_str() { | |
| 656 | "members" => { | |
| 657 | let room_id = OwnedRoomId::from_str(&v).map_err(|e| { | |
| 658 | CommandError::Error(format!( | |
| 659 | "{v:?} is not a valid room identifier: {e}" | |
| 660 | )) | |
| 661 | })?; | |
| 662 | Ok(AllowRule::room_membership(room_id)) | |
| 663 | }, | |
| 664 | _ => Err(CommandError::Error(format!("unknown flag {flag:?}"))), | |
| 665 | } | |
| 666 | }) | |
| 667 | .collect::<Result<Vec<_>, _>>()?; | |
| 668 | let restrictions = JoinRestrictions::new(allow); | |
| 669 | ||
| 670 | let rule = match rule.as_str() { | |
| 671 | "invite" => JoinRule::Invite, | |
| 672 | "knock" => JoinRule::Knock, | |
| 673 | "knock-restricted" => JoinRule::KnockRestricted(restrictions), | |
| 674 | "public" => JoinRule::Public, | |
| 675 | "restricted" => JoinRule::Restricted(restrictions), | |
| 676 | _ => return Err(CommandError::InvalidArgument), | |
| 677 | }; | |
| 678 | RoomAction::SetAccess(rule).into() | |
| 679 | }, | |
| 680 | ("access", "set", None) => return Result::Err(CommandError::InvalidArgument), | |
| 681 | ||
| 682 | // :room access unset | |
| 683 | ("access", "unset", None) => RoomAction::SetAccess(JoinRule::Invite).into(), | |
| 684 | ("access", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument), | |
| 685 | ||
| 686 | // :room access unset | |
| 687 | ("access", "show", None) => RoomAction::Show(RoomField::Access).into(), | |
| 688 | ("access", "show", Some(_)) => return Result::Err(CommandError::InvalidArgument), | |
| 689 | ||
| 641 | 690 | // :room dm set |
| 642 | 691 | ("dm", "set", None) => RoomAction::SetDirect(true).into(), |
| 643 | 692 | ("dm", "set", Some(_)) => return Result::Err(CommandError::InvalidArgument), |
| @@ -1109,7 +1158,7 @@ pub fn setup_commands() -> ProgramCommands { | ||
| 1109 | 1158 | #[cfg(test)] |
| 1110 | 1159 | mod tests { |
| 1111 | 1160 | use super::*; |
| 1112 | use matrix_sdk::ruma::user_id; | |
| 1161 | use matrix_sdk::ruma::{owned_room_id, user_id}; | |
| 1113 | 1162 | use modalkit::actions::WindowAction; |
| 1114 | 1163 | use modalkit::editing::context::EditContext; |
| 1115 | 1164 | |
| @@ -1743,4 +1792,26 @@ mod tests { | ||
| 1743 | 1792 | let err = CommandError::InvalidArgument; |
| 1744 | 1793 | assert_eq!(res, err); |
| 1745 | 1794 | } |
| 1795 | ||
| 1796 | #[test] | |
| 1797 | fn test_cmd_room_access() { | |
| 1798 | let mut cmds = setup_commands(); | |
| 1799 | let ctx = EditContext::default(); | |
| 1800 | ||
| 1801 | let res = cmds.input_cmd("room access set knock", ctx.clone()).unwrap(); | |
| 1802 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::Knock)); | |
| 1803 | assert_eq!(res, vec![(act.into(), ctx.clone())]); | |
| 1804 | ||
| 1805 | let res = cmds | |
| 1806 | .input_cmd("room access set knock-restricted ++members=!abcde:example.org", ctx.clone()) | |
| 1807 | .unwrap(); | |
| 1808 | let allow = AllowRule::room_membership(owned_room_id!("!abcde:example.org")); | |
| 1809 | let restrictions = JoinRestrictions::new(vec![allow]); | |
| 1810 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::KnockRestricted(restrictions))); | |
| 1811 | assert_eq!(res, vec![(act.into(), ctx.clone())]); | |
| 1812 | ||
| 1813 | let res = cmds.input_cmd("room access unset", ctx.clone()).unwrap(); | |
| 1814 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::Invite)); | |
| 1815 | assert_eq!(res, vec![(act.into(), ctx.clone())]); | |
| 1816 | } | |
| 1746 | 1817 | } |
src/completions.rs +11 −1
| @@ -413,6 +413,7 @@ fn complete_iamb_create(args: Vec<String>) -> Vec<String> { | ||
| 413 | 413 | // "kick","ban","unban", ".. unset" and "dm/tag set/unset" |
| 414 | 414 | fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { |
| 415 | 415 | let subcmds = [ |
| 416 | "access", | |
| 416 | 417 | "dm", |
| 417 | 418 | "kick", |
| 418 | 419 | "ban", |
| @@ -437,7 +438,7 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { | ||
| 437 | 438 | "id" => complete_choices(input, &["show"]), |
| 438 | 439 | "dm" | "name" | "tag" => complete_choices(input, &["set", "unset"]), |
| 439 | 440 | |
| 440 | "history" | "topic" | "notify" | "alias" | "canonicalalias" | "canon" => { | |
| 441 | "access" | "history" | "topic" | "notify" | "alias" | "canonicalalias" | "canon" => { | |
| 441 | 442 | complete_choices(input, &["show", "set", "unset"]) |
| 442 | 443 | }, |
| 443 | 444 | |
| @@ -449,6 +450,15 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { | ||
| 449 | 450 | } else if args.len() == 3 { |
| 450 | 451 | let input = &args[2]; |
| 451 | 452 | match (args[0].as_str(), args[1].as_str()) { |
| 453 | ("access", "set") => { | |
| 454 | complete_choices(input, &[ | |
| 455 | "invite", | |
| 456 | "knock", | |
| 457 | "knock-restricted", | |
| 458 | "public", | |
| 459 | "restricted", | |
| 460 | ]) | |
| 461 | }, | |
| 452 | 462 | ("history", "set") => { |
| 453 | 463 | complete_choices(input, &["invited", "joined", "shared", "world_readable"]) |
| 454 | 464 | }, |
src/windows/room/mod.rs +64 −16
| @@ -24,6 +24,7 @@ use matrix_sdk::{ | ||
| 24 | 24 | }, |
| 25 | 25 | tag::{TagInfo, Tags}, |
| 26 | 26 | }, |
| 27 | room::{AllowRule, JoinRule, Restricted as JoinRestrictions}, | |
| 27 | 28 | }, |
| 28 | 29 | }; |
| 29 | 30 | |
| @@ -255,6 +256,16 @@ pub async fn room_command( | ||
| 255 | 256 | |
| 256 | 257 | Ok(vec![(act, cmd.context.clone())]) |
| 257 | 258 | }, |
| 259 | RoomAction::SetAccess(rule) => { | |
| 260 | let Some(room) = store.application.worker.client.get_room(id) else { | |
| 261 | return Err(IambError::NotJoined.into()); | |
| 262 | }; | |
| 263 | room.privacy_settings() | |
| 264 | .update_join_rule(rule) | |
| 265 | .await | |
| 266 | .map_err(IambError::from)?; | |
| 267 | Ok(vec![]) | |
| 268 | }, | |
| 258 | 269 | RoomAction::SetDirect(is_direct) => { |
| 259 | 270 | let room = store |
| 260 | 271 | .application |
| @@ -404,14 +415,8 @@ pub async fn room_command( | ||
| 404 | 415 | .await |
| 405 | 416 | .map_err(IambError::from)?; |
| 406 | 417 | }, |
| 407 | RoomField::Aliases => { | |
| 408 | // This never happens, aliases is only used for showing | |
| 409 | }, | |
| 410 | RoomField::Id => { | |
| 411 | // This never happens, id is only used for showing | |
| 412 | }, | |
| 413 | RoomField::Version => { | |
| 414 | // This never happens, version is only used for showing or upgrading. | |
| 418 | RoomField::Access | RoomField::Aliases | RoomField::Id | RoomField::Version => { | |
| 419 | // These variants exist for RoomAction::Show, so we never actually get here. | |
| 415 | 420 | }, |
| 416 | 421 | } |
| 417 | 422 | |
| @@ -506,14 +511,8 @@ pub async fn room_command( | ||
| 506 | 511 | RoomField::UserName => { |
| 507 | 512 | room.set_own_member_display_name(None).await.map_err(IambError::from)?; |
| 508 | 513 | }, |
| 509 | RoomField::Aliases => { | |
| 510 | // This will not happen, you cannot unset all aliases | |
| 511 | }, | |
| 512 | RoomField::Id => { | |
| 513 | // This never happens, id is only used for showing | |
| 514 | }, | |
| 515 | RoomField::Version => { | |
| 516 | // This never happens, version is only used for showing or upgrading. | |
| 514 | RoomField::Access | RoomField::Aliases | RoomField::Id | RoomField::Version => { | |
| 515 | // These variants exist for RoomAction::Show, so we never actually get here. | |
| 517 | 516 | }, |
| 518 | 517 | } |
| 519 | 518 | |
| @@ -566,6 +565,55 @@ pub async fn room_command( | ||
| 566 | 565 | |
| 567 | 566 | format!("Room notification level: {level:?}") |
| 568 | 567 | }, |
| 568 | RoomField::Access => { | |
| 569 | let show_restrictions = |rs: JoinRestrictions| { | |
| 570 | rs.allow | |
| 571 | .into_iter() | |
| 572 | .map(|a| { | |
| 573 | match a { | |
| 574 | AllowRule::RoomMembership(m) => { | |
| 575 | if let Some(alias) = | |
| 576 | store.application.get_joined_room_alias(&m.room_id) | |
| 577 | { | |
| 578 | format!("members of {} ({alias})", m.room_id) | |
| 579 | } else { | |
| 580 | format!("members of {}", m.room_id) | |
| 581 | } | |
| 582 | }, | |
| 583 | other => format!("{other:?}"), | |
| 584 | } | |
| 585 | }) | |
| 586 | .collect::<Vec<_>>() | |
| 587 | .join(", ") | |
| 588 | }; | |
| 589 | ||
| 590 | let desc = match room.join_rule() { | |
| 591 | None => "<unknown>".into(), | |
| 592 | Some(JoinRule::Invite) => "invite".into(), | |
| 593 | Some(JoinRule::Knock) => "knock".into(), | |
| 594 | Some(JoinRule::Private) => "private".into(), | |
| 595 | Some(JoinRule::Public) => "public".into(), | |
| 596 | Some(JoinRule::Restricted(restrictions)) => { | |
| 597 | let allowing = show_restrictions(restrictions); | |
| 598 | if allowing.is_empty() { | |
| 599 | "restricted".into() | |
| 600 | } else { | |
| 601 | format!("restricted, allowing {allowing}") | |
| 602 | } | |
| 603 | }, | |
| 604 | Some(JoinRule::KnockRestricted(restrictions)) => { | |
| 605 | let allowing = show_restrictions(restrictions); | |
| 606 | if allowing.is_empty() { | |
| 607 | "knock-restricted".into() | |
| 608 | } else { | |
| 609 | format!("knock-restricted, allowing {allowing}") | |
| 610 | } | |
| 611 | }, | |
| 612 | Some(other) => format!("{other:?}"), | |
| 613 | }; | |
| 614 | ||
| 615 | format!("Room join rules are set to: {desc}") | |
| 616 | }, | |
| 569 | 617 | RoomField::Aliases => { |
| 570 | 618 | let aliases = room |
| 571 | 619 | .alt_aliases() |