Commit 992459575f

992459575ff1cb34dd7095b9409333bf89e6e54f

parent: fc1c1649a8

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-09-04 20:39 UTC
committer: <noreply@github.com>

Add a `:room access` command for managing join rules (#673)

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
156156Leave the currently focused room.
157157.It Sy ":members"
158158View a list of members of the currently focused room.
159.It Sy ":room name set [name]"
160Set the name of the currently focused room.
161.It Sy ":room name unset"
162Unset the name of the currently focused room.
159.It Sy ":room access set [rule]"
160Set the join rules for the room to control who can access it.
161Possible values for the
162.Sy rule
163argument are
164.Dq invite ,
165.Dq knock ,
166.Dq knock-restricted ,
167.Dq public ,
168and
169.Dq restricted .
170The
171.Dq knock-restricted
172and
173.Dq restricted
174values allow additional
175.Sy ++members=!room:example.org
176arguments to specify rooms (and spaces) whose members are allowed to join without an invite or knocking.
177.It Sy ":room access unset"
178Set the room to being invite-only so that those not already members cannot join.
179.It Sy ":room access show"
180Show the current join rules for the room.
163181.It Sy ":room dm set"
164182Mark the currently focused room as a direct message.
165183.It Sy ":room dm unset"
166184Mark the currently focused room as a normal room.
185.It Sy ":room name set [name]"
186Set the name of the currently focused room.
187.It Sy ":room name unset"
188Unset the name of the currently focused room.
167189.It Sy ":room unread set"
168190Mark the currently focused room as unread.
169191.It Sy ":room unread unset"
src/base.rs +13
@@ -37,6 +37,7 @@ use matrix_sdk::{
3737 EventId,
3838 OwnedEventId,
3939 OwnedMxcUri,
40 OwnedRoomAliasId,
4041 OwnedRoomId,
4142 OwnedRoomOrAliasId,
4243 OwnedTransactionId,
@@ -67,6 +68,7 @@ use matrix_sdk::{
6768 },
6869 presence::PresenceState,
6970 profile::{ProfileFieldName, ProfileFieldValue},
71 room::JoinRule,
7072 },
7173};
7274
@@ -409,6 +411,9 @@ impl Visitor<'_> for SortUserVisitor {
409411/// A room property.
410412#[derive(Clone, Debug, Eq, PartialEq)]
411413pub enum RoomField {
414 /// The room's join rules, aka who can access this room.
415 Access,
416
412417 /// The room's history visibility.
413418 History,
414419
@@ -498,6 +503,9 @@ pub enum RoomAction {
498503 /// Set whether a room is a direct message.
499504 SetDirect(bool),
500505
506 /// Set the join rules for a room to control who can access it and how.
507 SetAccess(JoinRule),
508
501509 /// Set a room property.
502510 Set(RoomField, String),
503511
@@ -1909,6 +1917,11 @@ impl ChatStore {
19091917 }
19101918 }
19111919
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
19121925 /// Get the title for a room.
19131926 pub fn get_room_title(&self, room_id: &RoomId) -> String {
19141927 self.rooms
src/commands.rs +76 −5
@@ -6,11 +6,13 @@ use std::{convert::TryFrom, str::FromStr as _};
66
77use matrix_sdk::ruma::{
88 OwnedMxcUri,
9 OwnedRoomId,
910 OwnedRoomOrAliasId,
1011 OwnedUserId,
1112 RoomVersionId,
1213 events::tag::TagName,
1314 profile::{ProfileFieldName, ProfileFieldValue},
15 room::{AllowRule, JoinRule, Restricted as JoinRestrictions},
1416};
1517
1618use modalkit::{
@@ -628,16 +630,63 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
628630 let arg = iter.next();
629631 let trailing = iter.collect::<Vec<_>>();
630632
631 match (field.as_str(), action.as_str()) {
633 match (field.as_str(), action.as_str(), arg.as_deref()) {
632634 // Skip check for commands that takes a variable number of arguments:
633 ("version", "upgrade") => (),
635 ("access", "set", Some("restricted" | "knock-restricted")) => (),
636 ("version", "upgrade", _) => (),
634637
635638 // 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 (_, _, _) => (),
638641 }
639642
640643 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
641690 // :room dm set
642691 ("dm", "set", None) => RoomAction::SetDirect(true).into(),
643692 ("dm", "set", Some(_)) => return Result::Err(CommandError::InvalidArgument),
@@ -1109,7 +1158,7 @@ pub fn setup_commands() -> ProgramCommands {
11091158#[cfg(test)]
11101159mod tests {
11111160 use super::*;
1112 use matrix_sdk::ruma::user_id;
1161 use matrix_sdk::ruma::{owned_room_id, user_id};
11131162 use modalkit::actions::WindowAction;
11141163 use modalkit::editing::context::EditContext;
11151164
@@ -1743,4 +1792,26 @@ mod tests {
17431792 let err = CommandError::InvalidArgument;
17441793 assert_eq!(res, err);
17451794 }
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 }
17461817}
src/completions.rs +11 −1
@@ -413,6 +413,7 @@ fn complete_iamb_create(args: Vec<String>) -> Vec<String> {
413413// "kick","ban","unban", ".. unset" and "dm/tag set/unset"
414414fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> {
415415 let subcmds = [
416 "access",
416417 "dm",
417418 "kick",
418419 "ban",
@@ -437,7 +438,7 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> {
437438 "id" => complete_choices(input, &["show"]),
438439 "dm" | "name" | "tag" => complete_choices(input, &["set", "unset"]),
439440
440 "history" | "topic" | "notify" | "alias" | "canonicalalias" | "canon" => {
441 "access" | "history" | "topic" | "notify" | "alias" | "canonicalalias" | "canon" => {
441442 complete_choices(input, &["show", "set", "unset"])
442443 },
443444
@@ -449,6 +450,15 @@ fn complete_iamb_room(args: Vec<String>, store: &ChatStore) -> Vec<String> {
449450 } else if args.len() == 3 {
450451 let input = &args[2];
451452 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 },
452462 ("history", "set") => {
453463 complete_choices(input, &["invited", "joined", "shared", "world_readable"])
454464 },
src/windows/room/mod.rs +64 −16
@@ -24,6 +24,7 @@ use matrix_sdk::{
2424 },
2525 tag::{TagInfo, Tags},
2626 },
27 room::{AllowRule, JoinRule, Restricted as JoinRestrictions},
2728 },
2829};
2930
@@ -255,6 +256,16 @@ pub async fn room_command(
255256
256257 Ok(vec![(act, cmd.context.clone())])
257258 },
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 },
258269 RoomAction::SetDirect(is_direct) => {
259270 let room = store
260271 .application
@@ -404,14 +415,8 @@ pub async fn room_command(
404415 .await
405416 .map_err(IambError::from)?;
406417 },
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.
415420 },
416421 }
417422
@@ -506,14 +511,8 @@ pub async fn room_command(
506511 RoomField::UserName => {
507512 room.set_own_member_display_name(None).await.map_err(IambError::from)?;
508513 },
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.
517516 },
518517 }
519518
@@ -566,6 +565,55 @@ pub async fn room_command(
566565
567566 format!("Room notification level: {level:?}")
568567 },
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 },
569617 RoomField::Aliases => {
570618 let aliases = room
571619 .alt_aliases()