Commit fa91b75924
Unregistered key
Layout: unified · split
docs/iamb.1 +2 −1
| @@ -174,8 +174,9 @@ The | ||
| 174 | 174 | and |
| 175 | 175 | .Dq restricted |
| 176 | 176 | values allow additional |
| 177 | .Sy ++members=!room:example.org | |
| 177 | .Sy ++members=[room] | |
| 178 | 178 | arguments to specify rooms (and spaces) whose members are allowed to join without an invite or knocking. |
| 179 | If a room alias is given instead of an id, it is resolved immediately. | |
| 179 | 180 | .It Sy ":room access unset" |
| 180 | 181 | Set the room to being invite-only so that those not already members cannot join. |
| 181 | 182 | .It Sy ":room access show" |
src/base.rs +49 −1
| @@ -12,7 +12,9 @@ use std::time::{Duration, Instant}; | ||
| 12 | 12 | |
| 13 | 13 | use emojis::Emoji; |
| 14 | 14 | |
| 15 | use matrix_sdk::Client; | |
| 15 | 16 | use matrix_sdk::encryption::verification::VerificationRequest; |
| 17 | use matrix_sdk::ruma::room::{AllowRule, Restricted}; | |
| 16 | 18 | use ratatui::{ |
| 17 | 19 | buffer::Buffer, |
| 18 | 20 | layout::{Alignment, Rect}, |
| @@ -469,6 +471,52 @@ impl Display for MemberUpdateAction { | ||
| 469 | 471 | } |
| 470 | 472 | } |
| 471 | 473 | |
| 474 | /// An internal version of [`JoinRule`] | |
| 475 | #[derive(Clone, Debug, Eq, PartialEq)] | |
| 476 | pub enum IambJoinRule { | |
| 477 | Public, | |
| 478 | Restricted(Vec<OwnedRoomOrAliasId>), | |
| 479 | Knock, | |
| 480 | KnockRestricted(Vec<OwnedRoomOrAliasId>), | |
| 481 | Invite, | |
| 482 | } | |
| 483 | ||
| 484 | impl IambJoinRule { | |
| 485 | pub async fn into_join_rule(self, client: &Client) -> Result<JoinRule, IambError> { | |
| 486 | async fn resolve_aliases( | |
| 487 | rooms: Vec<OwnedRoomOrAliasId>, | |
| 488 | client: &Client, | |
| 489 | ) -> Result<Restricted, IambError> { | |
| 490 | let mut allow = vec![]; | |
| 491 | for room in rooms { | |
| 492 | let alias = match OwnedRoomId::try_from(room) { | |
| 493 | Ok(room_id) => { | |
| 494 | allow.push(AllowRule::room_membership(room_id)); | |
| 495 | continue; | |
| 496 | }, | |
| 497 | Err(alias) => alias, | |
| 498 | }; | |
| 499 | ||
| 500 | let resp = client.resolve_room_alias(&alias).await?; | |
| 501 | ||
| 502 | allow.push(AllowRule::room_membership(resp.room_id)); | |
| 503 | } | |
| 504 | ||
| 505 | Ok(Restricted::new(allow)) | |
| 506 | } | |
| 507 | ||
| 508 | Ok(match self { | |
| 509 | Self::Public => JoinRule::Public, | |
| 510 | Self::Invite => JoinRule::Invite, | |
| 511 | Self::Knock => JoinRule::Knock, | |
| 512 | Self::Restricted(rooms) => JoinRule::Restricted(resolve_aliases(rooms, client).await?), | |
| 513 | Self::KnockRestricted(rooms) => { | |
| 514 | JoinRule::KnockRestricted(resolve_aliases(rooms, client).await?) | |
| 515 | }, | |
| 516 | }) | |
| 517 | } | |
| 518 | } | |
| 519 | ||
| 472 | 520 | /// An action that operates on a focused room. |
| 473 | 521 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 474 | 522 | pub enum RoomAction { |
| @@ -507,7 +555,7 @@ pub enum RoomAction { | ||
| 507 | 555 | SetDirect(bool), |
| 508 | 556 | |
| 509 | 557 | /// Set the join rules for a room to control who can access it and how. |
| 510 | SetAccess(JoinRule), | |
| 558 | SetAccess(IambJoinRule), | |
| 511 | 559 | |
| 512 | 560 | /// Set a room property. |
| 513 | 561 | Set(RoomField, String), |
src/commands.rs +15 −18
| @@ -6,13 +6,11 @@ use std::{convert::TryFrom, str::FromStr as _}; | ||
| 6 | 6 | |
| 7 | 7 | use matrix_sdk::ruma::{ |
| 8 | 8 | OwnedMxcUri, |
| 9 | OwnedRoomId, | |
| 10 | 9 | OwnedRoomOrAliasId, |
| 11 | 10 | OwnedUserId, |
| 12 | 11 | RoomVersionId, |
| 13 | 12 | events::tag::TagName, |
| 14 | 13 | profile::{ProfileFieldName, ProfileFieldValue}, |
| 15 | room::{AllowRule, JoinRule, Restricted as JoinRestrictions}, | |
| 16 | 14 | }; |
| 17 | 15 | |
| 18 | 16 | use modalkit::{ |
| @@ -28,6 +26,7 @@ use crate::base::{ | ||
| 28 | 26 | HomeserverAction, |
| 29 | 27 | IambAction, |
| 30 | 28 | IambId, |
| 29 | IambJoinRule, | |
| 31 | 30 | KeysAction, |
| 32 | 31 | MemberUpdateAction, |
| 33 | 32 | MessageAction, |
| @@ -644,7 +643,7 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 644 | 643 | let act: IambAction = match (field.as_str(), action.as_str(), arg) { |
| 645 | 644 | // :room access set |
| 646 | 645 | ("access", "set", Some(rule)) => { |
| 647 | let allow = trailing | |
| 646 | let rooms = trailing | |
| 648 | 647 | .iter() |
| 649 | 648 | .map(|a| { |
| 650 | 649 | let (flag, v) = match OptionType::from_str(a)? { |
| @@ -655,25 +654,23 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 655 | 654 | |
| 656 | 655 | match flag.as_str() { |
| 657 | 656 | "members" => { |
| 658 | let room_id = OwnedRoomId::from_str(&v).map_err(|e| { | |
| 657 | OwnedRoomOrAliasId::from_str(&v).map_err(|e| { | |
| 659 | 658 | CommandError::Error(format!( |
| 660 | 659 | "{v:?} is not a valid room identifier: {e}" |
| 661 | 660 | )) |
| 662 | })?; | |
| 663 | Ok(AllowRule::room_membership(room_id)) | |
| 661 | }) | |
| 664 | 662 | }, |
| 665 | 663 | _ => Err(CommandError::Error(format!("unknown flag {flag:?}"))), |
| 666 | 664 | } |
| 667 | 665 | }) |
| 668 | 666 | .collect::<Result<Vec<_>, _>>()?; |
| 669 | let restrictions = JoinRestrictions::new(allow); | |
| 670 | 667 | |
| 671 | 668 | let rule = match rule.as_str() { |
| 672 | "invite" => JoinRule::Invite, | |
| 673 | "knock" => JoinRule::Knock, | |
| 674 | "knock-restricted" => JoinRule::KnockRestricted(restrictions), | |
| 675 | "public" => JoinRule::Public, | |
| 676 | "restricted" => JoinRule::Restricted(restrictions), | |
| 669 | "invite" => IambJoinRule::Invite, | |
| 670 | "knock" => IambJoinRule::Knock, | |
| 671 | "knock-restricted" => IambJoinRule::KnockRestricted(rooms), | |
| 672 | "public" => IambJoinRule::Public, | |
| 673 | "restricted" => IambJoinRule::Restricted(rooms), | |
| 677 | 674 | _ => return Err(CommandError::InvalidArgument), |
| 678 | 675 | }; |
| 679 | 676 | RoomAction::SetAccess(rule).into() |
| @@ -681,7 +678,7 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 681 | 678 | ("access", "set", None) => return Result::Err(CommandError::InvalidArgument), |
| 682 | 679 | |
| 683 | 680 | // :room access unset |
| 684 | ("access", "unset", None) => RoomAction::SetAccess(JoinRule::Invite).into(), | |
| 681 | ("access", "unset", None) => RoomAction::SetAccess(IambJoinRule::Invite).into(), | |
| 685 | 682 | ("access", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument), |
| 686 | 683 | |
| 687 | 684 | // :room access unset |
| @@ -1800,19 +1797,19 @@ mod tests { | ||
| 1800 | 1797 | let ctx = EditContext::default(); |
| 1801 | 1798 | |
| 1802 | 1799 | let res = cmds.input_cmd("room access set knock", ctx.clone()).unwrap(); |
| 1803 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::Knock)); | |
| 1800 | let act = IambAction::Room(RoomAction::SetAccess(IambJoinRule::Knock)); | |
| 1804 | 1801 | assert_eq!(res, vec![(act.into(), ctx.clone())]); |
| 1805 | 1802 | |
| 1806 | 1803 | let res = cmds |
| 1807 | 1804 | .input_cmd("room access set knock-restricted ++members=!abcde:example.org", ctx.clone()) |
| 1808 | 1805 | .unwrap(); |
| 1809 | let allow = AllowRule::room_membership(owned_room_id!("!abcde:example.org")); | |
| 1810 | let restrictions = JoinRestrictions::new(vec![allow]); | |
| 1811 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::KnockRestricted(restrictions))); | |
| 1806 | let restrictions = vec![owned_room_id!("!abcde:example.org").into()]; | |
| 1807 | let act = | |
| 1808 | IambAction::Room(RoomAction::SetAccess(IambJoinRule::KnockRestricted(restrictions))); | |
| 1812 | 1809 | assert_eq!(res, vec![(act.into(), ctx.clone())]); |
| 1813 | 1810 | |
| 1814 | 1811 | let res = cmds.input_cmd("room access unset", ctx.clone()).unwrap(); |
| 1815 | let act = IambAction::Room(RoomAction::SetAccess(JoinRule::Invite)); | |
| 1812 | let act = IambAction::Room(RoomAction::SetAccess(IambJoinRule::Invite)); | |
| 1816 | 1813 | assert_eq!(res, vec![(act.into(), ctx.clone())]); |
| 1817 | 1814 | } |
| 1818 | 1815 | } |
src/completions.rs +22 −2
| @@ -295,6 +295,16 @@ fn complete_matrix_names(input: &str, store: &ChatStore) -> Vec<String> { | ||
| 295 | 295 | store.rooms.complete(input).into_iter().map(|i| i.to_string()).collect() |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | /// Tab completion for known room aliases and ids. | |
| 299 | fn complete_room_alias_or_id(input: &str, store: &ChatStore) -> Vec<String> { | |
| 300 | let list = store.names.complete(input); | |
| 301 | if !list.is_empty() { | |
| 302 | return list; | |
| 303 | } | |
| 304 | ||
| 305 | store.rooms.complete(input).into_iter().map(|i| i.to_string()).collect() | |
| 306 | } | |
| 307 | ||
| 298 | 308 | /// Tab completion for open verification requests |
| 299 | 309 | fn complete_verification(input: &str, store: &ChatStore) -> Vec<String> { |
| 300 | 310 | store.verifications.complete(input) |
| @@ -483,8 +493,18 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> { | ||
| 483 | 493 | } |
| 484 | 494 | } else { |
| 485 | 495 | let input = args.last().unwrap(); |
| 486 | match (args[0].as_str(), args[1].as_str()) { | |
| 487 | ("version", "upgrade") => complete_users(input, store), | |
| 496 | match (args[0].as_str(), args[1].as_str(), args[2].as_str()) { | |
| 497 | ("version", "upgrade", _) => complete_users(input, store), | |
| 498 | ("access", "set", "restricted") | ("access", "set", "knock-restricted") => { | |
| 499 | if let Some(remaining) = input.strip_prefix("++members=") { | |
| 500 | complete_room_alias_or_id(remaining, store) | |
| 501 | .into_iter() | |
| 502 | .map(|id| format!("++members={id}")) | |
| 503 | .collect() | |
| 504 | } else { | |
| 505 | complete_choices(input, &["++members="]) | |
| 506 | } | |
| 507 | }, | |
| 488 | 508 | |
| 489 | 509 | _ => vec![], |
| 490 | 510 | } |
src/windows/room/mod.rs +1
| @@ -260,6 +260,7 @@ pub async fn room_command( | ||
| 260 | 260 | let Some(room) = store.application.worker.client.get_room(id) else { |
| 261 | 261 | return Err(IambError::NotJoined.into()); |
| 262 | 262 | }; |
| 263 | let rule = rule.into_join_rule(&store.application.worker.client).await?; | |
| 263 | 264 | room.privacy_settings() |
| 264 | 265 | .update_join_rule(rule) |
| 265 | 266 | .await |