Commit 966c5ed6e9
Unregistered key
Layout: unified · split
src/windows/mod.rs +17 −2
| @@ -78,6 +78,7 @@ use crate::base::{ | ||
| 78 | 78 | SpaceAction, |
| 79 | 79 | UnreadInfo, |
| 80 | 80 | }; |
| 81 | use crate::windows::room::room_command; | |
| 81 | 82 | |
| 82 | 83 | use self::{room::RoomState, welcome::WelcomeState}; |
| 83 | 84 | use crate::message::MessageTimeStamp; |
| @@ -386,8 +387,22 @@ impl IambWindow { | ||
| 386 | 387 | ctx: ProgramContext, |
| 387 | 388 | store: &mut ProgramStore, |
| 388 | 389 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { |
| 389 | if let IambWindow::Room(w) = self { | |
| 390 | w.room_command(act, ctx, store).await | |
| 390 | let id = match self { | |
| 391 | IambWindow::Room(state) => Some(state.id()), | |
| 392 | IambWindow::MemberList(_, room_id, _) => Some(&**room_id), | |
| 393 | ||
| 394 | IambWindow::DirectList(state) => state.get().map(|state| state.room_id()), | |
| 395 | IambWindow::RoomList(state) => state.get().map(|state| state.room_id()), | |
| 396 | IambWindow::SpaceList(state) => state.get().map(|state| state.room_id()), | |
| 397 | IambWindow::ChatList(state) | IambWindow::UnreadList(state) => { | |
| 398 | state.get().map(|state| state.room_id()) | |
| 399 | }, | |
| 400 | ||
| 401 | _ => None, | |
| 402 | }; | |
| 403 | ||
| 404 | if let Some(id) = id { | |
| 405 | room_command(id, act, ctx, store).await | |
| 391 | 406 | } else { |
| 392 | 407 | return Err(IambError::NoSelectedRoomOrSpace.into()); |
| 393 | 408 | } |
src/windows/room/mod.rs +399 −405
| @@ -109,6 +109,405 @@ fn hist_visibility_mode(name: impl Into<String>) -> IambResult<HistoryVisibility | ||
| 109 | 109 | Ok(mode) |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | pub async fn room_command( | |
| 113 | id: &RoomId, | |
| 114 | act: RoomAction, | |
| 115 | ctx: ProgramContext, | |
| 116 | store: &mut ProgramStore, | |
| 117 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | |
| 118 | match act { | |
| 119 | RoomAction::InviteAccept => { | |
| 120 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 121 | room.join().await.map_err(IambError::from)?; | |
| 122 | Ok(vec![]) | |
| 123 | } else { | |
| 124 | Err(IambError::NotInvited.into()) | |
| 125 | } | |
| 126 | }, | |
| 127 | RoomAction::InviteReject => { | |
| 128 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 129 | room.leave().await.map_err(IambError::from)?; | |
| 130 | ||
| 131 | Ok(vec![]) | |
| 132 | } else { | |
| 133 | Err(IambError::NotInvited.into()) | |
| 134 | } | |
| 135 | }, | |
| 136 | RoomAction::InviteSend(user) => { | |
| 137 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 138 | room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?; | |
| 139 | ||
| 140 | Ok(vec![]) | |
| 141 | } else { | |
| 142 | Err(IambError::NotJoined.into()) | |
| 143 | } | |
| 144 | }, | |
| 145 | RoomAction::Leave(skip_confirm) => { | |
| 146 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 147 | if skip_confirm { | |
| 148 | room.leave().await.map_err(IambError::from)?; | |
| 149 | ||
| 150 | Ok(vec![]) | |
| 151 | } else { | |
| 152 | let msg = "Do you really want to leave this room?"; | |
| 153 | let leave = IambAction::Room(RoomAction::Leave(true)); | |
| 154 | let prompt = PromptYesNo::new(msg, vec![Action::from(leave)]); | |
| 155 | let prompt = Box::new(prompt); | |
| 156 | ||
| 157 | Err(UIError::NeedConfirm(prompt)) | |
| 158 | } | |
| 159 | } else { | |
| 160 | Err(IambError::NotJoined.into()) | |
| 161 | } | |
| 162 | }, | |
| 163 | RoomAction::MemberUpdate(mua, user, reason, skip_confirm) => { | |
| 164 | let Some(room) = store.application.worker.client.get_room(id) else { | |
| 165 | return Err(IambError::NotJoined.into()); | |
| 166 | }; | |
| 167 | ||
| 168 | let Ok(user_id) = OwnedUserId::try_from(user.as_str()) else { | |
| 169 | let err = IambError::InvalidUserId(user); | |
| 170 | ||
| 171 | return Err(err.into()); | |
| 172 | }; | |
| 173 | ||
| 174 | if !skip_confirm { | |
| 175 | let msg = format!("Do you really want to {mua} {user} from this room?"); | |
| 176 | let act = RoomAction::MemberUpdate(mua, user, reason, true); | |
| 177 | let act = IambAction::from(act); | |
| 178 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 179 | let prompt = Box::new(prompt); | |
| 180 | ||
| 181 | return Err(UIError::NeedConfirm(prompt)); | |
| 182 | } | |
| 183 | ||
| 184 | match mua { | |
| 185 | MemberUpdateAction::Ban => { | |
| 186 | room.ban_user(&user_id, reason.as_deref()).await.map_err(IambError::from)?; | |
| 187 | }, | |
| 188 | MemberUpdateAction::Unban => { | |
| 189 | room.unban_user(&user_id, reason.as_deref()) | |
| 190 | .await | |
| 191 | .map_err(IambError::from)?; | |
| 192 | }, | |
| 193 | MemberUpdateAction::Kick => { | |
| 194 | room.kick_user(&user_id, reason.as_deref()) | |
| 195 | .await | |
| 196 | .map_err(IambError::from)?; | |
| 197 | }, | |
| 198 | } | |
| 199 | ||
| 200 | Ok(vec![]) | |
| 201 | }, | |
| 202 | RoomAction::Members(mut cmd) => { | |
| 203 | let width = Count::Exact(30); | |
| 204 | let act = cmd | |
| 205 | .default_axis(Axis::Vertical) | |
| 206 | .default_relation(MoveDir1D::Next) | |
| 207 | .window(OpenTarget::Application(IambId::MemberList(id.to_owned())), width.into()); | |
| 208 | ||
| 209 | Ok(vec![(act, cmd.context.clone())]) | |
| 210 | }, | |
| 211 | RoomAction::SetDirect(is_direct) => { | |
| 212 | let room = store | |
| 213 | .application | |
| 214 | .get_joined_room(id) | |
| 215 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 216 | ||
| 217 | room.set_is_direct(is_direct).await.map_err(IambError::from)?; | |
| 218 | ||
| 219 | Ok(vec![]) | |
| 220 | }, | |
| 221 | RoomAction::Set(field, value) => { | |
| 222 | let room = store | |
| 223 | .application | |
| 224 | .get_joined_room(id) | |
| 225 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 226 | ||
| 227 | match field { | |
| 228 | RoomField::History => { | |
| 229 | let visibility = hist_visibility_mode(value)?; | |
| 230 | let ev = RoomHistoryVisibilityEventContent::new(visibility); | |
| 231 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 232 | }, | |
| 233 | RoomField::Name => { | |
| 234 | let ev = RoomNameEventContent::new(value); | |
| 235 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 236 | }, | |
| 237 | RoomField::Tag(tag) => { | |
| 238 | let mut info = TagInfo::new(); | |
| 239 | info.order = Some(1.0); | |
| 240 | ||
| 241 | let _ = room.set_tag(tag, info).await.map_err(IambError::from)?; | |
| 242 | }, | |
| 243 | RoomField::Topic => { | |
| 244 | let ev = RoomTopicEventContent::new(value); | |
| 245 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 246 | }, | |
| 247 | RoomField::NotificationMode => { | |
| 248 | let mode = notification_mode(value)?; | |
| 249 | let client = &store.application.worker.client; | |
| 250 | let notifications = client.notification_settings().await; | |
| 251 | ||
| 252 | notifications | |
| 253 | .set_room_notification_mode(id, mode) | |
| 254 | .await | |
| 255 | .map_err(IambError::from)?; | |
| 256 | }, | |
| 257 | RoomField::CanonicalAlias => { | |
| 258 | let client = &mut store.application.worker.client; | |
| 259 | ||
| 260 | let Ok(orai) = OwnedRoomAliasId::try_from(value.as_str()) else { | |
| 261 | let err = IambError::InvalidRoomAlias(value); | |
| 262 | ||
| 263 | return Err(err.into()); | |
| 264 | }; | |
| 265 | ||
| 266 | let mut alt_aliases = room.alt_aliases().into_iter().collect::<HashSet<_>>(); | |
| 267 | let canonical_old = room.canonical_alias(); | |
| 268 | ||
| 269 | // If the room's alias is already that, ignore it | |
| 270 | if canonical_old.as_ref() == Some(&orai) { | |
| 271 | let msg = format!("The canonical room alias is already {orai}"); | |
| 272 | ||
| 273 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 274 | } | |
| 275 | ||
| 276 | // Try creating the room alias on the server. | |
| 277 | if let Err(e) = client.create_room_alias(&orai, room.room_id()).await { | |
| 278 | if let Some(ClientApiErrorKind::Unknown) = e.client_api_error_kind() { | |
| 279 | // Ignore when it already exists. | |
| 280 | } else { | |
| 281 | return Err(IambError::from(e).into()); | |
| 282 | } | |
| 283 | } | |
| 284 | ||
| 285 | // Demote the previous one to an alt alias. | |
| 286 | alt_aliases.extend(canonical_old); | |
| 287 | ||
| 288 | // At this point the room alias definitely exists, and we can update the | |
| 289 | // state event. | |
| 290 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 291 | ev.alias = Some(orai); | |
| 292 | ev.alt_aliases = alt_aliases.into_iter().collect(); | |
| 293 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 294 | }, | |
| 295 | RoomField::Alias(alias) => { | |
| 296 | let client = &mut store.application.worker.client; | |
| 297 | ||
| 298 | let Ok(orai) = OwnedRoomAliasId::try_from(alias.as_str()) else { | |
| 299 | let err = IambError::InvalidRoomAlias(alias); | |
| 300 | ||
| 301 | return Err(err.into()); | |
| 302 | }; | |
| 303 | ||
| 304 | let mut alt_aliases = room.alt_aliases().into_iter().collect::<HashSet<_>>(); | |
| 305 | let canonical = room.canonical_alias(); | |
| 306 | ||
| 307 | if alt_aliases.contains(&orai) || canonical.as_ref() == Some(&orai) { | |
| 308 | let msg = format!("The alias {orai} already maps to this room"); | |
| 309 | ||
| 310 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 311 | } else { | |
| 312 | alt_aliases.insert(orai.clone()); | |
| 313 | } | |
| 314 | ||
| 315 | // If the room alias does not exist on the server, create it | |
| 316 | if let Err(e) = client.create_room_alias(&orai, room.room_id()).await { | |
| 317 | if let Some(ClientApiErrorKind::Unknown) = e.client_api_error_kind() { | |
| 318 | // Ignore when it already exists. | |
| 319 | } else { | |
| 320 | return Err(IambError::from(e).into()); | |
| 321 | } | |
| 322 | } | |
| 323 | ||
| 324 | // And add it to the aliases in the state event. | |
| 325 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 326 | ev.alias = canonical; | |
| 327 | ev.alt_aliases = alt_aliases.into_iter().collect(); | |
| 328 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 329 | }, | |
| 330 | RoomField::Aliases => { | |
| 331 | // This never happens, aliases is only used for showing | |
| 332 | }, | |
| 333 | RoomField::Id => { | |
| 334 | // This never happens, id is only used for showing | |
| 335 | }, | |
| 336 | } | |
| 337 | ||
| 338 | Ok(vec![]) | |
| 339 | }, | |
| 340 | RoomAction::Unset(field) => { | |
| 341 | let room = store | |
| 342 | .application | |
| 343 | .get_joined_room(id) | |
| 344 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 345 | ||
| 346 | match field { | |
| 347 | RoomField::History => { | |
| 348 | let visibility = HistoryVisibility::Joined; | |
| 349 | let ev = RoomHistoryVisibilityEventContent::new(visibility); | |
| 350 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 351 | }, | |
| 352 | RoomField::Name => { | |
| 353 | let ev = RoomNameEventContent::new("".into()); | |
| 354 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 355 | }, | |
| 356 | RoomField::Tag(tag) => { | |
| 357 | let _ = room.remove_tag(tag).await.map_err(IambError::from)?; | |
| 358 | }, | |
| 359 | RoomField::Topic => { | |
| 360 | let ev = RoomTopicEventContent::new("".into()); | |
| 361 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 362 | }, | |
| 363 | RoomField::NotificationMode => { | |
| 364 | let client = &store.application.worker.client; | |
| 365 | let notifications = client.notification_settings().await; | |
| 366 | ||
| 367 | notifications | |
| 368 | .delete_user_defined_room_rules(id) | |
| 369 | .await | |
| 370 | .map_err(IambError::from)?; | |
| 371 | }, | |
| 372 | RoomField::CanonicalAlias => { | |
| 373 | let Some(alias_to_destroy) = room.canonical_alias() else { | |
| 374 | let msg = "This room has no canonical alias to unset"; | |
| 375 | ||
| 376 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 377 | }; | |
| 378 | ||
| 379 | // Remove the canonical alias from the state event. | |
| 380 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 381 | ev.alias = None; | |
| 382 | ev.alt_aliases = room.alt_aliases(); | |
| 383 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 384 | ||
| 385 | // And then unmap it on the server. | |
| 386 | store | |
| 387 | .application | |
| 388 | .worker | |
| 389 | .client | |
| 390 | .remove_room_alias(&alias_to_destroy) | |
| 391 | .await | |
| 392 | .map_err(IambError::from)?; | |
| 393 | }, | |
| 394 | RoomField::Alias(alias) => { | |
| 395 | let Ok(orai) = OwnedRoomAliasId::try_from(alias.as_str()) else { | |
| 396 | let err = IambError::InvalidRoomAlias(alias); | |
| 397 | ||
| 398 | return Err(err.into()); | |
| 399 | }; | |
| 400 | ||
| 401 | let alt_aliases = room.alt_aliases(); | |
| 402 | let canonical = room.canonical_alias(); | |
| 403 | ||
| 404 | if !alt_aliases.contains(&orai) && canonical.as_ref() != Some(&orai) { | |
| 405 | let msg = format!("The alias {orai:?} isn't mapped to this room"); | |
| 406 | ||
| 407 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 408 | } | |
| 409 | ||
| 410 | // Remove the alias from the state event if it's in it. | |
| 411 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 412 | ev.alias = canonical.filter(|canon| canon != &orai); | |
| 413 | ev.alt_aliases = alt_aliases; | |
| 414 | ev.alt_aliases.retain(|in_orai| in_orai != &orai); | |
| 415 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 416 | ||
| 417 | // And then unmap it on the server. | |
| 418 | store | |
| 419 | .application | |
| 420 | .worker | |
| 421 | .client | |
| 422 | .remove_room_alias(&orai) | |
| 423 | .await | |
| 424 | .map_err(IambError::from)?; | |
| 425 | }, | |
| 426 | RoomField::Aliases => { | |
| 427 | // This will not happen, you cannot unset all aliases | |
| 428 | }, | |
| 429 | RoomField::Id => { | |
| 430 | // This never happens, id is only used for showing | |
| 431 | }, | |
| 432 | } | |
| 433 | ||
| 434 | Ok(vec![]) | |
| 435 | }, | |
| 436 | RoomAction::Show(field) => { | |
| 437 | let room = store | |
| 438 | .application | |
| 439 | .get_joined_room(id) | |
| 440 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 441 | ||
| 442 | let msg = match field { | |
| 443 | RoomField::History => { | |
| 444 | let visibility = room.history_visibility(); | |
| 445 | let visibility = visibility.as_ref().map(|v| v.as_str()); | |
| 446 | format!("Room history visibility: {}", visibility.unwrap_or("<unknown>")) | |
| 447 | }, | |
| 448 | RoomField::Id => { | |
| 449 | let id = room.room_id(); | |
| 450 | format!("Room identifier: {id}") | |
| 451 | }, | |
| 452 | RoomField::Name => { | |
| 453 | match room.name() { | |
| 454 | None => "Room has no name".into(), | |
| 455 | Some(name) => format!("Room name: {name:?}"), | |
| 456 | } | |
| 457 | }, | |
| 458 | RoomField::Topic => { | |
| 459 | match room.topic() { | |
| 460 | None => "Room has no topic".into(), | |
| 461 | Some(topic) => format!("Room topic: {topic:?}"), | |
| 462 | } | |
| 463 | }, | |
| 464 | RoomField::NotificationMode => { | |
| 465 | let client = &store.application.worker.client; | |
| 466 | let notifications = client.notification_settings().await; | |
| 467 | let mode = notifications.get_user_defined_room_notification_mode(id).await; | |
| 468 | ||
| 469 | let level = match mode { | |
| 470 | Some(RoomNotificationMode::Mute) => "mute", | |
| 471 | Some(RoomNotificationMode::MentionsAndKeywordsOnly) => "keywords", | |
| 472 | Some(RoomNotificationMode::AllMessages) => "all", | |
| 473 | None => "default", | |
| 474 | }; | |
| 475 | ||
| 476 | format!("Room notification level: {level:?}") | |
| 477 | }, | |
| 478 | RoomField::Aliases => { | |
| 479 | let aliases = room | |
| 480 | .alt_aliases() | |
| 481 | .iter() | |
| 482 | .map(OwnedRoomAliasId::to_string) | |
| 483 | .collect::<Vec<String>>(); | |
| 484 | ||
| 485 | if aliases.is_empty() { | |
| 486 | "No alternative aliases in room".into() | |
| 487 | } else { | |
| 488 | format!("Alternative aliases: {}.", aliases.join(", ")) | |
| 489 | } | |
| 490 | }, | |
| 491 | RoomField::CanonicalAlias => { | |
| 492 | match room.canonical_alias() { | |
| 493 | None => "No canonical alias for room".into(), | |
| 494 | Some(can) => format!("Canonical alias: {can}"), | |
| 495 | } | |
| 496 | }, | |
| 497 | RoomField::Tag(_) => "Cannot currently show value for a tag".into(), | |
| 498 | RoomField::Alias(_) => { | |
| 499 | "Cannot show a single alias; use `:room aliases show` instead.".into() | |
| 500 | }, | |
| 501 | }; | |
| 502 | ||
| 503 | let msg = InfoMessage::Pager(msg); | |
| 504 | let act = Action::ShowInfoMessage(msg); | |
| 505 | ||
| 506 | Ok(vec![(act, ctx)]) | |
| 507 | }, | |
| 508 | } | |
| 509 | } | |
| 510 | ||
| 112 | 511 | /// State for a Matrix room or space. |
| 113 | 512 | /// |
| 114 | 513 | /// Since spaces function as special rooms within Matrix, we wrap their window state together, so |
| @@ -234,411 +633,6 @@ impl RoomState { | ||
| 234 | 633 | } |
| 235 | 634 | } |
| 236 | 635 | |
| 237 | pub async fn room_command( | |
| 238 | &mut self, | |
| 239 | act: RoomAction, | |
| 240 | ctx: ProgramContext, | |
| 241 | store: &mut ProgramStore, | |
| 242 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | |
| 243 | match act { | |
| 244 | RoomAction::InviteAccept => { | |
| 245 | if let Some(room) = store.application.worker.client.get_room(self.id()) { | |
| 246 | room.join().await.map_err(IambError::from)?; | |
| 247 | Ok(vec![]) | |
| 248 | } else { | |
| 249 | Err(IambError::NotInvited.into()) | |
| 250 | } | |
| 251 | }, | |
| 252 | RoomAction::InviteReject => { | |
| 253 | if let Some(room) = store.application.worker.client.get_room(self.id()) { | |
| 254 | room.leave().await.map_err(IambError::from)?; | |
| 255 | ||
| 256 | Ok(vec![]) | |
| 257 | } else { | |
| 258 | Err(IambError::NotInvited.into()) | |
| 259 | } | |
| 260 | }, | |
| 261 | RoomAction::InviteSend(user) => { | |
| 262 | if let Some(room) = store.application.worker.client.get_room(self.id()) { | |
| 263 | room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?; | |
| 264 | ||
| 265 | Ok(vec![]) | |
| 266 | } else { | |
| 267 | Err(IambError::NotJoined.into()) | |
| 268 | } | |
| 269 | }, | |
| 270 | RoomAction::Leave(skip_confirm) => { | |
| 271 | if let Some(room) = store.application.worker.client.get_room(self.id()) { | |
| 272 | if skip_confirm { | |
| 273 | room.leave().await.map_err(IambError::from)?; | |
| 274 | ||
| 275 | Ok(vec![]) | |
| 276 | } else { | |
| 277 | let msg = "Do you really want to leave this room?"; | |
| 278 | let leave = IambAction::Room(RoomAction::Leave(true)); | |
| 279 | let prompt = PromptYesNo::new(msg, vec![Action::from(leave)]); | |
| 280 | let prompt = Box::new(prompt); | |
| 281 | ||
| 282 | Err(UIError::NeedConfirm(prompt)) | |
| 283 | } | |
| 284 | } else { | |
| 285 | Err(IambError::NotJoined.into()) | |
| 286 | } | |
| 287 | }, | |
| 288 | RoomAction::MemberUpdate(mua, user, reason, skip_confirm) => { | |
| 289 | let Some(room) = store.application.worker.client.get_room(self.id()) else { | |
| 290 | return Err(IambError::NotJoined.into()); | |
| 291 | }; | |
| 292 | ||
| 293 | let Ok(user_id) = OwnedUserId::try_from(user.as_str()) else { | |
| 294 | let err = IambError::InvalidUserId(user); | |
| 295 | ||
| 296 | return Err(err.into()); | |
| 297 | }; | |
| 298 | ||
| 299 | if !skip_confirm { | |
| 300 | let msg = format!("Do you really want to {mua} {user} from this room?"); | |
| 301 | let act = RoomAction::MemberUpdate(mua, user, reason, true); | |
| 302 | let act = IambAction::from(act); | |
| 303 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 304 | let prompt = Box::new(prompt); | |
| 305 | ||
| 306 | return Err(UIError::NeedConfirm(prompt)); | |
| 307 | } | |
| 308 | ||
| 309 | match mua { | |
| 310 | MemberUpdateAction::Ban => { | |
| 311 | room.ban_user(&user_id, reason.as_deref()) | |
| 312 | .await | |
| 313 | .map_err(IambError::from)?; | |
| 314 | }, | |
| 315 | MemberUpdateAction::Unban => { | |
| 316 | room.unban_user(&user_id, reason.as_deref()) | |
| 317 | .await | |
| 318 | .map_err(IambError::from)?; | |
| 319 | }, | |
| 320 | MemberUpdateAction::Kick => { | |
| 321 | room.kick_user(&user_id, reason.as_deref()) | |
| 322 | .await | |
| 323 | .map_err(IambError::from)?; | |
| 324 | }, | |
| 325 | } | |
| 326 | ||
| 327 | Ok(vec![]) | |
| 328 | }, | |
| 329 | RoomAction::Members(mut cmd) => { | |
| 330 | let width = Count::Exact(30); | |
| 331 | let act = | |
| 332 | cmd.default_axis(Axis::Vertical).default_relation(MoveDir1D::Next).window( | |
| 333 | OpenTarget::Application(IambId::MemberList(self.id().to_owned())), | |
| 334 | width.into(), | |
| 335 | ); | |
| 336 | ||
| 337 | Ok(vec![(act, cmd.context.clone())]) | |
| 338 | }, | |
| 339 | RoomAction::SetDirect(is_direct) => { | |
| 340 | let room = store | |
| 341 | .application | |
| 342 | .get_joined_room(self.id()) | |
| 343 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 344 | ||
| 345 | room.set_is_direct(is_direct).await.map_err(IambError::from)?; | |
| 346 | ||
| 347 | Ok(vec![]) | |
| 348 | }, | |
| 349 | RoomAction::Set(field, value) => { | |
| 350 | let room = store | |
| 351 | .application | |
| 352 | .get_joined_room(self.id()) | |
| 353 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 354 | ||
| 355 | match field { | |
| 356 | RoomField::History => { | |
| 357 | let visibility = hist_visibility_mode(value)?; | |
| 358 | let ev = RoomHistoryVisibilityEventContent::new(visibility); | |
| 359 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 360 | }, | |
| 361 | RoomField::Name => { | |
| 362 | let ev = RoomNameEventContent::new(value); | |
| 363 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 364 | }, | |
| 365 | RoomField::Tag(tag) => { | |
| 366 | let mut info = TagInfo::new(); | |
| 367 | info.order = Some(1.0); | |
| 368 | ||
| 369 | let _ = room.set_tag(tag, info).await.map_err(IambError::from)?; | |
| 370 | }, | |
| 371 | RoomField::Topic => { | |
| 372 | let ev = RoomTopicEventContent::new(value); | |
| 373 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 374 | }, | |
| 375 | RoomField::NotificationMode => { | |
| 376 | let mode = notification_mode(value)?; | |
| 377 | let client = &store.application.worker.client; | |
| 378 | let notifications = client.notification_settings().await; | |
| 379 | ||
| 380 | notifications | |
| 381 | .set_room_notification_mode(self.id(), mode) | |
| 382 | .await | |
| 383 | .map_err(IambError::from)?; | |
| 384 | }, | |
| 385 | RoomField::CanonicalAlias => { | |
| 386 | let client = &mut store.application.worker.client; | |
| 387 | ||
| 388 | let Ok(orai) = OwnedRoomAliasId::try_from(value.as_str()) else { | |
| 389 | let err = IambError::InvalidRoomAlias(value); | |
| 390 | ||
| 391 | return Err(err.into()); | |
| 392 | }; | |
| 393 | ||
| 394 | let mut alt_aliases = | |
| 395 | room.alt_aliases().into_iter().collect::<HashSet<_>>(); | |
| 396 | let canonical_old = room.canonical_alias(); | |
| 397 | ||
| 398 | // If the room's alias is already that, ignore it | |
| 399 | if canonical_old.as_ref() == Some(&orai) { | |
| 400 | let msg = format!("The canonical room alias is already {orai}"); | |
| 401 | ||
| 402 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 403 | } | |
| 404 | ||
| 405 | // Try creating the room alias on the server. | |
| 406 | if let Err(e) = client.create_room_alias(&orai, room.room_id()).await { | |
| 407 | if let Some(ClientApiErrorKind::Unknown) = e.client_api_error_kind() { | |
| 408 | // Ignore when it already exists. | |
| 409 | } else { | |
| 410 | return Err(IambError::from(e).into()); | |
| 411 | } | |
| 412 | } | |
| 413 | ||
| 414 | // Demote the previous one to an alt alias. | |
| 415 | alt_aliases.extend(canonical_old); | |
| 416 | ||
| 417 | // At this point the room alias definitely exists, and we can update the | |
| 418 | // state event. | |
| 419 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 420 | ev.alias = Some(orai); | |
| 421 | ev.alt_aliases = alt_aliases.into_iter().collect(); | |
| 422 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 423 | }, | |
| 424 | RoomField::Alias(alias) => { | |
| 425 | let client = &mut store.application.worker.client; | |
| 426 | ||
| 427 | let Ok(orai) = OwnedRoomAliasId::try_from(alias.as_str()) else { | |
| 428 | let err = IambError::InvalidRoomAlias(alias); | |
| 429 | ||
| 430 | return Err(err.into()); | |
| 431 | }; | |
| 432 | ||
| 433 | let mut alt_aliases = | |
| 434 | room.alt_aliases().into_iter().collect::<HashSet<_>>(); | |
| 435 | let canonical = room.canonical_alias(); | |
| 436 | ||
| 437 | if alt_aliases.contains(&orai) || canonical.as_ref() == Some(&orai) { | |
| 438 | let msg = format!("The alias {orai} already maps to this room"); | |
| 439 | ||
| 440 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 441 | } else { | |
| 442 | alt_aliases.insert(orai.clone()); | |
| 443 | } | |
| 444 | ||
| 445 | // If the room alias does not exist on the server, create it | |
| 446 | if let Err(e) = client.create_room_alias(&orai, room.room_id()).await { | |
| 447 | if let Some(ClientApiErrorKind::Unknown) = e.client_api_error_kind() { | |
| 448 | // Ignore when it already exists. | |
| 449 | } else { | |
| 450 | return Err(IambError::from(e).into()); | |
| 451 | } | |
| 452 | } | |
| 453 | ||
| 454 | // And add it to the aliases in the state event. | |
| 455 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 456 | ev.alias = canonical; | |
| 457 | ev.alt_aliases = alt_aliases.into_iter().collect(); | |
| 458 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 459 | }, | |
| 460 | RoomField::Aliases => { | |
| 461 | // This never happens, aliases is only used for showing | |
| 462 | }, | |
| 463 | RoomField::Id => { | |
| 464 | // This never happens, id is only used for showing | |
| 465 | }, | |
| 466 | } | |
| 467 | ||
| 468 | Ok(vec![]) | |
| 469 | }, | |
| 470 | RoomAction::Unset(field) => { | |
| 471 | let room = store | |
| 472 | .application | |
| 473 | .get_joined_room(self.id()) | |
| 474 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 475 | ||
| 476 | match field { | |
| 477 | RoomField::History => { | |
| 478 | let visibility = HistoryVisibility::Joined; | |
| 479 | let ev = RoomHistoryVisibilityEventContent::new(visibility); | |
| 480 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 481 | }, | |
| 482 | RoomField::Name => { | |
| 483 | let ev = RoomNameEventContent::new("".into()); | |
| 484 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 485 | }, | |
| 486 | RoomField::Tag(tag) => { | |
| 487 | let _ = room.remove_tag(tag).await.map_err(IambError::from)?; | |
| 488 | }, | |
| 489 | RoomField::Topic => { | |
| 490 | let ev = RoomTopicEventContent::new("".into()); | |
| 491 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 492 | }, | |
| 493 | RoomField::NotificationMode => { | |
| 494 | let client = &store.application.worker.client; | |
| 495 | let notifications = client.notification_settings().await; | |
| 496 | ||
| 497 | notifications | |
| 498 | .delete_user_defined_room_rules(self.id()) | |
| 499 | .await | |
| 500 | .map_err(IambError::from)?; | |
| 501 | }, | |
| 502 | RoomField::CanonicalAlias => { | |
| 503 | let Some(alias_to_destroy) = room.canonical_alias() else { | |
| 504 | let msg = "This room has no canonical alias to unset"; | |
| 505 | ||
| 506 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 507 | }; | |
| 508 | ||
| 509 | // Remove the canonical alias from the state event. | |
| 510 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 511 | ev.alias = None; | |
| 512 | ev.alt_aliases = room.alt_aliases(); | |
| 513 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 514 | ||
| 515 | // And then unmap it on the server. | |
| 516 | store | |
| 517 | .application | |
| 518 | .worker | |
| 519 | .client | |
| 520 | .remove_room_alias(&alias_to_destroy) | |
| 521 | .await | |
| 522 | .map_err(IambError::from)?; | |
| 523 | }, | |
| 524 | RoomField::Alias(alias) => { | |
| 525 | let Ok(orai) = OwnedRoomAliasId::try_from(alias.as_str()) else { | |
| 526 | let err = IambError::InvalidRoomAlias(alias); | |
| 527 | ||
| 528 | return Err(err.into()); | |
| 529 | }; | |
| 530 | ||
| 531 | let alt_aliases = room.alt_aliases(); | |
| 532 | let canonical = room.canonical_alias(); | |
| 533 | ||
| 534 | if !alt_aliases.contains(&orai) && canonical.as_ref() != Some(&orai) { | |
| 535 | let msg = format!("The alias {orai:?} isn't mapped to this room"); | |
| 536 | ||
| 537 | return Ok(vec![(Action::ShowInfoMessage(msg.into()), ctx)]); | |
| 538 | } | |
| 539 | ||
| 540 | // Remove the alias from the state event if it's in it. | |
| 541 | let mut ev = RoomCanonicalAliasEventContent::new(); | |
| 542 | ev.alias = canonical.filter(|canon| canon != &orai); | |
| 543 | ev.alt_aliases = alt_aliases; | |
| 544 | ev.alt_aliases.retain(|in_orai| in_orai != &orai); | |
| 545 | let _ = room.send_state_event(ev).await.map_err(IambError::from)?; | |
| 546 | ||
| 547 | // And then unmap it on the server. | |
| 548 | store | |
| 549 | .application | |
| 550 | .worker | |
| 551 | .client | |
| 552 | .remove_room_alias(&orai) | |
| 553 | .await | |
| 554 | .map_err(IambError::from)?; | |
| 555 | }, | |
| 556 | RoomField::Aliases => { | |
| 557 | // This will not happen, you cannot unset all aliases | |
| 558 | }, | |
| 559 | RoomField::Id => { | |
| 560 | // This never happens, id is only used for showing | |
| 561 | }, | |
| 562 | } | |
| 563 | ||
| 564 | Ok(vec![]) | |
| 565 | }, | |
| 566 | RoomAction::Show(field) => { | |
| 567 | let room = store | |
| 568 | .application | |
| 569 | .get_joined_room(self.id()) | |
| 570 | .ok_or(UIError::Application(IambError::NotJoined))?; | |
| 571 | ||
| 572 | let msg = match field { | |
| 573 | RoomField::History => { | |
| 574 | let visibility = room.history_visibility(); | |
| 575 | let visibility = visibility.as_ref().map(|v| v.as_str()); | |
| 576 | format!("Room history visibility: {}", visibility.unwrap_or("<unknown>")) | |
| 577 | }, | |
| 578 | RoomField::Id => { | |
| 579 | let id = room.room_id(); | |
| 580 | format!("Room identifier: {id}") | |
| 581 | }, | |
| 582 | RoomField::Name => { | |
| 583 | match room.name() { | |
| 584 | None => "Room has no name".into(), | |
| 585 | Some(name) => format!("Room name: {name:?}"), | |
| 586 | } | |
| 587 | }, | |
| 588 | RoomField::Topic => { | |
| 589 | match room.topic() { | |
| 590 | None => "Room has no topic".into(), | |
| 591 | Some(topic) => format!("Room topic: {topic:?}"), | |
| 592 | } | |
| 593 | }, | |
| 594 | RoomField::NotificationMode => { | |
| 595 | let client = &store.application.worker.client; | |
| 596 | let notifications = client.notification_settings().await; | |
| 597 | let mode = | |
| 598 | notifications.get_user_defined_room_notification_mode(self.id()).await; | |
| 599 | ||
| 600 | let level = match mode { | |
| 601 | Some(RoomNotificationMode::Mute) => "mute", | |
| 602 | Some(RoomNotificationMode::MentionsAndKeywordsOnly) => "keywords", | |
| 603 | Some(RoomNotificationMode::AllMessages) => "all", | |
| 604 | None => "default", | |
| 605 | }; | |
| 606 | ||
| 607 | format!("Room notification level: {level:?}") | |
| 608 | }, | |
| 609 | RoomField::Aliases => { | |
| 610 | let aliases = room | |
| 611 | .alt_aliases() | |
| 612 | .iter() | |
| 613 | .map(OwnedRoomAliasId::to_string) | |
| 614 | .collect::<Vec<String>>(); | |
| 615 | ||
| 616 | if aliases.is_empty() { | |
| 617 | "No alternative aliases in room".into() | |
| 618 | } else { | |
| 619 | format!("Alternative aliases: {}.", aliases.join(", ")) | |
| 620 | } | |
| 621 | }, | |
| 622 | RoomField::CanonicalAlias => { | |
| 623 | match room.canonical_alias() { | |
| 624 | None => "No canonical alias for room".into(), | |
| 625 | Some(can) => format!("Canonical alias: {can}"), | |
| 626 | } | |
| 627 | }, | |
| 628 | RoomField::Tag(_) => "Cannot currently show value for a tag".into(), | |
| 629 | RoomField::Alias(_) => { | |
| 630 | "Cannot show a single alias; use `:room aliases show` instead.".into() | |
| 631 | }, | |
| 632 | }; | |
| 633 | ||
| 634 | let msg = InfoMessage::Pager(msg); | |
| 635 | let act = Action::ShowInfoMessage(msg); | |
| 636 | ||
| 637 | Ok(vec![(act, ctx)]) | |
| 638 | }, | |
| 639 | } | |
| 640 | } | |
| 641 | ||
| 642 | 636 | pub fn get_title(&self, store: &mut ProgramStore) -> Line<'_> { |
| 643 | 637 | let room = store.application.worker.client.get_room(self.id()); |
| 644 | 638 | |