Commit 3003f0a528

3003f0a5286556bb0108ca3693d60f9b47ff078c

parent: df3896df9c

Unregistered key

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

Add command for setting room history visibility (#328)

Layout: unified · split

src/base.rs +7
@@ -369,6 +369,9 @@ impl<'de> Visitor<'de> for SortUserVisitor {
369369/// A room property.
370370#[derive(Clone, Debug, Eq, PartialEq)]
371371pub enum RoomField {
372 /// The room's history visibility.
373 History,
374
372375 /// The room name.
373376 Name,
374377
@@ -636,6 +639,10 @@ pub type MessageReactions = HashMap<OwnedEventId, (String, OwnedUserId)>;
636639/// Errors encountered during application use.
637640#[derive(thiserror::Error, Debug)]
638641pub enum IambError {
642 /// An invalid history visibility was specified.
643 #[error("Invalid history visibility setting: {0}")]
644 InvalidHistoryVisibility(String),
645
639646 /// An invalid notification level was specified.
640647 #[error("Invalid notification level: {0}")]
641648 InvalidNotificationLevel(String),
src/commands.rs +12
@@ -423,6 +423,18 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
423423 RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into()
424424 },
425425
426 // :room history set <visibility>
427 ("history", "set", Some(s)) => RoomAction::Set(RoomField::History, s).into(),
428 ("history", "set", None) => return Result::Err(CommandError::InvalidArgument),
429
430 // :room history unset
431 ("history", "unset", None) => RoomAction::Unset(RoomField::History).into(),
432 ("history", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
433
434 // :room history show
435 ("history", "show", None) => RoomAction::Show(RoomField::History).into(),
436 ("history", "show", Some(_)) => return Result::Err(CommandError::InvalidArgument),
437
426438 // :room name set <room-name>
427439 ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
428440 ("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
src/windows/room/mod.rs +29
@@ -15,6 +15,7 @@ use matrix_sdk::{
1515 events::{
1616 room::{
1717 canonical_alias::RoomCanonicalAliasEventContent,
18 history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
1819 name::RoomNameEventContent,
1920 topic::RoomTopicEventContent,
2021 },
@@ -98,6 +99,20 @@ fn notification_mode(name: impl Into<String>) -> IambResult<RoomNotificationMode
9899 Ok(mode)
99100}
100101
102fn hist_visibility_mode(name: impl Into<String>) -> IambResult<HistoryVisibility> {
103 let name = name.into();
104
105 let mode = match name.to_lowercase().as_str() {
106 "invited" => HistoryVisibility::Invited,
107 "joined" => HistoryVisibility::Joined,
108 "shared" => HistoryVisibility::Shared,
109 "world" | "world_readable" => HistoryVisibility::WorldReadable,
110 _ => return Err(IambError::InvalidHistoryVisibility(name).into()),
111 };
112
113 Ok(mode)
114}
115
101116/// State for a Matrix room or space.
102117///
103118/// Since spaces function as special rooms within Matrix, we wrap their window state together, so
@@ -339,6 +354,11 @@ impl RoomState {
339354 .ok_or(UIError::Application(IambError::NotJoined))?;
340355
341356 match field {
357 RoomField::History => {
358 let visibility = hist_visibility_mode(value)?;
359 let ev = RoomHistoryVisibilityEventContent::new(visibility);
360 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
361 },
342362 RoomField::Name => {
343363 let ev = RoomNameEventContent::new(value);
344364 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@@ -455,6 +475,11 @@ impl RoomState {
455475 .ok_or(UIError::Application(IambError::NotJoined))?;
456476
457477 match field {
478 RoomField::History => {
479 let visibility = HistoryVisibility::Joined;
480 let ev = RoomHistoryVisibilityEventContent::new(visibility);
481 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
482 },
458483 RoomField::Name => {
459484 let ev = RoomNameEventContent::new("".into());
460485 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@@ -545,6 +570,10 @@ impl RoomState {
545570 .ok_or(UIError::Application(IambError::NotJoined))?;
546571
547572 let msg = match field {
573 RoomField::History => {
574 let visibility = room.history_visibility();
575 format!("Room history visibility: {visibility}")
576 },
548577 RoomField::Name => {
549578 match room.name() {
550579 None => "Room has no name".into(),