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 | Leave the currently focused room. | 156 | Leave the currently focused room. |
| 157 | .It Sy ":members" | 157 | .It Sy ":members" |
| 158 | View a list of members of the currently focused room. | 158 | View a list of members of the currently focused room. |
| 159 | .It Sy ":room name set [name]" | 159 | .It Sy ":room access set [rule]" |
| 160 | Set the name of the currently focused room. | 160 | Set the join rules for the room to control who can access it. |
| 161 | .It Sy ":room name unset" | 161 | Possible values for the |
| 162 | Unset the name of the currently focused room. | 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 | .It Sy ":room dm set" | 181 | .It Sy ":room dm set" |
| 164 | Mark the currently focused room as a direct message. | 182 | Mark the currently focused room as a direct message. |
| 165 | .It Sy ":room dm unset" | 183 | .It Sy ":room dm unset" |
| 166 | Mark the currently focused room as a normal room. | 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 | .It Sy ":room unread set" | 189 | .It Sy ":room unread set" |
| 168 | Mark the currently focused room as unread. | 190 | Mark the currently focused room as unread. |
| 169 | .It Sy ":room unread unset" | 191 | .It Sy ":room unread unset" |
src/base.rs +13
| @@ -37,6 +37,7 @@ use matrix_sdk::{ | |||
| 37 | EventId, | 37 | EventId, |
| 38 | OwnedEventId, | 38 | OwnedEventId, |
| 39 | OwnedMxcUri, | 39 | OwnedMxcUri, |
| 40 | OwnedRoomAliasId, | ||
| 40 | OwnedRoomId, | 41 | OwnedRoomId, |
| 41 | OwnedRoomOrAliasId, | 42 | OwnedRoomOrAliasId, |
| 42 | OwnedTransactionId, | 43 | OwnedTransactionId, |
| @@ -67,6 +68,7 @@ use matrix_sdk::{ | |||
| 67 | }, | 68 | }, |
| 68 | presence::PresenceState, | 69 | presence::PresenceState, |
| 69 | profile::{ProfileFieldName, ProfileFieldValue}, | 70 | profile::{ProfileFieldName, ProfileFieldValue}, |
| 71 | room::JoinRule, | ||
| 70 | }, | 72 | }, |
| 71 | }; | 73 | }; |
| 72 | 74 | ||
| @@ -409,6 +411,9 @@ impl Visitor<'_> for SortUserVisitor { | |||
| 409 | /// A room property. | 411 | /// A room property. |
| 410 | #[derive(Clone, Debug, Eq, PartialEq)] | 412 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 411 | pub enum RoomField { | 413 | pub enum RoomField { |
| 414 | /// The room's join rules, aka who can access this room. | ||
| 415 | Access, | ||
| 416 | |||
| 412 | /// The room's history visibility. | 417 | /// The room's history visibility. |
| 413 | History, | 418 | History, |
| 414 | 419 | ||
| @@ -498,6 +503,9 @@ pub enum RoomAction { | |||
| 498 | /// Set whether a room is a direct message. | 503 | /// Set whether a room is a direct message. |
| 499 | SetDirect(bool), | 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 | /// Set a room property. | 509 | /// Set a room property. |
| 502 | Set(RoomField, String), | 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 | /// Get the title for a room. | 1925 | /// Get the title for a room. |
| 1913 | pub fn get_room_title(&self, room_id: &RoomId) -> String { | 1926 | pub fn get_room_title(&self, room_id: &RoomId) -> String { |
| 1914 | self.rooms | 1927 | self.rooms |
src/commands.rs +76 −5
| @@ -6,11 +6,13 @@ use std::{convert::TryFrom, str::FromStr as _}; | |||
| 6 | 6 | ||
| 7 | use matrix_sdk::ruma::{ | 7 | use matrix_sdk::ruma::{ |
| 8 | OwnedMxcUri, | 8 | OwnedMxcUri, |
| 9 | OwnedRoomId, | ||
| 9 | OwnedRoomOrAliasId, | 10 | OwnedRoomOrAliasId, |
| 10 | OwnedUserId, | 11 | OwnedUserId, |
| 11 | RoomVersionId, | 12 | RoomVersionId, |
| 12 | events::tag::TagName, | 13 | events::tag::TagName, |
| 13 | profile::{ProfileFieldName, ProfileFieldValue}, | 14 | profile::{ProfileFieldName, ProfileFieldValue}, |
| 15 | room::{AllowRule, JoinRule, Restricted as JoinRestrictions}, | ||
| 14 | }; | 16 | }; |
| 15 | 17 | ||
| 16 | use modalkit::{ | 18 | use modalkit::{ |
| @@ -628,16 +630,63 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | |||
| 628 | let arg = iter.next(); | 630 | let arg = iter.next(); |
| 629 | let trailing = iter.collect::<Vec<_>>(); | 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 | // Skip check for commands that takes a variable number of arguments: | 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 | // Reject if we have any trailing arguments: | 638 | // Reject if we have any trailing arguments: |
| 636 | (_, _) if !trailing.is_empty() => return Result::Err(CommandError::InvalidArgument), | 639 | (_, _, _) if !trailing.is_empty() => return Result::Err(CommandError::InvalidArgument), |
| 637 | (_, _) => (), | 640 | (_, _, _) => (), |
| 638 | } | 641 | } |
| 639 | 642 | ||
| 640 | let act: IambAction = match (field.as_str(), action.as_str(), arg) { | 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 | // :room dm set | 690 | // :room dm set |
| 642 | ("dm", "set", None) => RoomAction::SetDirect(true).into(), | 691 | ("dm", "set", None) => RoomAction::SetDirect(true).into(), |
| 643 | ("dm", "set", Some(_)) => return Result::Err(CommandError::InvalidArgument), | 692 | ("dm", "set", Some(_)) => return Result::Err(CommandError::InvalidArgument), |
| @@ -1109,7 +1158,7 @@ pub fn setup_commands() -> ProgramCommands { | |||
| 1109 | #[cfg(test)] | 1158 | #[cfg(test)] |
| 1110 | mod tests { | 1159 | mod tests { |
| 1111 | use super::*; | 1160 | use super::*; |
| 1112 | use matrix_sdk::ruma::user_id; | 1161 | use matrix_sdk::ruma::{owned_room_id, user_id}; |
| 1113 | use modalkit::actions::WindowAction; | 1162 | use modalkit::actions::WindowAction; |
| 1114 | use modalkit::editing::context::EditContext; | 1163 | use modalkit::editing::context::EditContext; |
| 1115 | 1164 | ||
| @@ -1743,4 +1792,26 @@ mod tests { | |||
| 1743 | let err = CommandError::InvalidArgument; | 1792 | let err = CommandError::InvalidArgument; |
| 1744 | assert_eq!(res, err); | 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 | // "kick","ban","unban", ".. unset" and "dm/tag set/unset" | 413 | // "kick","ban","unban", ".. unset" and "dm/tag set/unset" |
| 414 | fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { | 414 | fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { |
| 415 | let subcmds = [ | 415 | let subcmds = [ |
| 416 | "access", | ||
| 416 | "dm", | 417 | "dm", |
| 417 | "kick", | 418 | "kick", |
| 418 | "ban", | 419 | "ban", |
| @@ -437,7 +438,7 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { | |||
| 437 | "id" => complete_choices(input, &["show"]), | 438 | "id" => complete_choices(input, &["show"]), |
| 438 | "dm" | "name" | "tag" => complete_choices(input, &["set", "unset"]), | 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 | complete_choices(input, &["show", "set", "unset"]) | 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 | } else if args.len() == 3 { | 450 | } else if args.len() == 3 { |
| 450 | let input = &args[2]; | 451 | let input = &args[2]; |
| 451 | match (args[0].as_str(), args[1].as_str()) { | 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 | ("history", "set") => { | 462 | ("history", "set") => { |
| 453 | complete_choices(input, &["invited", "joined", "shared", "world_readable"]) | 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 | tag::{TagInfo, Tags}, | 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 | Ok(vec![(act, cmd.context.clone())]) | 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 | RoomAction::SetDirect(is_direct) => { | 269 | RoomAction::SetDirect(is_direct) => { |
| 259 | let room = store | 270 | let room = store |
| 260 | .application | 271 | .application |
| @@ -404,14 +415,8 @@ pub async fn room_command( | |||
| 404 | .await | 415 | .await |
| 405 | .map_err(IambError::from)?; | 416 | .map_err(IambError::from)?; |
| 406 | }, | 417 | }, |
| 407 | RoomField::Aliases => { | 418 | RoomField::Access | RoomField::Aliases | RoomField::Id | RoomField::Version => { |
| 408 | // This never happens, aliases is only used for showing | 419 | // These variants exist for RoomAction::Show, so we never actually get here. |
| 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. | ||
| 415 | }, | 420 | }, |
| 416 | } | 421 | } |
| 417 | 422 | ||
| @@ -506,14 +511,8 @@ pub async fn room_command( | |||
| 506 | RoomField::UserName => { | 511 | RoomField::UserName => { |
| 507 | room.set_own_member_display_name(None).await.map_err(IambError::from)?; | 512 | room.set_own_member_display_name(None).await.map_err(IambError::from)?; |
| 508 | }, | 513 | }, |
| 509 | RoomField::Aliases => { | 514 | RoomField::Access | RoomField::Aliases | RoomField::Id | RoomField::Version => { |
| 510 | // This will not happen, you cannot unset all aliases | 515 | // These variants exist for RoomAction::Show, so we never actually get here. |
| 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. | ||
| 517 | }, | 516 | }, |
| 518 | } | 517 | } |
| 519 | 518 | ||
| @@ -566,6 +565,55 @@ pub async fn room_command( | |||
| 566 | 565 | ||
| 567 | format!("Room notification level: {level:?}") | 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 | RoomField::Aliases => { | 617 | RoomField::Aliases => { |
| 570 | let aliases = room | 618 | let aliases = room |
| 571 | .alt_aliases() | 619 | .alt_aliases() |