Commit 1de8891b62
Unregistered key
Layout: unified · split
Cargo.lock +1
| @@ -2545,6 +2545,7 @@ dependencies = [ | |||
| 2545 | "nom 8.0.0", | 2545 | "nom 8.0.0", |
| 2546 | "notify-rust", | 2546 | "notify-rust", |
| 2547 | "open", | 2547 | "open", |
| 2548 | "percent-encoding", | ||
| 2548 | "pretty_assertions", | 2549 | "pretty_assertions", |
| 2549 | "ratatui", | 2550 | "ratatui", |
| 2550 | "ratatui-image", | 2551 | "ratatui-image", |
Cargo.toml +2 −1
| @@ -69,7 +69,8 @@ tracing-appender = "0.2.2" | |||
| 69 | tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } | 69 | tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } |
| 70 | unicode-segmentation = "1.7" | 70 | unicode-segmentation = "1.7" |
| 71 | unicode-width = "0.2" | 71 | unicode-width = "0.2" |
| 72 | url = {version = "2.2.2", features = ["serde"]} | 72 | url = {version = "2.5.8", features = ["serde"]} |
| 73 | percent-encoding = "2.3.2" | ||
| 73 | 74 | ||
| 74 | [dependencies.comrak] | 75 | [dependencies.comrak] |
| 75 | version = "0.55.0" | 76 | version = "0.55.0" |
docs/iamb.1 +4
| @@ -156,8 +156,12 @@ Accept an invitation to the currently focused room. | |||
| 156 | Reject an invitation to the currently focused room. | 156 | Reject an invitation to the currently focused room. |
| 157 | .It Sy ":invite send [user]" | 157 | .It Sy ":invite send [user]" |
| 158 | Send an invitation to a user to join the currently focused room. | 158 | Send an invitation to a user to join the currently focused room. |
| 159 | .It Sy ":join" | ||
| 160 | Join the focused preview room. | ||
| 159 | .It Sy ":join [room]" | 161 | .It Sy ":join [room]" |
| 160 | Join a room or open it if you are already joined. | 162 | Join a room or open it if you are already joined. |
| 163 | .It Sy ":knock" | ||
| 164 | Request to join the focused preview room. | ||
| 161 | .It Sy ":knock accept [user]" | 165 | .It Sy ":knock accept [user]" |
| 162 | Accept a knock from the specified user in the currently focused room. | 166 | Accept a knock from the specified user in the currently focused room. |
| 163 | .It Sy ":knock ban [user] [reason]" | 167 | .It Sy ":knock ban [user] [reason]" |
src/base.rs +94 −92
| @@ -44,11 +44,11 @@ use modalkit::env::vim::command::{CommandContext, VimCommand, VimCommandMachine} | |||
| 44 | use modalkit::env::vim::keybindings::VimMachine; | 44 | use modalkit::env::vim::keybindings::VimMachine; |
| 45 | use modalkit::errors::UIResult; | 45 | use modalkit::errors::UIResult; |
| 46 | use modalkit::keybindings::SequenceStatus; | 46 | use modalkit::keybindings::SequenceStatus; |
| 47 | use percent_encoding::{NON_ALPHANUMERIC, percent_decode, percent_encode}; | ||
| 47 | use serde::de::Error as SerdeError; | 48 | use serde::de::Error as SerdeError; |
| 48 | use serde::de::Visitor; | 49 | use serde::de::Visitor; |
| 49 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; | 50 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 50 | use tokio::sync::Mutex as AsyncMutex; | 51 | use tokio::sync::Mutex as AsyncMutex; |
| 51 | use url::form_urlencoded; | ||
| 52 | 52 | ||
| 53 | use crate::message::poll::{ | 53 | use crate::message::poll::{ |
| 54 | Poll, | 54 | Poll, |
| @@ -543,6 +543,16 @@ pub enum RoomAction { | |||
| 543 | SetUnread(bool), | 543 | SetUnread(bool), |
| 544 | } | 544 | } |
| 545 | 545 | ||
| 546 | /// An action that joins a room. | ||
| 547 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
| 548 | pub enum JoinAction { | ||
| 549 | /// Join the focused room. | ||
| 550 | Join, | ||
| 551 | |||
| 552 | /// Knock on the focused room. | ||
| 553 | Knock, | ||
| 554 | } | ||
| 555 | |||
| 546 | /// An action that sends a message to a room. | 556 | /// An action that sends a message to a room. |
| 547 | #[derive(Clone, Debug, Eq, PartialEq)] | 557 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 548 | pub enum SendAction { | 558 | pub enum SendAction { |
| @@ -618,12 +628,15 @@ pub enum IambAction { | |||
| 618 | /// Perform an action on the current space. | 628 | /// Perform an action on the current space. |
| 619 | Space(SpaceAction), | 629 | Space(SpaceAction), |
| 620 | 630 | ||
| 621 | /// Open a URL (and specify whether to join linked matrix rooms). | 631 | /// Open a URL. |
| 622 | OpenLink(String, bool), | 632 | OpenLink(String), |
| 623 | 633 | ||
| 624 | /// Perform an action on the currently focused room. | 634 | /// Perform an action on the currently focused room. |
| 625 | Room(RoomAction), | 635 | Room(RoomAction), |
| 626 | 636 | ||
| 637 | /// Join the focused room preview. | ||
| 638 | Join(JoinAction), | ||
| 639 | |||
| 627 | /// Send a message to the currently focused room. | 640 | /// Send a message to the currently focused room. |
| 628 | Send(SendAction), | 641 | Send(SendAction), |
| 629 | 642 | ||
| @@ -674,6 +687,12 @@ impl From<RoomAction> for IambAction { | |||
| 674 | } | 687 | } |
| 675 | } | 688 | } |
| 676 | 689 | ||
| 690 | impl From<JoinAction> for IambAction { | ||
| 691 | fn from(act: JoinAction) -> Self { | ||
| 692 | IambAction::Join(act) | ||
| 693 | } | ||
| 694 | } | ||
| 695 | |||
| 677 | impl From<SendAction> for IambAction { | 696 | impl From<SendAction> for IambAction { |
| 678 | fn from(act: SendAction) -> Self { | 697 | fn from(act: SendAction) -> Self { |
| 679 | IambAction::Send(act) | 698 | IambAction::Send(act) |
| @@ -702,6 +721,7 @@ impl ApplicationAction for IambAction { | |||
| 702 | IambAction::Verify(..) => SequenceStatus::Break, | 721 | IambAction::Verify(..) => SequenceStatus::Break, |
| 703 | IambAction::VerifyRequest(..) => SequenceStatus::Break, | 722 | IambAction::VerifyRequest(..) => SequenceStatus::Break, |
| 704 | IambAction::Recover(..) => SequenceStatus::Break, | 723 | IambAction::Recover(..) => SequenceStatus::Break, |
| 724 | IambAction::Join(..) => SequenceStatus::Break, | ||
| 705 | } | 725 | } |
| 706 | } | 726 | } |
| 707 | 727 | ||
| @@ -720,6 +740,7 @@ impl ApplicationAction for IambAction { | |||
| 720 | IambAction::Verify(..) => SequenceStatus::Atom, | 740 | IambAction::Verify(..) => SequenceStatus::Atom, |
| 721 | IambAction::VerifyRequest(..) => SequenceStatus::Atom, | 741 | IambAction::VerifyRequest(..) => SequenceStatus::Atom, |
| 722 | IambAction::Recover(..) => SequenceStatus::Atom, | 742 | IambAction::Recover(..) => SequenceStatus::Atom, |
| 743 | IambAction::Join(..) => SequenceStatus::Atom, | ||
| 723 | } | 744 | } |
| 724 | } | 745 | } |
| 725 | 746 | ||
| @@ -738,6 +759,7 @@ impl ApplicationAction for IambAction { | |||
| 738 | IambAction::Verify(..) => SequenceStatus::Ignore, | 759 | IambAction::Verify(..) => SequenceStatus::Ignore, |
| 739 | IambAction::VerifyRequest(..) => SequenceStatus::Ignore, | 760 | IambAction::VerifyRequest(..) => SequenceStatus::Ignore, |
| 740 | IambAction::Recover(..) => SequenceStatus::Ignore, | 761 | IambAction::Recover(..) => SequenceStatus::Ignore, |
| 762 | IambAction::Join(..) => SequenceStatus::Ignore, | ||
| 741 | } | 763 | } |
| 742 | } | 764 | } |
| 743 | 765 | ||
| @@ -756,6 +778,7 @@ impl ApplicationAction for IambAction { | |||
| 756 | IambAction::Verify(..) => false, | 778 | IambAction::Verify(..) => false, |
| 757 | IambAction::VerifyRequest(..) => false, | 779 | IambAction::VerifyRequest(..) => false, |
| 758 | IambAction::Recover(..) => false, | 780 | IambAction::Recover(..) => false, |
| 781 | IambAction::Join(..) => false, | ||
| 759 | } | 782 | } |
| 760 | } | 783 | } |
| 761 | } | 784 | } |
| @@ -2273,6 +2296,7 @@ pub struct Need { | |||
| 2273 | #[derive(Default, Debug)] | 2296 | #[derive(Default, Debug)] |
| 2274 | pub struct RoomNeeds { | 2297 | pub struct RoomNeeds { |
| 2275 | needs: HashMap<OwnedRoomId, Need>, | 2298 | needs: HashMap<OwnedRoomId, Need>, |
| 2299 | previews: HashSet<OwnedRoomOrAliasId>, | ||
| 2276 | } | 2300 | } |
| 2277 | 2301 | ||
| 2278 | impl RoomNeeds { | 2302 | impl RoomNeeds { |
| @@ -2308,6 +2332,16 @@ impl RoomNeeds { | |||
| 2308 | .extend(message_needs); | 2332 | .extend(message_needs); |
| 2309 | } | 2333 | } |
| 2310 | 2334 | ||
| 2335 | /// Request the load of a room preview. | ||
| 2336 | pub fn need_preview(&mut self, room: OwnedRoomOrAliasId) { | ||
| 2337 | self.previews.insert(room); | ||
| 2338 | } | ||
| 2339 | |||
| 2340 | /// Return all requested room previews | ||
| 2341 | pub fn preview_needs(&mut self) -> impl Iterator<Item = OwnedRoomOrAliasId> { | ||
| 2342 | std::mem::take(&mut self.previews).into_iter() | ||
| 2343 | } | ||
| 2344 | |||
| 2311 | pub fn rooms(&self) -> usize { | 2345 | pub fn rooms(&self) -> usize { |
| 2312 | self.needs.len() | 2346 | self.needs.len() |
| 2313 | } | 2347 | } |
| @@ -2333,8 +2367,17 @@ pub struct ChatStore { | |||
| 2333 | /// Map of joined rooms. | 2367 | /// Map of joined rooms. |
| 2334 | pub rooms: CompletionMap<OwnedRoomId, RoomInfo>, | 2368 | pub rooms: CompletionMap<OwnedRoomId, RoomInfo>, |
| 2335 | 2369 | ||
| 2336 | /// Map of room names. | 2370 | /// Map of loaded room previews with their fetch time |
| 2337 | pub names: CompletionMap<OwnedRoomAliasId, OwnedRoomId>, | 2371 | pub room_previews: |
| 2372 | HashMap<OwnedRoomOrAliasId, (Result<RoomPreview, matrix_sdk::Error>, Instant)>, | ||
| 2373 | |||
| 2374 | /// Cache of encountered `via` parameters in room links. | ||
| 2375 | /// | ||
| 2376 | /// This is stored here because this data is lost in the conversion to [IambId]. | ||
| 2377 | pub room_via: HashMap<OwnedRoomOrAliasId, Vec<OwnedServerName>>, | ||
| 2378 | |||
| 2379 | /// Map of room aliases. | ||
| 2380 | pub aliases: CompletionMap<OwnedRoomAliasId, OwnedRoomId>, | ||
| 2338 | 2381 | ||
| 2339 | /// Presence information for other users. | 2382 | /// Presence information for other users. |
| 2340 | pub presences: CompletionMap<OwnedUserId, PresenceState>, | 2383 | pub presences: CompletionMap<OwnedUserId, PresenceState>, |
| @@ -2391,8 +2434,10 @@ impl ChatStore { | |||
| 2391 | emojis: emoji_map(), | 2434 | emojis: emoji_map(), |
| 2392 | 2435 | ||
| 2393 | collator: Default::default(), | 2436 | collator: Default::default(), |
| 2394 | names: Default::default(), | 2437 | aliases: Default::default(), |
| 2395 | rooms: Default::default(), | 2438 | rooms: Default::default(), |
| 2439 | room_previews: Default::default(), | ||
| 2440 | room_via: Default::default(), | ||
| 2396 | presences: Default::default(), | 2441 | presences: Default::default(), |
| 2397 | verifications: Default::default(), | 2442 | verifications: Default::default(), |
| 2398 | need_load: Default::default(), | 2443 | need_load: Default::default(), |
| @@ -2438,7 +2483,17 @@ impl ChatStore { | |||
| 2438 | } | 2483 | } |
| 2439 | 2484 | ||
| 2440 | /// Set the name and tags for a room. | 2485 | /// Set the name and tags for a room. |
| 2441 | pub fn set_room_info(&mut self, room_id: OwnedRoomId, name: String, tags: Option<Tags>) { | 2486 | pub fn set_room_info( |
| 2487 | &mut self, | ||
| 2488 | room_id: OwnedRoomId, | ||
| 2489 | name: String, | ||
| 2490 | tags: Option<Tags>, | ||
| 2491 | aliases: Vec<OwnedRoomAliasId>, | ||
| 2492 | ) { | ||
| 2493 | for alias in aliases { | ||
| 2494 | self.aliases.insert(alias, room_id.clone()); | ||
| 2495 | } | ||
| 2496 | |||
| 2442 | let info = self.rooms.get_or_default(room_id); | 2497 | let info = self.rooms.get_or_default(room_id); |
| 2443 | info.name = name.into(); | 2498 | info.name = name.into(); |
| 2444 | info.tags = tags; | 2499 | info.tags = tags; |
| @@ -2451,13 +2506,7 @@ impl ApplicationStore for ChatStore {} | |||
| 2451 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] | 2506 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 2452 | pub enum IambId { | 2507 | pub enum IambId { |
| 2453 | /// A Matrix room, with an optional thread to show. | 2508 | /// A Matrix room, with an optional thread to show. |
| 2454 | Room(OwnedRoomId, Option<OwnedEventId>), | 2509 | Room(OwnedRoomOrAliasId, Option<OwnedEventId>), |
| 2455 | |||
| 2456 | /// A Matrix room that we're currently in the middle of joining. | ||
| 2457 | Joining(String), | ||
| 2458 | |||
| 2459 | /// A Matrix room that we haven't joined, and aren't currently joining. | ||
| 2460 | NotJoined(String), | ||
| 2461 | 2510 | ||
| 2462 | /// The `:dms` window. | 2511 | /// The `:dms` window. |
| 2463 | DirectList, | 2512 | DirectList, |
| @@ -2493,38 +2542,16 @@ pub enum IambId { | |||
| 2493 | InvitesList, | 2542 | InvitesList, |
| 2494 | } | 2543 | } |
| 2495 | 2544 | ||
| 2496 | /// Encode the room name for a [IambId::Joining] or [IambId::NotJoined] window URL. | ||
| 2497 | /// | ||
| 2498 | /// Note that since these names come straight from the user's argument to `:join`, | ||
| 2499 | /// they are likely room aliases containing characters like `#` that we should | ||
| 2500 | /// escape before putting them into the URL, so we can later reparse it. They can | ||
| 2501 | /// technically contain anything that the user tried to pass to `:join`. | ||
| 2502 | fn room_query(room: &str) -> String { | ||
| 2503 | form_urlencoded::Serializer::new(String::new()) | ||
| 2504 | .append_pair("room", room) | ||
| 2505 | .finish() | ||
| 2506 | } | ||
| 2507 | |||
| 2508 | /// Pull the room name back out of the query parameter for `iamb://joining` or | ||
| 2509 | /// `iamb://not-joined`. | ||
| 2510 | fn query_room(url: &Url) -> Option<String> { | ||
| 2511 | url.query_pairs().find_map(|(k, v)| (k == "room").then(|| v.into_owned())) | ||
| 2512 | } | ||
| 2513 | |||
| 2514 | impl Display for IambId { | 2545 | impl Display for IambId { |
| 2515 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 2546 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2516 | match self { | 2547 | match self { |
| 2517 | IambId::Room(room_id, None) => { | 2548 | IambId::Room(alias, None) => { |
| 2518 | write!(f, "iamb://room/{room_id}") | 2549 | let encoded = percent_encode(alias.as_bytes(), NON_ALPHANUMERIC); |
| 2519 | }, | 2550 | write!(f, "iamb://room/{}", encoded) |
| 2520 | IambId::Room(room_id, Some(thread)) => { | ||
| 2521 | write!(f, "iamb://room/{room_id}/threads/{thread}") | ||
| 2522 | }, | 2551 | }, |
| 2523 | IambId::Joining(room) => { | 2552 | IambId::Room(alias, Some(thread)) => { |
| 2524 | write!(f, "iamb://joining?{}", room_query(room)) | 2553 | let encoded = percent_encode(alias.as_bytes(), NON_ALPHANUMERIC); |
| 2525 | }, | 2554 | write!(f, "iamb://room/{}/threads/{thread}", encoded) |
| 2526 | IambId::NotJoined(room) => { | ||
| 2527 | write!(f, "iamb://not-joined?{}", room_query(room)) | ||
| 2528 | }, | 2555 | }, |
| 2529 | IambId::MemberList(room_id) => { | 2556 | IambId::MemberList(room_id) => { |
| 2530 | write!(f, "iamb://members/{room_id}") | 2557 | write!(f, "iamb://members/{room_id}") |
| @@ -2594,16 +2621,18 @@ impl Visitor<'_> for IambIdVisitor { | |||
| 2594 | }; | 2621 | }; |
| 2595 | 2622 | ||
| 2596 | match *path.collect::<Vec<_>>().as_slice() { | 2623 | match *path.collect::<Vec<_>>().as_slice() { |
| 2597 | [room_id] => { | 2624 | [alias] => { |
| 2598 | let Ok(room_id) = OwnedRoomId::try_from(room_id) else { | 2625 | let decoded = percent_decode(alias.as_bytes()).decode_utf8_lossy(); |
| 2599 | return Err(E::custom("Invalid room identifier")); | 2626 | let Ok(room_id) = OwnedRoomOrAliasId::try_from(decoded.as_ref()) else { |
| 2627 | return Err(E::custom(format!("Invalid room identifier: {decoded:?}"))); | ||
| 2600 | }; | 2628 | }; |
| 2601 | 2629 | ||
| 2602 | Ok(IambId::Room(room_id, None)) | 2630 | Ok(IambId::Room(room_id, None)) |
| 2603 | }, | 2631 | }, |
| 2604 | [room_id, "threads", thread_root] => { | 2632 | [alias, "threads", thread_root] => { |
| 2605 | let Ok(room_id) = OwnedRoomId::try_from(room_id) else { | 2633 | let decoded = percent_decode(alias.as_bytes()).decode_utf8_lossy(); |
| 2606 | return Err(E::custom("Invalid room identifier")); | 2634 | let Ok(room_id) = OwnedRoomOrAliasId::try_from(decoded.as_ref()) else { |
| 2635 | return Err(E::custom("Invalid room identifier: {decoded:?}")); | ||
| 2607 | }; | 2636 | }; |
| 2608 | 2637 | ||
| 2609 | let Ok(thread_root) = OwnedEventId::try_from(thread_root) else { | 2638 | let Ok(thread_root) = OwnedEventId::try_from(thread_root) else { |
| @@ -2622,20 +2651,6 @@ impl Visitor<'_> for IambIdVisitor { | |||
| 2622 | _ => return Err(E::custom("Invalid iamb window URL")), | 2651 | _ => return Err(E::custom("Invalid iamb window URL")), |
| 2623 | } | 2652 | } |
| 2624 | }, | 2653 | }, |
| 2625 | Some("joining") => { | ||
| 2626 | let Some(room) = query_room(&url) else { | ||
| 2627 | return Err(E::custom("iamb://joining requires a room parameter")); | ||
| 2628 | }; | ||
| 2629 | |||
| 2630 | Ok(IambId::Joining(room)) | ||
| 2631 | }, | ||
| 2632 | Some("not-joined") => { | ||
| 2633 | let Some(room) = query_room(&url) else { | ||
| 2634 | return Err(E::custom("iamb://not-joined requires a room parameter")); | ||
| 2635 | }; | ||
| 2636 | |||
| 2637 | Ok(IambId::NotJoined(room)) | ||
| 2638 | }, | ||
| 2639 | Some("members") => { | 2654 | Some("members") => { |
| 2640 | let Some(path) = url.path_segments() else { | 2655 | let Some(path) = url.path_segments() else { |
| 2641 | return Err(E::custom("Invalid members window URL")); | 2656 | return Err(E::custom("Invalid members window URL")); |
| @@ -2800,7 +2815,9 @@ impl IambBufferId { | |||
| 2800 | pub fn to_window(&self) -> Option<IambId> { | 2815 | pub fn to_window(&self) -> Option<IambId> { |
| 2801 | let id = match self { | 2816 | let id = match self { |
| 2802 | IambBufferId::Command(_) => return None, | 2817 | IambBufferId::Command(_) => return None, |
| 2803 | IambBufferId::Room(room, thread, _) => IambId::Room(room.clone(), thread.clone()), | 2818 | IambBufferId::Room(room, thread, _) => { |
| 2819 | IambId::Room(room.clone().into(), thread.clone()) | ||
| 2820 | }, | ||
| 2804 | IambBufferId::DirectList => IambId::DirectList, | 2821 | IambBufferId::DirectList => IambId::DirectList, |
| 2805 | IambBufferId::MemberList(room) => IambId::MemberList(room.clone()), | 2822 | IambBufferId::MemberList(room) => IambId::MemberList(room.clone()), |
| 2806 | IambBufferId::PinnedList(room) => IambId::PinnedList(room.clone()), | 2823 | IambBufferId::PinnedList(room) => IambId::PinnedList(room.clone()), |
| @@ -3131,35 +3148,6 @@ pub mod tests { | |||
| 3131 | ); | 3148 | ); |
| 3132 | } | 3149 | } |
| 3133 | 3150 | ||
| 3134 | #[test] | ||
| 3135 | fn test_unjoined_window_ids() { | ||
| 3136 | // Room names come from the user can contain characters that need escaping: | ||
| 3137 | for name in [ | ||
| 3138 | "#foo:example.com", | ||
| 3139 | "!abc123:example.com", | ||
| 3140 | "@user:example.com", | ||
| 3141 | "a b&c=d?e#f", | ||
| 3142 | ] { | ||
| 3143 | for id in [IambId::Joining(name.into()), IambId::NotJoined(name.into())] { | ||
| 3144 | let json = serde_json::to_string(&id).unwrap(); | ||
| 3145 | assert_eq!(serde_json::from_str::<IambId>(&json).unwrap(), id); | ||
| 3146 | } | ||
| 3147 | } | ||
| 3148 | |||
| 3149 | assert_eq!( | ||
| 3150 | IambId::Joining("#foo:example.com".into()).to_string(), | ||
| 3151 | "iamb://joining?room=%23foo%3Aexample.com" | ||
| 3152 | ); | ||
| 3153 | assert_eq!( | ||
| 3154 | IambId::NotJoined("#foo:example.com".into()).to_string(), | ||
| 3155 | "iamb://not-joined?room=%23foo%3Aexample.com" | ||
| 3156 | ); | ||
| 3157 | |||
| 3158 | // A window URL without a room name isn't valid and wasn't written by us: | ||
| 3159 | assert!(serde_json::from_str::<IambId>("\"iamb://joining\"").is_err()); | ||
| 3160 | assert!(serde_json::from_str::<IambId>("\"iamb://not-joined\"").is_err()); | ||
| 3161 | } | ||
| 3162 | |||
| 3163 | #[test] | 3151 | #[test] |
| 3164 | fn test_need_load() { | 3152 | fn test_need_load() { |
| 3165 | let room_id = TEST_ROOM1_ID.clone(); | 3153 | let room_id = TEST_ROOM1_ID.clone(); |
| @@ -3217,6 +3205,20 @@ pub mod tests { | |||
| 3217 | assert!(info.get_message_location(&unloaded).is_none()); | 3205 | assert!(info.get_message_location(&unloaded).is_none()); |
| 3218 | } | 3206 | } |
| 3219 | 3207 | ||
| 3208 | #[test] | ||
| 3209 | fn test_alias_window_id() { | ||
| 3210 | let room_id = TEST_ROOM1_ALIAS.clone(); | ||
| 3211 | let id = IambId::Room(room_id.into(), None); | ||
| 3212 | |||
| 3213 | // Hash gets replaced during encoding: | ||
| 3214 | let exp = "iamb://room/%23room1%3Aexample%2Ecom"; | ||
| 3215 | assert_eq!(id.to_string(), exp); | ||
| 3216 | |||
| 3217 | // Percent encoding turns back into hash during decoding: | ||
| 3218 | let parsed: IambId = serde_json::from_str(&format!("{exp:?}")).unwrap(); | ||
| 3219 | assert_eq!(parsed, id); | ||
| 3220 | } | ||
| 3221 | |||
| 3220 | #[test] | 3222 | #[test] |
| 3221 | fn test_pinned_window_id() { | 3223 | fn test_pinned_window_id() { |
| 3222 | let room_id = TEST_ROOM1_ID.clone(); | 3224 | let room_id = TEST_ROOM1_ID.clone(); |
src/commands.rs +16 −3
| @@ -122,6 +122,12 @@ fn iamb_keys(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | |||
| 122 | fn iamb_knock(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | 122 | fn iamb_knock(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { |
| 123 | let mut args = desc.arg.strings()?; | 123 | let mut args = desc.arg.strings()?; |
| 124 | 124 | ||
| 125 | if args.is_empty() { | ||
| 126 | let act = IambAction::from(JoinAction::Knock); | ||
| 127 | let step = CommandStep::Continue(act.into(), ctx.context.clone()); | ||
| 128 | return Ok(step); | ||
| 129 | } | ||
| 130 | |||
| 125 | if args.len() < 2 || args.len() > 3 { | 131 | if args.len() < 2 || args.len() > 3 { |
| 126 | return Err(CommandError::InvalidArgument); | 132 | return Err(CommandError::InvalidArgument); |
| 127 | } | 133 | } |
| @@ -601,10 +607,16 @@ fn iamb_welcome(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | |||
| 601 | fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | 607 | fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { |
| 602 | let mut args = desc.arg.filenames()?; | 608 | let mut args = desc.arg.filenames()?; |
| 603 | 609 | ||
| 604 | if args.len() != 1 { | 610 | if args.len() > 1 { |
| 605 | return Result::Err(CommandError::InvalidArgument); | 611 | return Result::Err(CommandError::InvalidArgument); |
| 606 | } | 612 | } |
| 607 | 613 | ||
| 614 | if args.is_empty() { | ||
| 615 | let act = IambAction::from(JoinAction::Join); | ||
| 616 | let step = CommandStep::Continue(act.into(), ctx.context.clone()); | ||
| 617 | return Ok(step); | ||
| 618 | } | ||
| 619 | |||
| 608 | let open = ctx.switch(args.remove(0)); | 620 | let open = ctx.switch(args.remove(0)); |
| 609 | let step = CommandStep::Continue(open, ctx.context.clone()); | 621 | let step = CommandStep::Continue(open, ctx.context.clone()); |
| 610 | 622 | ||
| @@ -1331,8 +1343,9 @@ mod tests { | |||
| 1331 | let act = WindowAction::Switch(OpenTarget::Alternate); | 1343 | let act = WindowAction::Switch(OpenTarget::Alternate); |
| 1332 | assert_eq!(res, vec![(act.into(), ctx.clone())]); | 1344 | assert_eq!(res, vec![(act.into(), ctx.clone())]); |
| 1333 | 1345 | ||
| 1334 | let res = cmds.input_cmd("join", ctx.clone()); | 1346 | let res = cmds.input_cmd("join", ctx.clone()).unwrap(); |
| 1335 | assert_eq!(res, Err(CommandError::InvalidArgument)); | 1347 | let act = IambAction::Join(JoinAction::Join); |
| 1348 | assert_eq!(res, vec![(act.into(), ctx.clone())]); | ||
| 1336 | 1349 | ||
| 1337 | let res = cmds.input_cmd("join foo bar", ctx.clone()); | 1350 | let res = cmds.input_cmd("join foo bar", ctx.clone()); |
| 1338 | assert_eq!(res, Err(CommandError::InvalidArgument)); | 1351 | assert_eq!(res, Err(CommandError::InvalidArgument)); |
src/completions.rs +3 −3
| @@ -261,7 +261,7 @@ fn complete_users(input: &str, store: &ChatStore) -> Vec<String> { | |||
| 261 | 261 | ||
| 262 | /// Tab completion for Matrix identifiers (usernames, room aliases, etc.) | 262 | /// Tab completion for Matrix identifiers (usernames, room aliases, etc.) |
| 263 | fn complete_matrix_names(input: &str, store: &ChatStore) -> Vec<String> { | 263 | fn complete_matrix_names(input: &str, store: &ChatStore) -> Vec<String> { |
| 264 | let list = store.names.complete(input); | 264 | let list = store.aliases.complete(input); |
| 265 | if !list.is_empty() { | 265 | if !list.is_empty() { |
| 266 | return list.into_iter().map(|i| i.to_string()).collect(); | 266 | return list.into_iter().map(|i| i.to_string()).collect(); |
| 267 | } | 267 | } |
| @@ -276,7 +276,7 @@ fn complete_matrix_names(input: &str, store: &ChatStore) -> Vec<String> { | |||
| 276 | 276 | ||
| 277 | /// Tab completion for known room aliases and ids. | 277 | /// Tab completion for known room aliases and ids. |
| 278 | fn complete_room_alias_or_id(input: &str, store: &ChatStore) -> Vec<String> { | 278 | fn complete_room_alias_or_id(input: &str, store: &ChatStore) -> Vec<String> { |
| 279 | let list = store.names.complete(input); | 279 | let list = store.aliases.complete(input); |
| 280 | if !list.is_empty() { | 280 | if !list.is_empty() { |
| 281 | return list.into_iter().map(|i| i.to_string()).collect(); | 281 | return list.into_iter().map(|i| i.to_string()).collect(); |
| 282 | } | 282 | } |
| @@ -722,7 +722,7 @@ fn complete_msgbar( | |||
| 722 | } | 722 | } |
| 723 | 723 | ||
| 724 | store | 724 | store |
| 725 | .names | 725 | .aliases |
| 726 | .complete(id.as_ref()) | 726 | .complete(id.as_ref()) |
| 727 | .into_iter() | 727 | .into_iter() |
| 728 | .map(|i| format!("[{}]", i)) | 728 | .map(|i| format!("[{}]", i)) |
src/main.rs +44 −88
| @@ -24,8 +24,8 @@ use std::process; | |||
| 24 | use std::sync::atomic::AtomicUsize; | 24 | use std::sync::atomic::AtomicUsize; |
| 25 | 25 | ||
| 26 | use clap::{CommandFactory, Parser}; | 26 | use clap::{CommandFactory, Parser}; |
| 27 | use matrix_sdk::OwnedServerName; | ||
| 27 | use matrix_sdk::ruma::api::error::ErrorKind; | 28 | use matrix_sdk::ruma::api::error::ErrorKind; |
| 28 | use matrix_sdk::{OwnedServerName, RoomState}; | ||
| 29 | use modalkit::actions::{Commandable, TabAction, TabContainer, TabCount, WindowContainer}; | 29 | use modalkit::actions::{Commandable, TabAction, TabContainer, TabCount, WindowContainer}; |
| 30 | use modalkit::crossterm; | 30 | use modalkit::crossterm; |
| 31 | use modalkit::crossterm::cursor::SetCursorStyle; | 31 | use modalkit::crossterm::cursor::SetCursorStyle; |
| @@ -76,20 +76,17 @@ fn config_tab_to_desc( | |||
| 76 | ) -> IambResult<WindowLayoutDescription<IambInfo>> { | 76 | ) -> IambResult<WindowLayoutDescription<IambInfo>> { |
| 77 | let desc = match layout { | 77 | let desc = match layout { |
| 78 | config::WindowLayout::Window { window } => { | 78 | config::WindowLayout::Window { window } => { |
| 79 | let ChatStore { names, worker, settings, .. } = &mut store.application; | ||
| 80 | let via = settings.tunables.default_via.clone(); | ||
| 81 | |||
| 82 | let window = match window { | 79 | let window = match window { |
| 83 | config::WindowPath::UserId(user_id) => { | 80 | config::WindowPath::UserId(user_id) => { |
| 84 | let room_id = worker.join_room(user_id.to_string(), via)?; | 81 | if let Some(dm) = store.application.worker.client.get_dm_room(&user_id) { |
| 85 | IambId::Room(room_id, None) | 82 | IambId::Room(dm.room_id().to_owned().into(), None) |
| 86 | }, | 83 | } else { |
| 87 | config::WindowPath::RoomId(room_id) => IambId::Room(room_id, None), | 84 | let room_id = store.application.worker.create_dm(user_id)?; |
| 88 | config::WindowPath::AliasId(alias) => { | 85 | IambId::Room(room_id.into(), None) |
| 89 | let room_id = worker.join_room(alias.to_string(), via)?; | 86 | } |
| 90 | names.insert(alias, room_id.clone()); | ||
| 91 | IambId::Room(room_id, None) | ||
| 92 | }, | 87 | }, |
| 88 | config::WindowPath::RoomId(room_id) => IambId::Room(room_id.into(), None), | ||
| 89 | config::WindowPath::AliasId(alias_id) => IambId::Room(alias_id.into(), None), | ||
| 93 | config::WindowPath::Window(id) => id, | 90 | config::WindowPath::Window(id) => id, |
| 94 | }; | 91 | }; |
| 95 | 92 | ||
| @@ -119,66 +116,40 @@ fn restore_layout( | |||
| 119 | tabs.to_layout(area.into(), store) | 116 | tabs.to_layout(area.into(), store) |
| 120 | } | 117 | } |
| 121 | 118 | ||
| 122 | /// Returns the `IambId` for the new window or a string to query the user. If they answer `y` this | ||
| 123 | /// function should be rerun with `join_or_create` set to `true`. | ||
| 124 | fn resolve_mxid( | 119 | fn resolve_mxid( |
| 125 | store: &mut ProgramStore, | 120 | store: &mut ProgramStore, |
| 126 | id: MatrixId, | 121 | id: MatrixId, |
| 127 | via: &[OwnedServerName], | 122 | via: Vec<OwnedServerName>, |
| 128 | join_or_create: bool, | 123 | ) -> IambResult<IambId> { |
| 129 | ) -> IambResult<Result<IambId, String>> { | 124 | let alias_id = match id { |
| 130 | let room_name; | 125 | MatrixId::Room(room_id) => room_id.into(), |
| 131 | let room_id = match id { | 126 | MatrixId::RoomAlias(alias) => { |
| 132 | MatrixId::Room(id) => { | 127 | if let Some(room_id) = store.application.aliases.get(&alias) { |
| 133 | room_name = id.to_string(); | 128 | room_id.to_owned().into() |
| 134 | id | 129 | } else { |
| 135 | }, | 130 | alias.into() |
| 136 | MatrixId::RoomAlias(alias_id) => { | 131 | } |
| 137 | room_name = alias_id.to_string(); | ||
| 138 | store.application.worker.resolve_alias(alias_id)? | ||
| 139 | }, | 132 | }, |
| 140 | MatrixId::User(user_id) => { | 133 | MatrixId::User(user_id) => { |
| 141 | let id = match store.application.worker.client.get_dm_room(&user_id) { | 134 | if let Some(dm) = store.application.worker.client.get_dm_room(&user_id) { |
| 142 | Some(room) => room.room_id().to_owned(), | 135 | dm.room_id().to_owned().into() |
| 143 | None if join_or_create => { | ||
| 144 | store.application.worker.join_room(user_id.to_string(), via.to_owned())? | ||
| 145 | }, | ||
| 146 | None => return Ok(Err(format!("No dm with {user_id} found. Create new DM?"))), | ||
| 147 | }; | ||
| 148 | room_name = id.to_string(); | ||
| 149 | id | ||
| 150 | }, | ||
| 151 | MatrixId::Event(owned_room_or_alias_id, _event_id) => { | ||
| 152 | // ignore event id for now | ||
| 153 | room_name = owned_room_or_alias_id.to_string(); | ||
| 154 | let room_or_alias_id: &matrix_sdk::ruma::RoomOrAliasId = &owned_room_or_alias_id; | ||
| 155 | if let Ok(alias_id) = <&matrix_sdk::ruma::RoomAliasId>::try_from(room_or_alias_id) { | ||
| 156 | store.application.worker.resolve_alias(alias_id.to_owned())? | ||
| 157 | } else { | 136 | } else { |
| 158 | matrix_sdk::ruma::OwnedRoomId::try_from(owned_room_or_alias_id).unwrap() | 137 | store.application.worker.create_dm(user_id.to_owned())?.into() |
| 159 | } | 138 | } |
| 160 | }, | 139 | }, |
| 140 | MatrixId::Event(alias_id, _) => { | ||
| 141 | // ignore event id for now | ||
| 142 | alias_id | ||
| 143 | }, | ||
| 161 | _ => { | 144 | _ => { |
| 162 | tracing::error!("encountered unrecoginsed matrix id: {id:?}"); | 145 | tracing::error!("encountered unrecoginsed matrix id: {id:?}"); |
| 163 | return Ok(Err("Matrix link cannot be opened. Press 'n' to continue.".to_owned())); | 146 | return Err(UIError::Failure("Matrix link cannot be opened.".to_string())); |
| 164 | }, | 147 | }, |
| 165 | }; | 148 | }; |
| 166 | 149 | ||
| 167 | if store | 150 | store.application.room_via.insert(alias_id.to_owned(), via); |
| 168 | .application | ||
| 169 | .worker | ||
| 170 | .client | ||
| 171 | .get_room(&room_id) | ||
| 172 | .is_none_or(|room| room.state() != RoomState::Joined) | ||
| 173 | { | ||
| 174 | if join_or_create { | ||
| 175 | store.application.worker.join_room(room_name, via.to_owned())?; | ||
| 176 | } else { | ||
| 177 | return Ok(Err(format!("Join room {room_name:?}?"))); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | 151 | ||
| 181 | Ok(Ok(IambId::Room(room_id, None))) | 152 | Ok(IambId::Room(alias_id, None)) |
| 182 | } | 153 | } |
| 183 | 154 | ||
| 184 | fn setup_screen( | 155 | fn setup_screen( |
| @@ -191,24 +162,12 @@ fn setup_screen( | |||
| 191 | let area = Rect::new(0, 0, dims.0, dims.1); | 162 | let area = Rect::new(0, 0, dims.0, dims.1); |
| 192 | 163 | ||
| 193 | if let Some((id, via)) = initial_room { | 164 | if let Some((id, via)) = initial_room { |
| 194 | match resolve_mxid(store, id.clone(), &via, false)? { | 165 | match resolve_mxid(store, id, via) { |
| 195 | Ok(id) => { | 166 | Ok(id) => { |
| 196 | return Ok(ScreenState::new(IambWindow::open(id, store)?, cmd)); | 167 | return Ok(ScreenState::new(IambWindow::open(id, store)?, cmd)); |
| 197 | }, | 168 | }, |
| 198 | Err(question) => { | 169 | Err(err) => { |
| 199 | restore_tty(false, settings.tunables.mouse.enabled); | 170 | store.application.draw_error = Some(format!("layout restore: {err}")); |
| 200 | let join_or_create = loop { | ||
| 201 | match read_yesno(&format!("{question} [y]es/[n]o")) { | ||
| 202 | Some('y') => break true, | ||
| 203 | Some('n') => break false, | ||
| 204 | Some(_) | None => continue, | ||
| 205 | } | ||
| 206 | }; | ||
| 207 | setup_tty(&settings)?; | ||
| 208 | |||
| 209 | if join_or_create && let Ok(id) = resolve_mxid(store, id, &via, true)? { | ||
| 210 | return Ok(ScreenState::new(IambWindow::open(id, store)?, cmd)); | ||
| 211 | } | ||
| 212 | }, | 171 | }, |
| 213 | } | 172 | } |
| 214 | } | 173 | } |
| @@ -646,6 +605,12 @@ impl Application { | |||
| 646 | 605 | ||
| 647 | None | 606 | None |
| 648 | }, | 607 | }, |
| 608 | IambAction::Join(act) => { | ||
| 609 | let acts = self.screen.current_window_mut()?.join_command(act, ctx, store).await?; | ||
| 610 | self.action_prepend(acts); | ||
| 611 | |||
| 612 | None | ||
| 613 | }, | ||
| 649 | IambAction::Send(act) => { | 614 | IambAction::Send(act) => { |
| 650 | if store.application.settings.tunables.normal_after_send { | 615 | if store.application.settings.tunables.normal_after_send { |
| 651 | self.bindings.reset_mode(); | 616 | self.bindings.reset_mode(); |
| @@ -653,7 +618,7 @@ impl Application { | |||
| 653 | self.screen.current_window_mut()?.send_command(act, ctx, store).await? | 618 | self.screen.current_window_mut()?.send_command(act, ctx, store).await? |
| 654 | }, | 619 | }, |
| 655 | 620 | ||
| 656 | IambAction::OpenLink(url, join_or_create) => { | 621 | IambAction::OpenLink(url) => { |
| 657 | let matrix_uri = MatrixUri::parse(&url).ok(); | 622 | let matrix_uri = MatrixUri::parse(&url).ok(); |
| 658 | let matrix_to_uri = MatrixToUri::parse(&url).ok(); | 623 | let matrix_to_uri = MatrixToUri::parse(&url).ok(); |
| 659 | 624 | ||
| @@ -663,20 +628,15 @@ impl Application { | |||
| 663 | .or(matrix_to_uri.as_ref().map(|uri| (uri.id(), uri.via()))); | 628 | .or(matrix_to_uri.as_ref().map(|uri| (uri.id(), uri.via()))); |
| 664 | 629 | ||
| 665 | if let Some((id, via)) = matrix_id { | 630 | if let Some((id, via)) = matrix_id { |
| 666 | match resolve_mxid(store, id.clone(), via, join_or_create)? { | 631 | match resolve_mxid(store, id.to_owned(), via.to_vec()) { |
| 667 | Ok(room) => { | 632 | Ok(id) => { |
| 668 | let target = OpenTarget::Application(room); | 633 | let target = OpenTarget::Application(id); |
| 669 | let action = WindowAction::Switch(target); | 634 | let action = WindowAction::Switch(target); |
| 670 | 635 | ||
| 671 | self.action_prepend(vec![(action.into(), ctx)]); | 636 | self.action_prepend(vec![(action.into(), ctx)]); |
| 672 | None | 637 | None |
| 673 | }, | 638 | }, |
| 674 | Err(prompt) => { | 639 | Err(err) => return Err(UIError::Failure(err.to_string())), |
| 675 | let act = IambAction::OpenLink(url, true).into(); | ||
| 676 | let dialog = PromptYesNo::new(prompt, vec![act]); | ||
| 677 | let err = UIError::NeedConfirm(Box::new(dialog)); | ||
| 678 | return Err(err); | ||
| 679 | }, | ||
| 680 | } | 640 | } |
| 681 | } else { | 641 | } else { |
| 682 | tokio::task::spawn_blocking(move || open::that(url)); | 642 | tokio::task::spawn_blocking(move || open::that(url)); |
| @@ -713,7 +673,7 @@ impl Application { | |||
| 713 | HomeserverAction::CreateRoom(alias, vis, flags) => { | 673 | HomeserverAction::CreateRoom(alias, vis, flags) => { |
| 714 | let client = &store.application.worker.client; | 674 | let client = &store.application.worker.client; |
| 715 | let room_id = create_room(client, alias, vis, flags).await?; | 675 | let room_id = create_room(client, alias, vis, flags).await?; |
| 716 | let room = IambId::Room(room_id, None); | 676 | let room = IambId::Room(room_id.into(), None); |
| 717 | let target = OpenTarget::Application(room); | 677 | let target = OpenTarget::Application(room); |
| 718 | let action = WindowAction::Switch(target); | 678 | let action = WindowAction::Switch(target); |
| 719 | 679 | ||
| @@ -916,10 +876,6 @@ fn read_response(question: &str) -> String { | |||
| 916 | input | 876 | input |
| 917 | } | 877 | } |
| 918 | 878 | ||
| 919 | fn read_yesno(question: &str) -> Option<char> { | ||
| 920 | read_response(question).chars().next().map(|c| c.to_ascii_lowercase()) | ||
| 921 | } | ||
| 922 | |||
| 923 | async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult<()> { | 879 | async fn login(worker: &Requester, settings: &ApplicationSettings) -> IambResult<()> { |
| 924 | if settings.session_json.is_file() { | 880 | if settings.session_json.is_file() { |
| 925 | let session = settings.read_session(&settings.session_json)?; | 881 | let session = settings.read_session(&settings.session_json)?; |
src/prelude.rs +2
| @@ -9,6 +9,7 @@ pub use std::str::FromStr; | |||
| 9 | pub use std::sync::Arc; | 9 | pub use std::sync::Arc; |
| 10 | pub use std::time::{Duration, Instant}; | 10 | pub use std::time::{Duration, Instant}; |
| 11 | 11 | ||
| 12 | pub use matrix_sdk::room_preview::RoomPreview; | ||
| 12 | pub use matrix_sdk::ruma::events::AnySyncStateEvent; | 13 | pub use matrix_sdk::ruma::events::AnySyncStateEvent; |
| 13 | pub use matrix_sdk::ruma::events::receipt::ReceiptThread; | 14 | pub use matrix_sdk::ruma::events::receipt::ReceiptThread; |
| 14 | pub use matrix_sdk::ruma::events::relation::Thread; | 15 | pub use matrix_sdk::ruma::events::relation::Thread; |
| @@ -78,6 +79,7 @@ pub use crate::base::{ | |||
| 78 | IambId, | 79 | IambId, |
| 79 | IambInfo, | 80 | IambInfo, |
| 80 | IambResult, | 81 | IambResult, |
| 82 | JoinAction, | ||
| 81 | MessageAction, | 83 | MessageAction, |
| 82 | ProgramAction, | 84 | ProgramAction, |
| 83 | ProgramContext, | 85 | ProgramContext, |
src/tests.rs +1 −1
| @@ -257,7 +257,7 @@ pub async fn mock_store() -> ProgramStore { | |||
| 257 | let info = mock_room(); | 257 | let info = mock_room(); |
| 258 | 258 | ||
| 259 | store.rooms.insert(room_id.clone(), info); | 259 | store.rooms.insert(room_id.clone(), info); |
| 260 | store.names.insert(TEST_ROOM1_ALIAS.clone(), room_id); | 260 | store.aliases.insert(TEST_ROOM1_ALIAS.clone(), room_id); |
| 261 | 261 | ||
| 262 | ProgramStore::new(store) | 262 | ProgramStore::new(store) |
| 263 | } | 263 | } |
src/windows/mod.rs +64 −29
| @@ -13,7 +13,7 @@ use std::fmt::{self}; | |||
| 13 | use feruca::Collator; | 13 | use feruca::Collator; |
| 14 | use matrix_sdk::room::RoomMember; | 14 | use matrix_sdk::room::RoomMember; |
| 15 | use matrix_sdk::ruma::events::room::member::MembershipState; | 15 | use matrix_sdk::ruma::events::room::member::MembershipState; |
| 16 | use matrix_sdk::ruma::{RoomAliasId, assign}; | 16 | use matrix_sdk::ruma::{RoomAliasId, RoomOrAliasId, assign}; |
| 17 | use modalkit::editing::completion::CompletionMap; | 17 | use modalkit::editing::completion::CompletionMap; |
| 18 | use modalkit_ratatui::Window; | 18 | use modalkit_ratatui::Window; |
| 19 | use modalkit_ratatui::list::{List, ListCursor, ListItem, ListState}; | 19 | use modalkit_ratatui::list::{List, ListCursor, ListItem, ListState}; |
| @@ -242,7 +242,7 @@ fn room_prompt( | |||
| 242 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { | 242 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 243 | match act { | 243 | match act { |
| 244 | PromptAction::Submit => { | 244 | PromptAction::Submit => { |
| 245 | let room = IambId::Room(room_id.to_owned(), None); | 245 | let room = IambId::Room(room_id.to_owned().into(), None); |
| 246 | let open = WindowAction::Switch(OpenTarget::Application(room)); | 246 | let open = WindowAction::Switch(OpenTarget::Application(room)); |
| 247 | let acts = vec![(open.into(), ctx.clone())]; | 247 | let acts = vec![(open.into(), ctx.clone())]; |
| 248 | 248 | ||
| @@ -352,7 +352,7 @@ impl IambWindow { | |||
| 352 | store: &mut ProgramStore, | 352 | store: &mut ProgramStore, |
| 353 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | 353 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { |
| 354 | let id = match self { | 354 | let id = match self { |
| 355 | IambWindow::Room(state) => state.id(), | 355 | IambWindow::Room(state) => state.id(store), |
| 356 | IambWindow::MemberList(_, room_id, _) => Some(&**room_id), | 356 | IambWindow::MemberList(_, room_id, _) => Some(&**room_id), |
| 357 | IambWindow::PinnedList(_, room_id, _) => Some(&**room_id), | 357 | IambWindow::PinnedList(_, room_id, _) => Some(&**room_id), |
| 358 | 358 | ||
| @@ -373,6 +373,19 @@ impl IambWindow { | |||
| 373 | } | 373 | } |
| 374 | } | 374 | } |
| 375 | 375 | ||
| 376 | pub async fn join_command( | ||
| 377 | &mut self, | ||
| 378 | act: JoinAction, | ||
| 379 | ctx: ProgramContext, | ||
| 380 | store: &mut ProgramStore, | ||
| 381 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | ||
| 382 | if let IambWindow::Room(w) = self { | ||
| 383 | w.join_command(act, ctx, store).await | ||
| 384 | } else { | ||
| 385 | return Err(IambError::NoSelectedRoom.into()); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 376 | pub async fn send_command( | 389 | pub async fn send_command( |
| 377 | &mut self, | 390 | &mut self, |
| 378 | act: SendAction, | 391 | act: SendAction, |
| @@ -472,7 +485,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 472 | fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { | 485 | fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { |
| 473 | let ChatStore { | 486 | let ChatStore { |
| 474 | collator, | 487 | collator, |
| 475 | names, | 488 | aliases, |
| 476 | rooms, | 489 | rooms, |
| 477 | settings, | 490 | settings, |
| 478 | sync_info, | 491 | sync_info, |
| @@ -490,7 +503,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 490 | let mut items = sync_info | 503 | let mut items = sync_info |
| 491 | .dms | 504 | .dms |
| 492 | .iter() | 505 | .iter() |
| 493 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, names)) | 506 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, aliases)) |
| 494 | .collect::<Vec<_>>(); | 507 | .collect::<Vec<_>>(); |
| 495 | let fields = &settings.tunables.sort.dms; | 508 | let fields = &settings.tunables.sort.dms; |
| 496 | items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator)); | 509 | items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator)); |
| @@ -563,7 +576,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 563 | let mut items = sync_info | 576 | let mut items = sync_info |
| 564 | .rooms | 577 | .rooms |
| 565 | .iter() | 578 | .iter() |
| 566 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, names)) | 579 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, aliases)) |
| 567 | .collect::<Vec<_>>(); | 580 | .collect::<Vec<_>>(); |
| 568 | let fields = &settings.tunables.sort.rooms; | 581 | let fields = &settings.tunables.sort.rooms; |
| 569 | items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator)); | 582 | items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator)); |
| @@ -583,7 +596,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 583 | .rooms | 596 | .rooms |
| 584 | .iter() | 597 | .iter() |
| 585 | .chain(sync_info.dms.iter()) | 598 | .chain(sync_info.dms.iter()) |
| 586 | .map(|room| GenericRoomItem::new(room, rooms, names)) | 599 | .map(|room| GenericRoomItem::new(room, rooms, aliases)) |
| 587 | .collect::<Vec<_>>(); | 600 | .collect::<Vec<_>>(); |
| 588 | 601 | ||
| 589 | let fields = &settings.tunables.sort.chats; | 602 | let fields = &settings.tunables.sort.chats; |
| @@ -604,7 +617,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 604 | .rooms | 617 | .rooms |
| 605 | .iter() | 618 | .iter() |
| 606 | .chain(sync_info.dms.iter()) | 619 | .chain(sync_info.dms.iter()) |
| 607 | .map(|room| GenericRoomItem::new(room, rooms, names)) | 620 | .map(|room| GenericRoomItem::new(room, rooms, aliases)) |
| 608 | .filter(RoomLikeItem::is_unread) | 621 | .filter(RoomLikeItem::is_unread) |
| 609 | .collect::<Vec<_>>(); | 622 | .collect::<Vec<_>>(); |
| 610 | 623 | ||
| @@ -626,7 +639,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 626 | .rooms | 639 | .rooms |
| 627 | .iter() | 640 | .iter() |
| 628 | .chain(sync_info.dms.iter()) | 641 | .chain(sync_info.dms.iter()) |
| 629 | .map(|room| GenericRoomItem::new(room, rooms, names)) | 642 | .map(|room| GenericRoomItem::new(room, rooms, aliases)) |
| 630 | .filter(RoomLikeItem::has_mention) | 643 | .filter(RoomLikeItem::has_mention) |
| 631 | .collect::<Vec<_>>(); | 644 | .collect::<Vec<_>>(); |
| 632 | 645 | ||
| @@ -648,7 +661,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 648 | .rooms | 661 | .rooms |
| 649 | .iter() | 662 | .iter() |
| 650 | .chain(sync_info.dms.iter()) | 663 | .chain(sync_info.dms.iter()) |
| 651 | .map(|room| GenericRoomItem::new(room, rooms, names)) | 664 | .map(|room| GenericRoomItem::new(room, rooms, aliases)) |
| 652 | .filter(RoomLikeItem::is_invite) | 665 | .filter(RoomLikeItem::is_invite) |
| 653 | .collect::<Vec<_>>(); | 666 | .collect::<Vec<_>>(); |
| 654 | 667 | ||
| @@ -669,7 +682,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 669 | let mut items = sync_info | 682 | let mut items = sync_info |
| 670 | .spaces | 683 | .spaces |
| 671 | .iter() | 684 | .iter() |
| 672 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, names)) | 685 | .map(|room| GenericRoomItem::new_unspecified(room, rooms, aliases)) |
| 673 | .collect::<Vec<_>>(); | 686 | .collect::<Vec<_>>(); |
| 674 | 687 | ||
| 675 | let fields = &settings.tunables.sort.spaces; | 688 | let fields = &settings.tunables.sort.spaces; |
| @@ -854,21 +867,28 @@ impl Window<IambInfo> for IambWindow { | |||
| 854 | 867 | ||
| 855 | fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> { | 868 | fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> { |
| 856 | match id { | 869 | match id { |
| 857 | IambId::Room(room_id, thread) => { | 870 | IambId::Room(alias_id, thread) => { |
| 858 | let (room, name, tags) = store.application.worker.get_room(room_id)?; | 871 | let alias: &RoomOrAliasId = &alias_id; |
| 859 | let room = RoomState::new(room, thread, name, tags, store); | 872 | let room_id = match <&RoomId>::try_from(alias) { |
| 873 | Ok(room_id) => room_id, | ||
| 874 | Err(alias) => { | ||
| 875 | if let Some(room_id) = store.application.aliases.get(alias) { | ||
| 876 | room_id | ||
| 877 | } else { | ||
| 878 | return Ok(RoomState::not_joined(alias_id, store).into()); | ||
| 879 | } | ||
| 880 | }, | ||
| 881 | }; | ||
| 860 | 882 | ||
| 861 | if let Some(room_id) = room.id() { | 883 | if let Some(room) = store.application.worker.client.get_room(room_id) { |
| 862 | store.application.need_load.need_members(room_id.to_owned()); | 884 | store.application.need_load.need_members(room_id.to_owned()); |
| 885 | |||
| 886 | let room = RoomState::new(room, thread, store); | ||
| 887 | |||
| 888 | return Ok(room.into()); | ||
| 863 | } | 889 | } |
| 864 | 890 | ||
| 865 | return Ok(room.into()); | 891 | return Ok(RoomState::not_joined(alias_id, store).into()); |
| 866 | }, | ||
| 867 | IambId::Joining(name) => { | ||
| 868 | return Ok(RoomState::join(name, store).into()); | ||
| 869 | }, | ||
| 870 | IambId::NotJoined(name) => { | ||
| 871 | return Ok(RoomState::not_joined(name).into()); | ||
| 872 | }, | 892 | }, |
| 873 | IambId::DirectList => { | 893 | IambId::DirectList => { |
| 874 | let list = RoomListState::new(IambBufferId::DirectList, vec![]); | 894 | let list = RoomListState::new(IambBufferId::DirectList, vec![]); |
| @@ -933,13 +953,28 @@ impl Window<IambInfo> for IambWindow { | |||
| 933 | } | 953 | } |
| 934 | 954 | ||
| 935 | fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> { | 955 | fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> { |
| 936 | if let Some(room) = store.application.names.get(&name) { | 956 | let room_alias = if let Ok(alias) = <&RoomAliasId>::try_from(name.as_str()) { |
| 937 | let id = IambId::Room(room.clone(), None); | 957 | if let Some(room_id) = store.application.aliases.get(alias) { |
| 938 | IambWindow::open(id, store) | 958 | room_id.to_owned().into() |
| 959 | } else { | ||
| 960 | alias.to_owned().into() | ||
| 961 | } | ||
| 962 | } else if let Ok(room_id) = <&RoomId>::try_from(name.as_str()) { | ||
| 963 | room_id.to_owned().into() | ||
| 964 | } else if let Ok(user_id) = <&UserId>::try_from(name.as_str()) { | ||
| 965 | if let Some(dm) = store.application.worker.client.get_dm_room(user_id) { | ||
| 966 | dm.room_id().to_owned().into() | ||
| 967 | } else { | ||
| 968 | store.application.worker.create_dm(user_id.to_owned())?.into() | ||
| 969 | } | ||
| 939 | } else { | 970 | } else { |
| 940 | let room = RoomState::join(name, store); | 971 | // XXX: support passing matrix uris to `:join` |
| 941 | Ok(room.into()) | 972 | |
| 942 | } | 973 | return Err(UIError::Failure("Could not parse room identifier".to_string())); |
| 974 | }; | ||
| 975 | |||
| 976 | let id = IambId::Room(room_alias, None); | ||
| 977 | IambWindow::open(id, store) | ||
| 943 | } | 978 | } |
| 944 | 979 | ||
| 945 | fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> { | 980 | fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> { |
| @@ -1348,7 +1383,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for PinnedItem { | |||
| 1348 | .and_then(|(thread, _)| thread) | 1383 | .and_then(|(thread, _)| thread) |
| 1349 | .map(ToOwned::to_owned); | 1384 | .map(ToOwned::to_owned); |
| 1350 | 1385 | ||
| 1351 | let room = IambId::Room(self.room_id.clone(), thread); | 1386 | let room = IambId::Room(self.room_id.clone().into(), thread); |
| 1352 | let open = WindowAction::Switch(OpenTarget::Application(room)); | 1387 | let open = WindowAction::Switch(OpenTarget::Application(room)); |
| 1353 | let jump = IambAction::from(TimelineAction::GotoEvent(self.event_id.clone())); | 1388 | let jump = IambAction::from(TimelineAction::GotoEvent(self.event_id.clone())); |
| 1354 | 1389 | ||
src/windows/room/chat.rs +1 −1
| @@ -958,7 +958,7 @@ fn open_links(msg: &Message) -> UIError<IambInfo> { | |||
| 958 | .into_iter() | 958 | .into_iter() |
| 959 | .map(|l| { | 959 | .map(|l| { |
| 960 | let url = l.1.to_string(); | 960 | let url = l.1.to_string(); |
| 961 | let act = IambAction::OpenLink(url.clone(), false).into(); | 961 | let act = IambAction::OpenLink(url.clone()).into(); |
| 962 | MultiChoiceItem::new(l.0, url, vec![act]) | 962 | MultiChoiceItem::new(l.0, url, vec![act]) |
| 963 | }) | 963 | }) |
| 964 | .collect(); | 964 | .collect(); |
src/windows/room/joining.rs deleted −98
| @@ -1,98 +0,0 @@ | |||
| 1 | use crate::prelude::*; | ||
| 2 | use crate::worker::ClientResponse; | ||
| 3 | |||
| 4 | pub struct JoiningState { | ||
| 5 | pub(super) room: String, | ||
| 6 | room_id: Option<IambResult<OwnedRoomId>>, | ||
| 7 | chan: ClientResponse<IambResult<OwnedRoomId>>, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl JoiningState { | ||
| 11 | pub fn new(room: String, store: &mut ProgramStore) -> Self { | ||
| 12 | let via = store.application.settings.tunables.default_via.clone(); | ||
| 13 | let chan = store.application.worker.join_room_chan(room.clone(), via); | ||
| 14 | Self { room, room_id: None, chan } | ||
| 15 | } | ||
| 16 | |||
| 17 | pub fn try_recv(&mut self) -> Option<&IambResult<OwnedRoomId>> { | ||
| 18 | if self.room_id.is_none() && | ||
| 19 | let Some(id) = self.chan.try_recv() | ||
| 20 | { | ||
| 21 | self.room_id = Some(id); | ||
| 22 | } | ||
| 23 | |||
| 24 | self.room_id.as_ref() | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | impl Editable<ProgramContext, ProgramStore, IambInfo> for JoiningState { | ||
| 29 | fn editor_command( | ||
| 30 | &mut self, | ||
| 31 | _: &EditorAction, | ||
| 32 | _: &ProgramContext, | ||
| 33 | _: &mut ProgramStore, | ||
| 34 | ) -> EditResult<EditInfo, IambInfo> { | ||
| 35 | Ok(None) | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | impl Jumpable<ProgramContext, IambInfo> for JoiningState { | ||
| 40 | fn jump( | ||
| 41 | &mut self, | ||
| 42 | _: PositionList, | ||
| 43 | _: MoveDir1D, | ||
| 44 | count: usize, | ||
| 45 | _: &ProgramContext, | ||
| 46 | ) -> IambResult<usize> { | ||
| 47 | Ok(count) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | impl Scrollable<ProgramContext, ProgramStore, IambInfo> for JoiningState { | ||
| 52 | fn scroll( | ||
| 53 | &mut self, | ||
| 54 | _: &ScrollStyle, | ||
| 55 | _: &ProgramContext, | ||
| 56 | _: &mut ProgramStore, | ||
| 57 | ) -> EditResult<EditInfo, IambInfo> { | ||
| 58 | Ok(None) | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for JoiningState { | ||
| 63 | fn prompt( | ||
| 64 | &mut self, | ||
| 65 | _: &PromptAction, | ||
| 66 | _: &ProgramContext, | ||
| 67 | _: &mut ProgramStore, | ||
| 68 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { | ||
| 69 | Ok(vec![]) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | impl TerminalCursor for JoiningState { | ||
| 74 | fn get_term_cursor(&self) -> Option<TermOffset> { | ||
| 75 | None | ||
| 76 | } | ||
| 77 | |||
| 78 | fn hide_term_cursor(&self) -> bool { | ||
| 79 | true | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | /// [StatefulWidget] for Matrix rooms being joined. | ||
| 84 | pub struct Joining; | ||
| 85 | |||
| 86 | impl StatefulWidget for Joining { | ||
| 87 | type State = JoiningState; | ||
| 88 | |||
| 89 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | ||
| 90 | let line = Line::from(vec![ | ||
| 91 | Span::raw("Joining "), | ||
| 92 | Span::styled(state.room.as_str(), StyleModifier::BOLD), | ||
| 93 | Span::raw("..."), | ||
| 94 | ]); | ||
| 95 | |||
| 96 | Paragraph::new(line).alignment(Alignment::Center).render(area, buffer); | ||
| 97 | } | ||
| 98 | } | ||
src/windows/room/mod.rs +56 −105
| @@ -1,7 +1,6 @@ | |||
| 1 | //! # Windows for Matrix rooms and spaces | 1 | //! # Windows for Matrix rooms and spaces |
| 2 | use std::collections::HashSet; | 2 | use std::collections::HashSet; |
| 3 | 3 | ||
| 4 | use matrix_sdk::RoomDisplayName; | ||
| 5 | use matrix_sdk::notification_settings::RoomNotificationMode; | 4 | use matrix_sdk::notification_settings::RoomNotificationMode; |
| 6 | use matrix_sdk::ruma::api::client::room::upgrade_room::v3::Request as UpgradeRoomRequest; | 5 | use matrix_sdk::ruma::api::client::room::upgrade_room::v3::Request as UpgradeRoomRequest; |
| 7 | use matrix_sdk::ruma::api::error::ErrorKind as ClientApiErrorKind; | 6 | use matrix_sdk::ruma::api::error::ErrorKind as ClientApiErrorKind; |
| @@ -19,12 +18,10 @@ use crate::base::{MemberUpdateAction, RoomField}; | |||
| 19 | use crate::config::EncryptionIndicatorLocation; | 18 | use crate::config::EncryptionIndicatorLocation; |
| 20 | use crate::prelude::*; | 19 | use crate::prelude::*; |
| 21 | use crate::windows::room::chat::ChatState; | 20 | use crate::windows::room::chat::ChatState; |
| 22 | use crate::windows::room::joining::{Joining, JoiningState}; | ||
| 23 | use crate::windows::room::not_joined::{NotJoined, NotJoinedState}; | 21 | use crate::windows::room::not_joined::{NotJoined, NotJoinedState}; |
| 24 | use crate::windows::room::space::{Space, SpaceState}; | 22 | use crate::windows::room::space::{Space, SpaceState}; |
| 25 | 23 | ||
| 26 | mod chat; | 24 | mod chat; |
| 27 | mod joining; | ||
| 28 | mod not_joined; | 25 | mod not_joined; |
| 29 | mod scrollback; | 26 | mod scrollback; |
| 30 | mod space; | 27 | mod space; |
| @@ -33,7 +30,6 @@ macro_rules! delegate { | |||
| 33 | ($s: expr, $id: ident => $e: expr) => { | 30 | ($s: expr, $id: ident => $e: expr) => { |
| 34 | match $s { | 31 | match $s { |
| 35 | RoomState::NotJoined($id) => $e, | 32 | RoomState::NotJoined($id) => $e, |
| 36 | RoomState::Joining($id) => $e, | ||
| 37 | RoomState::Chat($id) => $e, | 33 | RoomState::Chat($id) => $e, |
| 38 | RoomState::Space($id) => $e, | 34 | RoomState::Space($id) => $e, |
| 39 | } | 35 | } |
| @@ -93,7 +89,7 @@ pub async fn room_command( | |||
| 93 | }, | 89 | }, |
| 94 | }; | 90 | }; |
| 95 | 91 | ||
| 96 | let id = IambId::Room(room_id.to_owned(), None); | 92 | let id = IambId::Room(room_id.to_owned().into(), None); |
| 97 | let target = OpenTarget::Application(id); | 93 | let target = OpenTarget::Application(id); |
| 98 | let act = cmd.switch(target); | 94 | let act = cmd.switch(target); |
| 99 | 95 | ||
| @@ -635,7 +631,7 @@ pub async fn room_command( | |||
| 635 | request.additional_creators = additional_creators; | 631 | request.additional_creators = additional_creators; |
| 636 | 632 | ||
| 637 | let response = worker.client.send(request).await.map_err(IambError::from)?; | 633 | let response = worker.client.send(request).await.map_err(IambError::from)?; |
| 638 | let id = IambId::Room(response.replacement_room, None); | 634 | let id = IambId::Room(response.replacement_room.into(), None); |
| 639 | let target = OpenTarget::Application(id); | 635 | let target = OpenTarget::Application(id); |
| 640 | let act = WindowAction::Switch(target); | 636 | let act = WindowAction::Switch(target); |
| 641 | 637 | ||
| @@ -651,7 +647,6 @@ pub async fn room_command( | |||
| 651 | /// similarly. | 647 | /// similarly. |
| 652 | pub enum RoomState { | 648 | pub enum RoomState { |
| 653 | NotJoined(Box<NotJoinedState>), | 649 | NotJoined(Box<NotJoinedState>), |
| 654 | Joining(Box<JoiningState>), | ||
| 655 | Chat(Box<ChatState>), | 650 | Chat(Box<ChatState>), |
| 656 | Space(Box<SpaceState>), | 651 | Space(Box<SpaceState>), |
| 657 | } | 652 | } |
| @@ -662,12 +657,6 @@ impl From<NotJoinedState> for RoomState { | |||
| 662 | } | 657 | } |
| 663 | } | 658 | } |
| 664 | 659 | ||
| 665 | impl From<JoiningState> for RoomState { | ||
| 666 | fn from(chat: JoiningState) -> Self { | ||
| 667 | RoomState::Joining(Box::new(chat)) | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | impl From<ChatState> for RoomState { | 660 | impl From<ChatState> for RoomState { |
| 672 | fn from(chat: ChatState) -> Self { | 661 | fn from(chat: ChatState) -> Self { |
| 673 | RoomState::Chat(Box::new(chat)) | 662 | RoomState::Chat(Box::new(chat)) |
| @@ -681,18 +670,7 @@ impl From<SpaceState> for RoomState { | |||
| 681 | } | 670 | } |
| 682 | 671 | ||
| 683 | impl RoomState { | 672 | impl RoomState { |
| 684 | pub fn new( | 673 | pub fn new(room: MatrixRoom, thread: Option<OwnedEventId>, store: &mut ProgramStore) -> Self { |
| 685 | room: MatrixRoom, | ||
| 686 | thread: Option<OwnedEventId>, | ||
| 687 | name: RoomDisplayName, | ||
| 688 | tags: Option<Tags>, | ||
| 689 | store: &mut ProgramStore, | ||
| 690 | ) -> Self { | ||
| 691 | let room_id = room.room_id().to_owned(); | ||
| 692 | let info = store.application.get_room_info(room_id); | ||
| 693 | info.name = name.to_string().into(); | ||
| 694 | info.tags = tags; | ||
| 695 | |||
| 696 | if room.is_space() { | 674 | if room.is_space() { |
| 697 | SpaceState::new(room).into() | 675 | SpaceState::new(room).into() |
| 698 | } else { | 676 | } else { |
| @@ -700,63 +678,46 @@ impl RoomState { | |||
| 700 | } | 678 | } |
| 701 | } | 679 | } |
| 702 | 680 | ||
| 703 | pub fn join(name: String, store: &mut ProgramStore) -> Self { | 681 | pub fn not_joined(alias: OwnedRoomOrAliasId, store: &mut ProgramStore) -> Self { |
| 704 | let joining = JoiningState::new(name, store); | 682 | Self::from(NotJoinedState::new(alias, store)) |
| 705 | Self::from(joining) | ||
| 706 | } | ||
| 707 | |||
| 708 | pub fn not_joined(name: String) -> Self { | ||
| 709 | Self::from(NotJoinedState::new(name)) | ||
| 710 | } | 683 | } |
| 711 | 684 | ||
| 712 | pub fn window_id(&self) -> IambId { | 685 | pub fn window_id(&self) -> IambId { |
| 713 | match self { | 686 | match self { |
| 714 | RoomState::NotJoined(nj) => IambId::NotJoined(nj.room.clone()), | 687 | RoomState::NotJoined(nj) => IambId::Room(nj.alias().to_owned(), None), |
| 715 | RoomState::Joining(joining) => IambId::Joining(joining.room.clone()), | 688 | RoomState::Chat(chat) => { |
| 716 | RoomState::Chat(chat) => IambId::Room(chat.id().to_owned(), chat.thread().cloned()), | 689 | IambId::Room(chat.id().to_owned().into(), chat.thread().cloned()) |
| 717 | RoomState::Space(space) => IambId::Room(space.id().to_owned(), None), | 690 | }, |
| 691 | RoomState::Space(space) => IambId::Room(space.id().to_owned().into(), None), | ||
| 718 | } | 692 | } |
| 719 | } | 693 | } |
| 720 | 694 | ||
| 721 | pub fn room_state(&self) -> Option<MatrixRoomState> { | 695 | pub fn room_state(&self) -> Option<MatrixRoomState> { |
| 722 | match self { | 696 | match self { |
| 723 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | 697 | RoomState::NotJoined(_) => None, |
| 724 | RoomState::Chat(chat) => Some(chat.room().state()), | 698 | RoomState::Chat(chat) => Some(chat.room().state()), |
| 725 | RoomState::Space(space) => Some(space.room().state()), | 699 | RoomState::Space(space) => Some(space.room().state()), |
| 726 | } | 700 | } |
| 727 | } | 701 | } |
| 728 | 702 | ||
| 729 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { | 703 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { |
| 704 | if let Some(room) = self.room() && | ||
| 705 | room.state() == MatrixRoomState::Left | ||
| 706 | { | ||
| 707 | // room previews have more information than left rooms | ||
| 708 | let alias_id = if let Some(alias) = room.canonical_alias() { | ||
| 709 | alias.into() | ||
| 710 | } else { | ||
| 711 | room.room_id().to_owned().into() | ||
| 712 | }; | ||
| 713 | *self = Self::not_joined(alias_id, store); | ||
| 714 | } | ||
| 715 | |||
| 730 | match self { | 716 | match self { |
| 731 | RoomState::NotJoined(_) => {}, | 717 | RoomState::NotJoined(not_joined) => { |
| 732 | RoomState::Joining(joining) => { | 718 | if let Some(state) = not_joined.refresh_room(store) { |
| 733 | let joined = joining.room.clone(); | 719 | *self = state; |
| 734 | |||
| 735 | let room_id = match joining.try_recv() { | ||
| 736 | None => return, | ||
| 737 | Some(Ok(room_id)) => room_id.clone(), | ||
| 738 | Some(Err(e)) => { | ||
| 739 | // We failed to join the room, so show the NotJoined window, | ||
| 740 | // and let the user try again if the error was transient: | ||
| 741 | *self = NotJoinedState::failed(joined, e).into(); | ||
| 742 | return; | ||
| 743 | }, | ||
| 744 | }; | ||
| 745 | |||
| 746 | let Ok((room, name, tags)) = store.application.worker.get_room(room_id.clone()) | ||
| 747 | else { | ||
| 748 | return; | ||
| 749 | }; | ||
| 750 | |||
| 751 | if let Ok(alias) = OwnedRoomAliasId::try_from(joined.as_str()) { | ||
| 752 | // If the `:join` was for a room alias, then track it so | ||
| 753 | // we can reuse it later for `:join`/`:sp`/etc: | ||
| 754 | store.application.names.insert(alias, room_id.clone()); | ||
| 755 | } | 720 | } |
| 756 | |||
| 757 | store.application.need_load.need_members(room_id); | ||
| 758 | |||
| 759 | *self = RoomState::new(room, None, name, tags, store); | ||
| 760 | }, | 721 | }, |
| 761 | RoomState::Chat(chat) => chat.refresh_room(store), | 722 | RoomState::Chat(chat) => chat.refresh_room(store), |
| 762 | RoomState::Space(space) => space.refresh_room(store), | 723 | RoomState::Space(space) => space.refresh_room(store), |
| @@ -820,25 +781,6 @@ impl RoomState { | |||
| 820 | return; | 781 | return; |
| 821 | } | 782 | } |
| 822 | 783 | ||
| 823 | fn draw_left(&self, room: &MatrixRoom, area: Rect, buf: &mut Buffer, store: &mut ProgramStore) { | ||
| 824 | let name = match room.canonical_alias() { | ||
| 825 | Some(alias) => alias.to_string(), | ||
| 826 | None => format!("{:?}", store.application.get_room_title(room.room_id())), | ||
| 827 | }; | ||
| 828 | |||
| 829 | let mut lines = vec![Line::from(format!("You have left {name}!"))]; | ||
| 830 | |||
| 831 | if room.is_public().is_some_and(|b| b) { | ||
| 832 | lines.push(Line::from(format!("You can run `:join {name}` to rejoin."))); | ||
| 833 | } | ||
| 834 | |||
| 835 | let text = Text::from(lines); | ||
| 836 | |||
| 837 | Paragraph::new(text).alignment(Alignment::Center).render(area, buf); | ||
| 838 | |||
| 839 | return; | ||
| 840 | } | ||
| 841 | |||
| 842 | pub async fn timeline_command( | 784 | pub async fn timeline_command( |
| 843 | &mut self, | 785 | &mut self, |
| 844 | act: TimelineAction, | 786 | act: TimelineAction, |
| @@ -878,6 +820,19 @@ impl RoomState { | |||
| 878 | } | 820 | } |
| 879 | } | 821 | } |
| 880 | 822 | ||
| 823 | pub async fn join_command( | ||
| 824 | &mut self, | ||
| 825 | act: JoinAction, | ||
| 826 | ctx: ProgramContext, | ||
| 827 | store: &mut ProgramStore, | ||
| 828 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | ||
| 829 | if let RoomState::NotJoined(nj) = self { | ||
| 830 | nj.join_command(act, ctx, store).await | ||
| 831 | } else { | ||
| 832 | Err(IambError::NoSelectedSpace.into()) | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 881 | pub async fn send_command( | 836 | pub async fn send_command( |
| 882 | &mut self, | 837 | &mut self, |
| 883 | act: SendAction, | 838 | act: SendAction, |
| @@ -897,7 +852,11 @@ impl RoomState { | |||
| 897 | let title_style = theme.windows.title; | 852 | let title_style = theme.windows.title; |
| 898 | 853 | ||
| 899 | let Some(room) = self.room() else { | 854 | let Some(room) = self.room() else { |
| 900 | return Line::styled("Unjoined Room", title_style); | 855 | if let RoomState::NotJoined(nj) = self { |
| 856 | return nj.get_title(store); | ||
| 857 | } else { | ||
| 858 | return Line::styled("Unknown Room", title_style); | ||
| 859 | } | ||
| 901 | }; | 860 | }; |
| 902 | 861 | ||
| 903 | let room_id = room.room_id(); | 862 | let room_id = room.room_id(); |
| @@ -939,17 +898,14 @@ impl RoomState { | |||
| 939 | match self { | 898 | match self { |
| 940 | RoomState::Space(w) => store.application.get_room_title(w.id()).into(), | 899 | RoomState::Space(w) => store.application.get_room_title(w.id()).into(), |
| 941 | RoomState::Chat(w) => store.application.get_room_title(w.id()).into(), | 900 | RoomState::Chat(w) => store.application.get_room_title(w.id()).into(), |
| 942 | RoomState::NotJoined(_) => Line::from("Unjoined Room"), | 901 | RoomState::NotJoined(w) => w.get_tab_title(store), |
| 943 | RoomState::Joining(w) => { | ||
| 944 | Line::from(vec![Span::raw("Joining "), Span::raw(w.room.as_str())]) | ||
| 945 | }, | ||
| 946 | } | 902 | } |
| 947 | } | 903 | } |
| 948 | 904 | ||
| 949 | pub fn focus_toggle(&mut self) { | 905 | pub fn focus_toggle(&mut self) { |
| 950 | match self { | 906 | match self { |
| 951 | RoomState::Chat(chat) => chat.focus_toggle(), | 907 | RoomState::Chat(chat) => chat.focus_toggle(), |
| 952 | RoomState::Joining(_) | RoomState::NotJoined(_) | RoomState::Space(_) => return, | 908 | RoomState::NotJoined(_) | RoomState::Space(_) => return, |
| 953 | } | 909 | } |
| 954 | } | 910 | } |
| 955 | 911 | ||
| @@ -957,15 +913,15 @@ impl RoomState { | |||
| 957 | match self { | 913 | match self { |
| 958 | RoomState::Chat(chat) => Some(chat.room()), | 914 | RoomState::Chat(chat) => Some(chat.room()), |
| 959 | RoomState::Space(space) => Some(space.room()), | 915 | RoomState::Space(space) => Some(space.room()), |
| 960 | RoomState::Joining(_) | RoomState::NotJoined(_) => None, | 916 | RoomState::NotJoined(_) => None, |
| 961 | } | 917 | } |
| 962 | } | 918 | } |
| 963 | 919 | ||
| 964 | pub fn id(&self) -> Option<&RoomId> { | 920 | pub fn id(&mut self, store: &ProgramStore) -> Option<&RoomId> { |
| 965 | match self { | 921 | match self { |
| 966 | RoomState::Chat(chat) => Some(chat.id()), | 922 | RoomState::Chat(chat) => Some(chat.id()), |
| 967 | RoomState::Space(space) => Some(space.id()), | 923 | RoomState::Space(space) => Some(space.id()), |
| 968 | RoomState::Joining(_) | RoomState::NotJoined(_) => None, | 924 | RoomState::NotJoined(nj) => nj.room_id(store), |
| 969 | } | 925 | } |
| 970 | } | 926 | } |
| 971 | } | 927 | } |
| @@ -1035,7 +991,6 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1035 | match room.state() { | 991 | match room.state() { |
| 1036 | MatrixRoomState::Invited => return self.draw_invite(room, area, buf, store), | 992 | MatrixRoomState::Invited => return self.draw_invite(room, area, buf, store), |
| 1037 | MatrixRoomState::Knocked => return self.draw_knock(room, area, buf, store), | 993 | MatrixRoomState::Knocked => return self.draw_knock(room, area, buf, store), |
| 1038 | MatrixRoomState::Left => return self.draw_left(room, area, buf, store), | ||
| 1039 | _ => (), | 994 | _ => (), |
| 1040 | } | 995 | } |
| 1041 | } | 996 | } |
| @@ -1044,11 +999,8 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1044 | RoomState::Chat(chat) => { | 999 | RoomState::Chat(chat) => { |
| 1045 | chat.draw(area, buf, focused, store); | 1000 | chat.draw(area, buf, focused, store); |
| 1046 | }, | 1001 | }, |
| 1047 | RoomState::Joining(state) => { | ||
| 1048 | Joining.render(area, buf, state); | ||
| 1049 | }, | ||
| 1050 | RoomState::NotJoined(state) => { | 1002 | RoomState::NotJoined(state) => { |
| 1051 | NotJoined.render(area, buf, state); | 1003 | NotJoined::new(store).render(area, buf, state); |
| 1052 | }, | 1004 | }, |
| 1053 | RoomState::Space(space) => { | 1005 | RoomState::Space(space) => { |
| 1054 | Space::new(store).focus(focused).render(area, buf, space); | 1006 | Space::new(store).focus(focused).render(area, buf, space); |
| @@ -1058,8 +1010,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1058 | 1010 | ||
| 1059 | fn dup(&self, store: &mut ProgramStore) -> Self { | 1011 | fn dup(&self, store: &mut ProgramStore) -> Self { |
| 1060 | match self { | 1012 | match self { |
| 1061 | RoomState::Joining(w) => RoomState::join(w.room.clone(), store), | 1013 | RoomState::NotJoined(w) => RoomState::NotJoined(Box::new(w.dup())), |
| 1062 | RoomState::NotJoined(w) => RoomState::NotJoined((*w).clone()), | ||
| 1063 | RoomState::Chat(chat) => RoomState::Chat(Box::new(chat.dup(store))), | 1014 | RoomState::Chat(chat) => RoomState::Chat(Box::new(chat.dup(store))), |
| 1064 | RoomState::Space(space) => RoomState::Space(Box::new(space.dup(store))), | 1015 | RoomState::Space(space) => RoomState::Space(Box::new(space.dup(store))), |
| 1065 | } | 1016 | } |
| @@ -1067,7 +1018,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1067 | 1018 | ||
| 1068 | fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool { | 1019 | fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool { |
| 1069 | match self { | 1020 | match self { |
| 1070 | RoomState::NotJoined(_) | RoomState::Joining(_) => true, | 1021 | RoomState::NotJoined(_) => true, |
| 1071 | RoomState::Chat(chat) => chat.close(flags, store), | 1022 | RoomState::Chat(chat) => chat.close(flags, store), |
| 1072 | RoomState::Space(space) => space.close(flags, store), | 1023 | RoomState::Space(space) => space.close(flags, store), |
| 1073 | } | 1024 | } |
| @@ -1080,7 +1031,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1080 | store: &mut ProgramStore, | 1031 | store: &mut ProgramStore, |
| 1081 | ) -> IambResult<EditInfo> { | 1032 | ) -> IambResult<EditInfo> { |
| 1082 | match self { | 1033 | match self { |
| 1083 | RoomState::NotJoined(_) | RoomState::Joining(_) => Err(EditError::ReadOnly.into()), | 1034 | RoomState::NotJoined(_) => Err(EditError::ReadOnly.into()), |
| 1084 | RoomState::Chat(chat) => chat.write(path, flags, store), | 1035 | RoomState::Chat(chat) => chat.write(path, flags, store), |
| 1085 | RoomState::Space(space) => space.write(path, flags, store), | 1036 | RoomState::Space(space) => space.write(path, flags, store), |
| 1086 | } | 1037 | } |
| @@ -1088,7 +1039,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1088 | 1039 | ||
| 1089 | fn get_completions(&self) -> Option<CompletionList> { | 1040 | fn get_completions(&self) -> Option<CompletionList> { |
| 1090 | match self { | 1041 | match self { |
| 1091 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | 1042 | RoomState::NotJoined(_) => None, |
| 1092 | RoomState::Chat(chat) => chat.get_completions(), | 1043 | RoomState::Chat(chat) => chat.get_completions(), |
| 1093 | RoomState::Space(space) => space.get_completions(), | 1044 | RoomState::Space(space) => space.get_completions(), |
| 1094 | } | 1045 | } |
| @@ -1096,7 +1047,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1096 | 1047 | ||
| 1097 | fn get_cursor_word(&self, style: &WordStyle) -> Option<String> { | 1048 | fn get_cursor_word(&self, style: &WordStyle) -> Option<String> { |
| 1098 | match self { | 1049 | match self { |
| 1099 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | 1050 | RoomState::NotJoined(_) => None, |
| 1100 | RoomState::Chat(chat) => chat.get_cursor_word(style), | 1051 | RoomState::Chat(chat) => chat.get_cursor_word(style), |
| 1101 | RoomState::Space(space) => space.get_cursor_word(style), | 1052 | RoomState::Space(space) => space.get_cursor_word(style), |
| 1102 | } | 1053 | } |
| @@ -1104,7 +1055,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 1104 | 1055 | ||
| 1105 | fn get_selected_word(&self) -> Option<String> { | 1056 | fn get_selected_word(&self) -> Option<String> { |
| 1106 | match self { | 1057 | match self { |
| 1107 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | 1058 | RoomState::NotJoined(_) => None, |
| 1108 | RoomState::Chat(chat) => chat.get_selected_word(), | 1059 | RoomState::Chat(chat) => chat.get_selected_word(), |
| 1109 | RoomState::Space(space) => space.get_selected_word(), | 1060 | RoomState::Space(space) => space.get_selected_word(), |
| 1110 | } | 1061 | } |
src/windows/room/not_joined.rs +268 −18
| @@ -1,20 +1,200 @@ | |||
| 1 | use matrix_sdk::ruma::RoomOrAliasId; | ||
| 2 | use matrix_sdk::ruma::room::{JoinRuleSummary, RestrictedSummary}; | ||
| 3 | |||
| 4 | use crate::base::{HomeserverAction, SyncInfo}; | ||
| 1 | use crate::prelude::*; | 5 | use crate::prelude::*; |
| 6 | use crate::windows::room::RoomState; | ||
| 7 | use crate::worker::ClientResponse; | ||
| 8 | |||
| 9 | const ROOM_PREVIEW_DEBOUNCE: Duration = Duration::from_secs(15); | ||
| 10 | |||
| 11 | fn can_join_restricted(summary: &RestrictedSummary, sync_info: &SyncInfo) -> bool { | ||
| 12 | sync_info | ||
| 13 | .spaces | ||
| 14 | .iter() | ||
| 15 | .map(|r| r.room_id()) | ||
| 16 | .chain(sync_info.chats()) | ||
| 17 | .any(|room_id| summary.allowed_room_ids.iter().any(|allowed| allowed == room_id)) | ||
| 18 | } | ||
| 19 | |||
| 20 | fn preview_join_rule( | ||
| 21 | preview: &RoomPreview, | ||
| 22 | store: &ProgramStore, | ||
| 23 | ) -> (&'static str, Option<&'static str>) { | ||
| 24 | match &preview.join_rule { | ||
| 25 | Some(JoinRuleSummary::Public) => ("This room is public.", Some(":join")), | ||
| 26 | Some(JoinRuleSummary::Knock) => ("This room is knock-only.", Some(":knock")), | ||
| 27 | Some(JoinRuleSummary::Invite) => ("This room is invite-only.", None), | ||
| 28 | Some(JoinRuleSummary::Restricted(summary)) => { | ||
| 29 | if can_join_restricted(summary, &store.application.sync_info) { | ||
| 30 | ("This room is restricted to members of a room you are in.", Some(":join")) | ||
| 31 | } else { | ||
| 32 | ("This room is restricted to members of a room you are not in.", None) | ||
| 33 | } | ||
| 34 | }, | ||
| 35 | Some(JoinRuleSummary::KnockRestricted(summary)) => { | ||
| 36 | if can_join_restricted(summary, &store.application.sync_info) { | ||
| 37 | ("This room is knock-restricted to members of a room you are in.", Some(":knock")) | ||
| 38 | } else { | ||
| 39 | ("This room is knock-restricted to members of a room you are not in.", None) | ||
| 40 | } | ||
| 41 | }, | ||
| 42 | _ => ("Room join rules unknown. Try `:join` or `:knock` to join this room.", None), | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | fn preview_state( | ||
| 47 | preview: &RoomPreview, | ||
| 48 | store: &ProgramStore, | ||
| 49 | ) -> (&'static str, Option<&'static str>) { | ||
| 50 | match preview.state { | ||
| 51 | Some(MatrixRoomState::Invited) => { | ||
| 52 | ("You have been invited to join this room.", Some(":invite accept")) | ||
| 53 | }, | ||
| 54 | Some(MatrixRoomState::Knocked) => ("You have knocked on this room.", None), | ||
| 55 | Some(MatrixRoomState::Banned) => ("You have been banned from this room.", None), | ||
| 56 | Some(MatrixRoomState::Joined) => ("You have already joined this room.", None), | ||
| 57 | Some(MatrixRoomState::Left) | None => preview_join_rule(preview, store), | ||
| 58 | } | ||
| 59 | } | ||
| 2 | 60 | ||
| 3 | #[derive(Clone)] | ||
| 4 | pub struct NotJoinedState { | 61 | pub struct NotJoinedState { |
| 5 | pub(super) room: String, | 62 | alias: OwnedRoomOrAliasId, |
| 63 | /// The resolved [`alias`](`Self::alias`). | ||
| 64 | room_id: Option<OwnedRoomId>, | ||
| 6 | err: Option<String>, | 65 | err: Option<String>, |
| 66 | joining: Option<ClientResponse<IambResult<OwnedRoomId>>>, | ||
| 67 | |||
| 68 | term_cursor: (u16, u16), | ||
| 7 | } | 69 | } |
| 8 | 70 | ||
| 9 | impl NotJoinedState { | 71 | impl NotJoinedState { |
| 10 | /// Create a window for a room the user hasn't yet joined. | 72 | /// Create a window for a room the user hasn't joined. |
| 11 | pub fn new(room: String) -> Self { | 73 | pub fn new(alias: OwnedRoomOrAliasId, store: &mut ProgramStore) -> Self { |
| 12 | Self { room, err: None } | 74 | store.application.need_load.need_preview(alias.clone()); |
| 75 | |||
| 76 | Self { | ||
| 77 | alias, | ||
| 78 | room_id: None, | ||
| 79 | err: None, | ||
| 80 | joining: None, | ||
| 81 | term_cursor: (0, 0), | ||
| 82 | } | ||
| 13 | } | 83 | } |
| 14 | 84 | ||
| 15 | /// Create a window for a room whose `:join` command failed. | 85 | pub fn alias(&self) -> &RoomOrAliasId { |
| 16 | pub fn failed(room: String, err: &UIError<IambInfo>) -> Self { | 86 | &self.alias |
| 17 | Self { room, err: Some(err.to_string()) } | 87 | } |
| 88 | |||
| 89 | pub fn room_id(&mut self, store: &ProgramStore) -> Option<&RoomId> { | ||
| 90 | let alias_or_id: &RoomOrAliasId = &self.alias; | ||
| 91 | match <&RoomId>::try_from(alias_or_id) { | ||
| 92 | Ok(id) => Some(id), | ||
| 93 | Err(alias) => { | ||
| 94 | let room_id = store.application.aliases.get(alias); | ||
| 95 | self.room_id = room_id.cloned(); | ||
| 96 | self.room_id.as_deref() | ||
| 97 | }, | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | pub fn refresh_room(&mut self, store: &mut ProgramStore) -> Option<RoomState> { | ||
| 102 | let room = if let Some(room) = self | ||
| 103 | .room_id(store) | ||
| 104 | .and_then(|id| store.application.worker.client.get_room(id)) | ||
| 105 | { | ||
| 106 | room | ||
| 107 | } else { | ||
| 108 | if let Some(Err(e)) = self.joining.as_ref().and_then(|chan| chan.try_recv()) { | ||
| 109 | self.err = Some(e.to_string()); | ||
| 110 | self.joining = None; | ||
| 111 | } | ||
| 112 | return None; | ||
| 113 | }; | ||
| 114 | |||
| 115 | if room.state() == MatrixRoomState::Left { | ||
| 116 | // room previews have more information than left rooms | ||
| 117 | return None; | ||
| 118 | } | ||
| 119 | |||
| 120 | store.application.need_load.need_members(room.room_id().to_owned()); | ||
| 121 | |||
| 122 | Some(RoomState::new(room, None, store)) | ||
| 123 | } | ||
| 124 | |||
| 125 | pub fn dup(&self) -> Self { | ||
| 126 | Self { | ||
| 127 | alias: self.alias.clone(), | ||
| 128 | room_id: self.room_id.clone(), | ||
| 129 | err: self.err.clone(), | ||
| 130 | joining: None, | ||
| 131 | term_cursor: (0, 0), | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | pub fn get_tab_title(&self, store: &ProgramStore) -> Line<'_> { | ||
| 136 | if let Some((Ok(preview), _)) = store.application.room_previews.get(self.alias()) { | ||
| 137 | if let Some(name) = &preview.name { | ||
| 138 | return Line::from(name.to_string()); | ||
| 139 | } | ||
| 140 | |||
| 141 | if let Some(alias) = &preview.canonical_alias { | ||
| 142 | return Line::from(alias.to_string()); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | Line::from(self.alias.as_str()) | ||
| 147 | } | ||
| 148 | |||
| 149 | pub fn get_title(&self, store: &ProgramStore) -> Line<'_> { | ||
| 150 | if let Some((Ok(preview), _)) = store.application.room_previews.get(self.alias()) { | ||
| 151 | if let Some(name) = &preview.name { | ||
| 152 | return Line::from(vec![ | ||
| 153 | Span::styled(name.to_string(), StyleModifier::BOLD), | ||
| 154 | Span::raw(" (unjoined)"), | ||
| 155 | ]); | ||
| 156 | } | ||
| 157 | |||
| 158 | if let Some(alias) = &preview.canonical_alias { | ||
| 159 | return Line::from(vec![ | ||
| 160 | Span::styled(alias.to_string(), StyleModifier::BOLD), | ||
| 161 | Span::raw(" (unjoined)"), | ||
| 162 | ]); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | Line::from(vec![ | ||
| 167 | Span::styled(self.alias.as_str(), StyleModifier::BOLD), | ||
| 168 | Span::raw(" (unjoined)"), | ||
| 169 | ]) | ||
| 170 | } | ||
| 171 | |||
| 172 | pub async fn join_command( | ||
| 173 | &mut self, | ||
| 174 | act: JoinAction, | ||
| 175 | ctx: ProgramContext, | ||
| 176 | store: &mut ProgramStore, | ||
| 177 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | ||
| 178 | match act { | ||
| 179 | JoinAction::Join => { | ||
| 180 | let via = store | ||
| 181 | .application | ||
| 182 | .room_via | ||
| 183 | .get(self.alias()) | ||
| 184 | .unwrap_or(&store.application.settings.tunables.default_via) | ||
| 185 | .to_vec(); | ||
| 186 | |||
| 187 | let chan = store.application.worker.join_room_chan(self.alias().to_owned(), via); | ||
| 188 | self.joining = Some(chan); | ||
| 189 | |||
| 190 | Ok(vec![]) | ||
| 191 | }, | ||
| 192 | JoinAction::Knock => { | ||
| 193 | let act = HomeserverAction::KnockSend(self.alias().to_owned(), None); | ||
| 194 | |||
| 195 | Ok(vec![(IambAction::from(act).into(), ctx)]) | ||
| 196 | }, | ||
| 197 | } | ||
| 18 | } | 198 | } |
| 19 | } | 199 | } |
| 20 | 200 | ||
| @@ -65,7 +245,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState { | |||
| 65 | 245 | ||
| 66 | impl TerminalCursor for NotJoinedState { | 246 | impl TerminalCursor for NotJoinedState { |
| 67 | fn get_term_cursor(&self) -> Option<TermOffset> { | 247 | fn get_term_cursor(&self) -> Option<TermOffset> { |
| 68 | None | 248 | self.term_cursor.into() |
| 69 | } | 249 | } |
| 70 | 250 | ||
| 71 | fn hide_term_cursor(&self) -> bool { | 251 | fn hide_term_cursor(&self) -> bool { |
| @@ -74,23 +254,93 @@ impl TerminalCursor for NotJoinedState { | |||
| 74 | } | 254 | } |
| 75 | 255 | ||
| 76 | /// [StatefulWidget] for Matrix rooms that haven't been joined. | 256 | /// [StatefulWidget] for Matrix rooms that haven't been joined. |
| 77 | pub struct NotJoined; | 257 | pub struct NotJoined<'a> { |
| 258 | store: &'a mut ProgramStore, | ||
| 259 | } | ||
| 78 | 260 | ||
| 79 | impl StatefulWidget for NotJoined { | 261 | impl<'a> NotJoined<'a> { |
| 262 | pub fn new(store: &'a mut ProgramStore) -> Self { | ||
| 263 | Self { store } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | impl StatefulWidget for NotJoined<'_> { | ||
| 80 | type State = NotJoinedState; | 268 | type State = NotJoinedState; |
| 81 | 269 | ||
| 82 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | 270 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { |
| 271 | // Ensure the whole window receives the default timeline styling: | ||
| 272 | let theme = &self.store.application.settings.theme; | ||
| 273 | let default_style = theme.timeline.default; | ||
| 274 | let err_style = default_style.fg(Color::Red); | ||
| 275 | buffer.set_style(area, default_style); | ||
| 276 | |||
| 277 | // Save the top-left corner for indicating cursor placement: | ||
| 278 | state.term_cursor = (area.left(), area.top()); | ||
| 279 | |||
| 83 | let mut lines = vec![]; | 280 | let mut lines = vec![]; |
| 84 | 281 | ||
| 85 | lines.push(Line::from(vec![ | 282 | let preview = self.store.application.room_previews.get(state.alias()); |
| 86 | Span::raw("Use "), | 283 | if let Some((_, fetched)) = &preview && |
| 87 | Span::styled(format!("`:join {}`", state.room), StyleModifier::BOLD), | 284 | fetched.elapsed() > ROOM_PREVIEW_DEBOUNCE |
| 88 | Span::raw(" to join this room"), | 285 | { |
| 89 | ])); | 286 | self.store.application.need_load.need_preview(state.alias().to_owned()); |
| 287 | } | ||
| 288 | |||
| 289 | match preview { | ||
| 290 | Some((Ok(preview), _)) => { | ||
| 291 | let mut name_line = vec![]; | ||
| 292 | if let Some(name) = &preview.name { | ||
| 293 | name_line.push(Span::styled(name, StyleModifier::BOLD)); | ||
| 294 | name_line.push(Span::raw(" ")); | ||
| 295 | } | ||
| 296 | if let Some(alias) = &preview.canonical_alias { | ||
| 297 | name_line.push(Span::raw(alias.as_str())); | ||
| 298 | name_line.push(Span::raw(" ")); | ||
| 299 | } | ||
| 300 | |||
| 301 | name_line.push(Span::raw("(")); | ||
| 302 | name_line.push(Span::raw(state.alias().as_str())); | ||
| 303 | name_line.push(Span::raw(")")); | ||
| 304 | lines.push(Line::from(name_line)); | ||
| 305 | if let Some(topic) = &preview.topic { | ||
| 306 | lines.push(Line::raw(topic)); | ||
| 307 | } | ||
| 308 | |||
| 309 | lines.push(Line::raw("")); | ||
| 310 | |||
| 311 | let (status, join_cmd) = preview_state(preview, self.store); | ||
| 312 | |||
| 313 | lines.push(Line::raw(status)); | ||
| 314 | if let Some(join_cmd) = join_cmd { | ||
| 315 | lines.push(Line::from(vec![ | ||
| 316 | Span::raw("Use `"), | ||
| 317 | Span::styled(join_cmd, StyleModifier::BOLD), | ||
| 318 | Span::raw("` to join this room"), | ||
| 319 | ])); | ||
| 320 | } | ||
| 321 | }, | ||
| 322 | Some((Err(matrix_sdk::Error::InsufficientData), _)) => { | ||
| 323 | lines.push(Line::from(Span::styled( | ||
| 324 | "This room doesn't exist or room previews are disabled", | ||
| 325 | err_style, | ||
| 326 | ))); | ||
| 327 | }, | ||
| 328 | Some((Err(err), _)) => { | ||
| 329 | lines.push(Line::from(Span::styled(err.to_string(), err_style))); | ||
| 330 | }, | ||
| 331 | None => { | ||
| 332 | self.store.application.need_load.need_preview(state.alias().to_owned()); | ||
| 333 | lines.push(Line::raw("Loading room preview...")); | ||
| 334 | }, | ||
| 335 | } | ||
| 336 | |||
| 337 | if state.joining.is_some() { | ||
| 338 | lines.push(Line::raw("")); | ||
| 339 | lines.push(Line::raw("Joining this room...")); | ||
| 340 | } | ||
| 90 | 341 | ||
| 91 | if let Some(err) = &state.err { | 342 | if let Some(err) = &state.err { |
| 92 | let style = Style::default().fg(Color::Red); | 343 | lines.push(Line::from(Span::styled(err.as_str(), err_style))); |
| 93 | lines.push(Line::from(Span::styled(err.as_str(), style))); | ||
| 94 | } | 344 | } |
| 95 | 345 | ||
| 96 | Paragraph::new(Text::from(lines)) | 346 | Paragraph::new(Text::from(lines)) |
src/windows/room/scrollback.rs +1 −1
| @@ -1021,7 +1021,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | |||
| 1021 | return Err(err); | 1021 | return Err(err); |
| 1022 | }; | 1022 | }; |
| 1023 | let room_id = self.room_id.clone(); | 1023 | let room_id = self.room_id.clone(); |
| 1024 | let id = IambId::Room(room_id, Some(root.to_owned())); | 1024 | let id = IambId::Room(room_id.into(), Some(root.to_owned())); |
| 1025 | let open = WindowAction::Switch(OpenTarget::Application(id)); | 1025 | let open = WindowAction::Switch(OpenTarget::Application(id)); |
| 1026 | Ok(vec![(open.into(), ctx.clone())]) | 1026 | Ok(vec![(open.into(), ctx.clone())]) |
| 1027 | } | 1027 | } |
src/windows/room/space.rs +5 −5
| @@ -189,8 +189,9 @@ impl StatefulWidget for Space<'_> { | |||
| 189 | type State = SpaceState; | 189 | type State = SpaceState; |
| 190 | 190 | ||
| 191 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | 191 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { |
| 192 | let ChatStore { rooms, names, worker, settings, collator, .. } = | 192 | let ChatStore { |
| 193 | &mut self.store.application; | 193 | rooms, aliases: names, worker, settings, collator, .. |
| 194 | } = &mut self.store.application; | ||
| 194 | state.set_ignorecase(settings.tunables.ignorecase); | 195 | state.set_ignorecase(settings.tunables.ignorecase); |
| 195 | 196 | ||
| 196 | let mut empty_message = None; | 197 | let mut empty_message = None; |
| @@ -207,11 +208,10 @@ impl StatefulWidget for Space<'_> { | |||
| 207 | let mut items = members | 208 | let mut items = members |
| 208 | .into_iter() | 209 | .into_iter() |
| 209 | .filter_map(|id| { | 210 | .filter_map(|id| { |
| 210 | let (room, _, tags) = worker.get_room(id.clone()).ok()?; | 211 | let (room, _) = worker.get_room(id.clone()).ok()?; |
| 211 | let room_info = std::sync::Arc::new((room, tags)); | ||
| 212 | 212 | ||
| 213 | if id != state.room_id { | 213 | if id != state.room_id { |
| 214 | Some(GenericRoomItem::new(&room_info.0, rooms, names)) | 214 | Some(GenericRoomItem::new(&room, rooms, names)) |
| 215 | } else { | 215 | } else { |
| 216 | None | 216 | None |
| 217 | } | 217 | } |
src/worker.rs +76 −30
| @@ -185,6 +185,7 @@ enum Plan { | |||
| 185 | Messages(OwnedRoomId, Vec<MessageNeed>), | 185 | Messages(OwnedRoomId, Vec<MessageNeed>), |
| 186 | Members(OwnedRoomId), | 186 | Members(OwnedRoomId), |
| 187 | Pinned(OwnedRoomId, Vec<OwnedEventId>), | 187 | Pinned(OwnedRoomId, Vec<OwnedEventId>), |
| 188 | RoomPreview(OwnedRoomOrAliasId), | ||
| 188 | } | 189 | } |
| 189 | 190 | ||
| 190 | async fn load_plans(store: &AsyncProgramStore) -> Vec<Plan> { | 191 | async fn load_plans(store: &AsyncProgramStore) -> Vec<Plan> { |
| @@ -192,6 +193,10 @@ async fn load_plans(store: &AsyncProgramStore) -> Vec<Plan> { | |||
| 192 | let ChatStore { need_load, rooms, .. } = &mut locked.application; | 193 | let ChatStore { need_load, rooms, .. } = &mut locked.application; |
| 193 | let mut plan = Vec::with_capacity(need_load.rooms() * 2); | 194 | let mut plan = Vec::with_capacity(need_load.rooms() * 2); |
| 194 | 195 | ||
| 196 | for room in need_load.preview_needs() { | ||
| 197 | plan.push(Plan::RoomPreview(room)); | ||
| 198 | } | ||
| 199 | |||
| 195 | for (room_id, need) in std::mem::take(need_load).into_iter() { | 200 | for (room_id, need) in std::mem::take(need_load).into_iter() { |
| 196 | if need.pinned { | 201 | if need.pinned { |
| 197 | let missing = rooms.get_or_default(room_id.clone()).missing_pinned(); | 202 | let missing = rooms.get_or_default(room_id.clone()).missing_pinned(); |
| @@ -256,6 +261,31 @@ async fn run_plan(client: &Client, store: &AsyncProgramStore, plan: Plan) { | |||
| 256 | info.insert_pinned(event_id, msg); | 261 | info.insert_pinned(event_id, msg); |
| 257 | } | 262 | } |
| 258 | }, | 263 | }, |
| 264 | Plan::RoomPreview(alias_id) => { | ||
| 265 | let via = { | ||
| 266 | let locked = store.lock().await; | ||
| 267 | locked | ||
| 268 | .application | ||
| 269 | .room_via | ||
| 270 | .get(&alias_id) | ||
| 271 | .unwrap_or(&locked.application.settings.tunables.default_via) | ||
| 272 | .to_vec() | ||
| 273 | }; | ||
| 274 | |||
| 275 | let res = client.get_room_preview(&alias_id, via).await; | ||
| 276 | |||
| 277 | let mut locked = store.lock().await; | ||
| 278 | |||
| 279 | if let Ok(preview) = &res && | ||
| 280 | let Some(alias) = &preview.canonical_alias | ||
| 281 | { | ||
| 282 | locked | ||
| 283 | .application | ||
| 284 | .aliases | ||
| 285 | .insert(alias.to_owned(), preview.room_id.clone()); | ||
| 286 | } | ||
| 287 | locked.application.room_previews.insert(alias_id, (res, Instant::now())); | ||
| 288 | }, | ||
| 259 | } | 289 | } |
| 260 | } | 290 | } |
| 261 | 291 | ||
| @@ -667,9 +697,11 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore, first_sync: b | |||
| 667 | 697 | ||
| 668 | let name = display.to_string(); | 698 | let name = display.to_string(); |
| 669 | let tags = room.tags().await.unwrap_or_default(); | 699 | let tags = room.tags().await.unwrap_or_default(); |
| 700 | let mut aliases = room.alt_aliases(); | ||
| 701 | aliases.extend(room.canonical_alias()); | ||
| 670 | 702 | ||
| 671 | pinned.push((room.room_id().to_owned(), room.pinned_event_ids().unwrap_or_default())); | 703 | pinned.push((room.room_id().to_owned(), room.pinned_event_ids().unwrap_or_default())); |
| 672 | names_and_tags.push((room.room_id().to_owned(), name, tags)); | 704 | names_and_tags.push((room.room_id().to_owned(), name, tags, aliases)); |
| 673 | 705 | ||
| 674 | if room.is_direct().await.unwrap_or_default() { | 706 | if room.is_direct().await.unwrap_or_default() { |
| 675 | dms.push(room); | 707 | dms.push(room); |
| @@ -685,8 +717,8 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore, first_sync: b | |||
| 685 | locked.application.sync_info.rooms = rooms; | 717 | locked.application.sync_info.rooms = rooms; |
| 686 | locked.application.sync_info.dms = dms; | 718 | locked.application.sync_info.dms = dms; |
| 687 | 719 | ||
| 688 | for (room_id, name, tags) in names_and_tags { | 720 | for (room_id, name, tags, aliases) in names_and_tags { |
| 689 | locked.application.set_room_info(room_id, name, tags); | 721 | locked.application.set_room_info(room_id, name, tags, aliases); |
| 690 | } | 722 | } |
| 691 | 723 | ||
| 692 | for (room_id, pinned_events) in pinned { | 724 | for (room_id, pinned_events) in pinned { |
| @@ -977,7 +1009,7 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) { | |||
| 977 | return (reply, response); | 1009 | return (reply, response); |
| 978 | } | 1010 | } |
| 979 | 1011 | ||
| 980 | pub type FetchedRoom = (MatrixRoom, RoomDisplayName, Option<Tags>); | 1012 | pub type FetchedRoom = (MatrixRoom, RoomDisplayName); |
| 981 | 1013 | ||
| 982 | pub enum WorkerTask { | 1014 | pub enum WorkerTask { |
| 983 | Init(AsyncProgramStore, ClientReply<()>), | 1015 | Init(AsyncProgramStore, ClientReply<()>), |
| @@ -986,7 +1018,8 @@ pub enum WorkerTask { | |||
| 986 | GetInviter(MatrixRoom, ClientReply<IambResult<Option<RoomMember>>>), | 1018 | GetInviter(MatrixRoom, ClientReply<IambResult<Option<RoomMember>>>), |
| 987 | GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>), | 1019 | GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>), |
| 988 | ResolveAlias(OwnedRoomAliasId, ClientReply<IambResult<OwnedRoomId>>), | 1020 | ResolveAlias(OwnedRoomAliasId, ClientReply<IambResult<OwnedRoomId>>), |
| 989 | JoinRoom(String, Vec<OwnedServerName>, ClientReply<IambResult<OwnedRoomId>>), | 1021 | JoinRoom(OwnedRoomOrAliasId, Vec<OwnedServerName>, ClientReply<IambResult<OwnedRoomId>>), |
| 1022 | CreateDM(OwnedUserId, ClientReply<IambResult<OwnedRoomId>>), | ||
| 990 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), | 1023 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), |
| 991 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), | 1024 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), |
| 992 | TypingNotice(OwnedRoomId), | 1025 | TypingNotice(OwnedRoomId), |
| @@ -1033,6 +1066,12 @@ impl Debug for WorkerTask { | |||
| 1033 | .field(&format_args!("_")) | 1066 | .field(&format_args!("_")) |
| 1034 | .finish() | 1067 | .finish() |
| 1035 | }, | 1068 | }, |
| 1069 | WorkerTask::CreateDM(user_id, _) => { | ||
| 1070 | f.debug_tuple("WorkerTask::CreateDM") | ||
| 1071 | .field(user_id) | ||
| 1072 | .field(&format_args!("_")) | ||
| 1073 | .finish() | ||
| 1074 | }, | ||
| 1036 | WorkerTask::Members(room_id, _) => { | 1075 | WorkerTask::Members(room_id, _) => { |
| 1037 | f.debug_tuple("WorkerTask::Members") | 1076 | f.debug_tuple("WorkerTask::Members") |
| 1038 | .field(room_id) | 1077 | .field(room_id) |
| @@ -1156,7 +1195,7 @@ pub async fn create_client(settings: &ApplicationSettings) -> Client { | |||
| 1156 | client | 1195 | client |
| 1157 | } | 1196 | } |
| 1158 | 1197 | ||
| 1159 | async fn direct_message(user: OwnedUserId, client: Client) -> IambResult<OwnedRoomId> { | 1198 | async fn direct_message(user: OwnedUserId, client: &Client) -> IambResult<OwnedRoomId> { |
| 1160 | if let Some(room) = client.get_dm_room(&user) { | 1199 | if let Some(room) = client.get_dm_room(&user) { |
| 1161 | return Ok(room.room_id().to_owned()); | 1200 | return Ok(room.room_id().to_owned()); |
| 1162 | } | 1201 | } |
| @@ -1178,25 +1217,17 @@ async fn direct_message(user: OwnedUserId, client: Client) -> IambResult<OwnedRo | |||
| 1178 | } | 1217 | } |
| 1179 | 1218 | ||
| 1180 | async fn join_room( | 1219 | async fn join_room( |
| 1181 | name: String, | 1220 | alias_id: OwnedRoomOrAliasId, |
| 1182 | via: Vec<OwnedServerName>, | 1221 | via: Vec<OwnedServerName>, |
| 1183 | client: Client, | 1222 | client: Client, |
| 1184 | ) -> IambResult<OwnedRoomId> { | 1223 | ) -> IambResult<OwnedRoomId> { |
| 1185 | if let Ok(alias_id) = OwnedRoomOrAliasId::from_str(name.as_str()) { | 1224 | match client.join_room_by_id_or_alias(&alias_id, &via).await { |
| 1186 | match client.join_room_by_id_or_alias(&alias_id, &via).await { | 1225 | Ok(resp) => Ok(resp.room_id().to_owned()), |
| 1187 | Ok(resp) => Ok(resp.room_id().to_owned()), | 1226 | Err(e) => { |
| 1188 | Err(e) => { | 1227 | let msg = e.to_string(); |
| 1189 | let msg = e.to_string(); | 1228 | let err = UIError::Failure(msg); |
| 1190 | let err = UIError::Failure(msg); | 1229 | return Err(err); |
| 1191 | return Err(err); | 1230 | }, |
| 1192 | }, | ||
| 1193 | } | ||
| 1194 | } else if let Ok(user) = OwnedUserId::try_from(name.as_str()) { | ||
| 1195 | direct_message(user, client).await | ||
| 1196 | } else { | ||
| 1197 | let msg = format!("{:?} is not a valid room or user name", name.as_str()); | ||
| 1198 | let err = UIError::Failure(msg); | ||
| 1199 | return Err(err); | ||
| 1200 | } | 1231 | } |
| 1201 | } | 1232 | } |
| 1202 | 1233 | ||
| @@ -1277,16 +1308,28 @@ impl Requester { | |||
| 1277 | 1308 | ||
| 1278 | pub fn join_room_chan( | 1309 | pub fn join_room_chan( |
| 1279 | &self, | 1310 | &self, |
| 1280 | name: String, | 1311 | alias_id: OwnedRoomOrAliasId, |
| 1281 | via: Vec<OwnedServerName>, | 1312 | via: Vec<OwnedServerName>, |
| 1282 | ) -> ClientResponse<IambResult<OwnedRoomId>> { | 1313 | ) -> ClientResponse<IambResult<OwnedRoomId>> { |
| 1283 | let (reply, response) = oneshot(); | 1314 | let (reply, response) = oneshot(); |
| 1284 | self.tx.send(WorkerTask::JoinRoom(name, via, reply)).unwrap(); | 1315 | self.tx.send(WorkerTask::JoinRoom(alias_id, via, reply)).unwrap(); |
| 1285 | response | 1316 | response |
| 1286 | } | 1317 | } |
| 1287 | 1318 | ||
| 1288 | pub fn join_room(&self, name: String, via: Vec<OwnedServerName>) -> IambResult<OwnedRoomId> { | 1319 | pub fn join_room( |
| 1289 | self.join_room_chan(name, via).recv() | 1320 | &self, |
| 1321 | alias_id: OwnedRoomOrAliasId, | ||
| 1322 | via: Vec<OwnedServerName>, | ||
| 1323 | ) -> IambResult<OwnedRoomId> { | ||
| 1324 | self.join_room_chan(alias_id, via).recv() | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | pub fn create_dm(&self, user_id: OwnedUserId) -> IambResult<OwnedRoomId> { | ||
| 1328 | let (reply, response) = oneshot(); | ||
| 1329 | |||
| 1330 | self.tx.send(WorkerTask::CreateDM(user_id, reply)).unwrap(); | ||
| 1331 | |||
| 1332 | return response.recv(); | ||
| 1290 | } | 1333 | } |
| 1291 | 1334 | ||
| 1292 | pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { | 1335 | pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { |
| @@ -1387,10 +1430,14 @@ impl ClientWorker { | |||
| 1387 | assert!(self.initialized); | 1430 | assert!(self.initialized); |
| 1388 | reply.send(self.resolve_alias(alias_id).await); | 1431 | reply.send(self.resolve_alias(alias_id).await); |
| 1389 | }, | 1432 | }, |
| 1390 | WorkerTask::JoinRoom(name, via, reply) => { | 1433 | WorkerTask::JoinRoom(alias_id, via, reply) => { |
| 1391 | assert!(self.initialized); | 1434 | assert!(self.initialized); |
| 1392 | let client = self.client.clone(); | 1435 | let client = self.client.clone(); |
| 1393 | tokio::spawn(async move { reply.send(join_room(name, via, client).await) }); | 1436 | tokio::spawn(async move { reply.send(join_room(alias_id, via, client).await) }); |
| 1437 | }, | ||
| 1438 | WorkerTask::CreateDM(user_id, reply) => { | ||
| 1439 | assert!(self.initialized); | ||
| 1440 | reply.send(direct_message(user_id, &self.client).await); | ||
| 1394 | }, | 1441 | }, |
| 1395 | WorkerTask::GetInviter(invited, reply) => { | 1442 | WorkerTask::GetInviter(invited, reply) => { |
| 1396 | assert!(self.initialized); | 1443 | assert!(self.initialized); |
| @@ -2002,9 +2049,8 @@ impl ClientWorker { | |||
| 2002 | } else { | 2049 | } else { |
| 2003 | room.display_name().await.map_err(IambError::from)? | 2050 | room.display_name().await.map_err(IambError::from)? |
| 2004 | }; | 2051 | }; |
| 2005 | let tags = room.tags().await.map_err(IambError::from)?; | ||
| 2006 | 2052 | ||
| 2007 | Ok((room, name, tags)) | 2053 | Ok((room, name)) |
| 2008 | } else { | 2054 | } else { |
| 2009 | Err(IambError::UnknownRoom(room_id).into()) | 2055 | Err(IambError::UnknownRoom(room_id).into()) |
| 2010 | } | 2056 | } |