Add command for setting room history visibility (#328)

This commit is contained in:
Ulyssa 2024-08-18 00:33:45 -07:00 committed by GitHub
parent df3896df9c
commit 3003f0a528
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 0 deletions

View file

@ -369,6 +369,9 @@ impl<'de> Visitor<'de> for SortUserVisitor {
/// A room property. /// A room property.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum RoomField { pub enum RoomField {
/// The room's history visibility.
History,
/// The room name. /// The room name.
Name, Name,
@ -636,6 +639,10 @@ pub type MessageReactions = HashMap<OwnedEventId, (String, OwnedUserId)>;
/// Errors encountered during application use. /// Errors encountered during application use.
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum IambError { pub enum IambError {
/// An invalid history visibility was specified.
#[error("Invalid history visibility setting: {0}")]
InvalidHistoryVisibility(String),
/// An invalid notification level was specified. /// An invalid notification level was specified.
#[error("Invalid notification level: {0}")] #[error("Invalid notification level: {0}")]
InvalidNotificationLevel(String), InvalidNotificationLevel(String),

View file

@ -423,6 +423,18 @@ fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into() RoomAction::MemberUpdate(MemberUpdateAction::Unban, u.into(), r, desc.bang).into()
}, },
// :room history set <visibility>
("history", "set", Some(s)) => RoomAction::Set(RoomField::History, s).into(),
("history", "set", None) => return Result::Err(CommandError::InvalidArgument),
// :room history unset
("history", "unset", None) => RoomAction::Unset(RoomField::History).into(),
("history", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
// :room history show
("history", "show", None) => RoomAction::Show(RoomField::History).into(),
("history", "show", Some(_)) => return Result::Err(CommandError::InvalidArgument),
// :room name set <room-name> // :room name set <room-name>
("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(), ("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
("name", "set", None) => return Result::Err(CommandError::InvalidArgument), ("name", "set", None) => return Result::Err(CommandError::InvalidArgument),

View file

@ -15,6 +15,7 @@ use matrix_sdk::{
events::{ events::{
room::{ room::{
canonical_alias::RoomCanonicalAliasEventContent, canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
name::RoomNameEventContent, name::RoomNameEventContent,
topic::RoomTopicEventContent, topic::RoomTopicEventContent,
}, },
@ -98,6 +99,20 @@ fn notification_mode(name: impl Into<String>) -> IambResult<RoomNotificationMode
Ok(mode) Ok(mode)
} }
fn hist_visibility_mode(name: impl Into<String>) -> IambResult<HistoryVisibility> {
let name = name.into();
let mode = match name.to_lowercase().as_str() {
"invited" => HistoryVisibility::Invited,
"joined" => HistoryVisibility::Joined,
"shared" => HistoryVisibility::Shared,
"world" | "world_readable" => HistoryVisibility::WorldReadable,
_ => return Err(IambError::InvalidHistoryVisibility(name).into()),
};
Ok(mode)
}
/// State for a Matrix room or space. /// State for a Matrix room or space.
/// ///
/// Since spaces function as special rooms within Matrix, we wrap their window state together, so /// Since spaces function as special rooms within Matrix, we wrap their window state together, so
@ -339,6 +354,11 @@ impl RoomState {
.ok_or(UIError::Application(IambError::NotJoined))?; .ok_or(UIError::Application(IambError::NotJoined))?;
match field { match field {
RoomField::History => {
let visibility = hist_visibility_mode(value)?;
let ev = RoomHistoryVisibilityEventContent::new(visibility);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
RoomField::Name => { RoomField::Name => {
let ev = RoomNameEventContent::new(value); let ev = RoomNameEventContent::new(value);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?; let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@ -455,6 +475,11 @@ impl RoomState {
.ok_or(UIError::Application(IambError::NotJoined))?; .ok_or(UIError::Application(IambError::NotJoined))?;
match field { match field {
RoomField::History => {
let visibility = HistoryVisibility::Joined;
let ev = RoomHistoryVisibilityEventContent::new(visibility);
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
},
RoomField::Name => { RoomField::Name => {
let ev = RoomNameEventContent::new("".into()); let ev = RoomNameEventContent::new("".into());
let _ = room.send_state_event(ev).await.map_err(IambError::from)?; let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
@ -545,6 +570,10 @@ impl RoomState {
.ok_or(UIError::Application(IambError::NotJoined))?; .ok_or(UIError::Application(IambError::NotJoined))?;
let msg = match field { let msg = match field {
RoomField::History => {
let visibility = room.history_visibility();
format!("Room history visibility: {visibility}")
},
RoomField::Name => { RoomField::Name => {
match room.name() { match room.name() {
None => "Room has no name".into(), None => "Room has no name".into(),