Commit fa7e45088e
Unregistered key
Layout: unified · split
src/base.rs +74
| @@ -42,6 +42,7 @@ use serde::de::Error as SerdeError; | |||
| 42 | use serde::de::Visitor; | 42 | use serde::de::Visitor; |
| 43 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; | 43 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 44 | use tokio::sync::Mutex as AsyncMutex; | 44 | use tokio::sync::Mutex as AsyncMutex; |
| 45 | use url::form_urlencoded; | ||
| 45 | 46 | ||
| 46 | use crate::notifications::NotificationHandle; | 47 | use crate::notifications::NotificationHandle; |
| 47 | use crate::prelude::*; | 48 | use crate::prelude::*; |
| @@ -1990,6 +1991,12 @@ pub enum IambId { | |||
| 1990 | /// A Matrix room, with an optional thread to show. | 1991 | /// A Matrix room, with an optional thread to show. |
| 1991 | Room(OwnedRoomId, Option<OwnedEventId>), | 1992 | Room(OwnedRoomId, Option<OwnedEventId>), |
| 1992 | 1993 | ||
| 1994 | /// A Matrix room that we're currently in the middle of joining. | ||
| 1995 | Joining(String), | ||
| 1996 | |||
| 1997 | /// A Matrix room that we haven't joined, and aren't currently joining. | ||
| 1998 | NotJoined(String), | ||
| 1999 | |||
| 1993 | /// The `:dms` window. | 2000 | /// The `:dms` window. |
| 1994 | DirectList, | 2001 | DirectList, |
| 1995 | 2002 | ||
| @@ -2021,6 +2028,24 @@ pub enum IambId { | |||
| 2021 | InvitesList, | 2028 | InvitesList, |
| 2022 | } | 2029 | } |
| 2023 | 2030 | ||
| 2031 | /// Encode the room name for a [IambId::Joining] or [IambId::NotJoined] window URL. | ||
| 2032 | /// | ||
| 2033 | /// Note that since these names come straight from the user's argument to `:join`, | ||
| 2034 | /// they are likely room aliases containing characters like `#` that we should | ||
| 2035 | /// escape before putting them into the URL, so we can later reparse it. They can | ||
| 2036 | /// technically contain anything that the user tried to pass to `:join`. | ||
| 2037 | fn room_query(room: &str) -> String { | ||
| 2038 | form_urlencoded::Serializer::new(String::new()) | ||
| 2039 | .append_pair("room", room) | ||
| 2040 | .finish() | ||
| 2041 | } | ||
| 2042 | |||
| 2043 | /// Pull the room name back out of the query parameter for `iamb://joining` or | ||
| 2044 | /// `iamb://not-joined`. | ||
| 2045 | fn query_room(url: &Url) -> Option<String> { | ||
| 2046 | url.query_pairs().find_map(|(k, v)| (k == "room").then(|| v.into_owned())) | ||
| 2047 | } | ||
| 2048 | |||
| 2024 | impl Display for IambId { | 2049 | impl Display for IambId { |
| 2025 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 2050 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2026 | match self { | 2051 | match self { |
| @@ -2030,6 +2055,12 @@ impl Display for IambId { | |||
| 2030 | IambId::Room(room_id, Some(thread)) => { | 2055 | IambId::Room(room_id, Some(thread)) => { |
| 2031 | write!(f, "iamb://room/{room_id}/threads/{thread}") | 2056 | write!(f, "iamb://room/{room_id}/threads/{thread}") |
| 2032 | }, | 2057 | }, |
| 2058 | IambId::Joining(room) => { | ||
| 2059 | write!(f, "iamb://joining?{}", room_query(room)) | ||
| 2060 | }, | ||
| 2061 | IambId::NotJoined(room) => { | ||
| 2062 | write!(f, "iamb://not-joined?{}", room_query(room)) | ||
| 2063 | }, | ||
| 2033 | IambId::MemberList(room_id) => { | 2064 | IambId::MemberList(room_id) => { |
| 2034 | write!(f, "iamb://members/{room_id}") | 2065 | write!(f, "iamb://members/{room_id}") |
| 2035 | }, | 2066 | }, |
| @@ -2116,6 +2147,20 @@ impl Visitor<'_> for IambIdVisitor { | |||
| 2116 | _ => return Err(E::custom("Invalid members window URL")), | 2147 | _ => return Err(E::custom("Invalid members window URL")), |
| 2117 | } | 2148 | } |
| 2118 | }, | 2149 | }, |
| 2150 | Some("joining") => { | ||
| 2151 | let Some(room) = query_room(&url) else { | ||
| 2152 | return Err(E::custom("iamb://joining requires a room parameter")); | ||
| 2153 | }; | ||
| 2154 | |||
| 2155 | Ok(IambId::Joining(room)) | ||
| 2156 | }, | ||
| 2157 | Some("not-joined") => { | ||
| 2158 | let Some(room) = query_room(&url) else { | ||
| 2159 | return Err(E::custom("iamb://not-joined requires a room parameter")); | ||
| 2160 | }; | ||
| 2161 | |||
| 2162 | Ok(IambId::NotJoined(room)) | ||
| 2163 | }, | ||
| 2119 | Some("members") => { | 2164 | Some("members") => { |
| 2120 | let Some(path) = url.path_segments() else { | 2165 | let Some(path) = url.path_segments() else { |
| 2121 | return Err(E::custom("Invalid members window URL")); | 2166 | return Err(E::custom("Invalid members window URL")); |
| @@ -2460,6 +2505,35 @@ pub mod tests { | |||
| 2460 | ); | 2505 | ); |
| 2461 | } | 2506 | } |
| 2462 | 2507 | ||
| 2508 | #[test] | ||
| 2509 | fn test_unjoined_window_ids() { | ||
| 2510 | // Room names come from the user can contain characters that need escaping: | ||
| 2511 | for name in [ | ||
| 2512 | "#foo:example.com", | ||
| 2513 | "!abc123:example.com", | ||
| 2514 | "@user:example.com", | ||
| 2515 | "a b&c=d?e#f", | ||
| 2516 | ] { | ||
| 2517 | for id in [IambId::Joining(name.into()), IambId::NotJoined(name.into())] { | ||
| 2518 | let json = serde_json::to_string(&id).unwrap(); | ||
| 2519 | assert_eq!(serde_json::from_str::<IambId>(&json).unwrap(), id); | ||
| 2520 | } | ||
| 2521 | } | ||
| 2522 | |||
| 2523 | assert_eq!( | ||
| 2524 | IambId::Joining("#foo:example.com".into()).to_string(), | ||
| 2525 | "iamb://joining?room=%23foo%3Aexample.com" | ||
| 2526 | ); | ||
| 2527 | assert_eq!( | ||
| 2528 | IambId::NotJoined("#foo:example.com".into()).to_string(), | ||
| 2529 | "iamb://not-joined?room=%23foo%3Aexample.com" | ||
| 2530 | ); | ||
| 2531 | |||
| 2532 | // A window URL without a room name isn't valid and wasn't written by us: | ||
| 2533 | assert!(serde_json::from_str::<IambId>("\"iamb://joining\"").is_err()); | ||
| 2534 | assert!(serde_json::from_str::<IambId>("\"iamb://not-joined\"").is_err()); | ||
| 2535 | } | ||
| 2536 | |||
| 2463 | #[test] | 2537 | #[test] |
| 2464 | fn test_need_load() { | 2538 | fn test_need_load() { |
| 2465 | let room_id = TEST_ROOM1_ID.clone(); | 2539 | let room_id = TEST_ROOM1_ID.clone(); |
src/windows/mod.rs +15 −23
| @@ -368,7 +368,7 @@ impl IambWindow { | |||
| 368 | store: &mut ProgramStore, | 368 | store: &mut ProgramStore, |
| 369 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { | 369 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { |
| 370 | let id = match self { | 370 | let id = match self { |
| 371 | IambWindow::Room(state) => Some(state.id()), | 371 | IambWindow::Room(state) => state.id(), |
| 372 | IambWindow::MemberList(_, room_id, _) => Some(&**room_id), | 372 | IambWindow::MemberList(_, room_id, _) => Some(&**room_id), |
| 373 | 373 | ||
| 374 | IambWindow::DirectList(state) => state.get().map(|state| state.room_id()), | 374 | IambWindow::DirectList(state) => state.get().map(|state| state.room_id()), |
| @@ -817,7 +817,7 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 817 | impl Window<IambInfo> for IambWindow { | 817 | impl Window<IambInfo> for IambWindow { |
| 818 | fn id(&self) -> IambId { | 818 | fn id(&self) -> IambId { |
| 819 | match self { | 819 | match self { |
| 820 | IambWindow::Room(room) => IambId::Room(room.id().to_owned(), room.thread().cloned()), | 820 | IambWindow::Room(room) => room.window_id(), |
| 821 | IambWindow::DirectList(_) => IambId::DirectList, | 821 | IambWindow::DirectList(_) => IambId::DirectList, |
| 822 | IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()), | 822 | IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()), |
| 823 | IambWindow::RoomList(_) => IambId::RoomList, | 823 | IambWindow::RoomList(_) => IambId::RoomList, |
| @@ -843,11 +843,7 @@ impl Window<IambInfo> for IambWindow { | |||
| 843 | IambWindow::MentionsList(_) => bold_spans("Unread Mentions"), | 843 | IambWindow::MentionsList(_) => bold_spans("Unread Mentions"), |
| 844 | IambWindow::InvitesList(_) => bold_spans("Open Invites"), | 844 | IambWindow::InvitesList(_) => bold_spans("Open Invites"), |
| 845 | 845 | ||
| 846 | IambWindow::Room(w) => { | 846 | IambWindow::Room(w) => w.get_tab_title(store), |
| 847 | let title = store.application.get_room_title(w.id()); | ||
| 848 | |||
| 849 | Line::from(title) | ||
| 850 | }, | ||
| 851 | IambWindow::MemberList(state, room_id, _) => { | 847 | IambWindow::MemberList(state, room_id, _) => { |
| 852 | let title = store.application.get_room_title(room_id.as_ref()); | 848 | let title = store.application.get_room_title(room_id.as_ref()); |
| 853 | let n = state.len(); | 849 | let n = state.len(); |
| @@ -893,9 +889,18 @@ impl Window<IambInfo> for IambWindow { | |||
| 893 | let (room, name, tags) = store.application.worker.get_room(room_id)?; | 889 | let (room, name, tags) = store.application.worker.get_room(room_id)?; |
| 894 | let room = RoomState::new(room, thread, name, tags, store); | 890 | let room = RoomState::new(room, thread, name, tags, store); |
| 895 | 891 | ||
| 896 | store.application.need_load.need_members(room.id().to_owned()); | 892 | if let Some(room_id) = room.id() { |
| 893 | store.application.need_load.need_members(room_id.to_owned()); | ||
| 894 | } | ||
| 895 | |||
| 897 | return Ok(room.into()); | 896 | return Ok(room.into()); |
| 898 | }, | 897 | }, |
| 898 | IambId::Joining(name) => { | ||
| 899 | return Ok(RoomState::join(name, store).into()); | ||
| 900 | }, | ||
| 901 | IambId::NotJoined(name) => { | ||
| 902 | return Ok(RoomState::not_joined(name).into()); | ||
| 903 | }, | ||
| 899 | IambId::DirectList => { | 904 | IambId::DirectList => { |
| 900 | let list = DirectListState::new(IambBufferId::DirectList, vec![]); | 905 | let list = DirectListState::new(IambBufferId::DirectList, vec![]); |
| 901 | 906 | ||
| @@ -952,24 +957,11 @@ impl Window<IambInfo> for IambWindow { | |||
| 952 | } | 957 | } |
| 953 | 958 | ||
| 954 | fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> { | 959 | fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> { |
| 955 | let ChatStore { names, worker, settings, .. } = &mut store.application; | 960 | if let Some(room) = store.application.names.get(&name) { |
| 956 | |||
| 957 | if let Some(room) = names.get_mut(&name) { | ||
| 958 | let id = IambId::Room(room.clone(), None); | 961 | let id = IambId::Room(room.clone(), None); |
| 959 | |||
| 960 | IambWindow::open(id, store) | 962 | IambWindow::open(id, store) |
| 961 | } else { | 963 | } else { |
| 962 | let via = settings.tunables.default_via.clone(); | 964 | let room = RoomState::join(name, store); |
| 963 | let room_id = worker.join_room(name.clone(), via)?; | ||
| 964 | |||
| 965 | if let Ok(alias) = OwnedRoomAliasId::from_str(&name) { | ||
| 966 | names.insert(alias, room_id.clone()); | ||
| 967 | } | ||
| 968 | |||
| 969 | let (room, name, tags) = store.application.worker.get_room(room_id)?; | ||
| 970 | let room = RoomState::new(room, None, name, tags, store); | ||
| 971 | |||
| 972 | store.application.need_load.need_members(room.id().to_owned()); | ||
| 973 | Ok(room.into()) | 965 | Ok(room.into()) |
| 974 | } | 966 | } |
| 975 | } | 967 | } |
src/windows/room/joining.rs added +98
| @@ -0,0 +1,98 @@ | |||
| 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 +144 −40
| @@ -19,15 +19,21 @@ use crate::base::{MemberUpdateAction, RoomField}; | |||
| 19 | use crate::config::EncryptionIndicatorLocation; | 19 | use crate::config::EncryptionIndicatorLocation; |
| 20 | use crate::prelude::*; | 20 | use crate::prelude::*; |
| 21 | use crate::windows::room::chat::ChatState; | 21 | use crate::windows::room::chat::ChatState; |
| 22 | use crate::windows::room::joining::{Joining, JoiningState}; | ||
| 23 | use crate::windows::room::not_joined::{NotJoined, NotJoinedState}; | ||
| 22 | use crate::windows::room::space::{Space, SpaceState}; | 24 | use crate::windows::room::space::{Space, SpaceState}; |
| 23 | 25 | ||
| 24 | mod chat; | 26 | mod chat; |
| 27 | mod joining; | ||
| 28 | mod not_joined; | ||
| 25 | mod scrollback; | 29 | mod scrollback; |
| 26 | mod space; | 30 | mod space; |
| 27 | 31 | ||
| 28 | macro_rules! delegate { | 32 | macro_rules! delegate { |
| 29 | ($s: expr, $id: ident => $e: expr) => { | 33 | ($s: expr, $id: ident => $e: expr) => { |
| 30 | match $s { | 34 | match $s { |
| 35 | RoomState::NotJoined($id) => $e, | ||
| 36 | RoomState::Joining($id) => $e, | ||
| 31 | RoomState::Chat($id) => $e, | 37 | RoomState::Chat($id) => $e, |
| 32 | RoomState::Space($id) => $e, | 38 | RoomState::Space($id) => $e, |
| 33 | } | 39 | } |
| @@ -640,10 +646,24 @@ pub async fn room_command( | |||
| 640 | /// that operations like sending and accepting invites, opening the members window, etc., all work | 646 | /// that operations like sending and accepting invites, opening the members window, etc., all work |
| 641 | /// similarly. | 647 | /// similarly. |
| 642 | pub enum RoomState { | 648 | pub enum RoomState { |
| 649 | NotJoined(Box<NotJoinedState>), | ||
| 650 | Joining(Box<JoiningState>), | ||
| 643 | Chat(Box<ChatState>), | 651 | Chat(Box<ChatState>), |
| 644 | Space(Box<SpaceState>), | 652 | Space(Box<SpaceState>), |
| 645 | } | 653 | } |
| 646 | 654 | ||
| 655 | impl From<NotJoinedState> for RoomState { | ||
| 656 | fn from(chat: NotJoinedState) -> Self { | ||
| 657 | RoomState::NotJoined(Box::new(chat)) | ||
| 658 | } | ||
| 659 | } | ||
| 660 | |||
| 661 | impl From<JoiningState> for RoomState { | ||
| 662 | fn from(chat: JoiningState) -> Self { | ||
| 663 | RoomState::Joining(Box::new(chat)) | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 647 | impl From<ChatState> for RoomState { | 667 | impl From<ChatState> for RoomState { |
| 648 | fn from(chat: ChatState) -> Self { | 668 | fn from(chat: ChatState) -> Self { |
| 649 | RoomState::Chat(Box::new(chat)) | 669 | RoomState::Chat(Box::new(chat)) |
| @@ -676,15 +696,64 @@ impl RoomState { | |||
| 676 | } | 696 | } |
| 677 | } | 697 | } |
| 678 | 698 | ||
| 679 | pub fn thread(&self) -> Option<&OwnedEventId> { | 699 | pub fn join(name: String, store: &mut ProgramStore) -> Self { |
| 700 | let joining = JoiningState::new(name, store); | ||
| 701 | Self::from(joining) | ||
| 702 | } | ||
| 703 | |||
| 704 | pub fn not_joined(name: String) -> Self { | ||
| 705 | Self::from(NotJoinedState::new(name)) | ||
| 706 | } | ||
| 707 | |||
| 708 | pub fn window_id(&self) -> IambId { | ||
| 709 | match self { | ||
| 710 | RoomState::NotJoined(nj) => IambId::NotJoined(nj.room.clone()), | ||
| 711 | RoomState::Joining(joining) => IambId::Joining(joining.room.clone()), | ||
| 712 | RoomState::Chat(chat) => IambId::Room(chat.id().to_owned(), chat.thread().cloned()), | ||
| 713 | RoomState::Space(space) => IambId::Room(space.id().to_owned(), None), | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | pub fn room_state(&self) -> Option<MatrixRoomState> { | ||
| 680 | match self { | 718 | match self { |
| 681 | RoomState::Chat(chat) => chat.thread(), | 719 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, |
| 682 | RoomState::Space(_) => None, | 720 | RoomState::Chat(chat) => Some(chat.room().state()), |
| 721 | RoomState::Space(space) => Some(space.room().state()), | ||
| 683 | } | 722 | } |
| 684 | } | 723 | } |
| 685 | 724 | ||
| 686 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { | 725 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { |
| 687 | match self { | 726 | match self { |
| 727 | RoomState::NotJoined(_) => {}, | ||
| 728 | RoomState::Joining(joining) => { | ||
| 729 | let joined = joining.room.clone(); | ||
| 730 | |||
| 731 | let room_id = match joining.try_recv() { | ||
| 732 | None => return, | ||
| 733 | Some(Ok(room_id)) => room_id.clone(), | ||
| 734 | Some(Err(e)) => { | ||
| 735 | // We failed to join the room, so show the NotJoined window, | ||
| 736 | // and let the user try again if the error was transient: | ||
| 737 | *self = NotJoinedState::failed(joined, e).into(); | ||
| 738 | return; | ||
| 739 | }, | ||
| 740 | }; | ||
| 741 | |||
| 742 | let Ok((room, name, tags)) = store.application.worker.get_room(room_id.clone()) | ||
| 743 | else { | ||
| 744 | return; | ||
| 745 | }; | ||
| 746 | |||
| 747 | if let Ok(alias) = OwnedRoomAliasId::try_from(joined.as_str()) { | ||
| 748 | // If the `:join` was for a room alias, then track it so | ||
| 749 | // we can reuse it later for `:join`/`:sp`/etc: | ||
| 750 | store.application.names.insert(alias, room_id.clone()); | ||
| 751 | } | ||
| 752 | |||
| 753 | store.application.need_load.need_members(room_id); | ||
| 754 | |||
| 755 | *self = RoomState::new(room, None, name, tags, store); | ||
| 756 | }, | ||
| 688 | RoomState::Chat(chat) => chat.refresh_room(store), | 757 | RoomState::Chat(chat) => chat.refresh_room(store), |
| 689 | RoomState::Space(space) => space.refresh_room(store), | 758 | RoomState::Space(space) => space.refresh_room(store), |
| 690 | } | 759 | } |
| @@ -698,16 +767,17 @@ impl RoomState { | |||
| 698 | store: &mut ProgramStore, | 767 | store: &mut ProgramStore, |
| 699 | ) { | 768 | ) { |
| 700 | let inviter = store.application.worker.get_inviter(invited.clone()); | 769 | let inviter = store.application.worker.get_inviter(invited.clone()); |
| 770 | let room_id = invited.room_id(); | ||
| 701 | 771 | ||
| 702 | let name = match invited.canonical_alias() { | 772 | let name = match invited.canonical_alias() { |
| 703 | Some(alias) => alias.to_string(), | 773 | Some(alias) => alias.to_string(), |
| 704 | None => format!("{:?}", store.application.get_room_title(self.id())), | 774 | None => format!("{:?}", store.application.get_room_title(room_id)), |
| 705 | }; | 775 | }; |
| 706 | 776 | ||
| 707 | let mut invited = vec![Span::from(format!("You have been invited to join {name}"))]; | 777 | let mut invited = vec![Span::from(format!("You have been invited to join {name}"))]; |
| 708 | 778 | ||
| 709 | if let Ok(Some(inviter)) = &inviter { | 779 | if let Ok(Some(inviter)) = &inviter { |
| 710 | let info = store.application.rooms.get_or_default(self.id().to_owned()); | 780 | let info = store.application.rooms.get_or_default(room_id.to_owned()); |
| 711 | invited.push(Span::from(" by ")); | 781 | invited.push(Span::from(" by ")); |
| 712 | invited.push(store.application.settings.get_user_span(inviter.user_id(), info)); | 782 | invited.push(store.application.settings.get_user_span(inviter.user_id(), info)); |
| 713 | } | 783 | } |
| @@ -732,7 +802,7 @@ impl RoomState { | |||
| 732 | ) { | 802 | ) { |
| 733 | let name = match knocked.canonical_alias() { | 803 | let name = match knocked.canonical_alias() { |
| 734 | Some(alias) => alias.to_string(), | 804 | Some(alias) => alias.to_string(), |
| 735 | None => format!("{:?}", store.application.get_room_title(self.id())), | 805 | None => format!("{:?}", store.application.get_room_title(knocked.room_id())), |
| 736 | }; | 806 | }; |
| 737 | 807 | ||
| 738 | let l1 = Line::from(format!( | 808 | let l1 = Line::from(format!( |
| @@ -749,7 +819,7 @@ impl RoomState { | |||
| 749 | fn draw_left(&self, room: &MatrixRoom, area: Rect, buf: &mut Buffer, store: &mut ProgramStore) { | 819 | fn draw_left(&self, room: &MatrixRoom, area: Rect, buf: &mut Buffer, store: &mut ProgramStore) { |
| 750 | let name = match room.canonical_alias() { | 820 | let name = match room.canonical_alias() { |
| 751 | Some(alias) => alias.to_string(), | 821 | Some(alias) => alias.to_string(), |
| 752 | None => format!("{:?}", store.application.get_room_title(self.id())), | 822 | None => format!("{:?}", store.application.get_room_title(room.room_id())), |
| 753 | }; | 823 | }; |
| 754 | 824 | ||
| 755 | let mut lines = vec![Line::from(format!("You have left {name}!"))]; | 825 | let mut lines = vec![Line::from(format!("You have left {name}!"))]; |
| @@ -771,9 +841,10 @@ impl RoomState { | |||
| 771 | ctx: ProgramContext, | 841 | ctx: ProgramContext, |
| 772 | store: &mut ProgramStore, | 842 | store: &mut ProgramStore, |
| 773 | ) -> IambResult<EditInfo> { | 843 | ) -> IambResult<EditInfo> { |
| 774 | match self { | 844 | if let RoomState::Chat(chat) = self { |
| 775 | RoomState::Chat(chat) => chat.message_command(act, ctx, store).await, | 845 | chat.message_command(act, ctx, store).await |
| 776 | RoomState::Space(_) => Err(IambError::NoSelectedMessage.into()), | 846 | } else { |
| 847 | Err(IambError::NoSelectedMessage.into()) | ||
| 777 | } | 848 | } |
| 778 | } | 849 | } |
| 779 | 850 | ||
| @@ -783,9 +854,10 @@ impl RoomState { | |||
| 783 | ctx: ProgramContext, | 854 | ctx: ProgramContext, |
| 784 | store: &mut ProgramStore, | 855 | store: &mut ProgramStore, |
| 785 | ) -> IambResult<EditInfo> { | 856 | ) -> IambResult<EditInfo> { |
| 786 | match self { | 857 | if let RoomState::Space(space) = self { |
| 787 | RoomState::Space(space) => space.space_command(act, ctx, store).await, | 858 | space.space_command(act, ctx, store).await |
| 788 | RoomState::Chat(_) => Err(IambError::NoSelectedSpace.into()), | 859 | } else { |
| 860 | Err(IambError::NoSelectedSpace.into()) | ||
| 789 | } | 861 | } |
| 790 | } | 862 | } |
| 791 | 863 | ||
| @@ -795,26 +867,28 @@ impl RoomState { | |||
| 795 | ctx: ProgramContext, | 867 | ctx: ProgramContext, |
| 796 | store: &mut ProgramStore, | 868 | store: &mut ProgramStore, |
| 797 | ) -> IambResult<EditInfo> { | 869 | ) -> IambResult<EditInfo> { |
| 798 | match self { | 870 | if let RoomState::Chat(chat) = self { |
| 799 | RoomState::Chat(chat) => chat.send_command(act, ctx, store).await, | 871 | chat.send_command(act, ctx, store).await |
| 800 | RoomState::Space(_) => Err(IambError::NoSelectedRoom.into()), | 872 | } else { |
| 873 | Err(IambError::NoSelectedRoom.into()) | ||
| 801 | } | 874 | } |
| 802 | } | 875 | } |
| 803 | 876 | ||
| 804 | pub fn get_title(&self, store: &mut ProgramStore) -> Line<'_> { | 877 | pub fn get_title(&self, store: &mut ProgramStore) -> Line<'_> { |
| 805 | let room = store.application.worker.client.get_room(self.id()); | 878 | let Some(room) = self.room() else { |
| 879 | return Line::from("Unjoined Room"); | ||
| 880 | }; | ||
| 806 | 881 | ||
| 807 | let title = store.application.get_room_title(self.id()); | 882 | let room_id = room.room_id(); |
| 883 | let title = store.application.get_room_title(room_id); | ||
| 808 | let style = Style::default().add_modifier(StyleModifier::BOLD); | 884 | let style = Style::default().add_modifier(StyleModifier::BOLD); |
| 809 | let mut spans = vec![]; | 885 | let mut spans = vec![]; |
| 810 | 886 | ||
| 811 | if let Some(room) = room { | 887 | let encryption_settings = &store.application.settings.tunables.encryption; |
| 812 | let encryption_settings = &store.application.settings.tunables.encryption; | 888 | let encryption_indicator = encryption_settings |
| 813 | let encryption_indicator = encryption_settings | 889 | .get_indicator(EncryptionIndicatorLocation::TITLE, room.encryption_state()); |
| 814 | .get_indicator(EncryptionIndicatorLocation::TITLE, room.encryption_state()); | 890 | spans.extend(encryption_indicator); |
| 815 | spans.extend(encryption_indicator); | 891 | spans.push(Span::raw(" ")); |
| 816 | spans.push(Span::raw(" ")); | ||
| 817 | } | ||
| 818 | 892 | ||
| 819 | if let RoomState::Chat(chat) = self && | 893 | if let RoomState::Chat(chat) = self && |
| 820 | chat.thread().is_some() | 894 | chat.thread().is_some() |
| @@ -824,7 +898,7 @@ impl RoomState { | |||
| 824 | 898 | ||
| 825 | spans.push(Span::styled(title, style)); | 899 | spans.push(Span::styled(title, style)); |
| 826 | 900 | ||
| 827 | match self.room().topic() { | 901 | match room.topic() { |
| 828 | Some(desc) if !desc.is_empty() => { | 902 | Some(desc) if !desc.is_empty() => { |
| 829 | spans.push(" (".into()); | 903 | spans.push(" (".into()); |
| 830 | spans.push(desc.into()); | 904 | spans.push(desc.into()); |
| @@ -838,24 +912,37 @@ impl RoomState { | |||
| 838 | Line::from(spans) | 912 | Line::from(spans) |
| 839 | } | 913 | } |
| 840 | 914 | ||
| 915 | pub fn get_tab_title(&self, store: &mut ProgramStore) -> Line<'_> { | ||
| 916 | match self { | ||
| 917 | RoomState::Space(w) => store.application.get_room_title(w.id()).into(), | ||
| 918 | RoomState::Chat(w) => store.application.get_room_title(w.id()).into(), | ||
| 919 | RoomState::NotJoined(_) => Line::from("Unjoined Room"), | ||
| 920 | RoomState::Joining(w) => { | ||
| 921 | Line::from(vec![Span::raw("Joining "), Span::raw(w.room.as_str())]) | ||
| 922 | }, | ||
| 923 | } | ||
| 924 | } | ||
| 925 | |||
| 841 | pub fn focus_toggle(&mut self) { | 926 | pub fn focus_toggle(&mut self) { |
| 842 | match self { | 927 | match self { |
| 843 | RoomState::Chat(chat) => chat.focus_toggle(), | 928 | RoomState::Chat(chat) => chat.focus_toggle(), |
| 844 | RoomState::Space(_) => return, | 929 | RoomState::Joining(_) | RoomState::NotJoined(_) | RoomState::Space(_) => return, |
| 845 | } | 930 | } |
| 846 | } | 931 | } |
| 847 | 932 | ||
| 848 | pub fn room(&self) -> &MatrixRoom { | 933 | pub fn room(&self) -> Option<&MatrixRoom> { |
| 849 | match self { | 934 | match self { |
| 850 | RoomState::Chat(chat) => chat.room(), | 935 | RoomState::Chat(chat) => Some(chat.room()), |
| 851 | RoomState::Space(space) => space.room(), | 936 | RoomState::Space(space) => Some(space.room()), |
| 937 | RoomState::Joining(_) | RoomState::NotJoined(_) => None, | ||
| 852 | } | 938 | } |
| 853 | } | 939 | } |
| 854 | 940 | ||
| 855 | pub fn id(&self) -> &RoomId { | 941 | pub fn id(&self) -> Option<&RoomId> { |
| 856 | match self { | 942 | match self { |
| 857 | RoomState::Chat(chat) => chat.id(), | 943 | RoomState::Chat(chat) => Some(chat.id()), |
| 858 | RoomState::Space(space) => space.id(), | 944 | RoomState::Space(space) => Some(space.id()), |
| 945 | RoomState::Joining(_) | RoomState::NotJoined(_) => None, | ||
| 859 | } | 946 | } |
| 860 | } | 947 | } |
| 861 | } | 948 | } |
| @@ -917,19 +1004,29 @@ impl TerminalCursor for RoomState { | |||
| 917 | 1004 | ||
| 918 | impl WindowOps<IambInfo> for RoomState { | 1005 | impl WindowOps<IambInfo> for RoomState { |
| 919 | fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { | 1006 | fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { |
| 920 | if self.room().state() != MatrixRoomState::Joined { | 1007 | if self.room_state() != Some(MatrixRoomState::Joined) { |
| 921 | self.refresh_room(store); | 1008 | self.refresh_room(store); |
| 922 | } | 1009 | } |
| 923 | 1010 | ||
| 924 | match self.room().state() { | 1011 | if let Some(room) = self.room() { |
| 925 | MatrixRoomState::Invited => return self.draw_invite(self.room(), area, buf, store), | 1012 | match room.state() { |
| 926 | MatrixRoomState::Knocked => return self.draw_knock(self.room(), area, buf, store), | 1013 | MatrixRoomState::Invited => return self.draw_invite(room, area, buf, store), |
| 927 | MatrixRoomState::Left => return self.draw_left(self.room(), area, buf, store), | 1014 | MatrixRoomState::Knocked => return self.draw_knock(room, area, buf, store), |
| 928 | _ => (), | 1015 | MatrixRoomState::Left => return self.draw_left(room, area, buf, store), |
| 1016 | _ => (), | ||
| 1017 | } | ||
| 929 | } | 1018 | } |
| 930 | 1019 | ||
| 931 | match self { | 1020 | match self { |
| 932 | RoomState::Chat(chat) => chat.draw(area, buf, focused, store), | 1021 | RoomState::Chat(chat) => { |
| 1022 | chat.draw(area, buf, focused, store); | ||
| 1023 | }, | ||
| 1024 | RoomState::Joining(state) => { | ||
| 1025 | Joining.render(area, buf, state); | ||
| 1026 | }, | ||
| 1027 | RoomState::NotJoined(state) => { | ||
| 1028 | NotJoined.render(area, buf, state); | ||
| 1029 | }, | ||
| 933 | RoomState::Space(space) => { | 1030 | RoomState::Space(space) => { |
| 934 | Space::new(store).focus(focused).render(area, buf, space); | 1031 | Space::new(store).focus(focused).render(area, buf, space); |
| 935 | }, | 1032 | }, |
| @@ -938,6 +1035,8 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 938 | 1035 | ||
| 939 | fn dup(&self, store: &mut ProgramStore) -> Self { | 1036 | fn dup(&self, store: &mut ProgramStore) -> Self { |
| 940 | match self { | 1037 | match self { |
| 1038 | RoomState::Joining(w) => RoomState::join(w.room.clone(), store), | ||
| 1039 | RoomState::NotJoined(w) => RoomState::NotJoined((*w).clone()), | ||
| 941 | RoomState::Chat(chat) => RoomState::Chat(Box::new(chat.dup(store))), | 1040 | RoomState::Chat(chat) => RoomState::Chat(Box::new(chat.dup(store))), |
| 942 | RoomState::Space(space) => RoomState::Space(Box::new(space.dup(store))), | 1041 | RoomState::Space(space) => RoomState::Space(Box::new(space.dup(store))), |
| 943 | } | 1042 | } |
| @@ -945,6 +1044,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 945 | 1044 | ||
| 946 | fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool { | 1045 | fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool { |
| 947 | match self { | 1046 | match self { |
| 1047 | RoomState::NotJoined(_) | RoomState::Joining(_) => true, | ||
| 948 | RoomState::Chat(chat) => chat.close(flags, store), | 1048 | RoomState::Chat(chat) => chat.close(flags, store), |
| 949 | RoomState::Space(space) => space.close(flags, store), | 1049 | RoomState::Space(space) => space.close(flags, store), |
| 950 | } | 1050 | } |
| @@ -957,6 +1057,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 957 | store: &mut ProgramStore, | 1057 | store: &mut ProgramStore, |
| 958 | ) -> IambResult<EditInfo> { | 1058 | ) -> IambResult<EditInfo> { |
| 959 | match self { | 1059 | match self { |
| 1060 | RoomState::NotJoined(_) | RoomState::Joining(_) => Err(EditError::ReadOnly.into()), | ||
| 960 | RoomState::Chat(chat) => chat.write(path, flags, store), | 1061 | RoomState::Chat(chat) => chat.write(path, flags, store), |
| 961 | RoomState::Space(space) => space.write(path, flags, store), | 1062 | RoomState::Space(space) => space.write(path, flags, store), |
| 962 | } | 1063 | } |
| @@ -964,6 +1065,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 964 | 1065 | ||
| 965 | fn get_completions(&self) -> Option<CompletionList> { | 1066 | fn get_completions(&self) -> Option<CompletionList> { |
| 966 | match self { | 1067 | match self { |
| 1068 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | ||
| 967 | RoomState::Chat(chat) => chat.get_completions(), | 1069 | RoomState::Chat(chat) => chat.get_completions(), |
| 968 | RoomState::Space(space) => space.get_completions(), | 1070 | RoomState::Space(space) => space.get_completions(), |
| 969 | } | 1071 | } |
| @@ -971,6 +1073,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 971 | 1073 | ||
| 972 | fn get_cursor_word(&self, style: &WordStyle) -> Option<String> { | 1074 | fn get_cursor_word(&self, style: &WordStyle) -> Option<String> { |
| 973 | match self { | 1075 | match self { |
| 1076 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | ||
| 974 | RoomState::Chat(chat) => chat.get_cursor_word(style), | 1077 | RoomState::Chat(chat) => chat.get_cursor_word(style), |
| 975 | RoomState::Space(space) => space.get_cursor_word(style), | 1078 | RoomState::Space(space) => space.get_cursor_word(style), |
| 976 | } | 1079 | } |
| @@ -978,6 +1081,7 @@ impl WindowOps<IambInfo> for RoomState { | |||
| 978 | 1081 | ||
| 979 | fn get_selected_word(&self) -> Option<String> { | 1082 | fn get_selected_word(&self) -> Option<String> { |
| 980 | match self { | 1083 | match self { |
| 1084 | RoomState::NotJoined(_) | RoomState::Joining(_) => None, | ||
| 981 | RoomState::Chat(chat) => chat.get_selected_word(), | 1085 | RoomState::Chat(chat) => chat.get_selected_word(), |
| 982 | RoomState::Space(space) => space.get_selected_word(), | 1086 | RoomState::Space(space) => space.get_selected_word(), |
| 983 | } | 1087 | } |
src/windows/room/not_joined.rs added +100
| @@ -0,0 +1,100 @@ | |||
| 1 | use crate::prelude::*; | ||
| 2 | |||
| 3 | #[derive(Clone)] | ||
| 4 | pub struct NotJoinedState { | ||
| 5 | pub(super) room: String, | ||
| 6 | err: Option<String>, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl NotJoinedState { | ||
| 10 | /// Create a window for a room the user hasn't yet joined. | ||
| 11 | pub fn new(room: String) -> Self { | ||
| 12 | Self { room, err: None } | ||
| 13 | } | ||
| 14 | |||
| 15 | /// Create a window for a room whose `:join` command failed. | ||
| 16 | pub fn failed(room: String, err: &UIError<IambInfo>) -> Self { | ||
| 17 | Self { room, err: Some(err.to_string()) } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | impl Editable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState { | ||
| 22 | fn editor_command( | ||
| 23 | &mut self, | ||
| 24 | _: &EditorAction, | ||
| 25 | _: &ProgramContext, | ||
| 26 | _: &mut ProgramStore, | ||
| 27 | ) -> EditResult<EditInfo, IambInfo> { | ||
| 28 | Ok(None) | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | impl Jumpable<ProgramContext, IambInfo> for NotJoinedState { | ||
| 33 | fn jump( | ||
| 34 | &mut self, | ||
| 35 | _: PositionList, | ||
| 36 | _: MoveDir1D, | ||
| 37 | count: usize, | ||
| 38 | _: &ProgramContext, | ||
| 39 | ) -> IambResult<usize> { | ||
| 40 | Ok(count) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | impl Scrollable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState { | ||
| 45 | fn scroll( | ||
| 46 | &mut self, | ||
| 47 | _: &ScrollStyle, | ||
| 48 | _: &ProgramContext, | ||
| 49 | _: &mut ProgramStore, | ||
| 50 | ) -> EditResult<EditInfo, IambInfo> { | ||
| 51 | Ok(None) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState { | ||
| 56 | fn prompt( | ||
| 57 | &mut self, | ||
| 58 | _: &PromptAction, | ||
| 59 | _: &ProgramContext, | ||
| 60 | _: &mut ProgramStore, | ||
| 61 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { | ||
| 62 | Ok(vec![]) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | impl TerminalCursor for NotJoinedState { | ||
| 67 | fn get_term_cursor(&self) -> Option<TermOffset> { | ||
| 68 | None | ||
| 69 | } | ||
| 70 | |||
| 71 | fn hide_term_cursor(&self) -> bool { | ||
| 72 | true | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | /// [StatefulWidget] for Matrix rooms that haven't been joined. | ||
| 77 | pub struct NotJoined; | ||
| 78 | |||
| 79 | impl StatefulWidget for NotJoined { | ||
| 80 | type State = NotJoinedState; | ||
| 81 | |||
| 82 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | ||
| 83 | let mut lines = vec![]; | ||
| 84 | |||
| 85 | lines.push(Line::from(vec![ | ||
| 86 | Span::raw("Use "), | ||
| 87 | Span::styled(format!("`:join {}`", state.room), StyleModifier::BOLD), | ||
| 88 | Span::raw(" to join this room"), | ||
| 89 | ])); | ||
| 90 | |||
| 91 | if let Some(err) = &state.err { | ||
| 92 | let style = Style::default().fg(Color::Red); | ||
| 93 | lines.push(Line::from(Span::styled(err.as_str(), style))); | ||
| 94 | } | ||
| 95 | |||
| 96 | Paragraph::new(Text::from(lines)) | ||
| 97 | .alignment(Alignment::Center) | ||
| 98 | .render(area, buffer); | ||
| 99 | } | ||
| 100 | } | ||
src/worker.rs +61 −52
| @@ -683,11 +683,15 @@ impl<T> ClientResponse<T> { | |||
| 683 | fn recv(self) -> T { | 683 | fn recv(self) -> T { |
| 684 | self.0.recv().expect("failed to receive response from client thread") | 684 | self.0.recv().expect("failed to receive response from client thread") |
| 685 | } | 685 | } |
| 686 | |||
| 687 | pub fn try_recv(&self) -> Option<T> { | ||
| 688 | self.0.try_recv().ok() | ||
| 689 | } | ||
| 686 | } | 690 | } |
| 687 | 691 | ||
| 688 | impl<T> ClientReply<T> { | 692 | impl<T> ClientReply<T> { |
| 689 | fn send(self, t: T) { | 693 | fn send(self, t: T) { |
| 690 | self.0.send(t).unwrap(); | 694 | let _ = self.0.send(t); |
| 691 | } | 695 | } |
| 692 | } | 696 | } |
| 693 | 697 | ||
| @@ -878,6 +882,50 @@ pub async fn create_client(settings: &ApplicationSettings) -> Client { | |||
| 878 | client | 882 | client |
| 879 | } | 883 | } |
| 880 | 884 | ||
| 885 | async fn direct_message(user: OwnedUserId, client: Client) -> IambResult<OwnedRoomId> { | ||
| 886 | if let Some(room) = client.get_dm_room(&user) { | ||
| 887 | return Ok(room.room_id().to_owned()); | ||
| 888 | } | ||
| 889 | |||
| 890 | client | ||
| 891 | .create_dm(&user) | ||
| 892 | .await | ||
| 893 | .map(|room| room.room_id().to_owned()) | ||
| 894 | .map_err(|err| { | ||
| 895 | error!( | ||
| 896 | user_id = user.as_str(), | ||
| 897 | err = err.to_string(), | ||
| 898 | "Failed to create direct message room" | ||
| 899 | ); | ||
| 900 | |||
| 901 | let msg = format!("Could not open a room with {user}"); | ||
| 902 | UIError::Failure(msg) | ||
| 903 | }) | ||
| 904 | } | ||
| 905 | |||
| 906 | async fn join_room( | ||
| 907 | name: String, | ||
| 908 | via: Vec<OwnedServerName>, | ||
| 909 | client: Client, | ||
| 910 | ) -> IambResult<OwnedRoomId> { | ||
| 911 | if let Ok(alias_id) = OwnedRoomOrAliasId::from_str(name.as_str()) { | ||
| 912 | match client.join_room_by_id_or_alias(&alias_id, &via).await { | ||
| 913 | Ok(resp) => Ok(resp.room_id().to_owned()), | ||
| 914 | Err(e) => { | ||
| 915 | let msg = e.to_string(); | ||
| 916 | let err = UIError::Failure(msg); | ||
| 917 | return Err(err); | ||
| 918 | }, | ||
| 919 | } | ||
| 920 | } else if let Ok(user) = OwnedUserId::try_from(name.as_str()) { | ||
| 921 | direct_message(user, client).await | ||
| 922 | } else { | ||
| 923 | let msg = format!("{:?} is not a valid room or user name", name.as_str()); | ||
| 924 | let err = UIError::Failure(msg); | ||
| 925 | return Err(err); | ||
| 926 | } | ||
| 927 | } | ||
| 928 | |||
| 881 | #[derive(Clone)] | 929 | #[derive(Clone)] |
| 882 | pub struct Requester { | 930 | pub struct Requester { |
| 883 | pub client: Client, | 931 | pub client: Client, |
| @@ -933,12 +981,18 @@ impl Requester { | |||
| 933 | return response.recv(); | 981 | return response.recv(); |
| 934 | } | 982 | } |
| 935 | 983 | ||
| 936 | pub fn join_room(&self, name: String, via: Vec<OwnedServerName>) -> IambResult<OwnedRoomId> { | 984 | pub fn join_room_chan( |
| 985 | &self, | ||
| 986 | name: String, | ||
| 987 | via: Vec<OwnedServerName>, | ||
| 988 | ) -> ClientResponse<IambResult<OwnedRoomId>> { | ||
| 937 | let (reply, response) = oneshot(); | 989 | let (reply, response) = oneshot(); |
| 938 | |||
| 939 | self.tx.send(WorkerTask::JoinRoom(name, via, reply)).unwrap(); | 990 | self.tx.send(WorkerTask::JoinRoom(name, via, reply)).unwrap(); |
| 991 | response | ||
| 992 | } | ||
| 940 | 993 | ||
| 941 | return response.recv(); | 994 | pub fn join_room(&self, name: String, via: Vec<OwnedServerName>) -> IambResult<OwnedRoomId> { |
| 995 | self.join_room_chan(name, via).recv() | ||
| 942 | } | 996 | } |
| 943 | 997 | ||
| 944 | pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { | 998 | pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { |
| @@ -982,7 +1036,7 @@ pub struct ClientWorker { | |||
| 982 | load_handle: Option<JoinHandle<()>>, | 1036 | load_handle: Option<JoinHandle<()>>, |
| 983 | sync_handle: Option<JoinHandle<()>>, | 1037 | sync_handle: Option<JoinHandle<()>>, |
| 984 | 1038 | ||
| 985 | /// Take care when locking since worker commands are sent with the lock already hold | 1039 | /// Take care when locking since worker commands are sent with the lock already held |
| 986 | store: Option<AsyncProgramStore>, | 1040 | store: Option<AsyncProgramStore>, |
| 987 | } | 1041 | } |
| 988 | 1042 | ||
| @@ -1036,7 +1090,8 @@ impl ClientWorker { | |||
| 1036 | }, | 1090 | }, |
| 1037 | WorkerTask::JoinRoom(name, via, reply) => { | 1091 | WorkerTask::JoinRoom(name, via, reply) => { |
| 1038 | assert!(self.initialized); | 1092 | assert!(self.initialized); |
| 1039 | reply.send(self.join_room(name, via).await); | 1093 | let client = self.client.clone(); |
| 1094 | tokio::spawn(async move { reply.send(join_room(name, via, client).await) }); | ||
| 1040 | }, | 1095 | }, |
| 1041 | WorkerTask::GetInviter(invited, reply) => { | 1096 | WorkerTask::GetInviter(invited, reply) => { |
| 1042 | assert!(self.initialized); | 1097 | assert!(self.initialized); |
| @@ -1476,27 +1531,6 @@ impl ClientWorker { | |||
| 1476 | Ok(Some(InfoMessage::from("Successfully logged out"))) | 1531 | Ok(Some(InfoMessage::from("Successfully logged out"))) |
| 1477 | } | 1532 | } |
| 1478 | 1533 | ||
| 1479 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<OwnedRoomId> { | ||
| 1480 | if let Some(room) = self.client.get_dm_room(&user) { | ||
| 1481 | return Ok(room.room_id().to_owned()); | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | self.client | ||
| 1485 | .create_dm(&user) | ||
| 1486 | .await | ||
| 1487 | .map(|room| room.room_id().to_owned()) | ||
| 1488 | .map_err(|err| { | ||
| 1489 | error!( | ||
| 1490 | user_id = user.as_str(), | ||
| 1491 | err = err.to_string(), | ||
| 1492 | "Failed to create direct message room" | ||
| 1493 | ); | ||
| 1494 | |||
| 1495 | let msg = format!("Could not open a room with {user}"); | ||
| 1496 | UIError::Failure(msg) | ||
| 1497 | }) | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | async fn get_inviter(&mut self, invited: MatrixRoom) -> IambResult<Option<RoomMember>> { | 1534 | async fn get_inviter(&mut self, invited: MatrixRoom) -> IambResult<Option<RoomMember>> { |
| 1501 | let details = invited.invite_details().await.map_err(IambError::from)?; | 1535 | let details = invited.invite_details().await.map_err(IambError::from)?; |
| 1502 | 1536 | ||
| @@ -1530,31 +1564,6 @@ impl ClientWorker { | |||
| 1530 | } | 1564 | } |
| 1531 | } | 1565 | } |
| 1532 | 1566 | ||
| 1533 | async fn join_room( | ||
| 1534 | &mut self, | ||
| 1535 | name: String, | ||
| 1536 | via: Vec<OwnedServerName>, | ||
| 1537 | ) -> IambResult<OwnedRoomId> { | ||
| 1538 | if let Ok(alias_id) = OwnedRoomOrAliasId::from_str(name.as_str()) { | ||
| 1539 | match self.client.join_room_by_id_or_alias(&alias_id, &via).await { | ||
| 1540 | Ok(resp) => Ok(resp.room_id().to_owned()), | ||
| 1541 | Err(e) => { | ||
| 1542 | let msg = e.to_string(); | ||
| 1543 | let err = UIError::Failure(msg); | ||
| 1544 | |||
| 1545 | return Err(err); | ||
| 1546 | }, | ||
| 1547 | } | ||
| 1548 | } else if let Ok(user) = OwnedUserId::try_from(name.as_str()) { | ||
| 1549 | self.direct_message(user).await | ||
| 1550 | } else { | ||
| 1551 | let msg = format!("{:?} is not a valid room or user name", name.as_str()); | ||
| 1552 | let err = UIError::Failure(msg); | ||
| 1553 | |||
| 1554 | return Err(err); | ||
| 1555 | } | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | async fn members(&mut self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { | 1567 | async fn members(&mut self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { |
| 1559 | if let Some(room) = self.client.get_room(room_id.as_ref()) { | 1568 | if let Some(room) = self.client.get_room(room_id.as_ref()) { |
| 1560 | Ok(room | 1569 | Ok(room |