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 {
369/// A room property. 369/// A room property.
370#[derive(Clone, Debug, Eq, PartialEq)] 370#[derive(Clone, Debug, Eq, PartialEq)]
371pub enum RoomField { 371pub enum RoomField {
372 /// The room's history visibility.
373 History,
374
372 /// The room name. 375 /// The room name.
373 Name, 376 Name,
374 377
@@ -636,6 +639,10 @@ pub type MessageReactions = HashMap<OwnedEventId, (String, OwnedUserId)>;
636/// Errors encountered during application use. 639/// Errors encountered during application use.
637#[derive(thiserror::Error, Debug)] 640#[derive(thiserror::Error, Debug)]
638pub enum IambError { 641pub enum IambError {
642 /// An invalid history visibility was specified.
643 #[error("Invalid history visibility setting: {0}")]
644 InvalidHistoryVisibility(String),
645
639 /// An invalid notification level was specified. 646 /// An invalid notification level was specified.
640 #[error("Invalid notification level: {0}")] 647 #[error("Invalid notification level: {0}")]
641 InvalidNotificationLevel(String), 648 InvalidNotificationLevel(String),
src/commands.rs +12
@@ -423,6 +423,18 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
423 RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into() 423 RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into()
424 }, 424 },
425 425
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
426 // :room name set <room-name> 438 // :room name set <room-name>
427 ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(), 439 ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
428 ("name", "set", None) => return Result::Err(CommandError::InvalidArgument), 440 ("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
src/windows/room/mod.rs +29
@@ -15,6 +15,7 @@ use matrix_sdk::{
15 events::{ 15 events::{
16 room::{ 16 room::{
17 canonical_alias::RoomCanonicalAliasEventContent, 17 canonical_alias::RoomCanonicalAliasEventContent,
18 history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
18 name::RoomNameEventContent, 19 name::RoomNameEventContent,
19 topic::RoomTopicEventContent, 20 topic::RoomTopicEventContent,
20 }, 21 },
@@ -98,6 +99,20 @@ fn notification_mode(name: impl Into<String>) -> IambResult<RoomNotificationMode
98 Ok(mode) 99 Ok(mode)
99} 100}
100 101
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
101/// State for a Matrix room or space. 116/// State for a Matrix room or space.
102/// 117///
103/// Since spaces function as special rooms within Matrix, we wrap their window state together, so 118/// Since spaces function as special rooms within Matrix, we wrap their window state together, so
@@ -339,6 +354,11 @@ impl RoomState {
339 .ok_or(UIError::Application(IambError::NotJoined))?; 354 .ok_or(UIError::Application(IambError::NotJoined))?;
340 355
341 match field { 356 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 },
342 RoomField::Name => { 362 RoomField::Name => {
343 let ev = RoomNameEventContent::new(value); 363 let ev = RoomNameEventContent::new(value);
344 let _ = room.send_state_event(ev).await.map_err(IambError::from)?; 364 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@@ -455,6 +475,11 @@ impl RoomState {
455 .ok_or(UIError::Application(IambError::NotJoined))?; 475 .ok_or(UIError::Application(IambError::NotJoined))?;
456 476
457 match field { 477 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 },
458 RoomField::Name => { 483 RoomField::Name => {
459 let ev = RoomNameEventContent::new("".into()); 484 let ev = RoomNameEventContent::new("".into());
460 let _ = room.send_state_event(ev).await.map_err(IambError::from)?; 485 let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@@ -545,6 +570,10 @@ impl RoomState {
545 .ok_or(UIError::Application(IambError::NotJoined))?; 570 .ok_or(UIError::Application(IambError::NotJoined))?;
546 571
547 let msg = match field { 572 let msg = match field {
573 RoomField::History => {
574 let visibility = room.history_visibility();
575 format!("Room history visibility: {visibility}")
576 },
548 RoomField::Name => { 577 RoomField::Name => {
549 match room.name() { 578 match room.name() {
550 None => "Room has no name".into(), 579 None => "Room has no name".into(),