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