Commit 1a85a74fe7
Unregistered key
Layout: unified · split
docs/iamb.1 +2
| @@ -110,6 +110,8 @@ Recover the encryption secrets from the backup on your homeserver, verifying thi | ||
| 110 | 110 | |
| 111 | 111 | .Sh "MESSAGE COMMANDS" |
| 112 | 112 | .Bl -tag -width Ds |
| 113 | .It Sy ":message" | |
| 114 | View more information about the focused message. | |
| 113 | 115 | .It Sy ":download [path]" |
| 114 | 116 | Download an attachment from the selected message and save it to the optional path. |
| 115 | 117 | .It Sy ":open [path]" |
src/base.rs +116 −31
| @@ -602,6 +602,9 @@ pub enum RoomAction { | ||
| 602 | 602 | /// Open the members window. |
| 603 | 603 | Members(Box<CommandContext>), |
| 604 | 604 | |
| 605 | /// Open the message info window. | |
| 606 | Message(Box<CommandContext>), | |
| 607 | ||
| 605 | 608 | /// Open the pinned messages window. |
| 606 | 609 | Pinned(Box<CommandContext>), |
| 607 | 610 | |
| @@ -1512,27 +1515,23 @@ impl RoomInfo { | ||
| 1512 | 1515 | } |
| 1513 | 1516 | |
| 1514 | 1517 | /// Get the reactions and their counts for a message. |
| 1515 | pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize, &Option<MediaSource>)> { | |
| 1518 | pub fn get_reactions( | |
| 1519 | &self, | |
| 1520 | event_id: &EventId, | |
| 1521 | ) -> Vec<(&str, Vec<&UserId>, &Option<MediaSource>)> { | |
| 1516 | 1522 | if let Some(reacts) = self.reactions.get(event_id) { |
| 1517 | let mut counts = HashMap::new(); | |
| 1518 | ||
| 1519 | let mut seen_user_reactions = BTreeSet::new(); | |
| 1523 | let mut reactions = BTreeMap::new(); | |
| 1520 | 1524 | |
| 1521 | 1525 | for (key, user, source) in reacts.values() { |
| 1522 | if !seen_user_reactions.contains(&(key, user)) { | |
| 1523 | seen_user_reactions.insert((key, user)); | |
| 1524 | let count = counts.entry(key.as_str()).or_insert((0, source)); | |
| 1525 | count.0 += 1; | |
| 1526 | } | |
| 1526 | let react = | |
| 1527 | reactions.entry(key.as_str()).or_insert_with(|| (BTreeSet::new(), source)); | |
| 1528 | react.0.insert(user.deref()); | |
| 1527 | 1529 | } |
| 1528 | 1530 | |
| 1529 | let mut reactions = counts | |
| 1530 | .into_iter() | |
| 1531 | .map(|(key, (count, source))| (key, count, source)) | |
| 1532 | .collect::<Vec<_>>(); | |
| 1533 | reactions.sort_by_key(|item| (item.0, item.1)); | |
| 1534 | ||
| 1535 | 1531 | reactions |
| 1532 | .into_iter() | |
| 1533 | .map(|(key, (users, source))| (key, users.into_iter().collect::<Vec<_>>(), source)) | |
| 1534 | .collect::<Vec<_>>() | |
| 1536 | 1535 | } else { |
| 1537 | 1536 | vec![] |
| 1538 | 1537 | } |
| @@ -1571,6 +1570,44 @@ impl RoomInfo { | ||
| 1571 | 1570 | self.get_thread_mut(root.map(ToOwned::to_owned)).get_mut(key) |
| 1572 | 1571 | } |
| 1573 | 1572 | |
| 1573 | /// Map an event identifier to its [`MessageKey`] and thread root. | |
| 1574 | fn get_message_thread_key( | |
| 1575 | &self, | |
| 1576 | message_id: &MessageId, | |
| 1577 | ) -> Option<(Option<&EventId>, &MessageKey)> { | |
| 1578 | let event_id = match message_id { | |
| 1579 | MessageId::Local(id) => { | |
| 1580 | let loc = self.echo_keys.get(id)?; | |
| 1581 | match loc { | |
| 1582 | EchoLocation::Message(root, key) => return Some((root.as_deref(), key)), | |
| 1583 | EchoLocation::Replaced(event_id) => event_id, | |
| 1584 | } | |
| 1585 | }, | |
| 1586 | MessageId::Origin(event_id) => event_id, | |
| 1587 | }; | |
| 1588 | ||
| 1589 | let loc = self.keys.get(event_id)?; | |
| 1590 | ||
| 1591 | Some((loc.to_thread_root(), loc.to_message_key()?)) | |
| 1592 | } | |
| 1593 | ||
| 1594 | /// Get an event for an identifier. | |
| 1595 | pub fn get_message(&self, message_id: &MessageId) -> Option<&Message> { | |
| 1596 | let (root, key) = self.get_message_thread_key(message_id)?; | |
| 1597 | ||
| 1598 | self.get_thread(root)?.get(key) | |
| 1599 | } | |
| 1600 | ||
| 1601 | /// Get an event for an identifier as mutable. | |
| 1602 | pub fn get_message_mut(&mut self, message_id: &MessageId) -> Option<&mut Message> { | |
| 1603 | let (root, key) = self.get_message_thread_key(message_id)?; | |
| 1604 | ||
| 1605 | let root = root.map(ToOwned::to_owned); | |
| 1606 | let key = key.to_owned(); | |
| 1607 | ||
| 1608 | self.get_thread_mut(root).get_mut(&key) | |
| 1609 | } | |
| 1610 | ||
| 1574 | 1611 | pub fn redact(&mut self, ev: OriginalSyncRoomRedactionEvent) { |
| 1575 | 1612 | let Some(redacts) = &ev.redacts else { |
| 1576 | 1613 | return; |
| @@ -2287,7 +2324,7 @@ impl RoomInfo { | ||
| 2287 | 2324 | |
| 2288 | 2325 | /// Checks if a given user has reacted with the given emoji on the given event |
| 2289 | 2326 | pub fn user_reactions_contains( |
| 2290 | &mut self, | |
| 2327 | &self, | |
| 2291 | 2328 | user_id: &UserId, |
| 2292 | 2329 | event_id: &EventId, |
| 2293 | 2330 | emoji: &str, |
| @@ -2600,11 +2637,39 @@ impl ChatStore { | ||
| 2600 | 2637 | |
| 2601 | 2638 | impl ApplicationStore for ChatStore {} |
| 2602 | 2639 | |
| 2640 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] | |
| 2641 | pub enum RoomView { | |
| 2642 | /// The main timeline is shown. | |
| 2643 | Main, | |
| 2644 | /// A thread is shown. | |
| 2645 | Thread(OwnedEventId), | |
| 2646 | /// A single message is shown. | |
| 2647 | Message(MessageId), | |
| 2648 | } | |
| 2649 | ||
| 2650 | impl From<Option<OwnedEventId>> for RoomView { | |
| 2651 | fn from(thread: Option<OwnedEventId>) -> Self { | |
| 2652 | match thread { | |
| 2653 | Some(thread) => Self::Thread(thread), | |
| 2654 | None => Self::Main, | |
| 2655 | } | |
| 2656 | } | |
| 2657 | } | |
| 2658 | ||
| 2659 | impl From<Option<&OwnedEventId>> for RoomView { | |
| 2660 | fn from(thread: Option<&OwnedEventId>) -> Self { | |
| 2661 | match thread { | |
| 2662 | Some(thread) => Self::Thread(thread.to_owned()), | |
| 2663 | None => Self::Main, | |
| 2664 | } | |
| 2665 | } | |
| 2666 | } | |
| 2667 | ||
| 2603 | 2668 | /// Identified used to track window content. |
| 2604 | 2669 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 2605 | 2670 | pub enum IambId { |
| 2606 | /// A Matrix room, with an optional thread to show. | |
| 2607 | Room(OwnedRoomOrAliasId, Option<OwnedEventId>), | |
| 2671 | /// A Matrix room, with an item that is shown. | |
| 2672 | Room(OwnedRoomOrAliasId, RoomView), | |
| 2608 | 2673 | |
| 2609 | 2674 | /// The `:dms` window. |
| 2610 | 2675 | DirectList, |
| @@ -2646,13 +2711,17 @@ pub enum IambId { | ||
| 2646 | 2711 | impl Display for IambId { |
| 2647 | 2712 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2648 | 2713 | match self { |
| 2649 | IambId::Room(alias, None) => { | |
| 2714 | IambId::Room(alias, RoomView::Main) => { | |
| 2715 | let encoded = percent_encode(alias.as_bytes(), NON_ALPHANUMERIC); | |
| 2716 | write!(f, "iamb://room/{encoded}") | |
| 2717 | }, | |
| 2718 | IambId::Room(alias, RoomView::Thread(thread)) => { | |
| 2650 | 2719 | let encoded = percent_encode(alias.as_bytes(), NON_ALPHANUMERIC); |
| 2651 | write!(f, "iamb://room/{}", encoded) | |
| 2720 | write!(f, "iamb://room/{encoded}/threads/{thread}") | |
| 2652 | 2721 | }, |
| 2653 | IambId::Room(alias, Some(thread)) => { | |
| 2722 | IambId::Room(alias, RoomView::Message(message)) => { | |
| 2654 | 2723 | let encoded = percent_encode(alias.as_bytes(), NON_ALPHANUMERIC); |
| 2655 | write!(f, "iamb://room/{}/threads/{thread}", encoded) | |
| 2724 | write!(f, "iamb://room/{encoded}/messages/{message}") | |
| 2656 | 2725 | }, |
| 2657 | 2726 | IambId::MemberList(room_id) => { |
| 2658 | 2727 | write!(f, "iamb://members/{room_id}") |
| @@ -2729,7 +2798,7 @@ impl Visitor<'_> for IambIdVisitor { | ||
| 2729 | 2798 | return Err(E::custom(format!("Invalid room identifier: {decoded:?}"))); |
| 2730 | 2799 | }; |
| 2731 | 2800 | |
| 2732 | Ok(IambId::Room(room_id, None)) | |
| 2801 | Ok(IambId::Room(room_id, RoomView::Main)) | |
| 2733 | 2802 | }, |
| 2734 | 2803 | [alias, "threads", thread_root] => { |
| 2735 | 2804 | let decoded = percent_decode(alias.as_bytes()).decode_utf8_lossy(); |
| @@ -2741,7 +2810,19 @@ impl Visitor<'_> for IambIdVisitor { | ||
| 2741 | 2810 | return Err(E::custom("Invalid thread root identifier")); |
| 2742 | 2811 | }; |
| 2743 | 2812 | |
| 2744 | Ok(IambId::Room(room_id, Some(thread_root))) | |
| 2813 | Ok(IambId::Room(room_id, RoomView::Thread(thread_root))) | |
| 2814 | }, | |
| 2815 | [alias, "messages", message] => { | |
| 2816 | let decoded = percent_decode(alias.as_bytes()).decode_utf8_lossy(); | |
| 2817 | let Ok(room_id) = OwnedRoomOrAliasId::try_from(decoded.as_ref()) else { | |
| 2818 | return Err(E::custom("Invalid room identifier: {decoded:?}")); | |
| 2819 | }; | |
| 2820 | ||
| 2821 | let message_id = OwnedEventId::try_from(message) | |
| 2822 | .map(Into::into) | |
| 2823 | .unwrap_or_else(|_| OwnedTransactionId::from(message).into()); | |
| 2824 | ||
| 2825 | Ok(IambId::Room(room_id, RoomView::Message(message_id))) | |
| 2745 | 2826 | }, |
| 2746 | 2827 | [room_id, "pinned"] => { |
| 2747 | 2828 | let Ok(room_id) = OwnedRoomId::try_from(room_id) else { |
| @@ -2880,7 +2961,7 @@ pub enum IambBufferId { | ||
| 2880 | 2961 | Command(CommandType), |
| 2881 | 2962 | |
| 2882 | 2963 | /// The message buffer or a specific message in a room. |
| 2883 | Room(OwnedRoomId, Option<OwnedEventId>, RoomFocus), | |
| 2964 | Room(OwnedRoomId, RoomView, RoomFocus), | |
| 2884 | 2965 | |
| 2885 | 2966 | /// The `:dms` window. |
| 2886 | 2967 | DirectList, |
| @@ -2924,9 +3005,7 @@ impl IambBufferId { | ||
| 2924 | 3005 | pub fn to_window(&self) -> Option<IambId> { |
| 2925 | 3006 | let id = match self { |
| 2926 | 3007 | IambBufferId::Command(_) => return None, |
| 2927 | IambBufferId::Room(room, thread, _) => { | |
| 2928 | IambId::Room(room.clone().into(), thread.clone()) | |
| 2929 | }, | |
| 3008 | IambBufferId::Room(room, view, _) => IambId::Room(room.clone().into(), view.clone()), | |
| 2930 | 3009 | IambBufferId::DirectList => IambId::DirectList, |
| 2931 | 3010 | IambBufferId::MemberList(room) => IambId::MemberList(room.clone()), |
| 2932 | 3011 | IambBufferId::PinnedList(room) => IambId::PinnedList(room.clone()), |
| @@ -2967,7 +3046,7 @@ pub mod tests { | ||
| 2967 | 3046 | |
| 2968 | 3047 | use matrix_sdk::ruma::events::reaction::ReactionEventContent; |
| 2969 | 3048 | use matrix_sdk::ruma::events::relation::Annotation; |
| 2970 | use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, owned_event_id}; | |
| 3049 | use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, owned_event_id, owned_user_id}; | |
| 2971 | 3050 | use pretty_assertions::assert_eq; |
| 2972 | 3051 | use ratatui::style::Color; |
| 2973 | 3052 | use serde_json::{Map, Value}; |
| @@ -3043,7 +3122,13 @@ pub mod tests { | ||
| 3043 | 3122 | .into_iter() |
| 3044 | 3123 | .map(|(key, count, _)| (key, count)) |
| 3045 | 3124 | .collect(); |
| 3046 | assert_eq!(reacts, vec![("🏠", 1), ("🙂", 2)]); | |
| 3125 | assert_eq!(reacts, vec![ | |
| 3126 | ("🏠", vec![owned_user_id!("@foo:example.com").deref()]), | |
| 3127 | ("🙂", vec![ | |
| 3128 | owned_user_id!("@bar:example.com").deref(), | |
| 3129 | owned_user_id!("@foo:example.com").deref(), | |
| 3130 | ],) | |
| 3131 | ]); | |
| 3047 | 3132 | } |
| 3048 | 3133 | |
| 3049 | 3134 | #[test] |
| @@ -3319,7 +3404,7 @@ pub mod tests { | ||
| 3319 | 3404 | #[test] |
| 3320 | 3405 | fn test_alias_window_id() { |
| 3321 | 3406 | let room_id = TEST_ROOM1_ALIAS.clone(); |
| 3322 | let id = IambId::Room(room_id.into(), None); | |
| 3407 | let id = IambId::Room(room_id.into(), RoomView::Main); | |
| 3323 | 3408 | |
| 3324 | 3409 | // Hash gets replaced during encoding: |
| 3325 | 3410 | let exp = "iamb://room/%23room1%3Aexample%2Ecom"; |
src/commands.rs +16
| @@ -530,6 +530,17 @@ fn iamb_invites(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 530 | 530 | return Ok(step); |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | fn iamb_message(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | |
| 534 | if !desc.arg.text.is_empty() { | |
| 535 | return Result::Err(CommandError::InvalidArgument); | |
| 536 | } | |
| 537 | ||
| 538 | let open = IambAction::Room(RoomAction::Message(ctx.clone().into())); | |
| 539 | let step = CommandStep::Continue(open.into(), ctx.context.clone()); | |
| 540 | ||
| 541 | return Ok(step); | |
| 542 | } | |
| 543 | ||
| 533 | 544 | fn iamb_self(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { |
| 534 | 545 | let mut iter = desc.arg.strings()?.into_iter(); |
| 535 | 546 | let field = iter.next().ok_or(CommandError::InvalidArgument)?; |
| @@ -1246,6 +1257,11 @@ pub fn add_iamb_commands(cmds: &mut ProgramCommands) { | ||
| 1246 | 1257 | f: iamb_rooms, |
| 1247 | 1258 | }); |
| 1248 | 1259 | cmds.add_command(ProgramCommand { name: "room".into(), aliases: vec![], f: iamb_room }); |
| 1260 | cmds.add_command(ProgramCommand { | |
| 1261 | name: "message".into(), | |
| 1262 | aliases: vec![], | |
| 1263 | f: iamb_message, | |
| 1264 | }); | |
| 1249 | 1265 | cmds.add_command(ProgramCommand { |
| 1250 | 1266 | name: "space".into(), |
| 1251 | 1267 | aliases: vec![], |
src/completions.rs +2 −2
| @@ -653,8 +653,8 @@ fn complete_cmdarg( | ||
| 653 | 653 | |
| 654 | 654 | // These have no arguments |
| 655 | 655 | "cancel" | "chats" | "dms" | "editor" | "edit" | "forget" | "invites" | "leave" | |
| 656 | "members" | "mentions" | "pin" | "pinned" | "unpin" | "replied" | "reply" | "rooms" | | |
| 657 | "welcome" => vec![], | |
| 656 | "members" | "mentions" | "message" | "pin" | "pinned" | "unpin" | "replied" | "reply" | | |
| 657 | "rooms" | "welcome" => vec![], | |
| 658 | 658 | |
| 659 | 659 | "abo" | "aboveleft" | "bel" | "belowright" | "hor" | "horizontal" | "lefta" | |
| 660 | 660 | "leftabove" | "rightb" | "rightbelow" | "tab" | "vert" | "vertical" => { |
src/config/mod.rs +10 −2
| @@ -1705,7 +1705,11 @@ impl ApplicationSettings { | ||
| 1705 | 1705 | self.theme.users.style(user_id.as_str(), color) |
| 1706 | 1706 | } |
| 1707 | 1707 | |
| 1708 | pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> { | |
| 1708 | pub fn get_user_span_maybe<'a>( | |
| 1709 | &self, | |
| 1710 | user_id: &'a UserId, | |
| 1711 | info: Option<&'a RoomInfo>, | |
| 1712 | ) -> Span<'a> { | |
| 1709 | 1713 | let (color, name) = self.get_user_overrides(user_id); |
| 1710 | 1714 | |
| 1711 | 1715 | let style = self.theme.users.style(user_id.as_str(), color); |
| @@ -1714,7 +1718,7 @@ impl ApplicationSettings { | ||
| 1714 | 1718 | (None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()), |
| 1715 | 1719 | (None, UserDisplayStyle::LocalPart) => Cow::Borrowed(user_id.localpart()), |
| 1716 | 1720 | (None, UserDisplayStyle::DisplayName) => { |
| 1717 | if let Some(name) = info.display_names.get(user_id) { | |
| 1721 | if let Some(name) = info.and_then(|info| info.display_names.get(user_id)) { | |
| 1718 | 1722 | name |
| 1719 | 1723 | } else { |
| 1720 | 1724 | Cow::Borrowed(user_id.as_str()) |
| @@ -1724,6 +1728,10 @@ impl ApplicationSettings { | ||
| 1724 | 1728 | |
| 1725 | 1729 | Span::styled(name, style) |
| 1726 | 1730 | } |
| 1731 | ||
| 1732 | pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> { | |
| 1733 | self.get_user_span_maybe(user_id, Some(info)) | |
| 1734 | } | |
| 1727 | 1735 | } |
| 1728 | 1736 | |
| 1729 | 1737 | #[cfg(test)] |
src/config/theme.rs +4
| @@ -59,6 +59,10 @@ pub fn default_theme() -> Theme { | ||
| 59 | 59 | modifiers: vec![ModifierChange::Remove(StyleModifier::REVERSED)], |
| 60 | 60 | }, |
| 61 | 61 | }, |
| 62 | cmdbar: ThemeCommandBar { | |
| 63 | error: Stylable::fg(Color::Red), | |
| 64 | ..Default::default() | |
| 65 | }, | |
| 62 | 66 | timeline: ThemeTimeline { |
| 63 | 67 | date: Stylable::with_modifiers(StyleModifier::BOLD), |
| 64 | 68 | unread_marker: Stylable::with_modifiers(StyleModifier::DIM), |
src/keybindings.rs +1 −1
| @@ -9,7 +9,7 @@ use modalkit::env::vim::keybindings::{InputStep, VimBindings}; | ||
| 9 | 9 | use modalkit::keybindings::{EdgeEvent, EdgeRepeat, InputBindings}; |
| 10 | 10 | |
| 11 | 11 | use crate::base::{Keybindings, MATRIX_ID_WORD}; |
| 12 | use crate::config::{Keys, TunableValues}; | |
| 12 | use crate::config::Keys; | |
| 13 | 13 | use crate::prelude::*; |
| 14 | 14 | |
| 15 | 15 | pub type IambStep = InputStep<IambInfo>; |
src/main.rs +13 −7
| @@ -86,14 +86,16 @@ fn config_tab_to_desc( | ||
| 86 | 86 | let window = match window { |
| 87 | 87 | config::WindowPath::UserId(user_id) => { |
| 88 | 88 | if let Some(dm) = store.application.worker.client.get_dm_room(&user_id) { |
| 89 | IambId::Room(dm.room_id().to_owned().into(), None) | |
| 89 | IambId::Room(dm.room_id().to_owned().into(), RoomView::Main) | |
| 90 | 90 | } else { |
| 91 | 91 | let room_id = store.application.worker.create_dm(user_id)?; |
| 92 | IambId::Room(room_id.into(), None) | |
| 92 | IambId::Room(room_id.into(), RoomView::Main) | |
| 93 | 93 | } |
| 94 | 94 | }, |
| 95 | config::WindowPath::RoomId(room_id) => IambId::Room(room_id.into(), None), | |
| 96 | config::WindowPath::AliasId(alias_id) => IambId::Room(alias_id.into(), None), | |
| 95 | config::WindowPath::RoomId(room_id) => IambId::Room(room_id.into(), RoomView::Main), | |
| 96 | config::WindowPath::AliasId(alias_id) => { | |
| 97 | IambId::Room(alias_id.into(), RoomView::Main) | |
| 98 | }, | |
| 97 | 99 | config::WindowPath::Window(id) => id, |
| 98 | 100 | }; |
| 99 | 101 | |
| @@ -161,7 +163,7 @@ fn resolve_mxid( | ||
| 161 | 163 | .or_default() |
| 162 | 164 | .extend(via); |
| 163 | 165 | |
| 164 | Ok(IambId::Room(alias_id, None)) | |
| 166 | Ok(IambId::Room(alias_id, RoomView::Main)) | |
| 165 | 167 | } |
| 166 | 168 | |
| 167 | 169 | fn setup_screen( |
| @@ -618,7 +620,11 @@ impl Application { | ||
| 618 | 620 | }, |
| 619 | 621 | IambAction::Keys(act) => self.keys_command(act, ctx, store).await?, |
| 620 | 622 | IambAction::Message(act) => { |
| 621 | self.screen.current_window_mut()?.message_command(act, ctx, store).await? | |
| 623 | let acts = | |
| 624 | self.screen.current_window_mut()?.message_command(act, ctx, store).await?; | |
| 625 | self.action_prepend(acts); | |
| 626 | ||
| 627 | None | |
| 622 | 628 | }, |
| 623 | 629 | IambAction::Space(act) => { |
| 624 | 630 | self.screen.current_window_mut()?.space_command(act, ctx, store).await? |
| @@ -700,7 +706,7 @@ impl Application { | ||
| 700 | 706 | HomeserverAction::CreateRoom(alias, vis, flags) => { |
| 701 | 707 | let client = &store.application.worker.client; |
| 702 | 708 | let room_id = create_room(client, alias, vis, flags).await?; |
| 703 | let room = IambId::Room(room_id.into(), None); | |
| 709 | let room = IambId::Room(room_id.into(), RoomView::Main); | |
| 704 | 710 | let target = OpenTarget::Application(room); |
| 705 | 711 | let action = WindowAction::Switch(target); |
| 706 | 712 | |
src/message/html.rs +37 −16
| @@ -291,8 +291,8 @@ pub enum StyleTreeNode { | ||
| 291 | 291 | Table(Table), |
| 292 | 292 | Text(Cow<'static, str>), |
| 293 | 293 | Sequence(StyleTreeChildren), |
| 294 | RoomAlias(OwnedRoomAliasId, Option<char>), | |
| 295 | RoomId(OwnedRoomId, Vec<OwnedServerName>, Option<char>), | |
| 294 | RoomAlias(OwnedRoomAliasId, Option<char>, Option<OwnedEventId>), | |
| 295 | RoomId(OwnedRoomId, Vec<OwnedServerName>, Option<char>, Option<OwnedEventId>), | |
| 296 | 296 | UserId(OwnedUserId, Option<char>), |
| 297 | 297 | DisplayName(String, OwnedUserId, Option<char>), |
| 298 | 298 | } |
| @@ -342,17 +342,29 @@ impl StyleTreeNode { | ||
| 342 | 342 | urls.push((*c, to_url)); |
| 343 | 343 | } |
| 344 | 344 | }, |
| 345 | StyleTreeNode::RoomId(room_id, via, c) => { | |
| 345 | StyleTreeNode::RoomId(room_id, via, c, event_id) => { | |
| 346 | 346 | if let Some(c) = c { |
| 347 | let to_url = | |
| 348 | Url::parse(&room_id.matrix_uri_via(via.iter().cloned(), false).to_string()) | |
| 349 | .unwrap(); | |
| 347 | let uri = if let Some(ev_id) = event_id { | |
| 348 | room_id.matrix_event_uri(ev_id.to_owned()).to_string() | |
| 349 | } else { | |
| 350 | room_id.matrix_uri_via(via.iter().cloned(), false).to_string() | |
| 351 | }; | |
| 352 | let to_url = Url::parse(&uri).unwrap(); | |
| 350 | 353 | urls.push((*c, to_url)); |
| 351 | 354 | } |
| 352 | 355 | }, |
| 353 | StyleTreeNode::RoomAlias(alias, c) => { | |
| 356 | StyleTreeNode::RoomAlias(alias, c, event_id) => { | |
| 354 | 357 | if let Some(c) = c { |
| 355 | let to_url = Url::parse(&alias.matrix_uri(false).to_string()).unwrap(); | |
| 358 | let uri = if let Some(ev_id) = event_id { | |
| 359 | #[expect( | |
| 360 | deprecated, | |
| 361 | reason = "we only use this if we received a deprecated link" | |
| 362 | )] | |
| 363 | alias.matrix_event_uri(ev_id.to_owned()).to_string() | |
| 364 | } else { | |
| 365 | alias.matrix_uri(false).to_string() | |
| 366 | }; | |
| 367 | let to_url = Url::parse(&uri).unwrap(); | |
| 356 | 368 | urls.push((*c, to_url)); |
| 357 | 369 | } |
| 358 | 370 | }, |
| @@ -523,12 +535,18 @@ impl StyleTreeNode { | ||
| 523 | 535 | let style = printer.settings().get_user_style(user_id); |
| 524 | 536 | printer.push_str(display_name.as_str(), style); |
| 525 | 537 | }, |
| 526 | StyleTreeNode::RoomId(room_id, _, _) => { | |
| 538 | StyleTreeNode::RoomId(room_id, _, _, event_id) => { | |
| 527 | 539 | let bold = style.add_modifier(StyleModifier::BOLD); |
| 540 | if event_id.is_some() { | |
| 541 | printer.push_str("Event in ", bold); | |
| 542 | } | |
| 528 | 543 | printer.push_str(room_id.as_str(), bold); |
| 529 | 544 | }, |
| 530 | StyleTreeNode::RoomAlias(alias, _) => { | |
| 545 | StyleTreeNode::RoomAlias(alias, _, event_id) => { | |
| 531 | 546 | let bold = style.add_modifier(StyleModifier::BOLD); |
| 547 | if event_id.is_some() { | |
| 548 | printer.push_str("Event in ", bold); | |
| 549 | } | |
| 532 | 550 | printer.push_str(alias.as_str(), bold); |
| 533 | 551 | }, |
| 534 | 552 | } |
| @@ -749,17 +767,20 @@ fn mxid2t( | ||
| 749 | 767 | h: &Url, |
| 750 | 768 | ) -> StyleTreeNode { |
| 751 | 769 | match id { |
| 752 | MatrixId::Room(room_id) => StyleTreeNode::RoomId(room_id.to_owned(), via.to_owned(), n), | |
| 753 | MatrixId::RoomAlias(alias) => StyleTreeNode::RoomAlias(alias.to_owned(), n), | |
| 770 | MatrixId::Room(room_id) => { | |
| 771 | StyleTreeNode::RoomId(room_id.to_owned(), via.to_owned(), n, None) | |
| 772 | }, | |
| 773 | MatrixId::RoomAlias(alias) => StyleTreeNode::RoomAlias(alias.to_owned(), n, None), | |
| 754 | 774 | MatrixId::User(user_id) => StyleTreeNode::UserId(user_id.to_owned(), n), |
| 755 | MatrixId::Event(room_or_alias_id, _) => { | |
| 775 | MatrixId::Event(room_or_alias_id, event_id) => { | |
| 756 | 776 | let room_or_alias_id: &matrix_sdk::ruma::RoomOrAliasId = room_or_alias_id; |
| 757 | // ignore event id for now | |
| 777 | let event_id = Some(event_id.to_owned()); | |
| 778 | ||
| 758 | 779 | if let Ok(alias_id) = <&matrix_sdk::ruma::RoomAliasId>::try_from(room_or_alias_id) { |
| 759 | StyleTreeNode::RoomAlias(alias_id.to_owned(), n) | |
| 780 | StyleTreeNode::RoomAlias(alias_id.to_owned(), n, event_id) | |
| 760 | 781 | } else { |
| 761 | 782 | let room_id = <&matrix_sdk::ruma::RoomId>::try_from(room_or_alias_id).unwrap(); |
| 762 | StyleTreeNode::RoomId(room_id.to_owned(), via.to_owned(), n) | |
| 783 | StyleTreeNode::RoomId(room_id.to_owned(), via.to_owned(), n, event_id) | |
| 763 | 784 | } |
| 764 | 785 | }, |
| 765 | 786 | _ => { |
src/message/mod.rs +78 −23
| @@ -4,11 +4,12 @@ use std::cmp::{Ord, PartialOrd}; | ||
| 4 | 4 | use std::collections::hash_map::DefaultHasher; |
| 5 | 5 | use std::convert::TryInto; |
| 6 | 6 | use std::hash::{Hash, Hasher}; |
| 7 | use std::sync::Arc; | |
| 7 | 8 | |
| 8 | 9 | use chrono::{DateTime, Local as LocalTz}; |
| 9 | 10 | use humansize::{DECIMAL, format_size}; |
| 10 | use matrix_sdk::ruma::OwnedTransactionId; | |
| 11 | 11 | use matrix_sdk::ruma::UInt; |
| 12 | use matrix_sdk::ruma::events::Mentions; | |
| 12 | 13 | use matrix_sdk::ruma::events::RedactedUnsigned; |
| 13 | 14 | use matrix_sdk::ruma::events::poll::start::RedactedPollStartEvent; |
| 14 | 15 | use matrix_sdk::ruma::events::poll::unstable_start::RedactedUnstablePollStartEvent; |
| @@ -27,6 +28,7 @@ use matrix_sdk::ruma::events::room::message::{ | ||
| 27 | 28 | use matrix_sdk::ruma::events::room::redaction::SyncRoomRedactionEvent; |
| 28 | 29 | use matrix_sdk::ruma::events::sticker::{OriginalStickerEvent, RedactedStickerEvent, StickerEvent}; |
| 29 | 30 | use matrix_sdk::ruma::events::{AnyRedactionEvent, MessageLikeEvent}; |
| 31 | use matrix_sdk::ruma::{OwnedTransactionId, UserId}; | |
| 30 | 32 | use matrix_sdk::send_queue::SendHandle; |
| 31 | 33 | use modalkit::editing::cursor::Cursor; |
| 32 | 34 | use ratatui::symbols::line::{HORIZONTAL, THICK_VERTICAL}; |
| @@ -50,7 +52,7 @@ pub mod poll; | ||
| 50 | 52 | pub use self::compose::{text_to_message, text_to_text_message_event_content}; |
| 51 | 53 | pub use self::html::TreeGenState; |
| 52 | 54 | |
| 53 | type ProtocolPreview<'a> = (&'a SlicedProtocol, u16, u16); | |
| 55 | type ProtocolPreview = (Arc<SlicedProtocol>, u16, u16); | |
| 54 | 56 | |
| 55 | 57 | /// The key used for uniquely identifying messages within a room and its threads. |
| 56 | 58 | /// |
| @@ -171,12 +173,27 @@ impl MessageId { | ||
| 171 | 173 | } |
| 172 | 174 | } |
| 173 | 175 | |
| 176 | impl Display for MessageId { | |
| 177 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 178 | match self { | |
| 179 | MessageId::Origin(id) => write!(f, "{}", id), | |
| 180 | MessageId::Local(id) => write!(f, "{}", id), | |
| 181 | } | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 174 | 185 | impl From<OwnedEventId> for MessageId { |
| 175 | 186 | fn from(value: OwnedEventId) -> Self { |
| 176 | 187 | Self::Origin(value) |
| 177 | 188 | } |
| 178 | 189 | } |
| 179 | 190 | |
| 191 | impl From<OwnedTransactionId> for MessageId { | |
| 192 | fn from(value: OwnedTransactionId) -> Self { | |
| 193 | Self::Local(value) | |
| 194 | } | |
| 195 | } | |
| 196 | ||
| 180 | 197 | #[derive(thiserror::Error, Debug)] |
| 181 | 198 | pub enum TimeStampIntError { |
| 182 | 199 | #[error("Integer conversion error: {0}")] |
| @@ -190,7 +207,7 @@ pub enum TimeStampIntError { | ||
| 190 | 207 | pub struct MessageTimeStamp(pub MilliSecondsSinceUnixEpoch); |
| 191 | 208 | |
| 192 | 209 | impl MessageTimeStamp { |
| 193 | fn as_datetime(self) -> DateTime<LocalTz> { | |
| 210 | pub(crate) fn as_datetime(self) -> DateTime<LocalTz> { | |
| 194 | 211 | let time = i64::from(self.0.0) / 1000; |
| 195 | 212 | let time = DateTime::from_timestamp(time, 0).unwrap_or_default(); |
| 196 | 213 | time.into() |
| @@ -472,6 +489,25 @@ impl MessageEvent { | ||
| 472 | 489 | self.msgtype().and_then(content_filename) |
| 473 | 490 | } |
| 474 | 491 | |
| 492 | pub fn mentions(&self) -> Option<&Mentions> { | |
| 493 | match self { | |
| 494 | MessageEvent::EncryptedOriginal(..) | | |
| 495 | MessageEvent::EncryptedRedacted(..) | | |
| 496 | MessageEvent::Redacted(..) | | |
| 497 | MessageEvent::Sticker(..) | | |
| 498 | MessageEvent::Poll(..) | | |
| 499 | MessageEvent::UnstablePoll(..) | | |
| 500 | MessageEvent::State(..) => None, | |
| 501 | MessageEvent::Original(ev, edits) => { | |
| 502 | edits | |
| 503 | .last_key_value() | |
| 504 | .and_then(|(_, ev)| ev.mentions.as_ref()) | |
| 505 | .or(ev.content.mentions.as_ref()) | |
| 506 | }, | |
| 507 | MessageEvent::Local(_, _, ev) => ev.mentions.as_ref(), | |
| 508 | } | |
| 509 | } | |
| 510 | ||
| 475 | 511 | fn message_style(&self, settings: &ApplicationSettings) -> Style { |
| 476 | 512 | let content = match self { |
| 477 | 513 | MessageEvent::EncryptedOriginal(_) | MessageEvent::EncryptedRedacted(_) => { |
| @@ -824,7 +860,7 @@ impl<'a> MessageFormatter<'a> { | ||
| 824 | 860 | info: &'a RoomInfo, |
| 825 | 861 | settings: &'a ApplicationSettings, |
| 826 | 862 | previews: &'a PreviewManager, |
| 827 | ) -> Option<ProtocolPreview<'a>> { | |
| 863 | ) -> Option<ProtocolPreview> { | |
| 828 | 864 | let reply_style = if settings.tunables.message_user_color { |
| 829 | 865 | style.patch(settings.get_user_color(&msg.sender)) |
| 830 | 866 | } else { |
| @@ -872,12 +908,12 @@ impl<'a> MessageFormatter<'a> { | ||
| 872 | 908 | |
| 873 | 909 | fn push_reactions( |
| 874 | 910 | &mut self, |
| 875 | counts: Vec<(&'a str, usize, &'a Option<MediaSource>)>, | |
| 911 | counts: Vec<(&'a str, Vec<&'a UserId>, &'a Option<MediaSource>)>, | |
| 876 | 912 | style: Style, |
| 877 | 913 | text: &mut Text<'a>, |
| 878 | 914 | settings: &ApplicationSettings, |
| 879 | 915 | previews: &'a PreviewManager, |
| 880 | ) -> Vec<ProtocolPreview<'a>> { | |
| 916 | ) -> Vec<ProtocolPreview> { | |
| 881 | 917 | let mut emojis = printer::TextPrinter::new(self.width(), style, self.settings, self.info); |
| 882 | 918 | let mut reactions = 0; |
| 883 | 919 | let mut protos = Vec::new(); |
| @@ -920,6 +956,8 @@ impl<'a> MessageFormatter<'a> { | ||
| 920 | 956 | |
| 921 | 957 | emojis.push_str("[", style); |
| 922 | 958 | if let Some(Some(proto)) = proto { |
| 959 | let proto = Arc::clone(proto); | |
| 960 | ||
| 923 | 961 | let (x, y) = emojis.cursor_pos(); |
| 924 | 962 | let y = (y + text.lines.len()) as u16; |
| 925 | 963 | let x = x as u16 + self.cols.user_gutter_width(settings); |
| @@ -928,7 +966,7 @@ impl<'a> MessageFormatter<'a> { | ||
| 928 | 966 | } |
| 929 | 967 | emojis.push_str(name, style); |
| 930 | 968 | emojis.push_str(" ", style); |
| 931 | emojis.push_span_nobreak(Span::styled(count.to_string(), style)); | |
| 969 | emojis.push_span_nobreak(Span::styled(count.len().to_string(), style)); | |
| 932 | 970 | emojis.push_str("]", style); |
| 933 | 971 | |
| 934 | 972 | reactions += 1; |
| @@ -1245,17 +1283,15 @@ impl Message { | ||
| 1245 | 1283 | /// Render the message as a [Text] object for the terminal. |
| 1246 | 1284 | /// |
| 1247 | 1285 | /// This will also get the image preview Protocol with an x/y offset. |
| 1248 | pub fn show_with_preview<'a, C>( | |
| 1286 | pub fn show_with_preview<'a>( | |
| 1249 | 1287 | &'a self, |
| 1250 | 1288 | prev: Option<&Message>, |
| 1251 | 1289 | selected: bool, |
| 1252 | vwctx: &ViewportContext<C>, | |
| 1290 | width: usize, | |
| 1253 | 1291 | info: &'a RoomInfo, |
| 1254 | 1292 | settings: &'a ApplicationSettings, |
| 1255 | 1293 | previews: &'a PreviewManager, |
| 1256 | ) -> (Text<'a>, Vec<ProtocolPreview<'a>>) { | |
| 1257 | let width = vwctx.get_width(); | |
| 1258 | ||
| 1294 | ) -> (Text<'a>, Vec<ProtocolPreview>) { | |
| 1259 | 1295 | let style = self.get_render_style(selected, settings); |
| 1260 | 1296 | let mut fmt = self.get_render_format(prev, width, info, settings); |
| 1261 | 1297 | let mut text = Text::default(); |
| @@ -1339,16 +1375,16 @@ impl Message { | ||
| 1339 | 1375 | (text, protos) |
| 1340 | 1376 | } |
| 1341 | 1377 | |
| 1342 | pub fn show<'a, C>( | |
| 1378 | pub fn show<'a>( | |
| 1343 | 1379 | &'a self, |
| 1344 | 1380 | prev: Option<&Message>, |
| 1345 | 1381 | selected: bool, |
| 1346 | vwctx: &ViewportContext<C>, | |
| 1382 | width: usize, | |
| 1347 | 1383 | info: &'a RoomInfo, |
| 1348 | 1384 | settings: &'a ApplicationSettings, |
| 1349 | 1385 | previews: &'a PreviewManager, |
| 1350 | 1386 | ) -> Text<'a> { |
| 1351 | self.show_with_preview(prev, selected, vwctx, info, settings, previews).0 | |
| 1387 | self.show_with_preview(prev, selected, width, info, settings, previews).0 | |
| 1352 | 1388 | } |
| 1353 | 1389 | |
| 1354 | 1390 | fn show_msg<'a>( |
| @@ -1358,7 +1394,7 @@ impl Message { | ||
| 1358 | 1394 | settings: &'a ApplicationSettings, |
| 1359 | 1395 | previews: &'a PreviewManager, |
| 1360 | 1396 | info: &'a RoomInfo, |
| 1361 | ) -> (Text<'a>, Option<&'a SlicedProtocol>) { | |
| 1397 | ) -> (Text<'a>, Option<Arc<SlicedProtocol>>) { | |
| 1362 | 1398 | let mut proto = None; |
| 1363 | 1399 | let placeholder = match self |
| 1364 | 1400 | .image_preview() |
| @@ -1372,7 +1408,7 @@ impl Message { | ||
| 1372 | 1408 | placeholder_frame(Some("Downloading..."), width, image_preview_size) |
| 1373 | 1409 | }, |
| 1374 | 1410 | Some(ImageStatus::Loaded(backend)) => { |
| 1375 | proto = Some(backend); | |
| 1411 | proto = Some(Arc::clone(backend)); | |
| 1376 | 1412 | placeholder_frame(None, width, &backend.size()) |
| 1377 | 1413 | }, |
| 1378 | 1414 | Some(ImageStatus::Error(err)) => Some(format!("[Image error: {err}]\n")), |
| @@ -1501,6 +1537,26 @@ impl Message { | ||
| 1501 | 1537 | |
| 1502 | 1538 | self.html = content_html(content); |
| 1503 | 1539 | } |
| 1540 | ||
| 1541 | pub fn find_links(&self) -> Vec<(char, Url)> { | |
| 1542 | let mut links = if let Some(html) = &self.html { | |
| 1543 | html.get_links() | |
| 1544 | } else { | |
| 1545 | vec![] | |
| 1546 | }; | |
| 1547 | ||
| 1548 | if links.is_empty() { | |
| 1549 | links = linkify::LinkFinder::new() | |
| 1550 | .links(&self.event.body()) | |
| 1551 | .filter_map(|u| Url::parse(u.as_str()).ok()) | |
| 1552 | .scan(TreeGenState { link_num: 0 }, |state, u| { | |
| 1553 | state.next_link_char().map(|c| (c, u)) | |
| 1554 | }) | |
| 1555 | .collect(); | |
| 1556 | } | |
| 1557 | ||
| 1558 | links | |
| 1559 | } | |
| 1504 | 1560 | } |
| 1505 | 1561 | |
| 1506 | 1562 | impl From<RoomEncryptedEvent> for Message { |
| @@ -1967,20 +2023,19 @@ pub mod tests { | ||
| 1967 | 2023 | let settings = mock_settings(); |
| 1968 | 2024 | let previews = PreviewManager::new(&settings); |
| 1969 | 2025 | let mut info = mock_room(); |
| 1970 | let vwctx: ViewportContext<MessageCursor> = | |
| 1971 | ViewportContext { dimensions: (60, 5), ..Default::default() }; | |
| 2026 | let width = 60; | |
| 1972 | 2027 | |
| 1973 | 2028 | let prev = mock_message1(); |
| 1974 | 2029 | let msg = mock_message2(); |
| 1975 | 2030 | let user_id = settings.profile.user_id.clone(); |
| 1976 | 2031 | |
| 1977 | 2032 | // Without a marker, no trackbar is drawn. |
| 1978 | let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews); | |
| 2033 | let text = msg.show(Some(&prev), false, width, &info, &settings, &previews); | |
| 1979 | 2034 | assert!(!has_trackbar(&text, 60)); |
| 1980 | 2035 | |
| 1981 | 2036 | // When the marker names the previous message, the rule leads the message. |
| 1982 | 2037 | info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG1_EVID.clone()); |
| 1983 | let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews); | |
| 2038 | let text = msg.show(Some(&prev), false, width, &info, &settings, &previews); | |
| 1984 | 2039 | assert!(has_trackbar(&text, 60)); |
| 1985 | 2040 | assert_eq!(text.lines[0].spans[0].content.as_ref(), HORIZONTAL.repeat(60)); |
| 1986 | 2041 | |
| @@ -1988,9 +2043,9 @@ pub mod tests { | ||
| 1988 | 2043 | // already positioned on the rendered message, not moving an existing marker backwards. |
| 1989 | 2044 | let mut info = mock_room(); |
| 1990 | 2045 | info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG2_EVID.clone()); |
| 1991 | let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews); | |
| 2046 | let text = msg.show(Some(&prev), false, width, &info, &settings, &previews); | |
| 1992 | 2047 | assert!(!has_trackbar(&text, 60)); |
| 1993 | let text = prev.show(Some(&msg), false, &vwctx, &info, &settings, &previews); | |
| 2048 | let text = prev.show(Some(&msg), false, width, &info, &settings, &previews); | |
| 1994 | 2049 | assert!(has_trackbar(&text, 60)); |
| 1995 | 2050 | } |
| 1996 | 2051 | |
src/message/printer.rs −1
| @@ -5,7 +5,6 @@ | ||
| 5 | 5 | //! contents). |
| 6 | 6 | |
| 7 | 7 | use crate::base::RoomInfo; |
| 8 | use crate::config::TunableValues; | |
| 9 | 8 | use crate::prelude::*; |
| 10 | 9 | use crate::util::{ |
| 11 | 10 | replace_emojis_in_line, |
src/message/state.rs +5 −4
| @@ -511,7 +511,7 @@ pub fn html_state(ev: &AnySyncStateEvent) -> StyleTree { | ||
| 511 | 511 | .. |
| 512 | 512 | }) => { |
| 513 | 513 | if let Some(canon) = content.alias.as_ref() { |
| 514 | let canon = StyleTreeNode::RoomAlias(canon.to_owned(), Some('0')); | |
| 514 | let canon = StyleTreeNode::RoomAlias(canon.to_owned(), Some('0'), None); | |
| 515 | 515 | let prefix = |
| 516 | 516 | StyleTreeNode::Text("* updated the canonical alias for the room to: ".into()); |
| 517 | 517 | vec![prefix, canon] |
| @@ -768,7 +768,8 @@ pub fn html_state(ev: &AnySyncStateEvent) -> StyleTree { | ||
| 768 | 768 | .. |
| 769 | 769 | }) => { |
| 770 | 770 | let prefix = StyleTreeNode::Text("* upgraded the room; replacement room is ".into()); |
| 771 | let room = StyleTreeNode::RoomId(content.replacement_room.clone(), vec![], Some('0')); | |
| 771 | let room = | |
| 772 | StyleTreeNode::RoomId(content.replacement_room.clone(), vec![], Some('0'), None); | |
| 772 | 773 | vec![prefix, room] |
| 773 | 774 | }, |
| 774 | 775 | AnyStateEventContentChange::RoomTopic(StateEventContentChange::Original { |
| @@ -783,7 +784,7 @@ pub fn html_state(ev: &AnySyncStateEvent) -> StyleTree { | ||
| 783 | 784 | let prefix = StyleTreeNode::Text("* added a space child: ".into()); |
| 784 | 785 | |
| 785 | 786 | let room_id = if let Ok(room_id) = OwnedRoomId::from_str(ev.state_key()) { |
| 786 | StyleTreeNode::RoomId(room_id, vec![], Some('0')) | |
| 787 | StyleTreeNode::RoomId(room_id, vec![], Some('0'), None) | |
| 787 | 788 | } else { |
| 788 | 789 | bold(ev.state_key().to_string()) |
| 789 | 790 | }; |
| @@ -801,7 +802,7 @@ pub fn html_state(ev: &AnySyncStateEvent) -> StyleTree { | ||
| 801 | 802 | }; |
| 802 | 803 | |
| 803 | 804 | let room_id = if let Ok(room_id) = OwnedRoomId::from_str(ev.state_key()) { |
| 804 | StyleTreeNode::RoomId(room_id, vec![], Some('0')) | |
| 805 | StyleTreeNode::RoomId(room_id, vec![], Some('0'), None) | |
| 805 | 806 | } else { |
| 806 | 807 | bold(ev.state_key().to_string()) |
| 807 | 808 | }; |
src/prelude.rs +10 −2
| @@ -90,12 +90,20 @@ pub use crate::base::{ | ||
| 90 | 90 | RoomFocus, |
| 91 | 91 | RoomInfo, |
| 92 | 92 | RoomInfoFlags, |
| 93 | RoomView, | |
| 93 | 94 | SendAction, |
| 94 | 95 | SpaceAction, |
| 95 | 96 | TimelineAction, |
| 96 | 97 | }; |
| 97 | 98 | pub use crate::config::theme::ThemeValues; |
| 98 | pub use crate::config::{Aliases, ApplicationSettings}; | |
| 99 | pub use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages}; | |
| 99 | pub use crate::config::{Aliases, ApplicationSettings, TunableValues}; | |
| 100 | pub use crate::message::{ | |
| 101 | Message, | |
| 102 | MessageEvent, | |
| 103 | MessageId, | |
| 104 | MessageKey, | |
| 105 | MessageTimeStamp, | |
| 106 | Messages, | |
| 107 | }; | |
| 100 | 108 | pub use crate::preview::{PreviewKind, PreviewManager}; |
| 101 | 109 | pub use crate::worker::Requester; |
src/preview.rs +2 −2
| @@ -11,7 +11,7 @@ use crate::prelude::*; | ||
| 11 | 11 | pub enum ImageStatus { |
| 12 | 12 | Queued(Size), |
| 13 | 13 | Downloading(Size), |
| 14 | Loaded(SlicedProtocol), | |
| 14 | Loaded(Arc<SlicedProtocol>), | |
| 15 | 15 | Error(String), |
| 16 | 16 | } |
| 17 | 17 | |
| @@ -163,7 +163,7 @@ pub async fn load_image( | ||
| 163 | 163 | let image = handle.await.map_err(|err| IambError::Preview(err.to_string()))??; |
| 164 | 164 | std::mem::drop(permit); |
| 165 | 165 | |
| 166 | Ok(ImageStatus::Loaded(image)) | |
| 166 | Ok(ImageStatus::Loaded(Arc::new(image))) | |
| 167 | 167 | } |
| 168 | 168 | let key = source.unique_key(); |
| 169 | 169 | |
src/windows/mod.rs +18 −9
| @@ -18,11 +18,13 @@ use matrix_sdk::ruma::events::room::member::MembershipState; | ||
| 18 | 18 | use matrix_sdk::ruma::events::room::power_levels::UserPowerLevel; |
| 19 | 19 | use matrix_sdk::ruma::room::RoomType as MatrixRoomType; |
| 20 | 20 | use matrix_sdk::ruma::{RoomAliasId, RoomOrAliasId}; |
| 21 | use modalkit::editing::context::EditContext; | |
| 21 | 22 | use modalkit_ratatui::Window; |
| 22 | 23 | use modalkit_ratatui::list::{List, ListCursor, ListItem, ListState}; |
| 23 | 24 | use ratatui::prelude::Stylize; |
| 24 | 25 | |
| 25 | 26 | use crate::base::{ |
| 27 | RoomView, | |
| 26 | 28 | SortColumn, |
| 27 | 29 | SortFieldRoom, |
| 28 | 30 | SortFieldSpace, |
| @@ -464,7 +466,7 @@ fn room_prompt( | ||
| 464 | 466 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 465 | 467 | match act { |
| 466 | 468 | PromptAction::Submit => { |
| 467 | let room = IambId::Room(room_id.to_owned().into(), None); | |
| 469 | let room = IambId::Room(room_id.to_owned().into(), RoomView::Main); | |
| 468 | 470 | let open = WindowAction::Switch(OpenTarget::Application(room)); |
| 469 | 471 | let acts = vec![(open.into(), ctx.clone())]; |
| 470 | 472 | |
| @@ -548,7 +550,7 @@ impl IambWindow { | ||
| 548 | 550 | act: MessageAction, |
| 549 | 551 | ctx: ProgramContext, |
| 550 | 552 | store: &mut ProgramStore, |
| 551 | ) -> IambResult<EditInfo> { | |
| 553 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 552 | 554 | if let IambWindow::Room(w) = self { |
| 553 | 555 | w.message_command(act, ctx, store).await |
| 554 | 556 | } else { |
| @@ -591,11 +593,18 @@ impl IambWindow { | ||
| 591 | 593 | _ => None, |
| 592 | 594 | }; |
| 593 | 595 | |
| 594 | if let Some(id) = id { | |
| 595 | room_command(id, act, ctx, store).await | |
| 596 | } else { | |
| 596 | let Some(id) = id else { | |
| 597 | 597 | return Err(IambError::NoSelectedRoomOrSpace.into()); |
| 598 | } | |
| 598 | }; | |
| 599 | let id = id.to_owned(); | |
| 600 | ||
| 601 | let msg_id = match self { | |
| 602 | IambWindow::Room(RoomState::Chat(chat)) => chat.current_message(store), | |
| 603 | IambWindow::Room(RoomState::Message(message)) => Some(message.id().to_owned()), | |
| 604 | _ => None, | |
| 605 | }; | |
| 606 | ||
| 607 | room_command(id, act, ctx, store, msg_id).await | |
| 599 | 608 | } |
| 600 | 609 | |
| 601 | 610 | pub async fn join_command( |
| @@ -1313,7 +1322,7 @@ impl Window<IambInfo> for IambWindow { | ||
| 1313 | 1322 | return Err(UIError::Failure("Could not parse room identifier".to_string())); |
| 1314 | 1323 | }; |
| 1315 | 1324 | |
| 1316 | let id = IambId::Room(room_alias, None); | |
| 1325 | let id = IambId::Room(room_alias, RoomView::Main); | |
| 1317 | 1326 | IambWindow::open(id, store) |
| 1318 | 1327 | } |
| 1319 | 1328 | |
| @@ -1812,7 +1821,7 @@ impl ListItem<IambInfo> for PinnedItem { | ||
| 1812 | 1821 | }; |
| 1813 | 1822 | |
| 1814 | 1823 | let previews = &store.application.previews; |
| 1815 | msg.show(None, selected, vwctx, info, settings, previews) | |
| 1824 | msg.show(None, selected, vwctx.get_width(), info, settings, previews) | |
| 1816 | 1825 | } |
| 1817 | 1826 | |
| 1818 | 1827 | fn get_word(&self) -> Option<String> { |
| @@ -1835,7 +1844,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for PinnedItem { | ||
| 1835 | 1844 | .and_then(|(thread, _)| thread) |
| 1836 | 1845 | .map(ToOwned::to_owned); |
| 1837 | 1846 | |
| 1838 | let room = IambId::Room(self.room_id.clone().into(), thread); | |
| 1847 | let room = IambId::Room(self.room_id.clone().into(), thread.into()); | |
| 1839 | 1848 | let open = WindowAction::Switch(OpenTarget::Application(room)); |
| 1840 | 1849 | let jump = IambAction::from(TimelineAction::GotoEvent(self.event_id.clone())); |
| 1841 | 1850 | |
src/windows/room/chat.rs +453 −385
| @@ -24,6 +24,7 @@ use matrix_sdk::ruma::events::room::message::{ | ||
| 24 | 24 | }; |
| 25 | 25 | use matrix_sdk::ruma::events::room::pinned_events::RoomPinnedEventsEventContent; |
| 26 | 26 | use matrix_sdk::send_queue::RoomSendQueueError; |
| 27 | use modalkit::editing::context::EditContext; | |
| 27 | 28 | use modalkit::editing::history::{self, HistoryList}; |
| 28 | 29 | use modalkit::editing::store::RegisterError; |
| 29 | 30 | use modalkit::keybindings::dialog::{Dialog, MultiChoice, MultiChoiceItem}; |
| @@ -34,12 +35,7 @@ use regex::Regex; | ||
| 34 | 35 | |
| 35 | 36 | use crate::base::{DownloadFlags, EchoLocation}; |
| 36 | 37 | use crate::config::EncryptionIndicatorLocation; |
| 37 | use crate::message::{ | |
| 38 | MessageId, | |
| 39 | TreeGenState, | |
| 40 | text_to_message, | |
| 41 | text_to_text_message_event_content, | |
| 42 | }; | |
| 38 | use crate::message::{MessageId, text_to_message, text_to_text_message_event_content}; | |
| 43 | 39 | use crate::prelude::*; |
| 44 | 40 | use crate::util::SuspendedTty; |
| 45 | 41 | use crate::windows::room::scrollback::{Scrollback, ScrollbackState}; |
| @@ -72,7 +68,7 @@ impl ChatState { | ||
| 72 | 68 | pub fn new(room: MatrixRoom, thread: Option<OwnedEventId>, store: &mut ProgramStore) -> Self { |
| 73 | 69 | let room_id = room.room_id().to_owned(); |
| 74 | 70 | let scrollback = ScrollbackState::new(room_id.clone(), thread.clone()); |
| 75 | let id = IambBufferId::Room(room_id.clone(), thread, RoomFocus::MessageBar); | |
| 71 | let id = IambBufferId::Room(room_id.clone(), thread.into(), RoomFocus::MessageBar); | |
| 76 | 72 | let ebuf = store.load_buffer(id); |
| 77 | 73 | let tbox = TextBoxState::new(ebuf); |
| 78 | 74 | |
| @@ -97,18 +93,6 @@ impl ChatState { | ||
| 97 | 93 | self.scrollback.thread() |
| 98 | 94 | } |
| 99 | 95 | |
| 100 | fn get_joined(&self, worker: &Requester) -> Result<MatrixRoom, IambError> { | |
| 101 | let Some(room) = worker.client.get_room(self.id()) else { | |
| 102 | return Err(IambError::NotJoined); | |
| 103 | }; | |
| 104 | ||
| 105 | if room.state() == MatrixRoomState::Joined { | |
| 106 | Ok(room) | |
| 107 | } else { | |
| 108 | Err(IambError::NotJoined) | |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | 96 | fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> { |
| 113 | 97 | let thread = self.scrollback.get_thread(info)?; |
| 114 | 98 | let key = self.reply_to.as_ref()?; |
| @@ -205,9 +189,9 @@ impl ChatState { | ||
| 205 | 189 | pub async fn message_command( |
| 206 | 190 | &mut self, |
| 207 | 191 | act: MessageAction, |
| 208 | _: ProgramContext, | |
| 192 | ctx: ProgramContext, | |
| 209 | 193 | store: &mut ProgramStore, |
| 210 | ) -> IambResult<EditInfo> { | |
| 194 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 211 | 195 | let client = &store.application.worker.client; |
| 212 | 196 | |
| 213 | 197 | let settings = &store.application.settings; |
| @@ -220,7 +204,7 @@ impl ChatState { | ||
| 220 | 204 | if skip_confirm { |
| 221 | 205 | self.reset(); |
| 222 | 206 | |
| 223 | return Ok(None); | |
| 207 | return Ok(vec![]); | |
| 224 | 208 | } |
| 225 | 209 | |
| 226 | 210 | self.reply_to = None; |
| @@ -234,116 +218,16 @@ impl ChatState { | ||
| 234 | 218 | Err(UIError::NeedConfirm(prompt)) |
| 235 | 219 | }, |
| 236 | 220 | MessageAction::Download(filename, flags) => { |
| 237 | if let MessageEvent::State(_) = &msg.event { | |
| 238 | let err = open_links(msg); | |
| 239 | return Err(err); | |
| 240 | } | |
| 241 | ||
| 242 | if let Some(msgtype) = msg.event.msgtype() { | |
| 243 | // A location message has no attachment to download, so `:open` | |
| 244 | // hands its `geo:` URI over to the system handler, which will | |
| 245 | // let the desktop open it with an application of its choosing. | |
| 246 | if let Some(geo_uri) = location_geo_uri(msgtype) { | |
| 247 | if !flags.contains(DownloadFlags::OPEN) { | |
| 248 | return Err(IambError::NoAttachment.into()); | |
| 249 | } | |
| 250 | ||
| 251 | let target = OsString::from(geo_uri.to_owned()); | |
| 252 | ||
| 253 | return match open_command( | |
| 254 | store.application.settings.tunables.open_command.as_ref(), | |
| 255 | target, | |
| 256 | ) { | |
| 257 | Ok(_) => Ok(InfoMessage::from(format!("Opened {geo_uri}")).into()), | |
| 258 | Err(err) => Err(err), | |
| 259 | }; | |
| 260 | } | |
| 261 | ||
| 262 | let media = client.media(); | |
| 263 | let mut filename = match (filename, &settings.dirs.downloads) { | |
| 264 | (Some(f), _) => f, | |
| 265 | (None, Some(downloads)) => downloads.clone(), | |
| 266 | (None, None) => return Err(IambError::NoDownloadDir.into()), | |
| 267 | }; | |
| 268 | let (source, msg_filename) = match msgtype { | |
| 269 | MessageType::Audio(c) => (c.source.clone(), c.filename()), | |
| 270 | MessageType::File(c) => (c.source.clone(), c.filename()), | |
| 271 | MessageType::Image(c) => (c.source.clone(), c.filename()), | |
| 272 | MessageType::Video(c) => (c.source.clone(), c.filename()), | |
| 273 | _ => { | |
| 274 | if !flags.contains(DownloadFlags::OPEN) { | |
| 275 | return Err(IambError::NoAttachment.into()); | |
| 276 | } | |
| 277 | let err = open_links(msg); | |
| 278 | return Err(err); | |
| 279 | }, | |
| 280 | }; | |
| 281 | if filename.is_dir() { | |
| 282 | filename.push(msg_filename.replace(std::path::MAIN_SEPARATOR_STR, "_")); | |
| 283 | } | |
| 284 | if filename.exists() && !flags.contains(DownloadFlags::FORCE) { | |
| 285 | // Find an incrementally suffixed filename, e.g. image-2.jpg -> image-3.jpg | |
| 286 | if let Some(stem) = filename.file_stem().and_then(OsStr::to_str) { | |
| 287 | let ext = filename.extension(); | |
| 288 | let mut filename_incr = filename.clone(); | |
| 289 | for n in 1..=1000 { | |
| 290 | if let Some(ext) = ext.and_then(OsStr::to_str) { | |
| 291 | filename_incr.set_file_name(format!("{stem}-{n}.{ext}")); | |
| 292 | } else { | |
| 293 | filename_incr.set_file_name(format!("{stem}-{n}")); | |
| 294 | } | |
| 295 | ||
| 296 | if !filename_incr.exists() { | |
| 297 | filename = filename_incr; | |
| 298 | break; | |
| 299 | } | |
| 300 | } | |
| 301 | } | |
| 302 | } | |
| 303 | if !filename.exists() || flags.contains(DownloadFlags::FORCE) { | |
| 304 | let req = MediaRequestParameters { source, format: MediaFormat::File }; | |
| 305 | ||
| 306 | let bytes = | |
| 307 | media.get_media_content(&req, true).await.map_err(IambError::from)?; | |
| 308 | ||
| 309 | fs::write(filename.as_path(), bytes.as_slice())?; | |
| 310 | ||
| 311 | msg.downloaded = true; | |
| 312 | } else if !flags.contains(DownloadFlags::OPEN) { | |
| 313 | let msg = format!( | |
| 314 | "The file {} already exists; add ! to end of command to overwrite it.", | |
| 315 | filename.display() | |
| 316 | ); | |
| 317 | let err = UIError::Failure(msg); | |
| 318 | ||
| 319 | return Err(err); | |
| 320 | } | |
| 321 | let info = if flags.contains(DownloadFlags::OPEN) { | |
| 322 | let target = filename.clone().into_os_string(); | |
| 323 | match open_command( | |
| 324 | store.application.settings.tunables.open_command.as_ref(), | |
| 325 | target, | |
| 326 | ) { | |
| 327 | Ok(_) => { | |
| 328 | InfoMessage::from(format!( | |
| 329 | "Attachment downloaded to {} and opened", | |
| 330 | filename.display() | |
| 331 | )) | |
| 332 | }, | |
| 333 | Err(err) => { | |
| 334 | return Err(err); | |
| 335 | }, | |
| 336 | } | |
| 337 | } else { | |
| 338 | InfoMessage::from(format!( | |
| 339 | "Attachment downloaded to {}", | |
| 340 | filename.display() | |
| 341 | )) | |
| 342 | }; | |
| 343 | return Ok(info.into()); | |
| 344 | } | |
| 345 | ||
| 346 | Err(IambError::NoAttachment.into()) | |
| 221 | msg_download( | |
| 222 | ctx, | |
| 223 | client, | |
| 224 | msg, | |
| 225 | &store.application.settings, | |
| 226 | filename, | |
| 227 | flags, | |
| 228 | &store.application.settings.tunables, | |
| 229 | ) | |
| 230 | .await | |
| 347 | 231 | }, |
| 348 | 232 | MessageAction::Edit => { |
| 349 | 233 | if msg.sender != settings.profile.user_id { |
| @@ -375,182 +259,34 @@ impl ChatState { | ||
| 375 | 259 | self.editing = self.scrollback.get_key(info); |
| 376 | 260 | self.focus = RoomFocus::MessageBar; |
| 377 | 261 | |
| 378 | Ok(None) | |
| 262 | Ok(vec![]) | |
| 379 | 263 | }, |
| 380 | 264 | MessageAction::React(reaction, literal) => { |
| 381 | let emoji = if literal { | |
| 382 | reaction | |
| 383 | } else if let Some(emoji) = | |
| 384 | emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction)) | |
| 385 | { | |
| 386 | emoji.to_string() | |
| 387 | } else { | |
| 388 | let msg = format!( | |
| 389 | "{reaction:?} is not a known Emoji shortcode; do you want to react with exactly {reaction:?}?" | |
| 390 | ); | |
| 391 | let act = IambAction::Message(MessageAction::React(reaction, true)); | |
| 392 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 393 | let prompt = Box::new(prompt); | |
| 394 | ||
| 395 | return Err(UIError::NeedConfirm(prompt)); | |
| 396 | }; | |
| 397 | ||
| 398 | let room = self.get_joined(&store.application.worker)?; | |
| 399 | let event_id = match &msg.event { | |
| 400 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 401 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 402 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 403 | MessageEvent::Local(..) => { | |
| 404 | // XXX: Implement reactions for local echos | |
| 405 | ||
| 406 | let msg = "Cannot react to a local echo"; | |
| 407 | let err = UIError::Failure(msg.into()); | |
| 408 | ||
| 409 | return Err(err); | |
| 410 | }, | |
| 411 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 412 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 413 | MessageEvent::Redacted(_, _) => { | |
| 414 | let msg = "Cannot react to a redacted message"; | |
| 415 | let err = UIError::Failure(msg.into()); | |
| 416 | ||
| 417 | return Err(err); | |
| 418 | }, | |
| 419 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 420 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 421 | }; | |
| 422 | ||
| 423 | if info.user_reactions_contains(&settings.profile.user_id, &event_id, &emoji) { | |
| 424 | let msg = format!("You’ve already reacted to this message with {emoji}"); | |
| 425 | let err = UIError::Failure(msg); | |
| 426 | ||
| 427 | return Err(err); | |
| 428 | } | |
| 429 | ||
| 430 | let reaction = Annotation::new(event_id, emoji); | |
| 431 | let msg = ReactionEventContent::new(reaction); | |
| 432 | ||
| 433 | room.send_queue().send(msg.into()).await.map_err(IambError::from)?; | |
| 434 | ||
| 435 | Ok(None) | |
| 265 | let msg = self.scrollback.get(info).unwrap(); | |
| 266 | msg_react( | |
| 267 | msg, | |
| 268 | settings, | |
| 269 | info, | |
| 270 | &store.application.worker, | |
| 271 | self.id(), | |
| 272 | reaction, | |
| 273 | literal, | |
| 274 | ) | |
| 275 | .await | |
| 436 | 276 | }, |
| 437 | 277 | MessageAction::Pin | MessageAction::Unpin => { |
| 438 | 278 | let pin = act == MessageAction::Pin; |
| 439 | 279 | |
| 440 | let event_id = match &msg.event { | |
| 441 | MessageEvent::Local(..) => { | |
| 442 | let msg = "Cannot pin a message that hasn't been sent yet"; | |
| 443 | return Err(UIError::Failure(msg.into())); | |
| 444 | }, | |
| 445 | MessageEvent::Redacted(..) | MessageEvent::EncryptedRedacted(_) if pin => { | |
| 446 | let msg = "Cannot pin a redacted message"; | |
| 447 | return Err(UIError::Failure(msg.into())); | |
| 448 | }, | |
| 449 | event => { | |
| 450 | event | |
| 451 | .event_id() | |
| 452 | .map(ToOwned::to_owned) | |
| 453 | .ok_or(IambError::NoSelectedMessage)? | |
| 454 | }, | |
| 455 | }; | |
| 456 | ||
| 457 | let room = self.get_joined(&store.application.worker)?; | |
| 458 | ||
| 459 | let can_pin = room | |
| 460 | .power_levels() | |
| 461 | .await | |
| 462 | .map_err(matrix_sdk::Error::from) | |
| 463 | .map_err(IambError::from)? | |
| 464 | .user_can_send_state( | |
| 465 | &settings.profile.user_id, | |
| 466 | StateEventType::RoomPinnedEvents, | |
| 467 | ); | |
| 468 | ||
| 469 | if !can_pin { | |
| 470 | return Err(IambError::InsufficientPermission.into()); | |
| 471 | } | |
| 472 | ||
| 473 | // The state event holds the whole list, so rebuild it from the SDK's latest copy. | |
| 474 | let mut pinned = room.pinned_event_ids().unwrap_or_default(); | |
| 475 | let position = pinned.iter().position(|id| *id == event_id); | |
| 476 | ||
| 477 | match (pin, position) { | |
| 478 | (true, Some(_)) => { | |
| 479 | let msg = "This message is already pinned"; | |
| 480 | return Err(UIError::Failure(msg.into())); | |
| 481 | }, | |
| 482 | (false, None) => { | |
| 483 | let msg = "This message is not pinned"; | |
| 484 | return Err(UIError::Failure(msg.into())); | |
| 485 | }, | |
| 486 | (true, None) => { | |
| 487 | pinned.push(event_id); | |
| 488 | }, | |
| 489 | (false, Some(idx)) => { | |
| 490 | pinned.remove(idx); | |
| 491 | }, | |
| 492 | } | |
| 493 | ||
| 494 | room.send_state_event(RoomPinnedEventsEventContent::new(pinned)) | |
| 495 | .await | |
| 496 | .map_err(IambError::from)?; | |
| 497 | ||
| 498 | Ok(None) | |
| 280 | msg_pin(pin, msg, self.id(), &store.application.worker, settings).await | |
| 499 | 281 | }, |
| 500 | 282 | MessageAction::Redact(reason, skip_confirm) => { |
| 501 | if !skip_confirm { | |
| 502 | let msg = "Are you sure you want to redact this message?"; | |
| 503 | let act = IambAction::Message(MessageAction::Redact(reason, true)); | |
| 504 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 505 | let prompt = Box::new(prompt); | |
| 506 | ||
| 507 | return Err(UIError::NeedConfirm(prompt)); | |
| 508 | } | |
| 509 | ||
| 510 | let room = self.get_joined(&store.application.worker)?; | |
| 511 | let event_id = match &msg.event { | |
| 512 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 513 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 514 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 515 | MessageEvent::Local(_, handle, _) => { | |
| 516 | let succeeded = handle | |
| 517 | .abort() | |
| 518 | .await | |
| 519 | .map_err(RoomSendQueueError::from) | |
| 520 | .map_err(IambError::from)?; | |
| 521 | ||
| 522 | if !succeeded { | |
| 523 | let msg = "local echo was already sent; please retry"; | |
| 524 | let err = UIError::Failure(msg.into()); | |
| 525 | ||
| 526 | return Err(err); | |
| 527 | } | |
| 528 | ||
| 529 | return Ok(None); | |
| 530 | }, | |
| 531 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 532 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 533 | MessageEvent::Redacted(_, _) => { | |
| 534 | let msg = "Cannot redact already redacted message"; | |
| 535 | let err = UIError::Failure(msg.into()); | |
| 536 | ||
| 537 | return Err(err); | |
| 538 | }, | |
| 539 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 540 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 541 | }; | |
| 542 | ||
| 543 | let event_id = event_id.as_ref(); | |
| 544 | let reason = reason.as_deref(); | |
| 545 | let _ = room.redact(event_id, reason, None).await.map_err(IambError::from)?; | |
| 546 | ||
| 547 | Ok(None) | |
| 283 | msg_redact(msg, &store.application.worker, self.id(), reason, skip_confirm).await | |
| 548 | 284 | }, |
| 549 | 285 | MessageAction::Reply => { |
| 550 | 286 | self.reply_to = self.scrollback.get_key(info); |
| 551 | 287 | self.focus = RoomFocus::MessageBar; |
| 552 | 288 | |
| 553 | Ok(None) | |
| 289 | Ok(vec![]) | |
| 554 | 290 | }, |
| 555 | 291 | MessageAction::Replied => { |
| 556 | 292 | let Some(reply) = msg.reply_to() else { |
| @@ -558,81 +294,23 @@ impl ChatState { | ||
| 558 | 294 | return Err(UIError::Failure(msg.into())); |
| 559 | 295 | }; |
| 560 | 296 | |
| 561 | self.jump_to_message(reply, store) | |
| 297 | let msg = self.jump_to_message(reply, store)?; | |
| 298 | let msg = msg.map(|msg| (Action::ShowInfoMessage(msg), ctx)); | |
| 299 | ||
| 300 | Ok(msg.into_iter().collect()) | |
| 562 | 301 | }, |
| 563 | 302 | MessageAction::Unreact(reaction, literal) => { |
| 564 | let emoji = match reaction { | |
| 565 | reaction if literal => reaction, | |
| 566 | Some(reaction) => { | |
| 567 | if let Some(emoji) = | |
| 568 | emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction)) | |
| 569 | { | |
| 570 | Some(emoji.to_string()) | |
| 571 | } else { | |
| 572 | let msg = format!( | |
| 573 | "{reaction:?} is not a known Emoji shortcode; do you want to remove exactly {reaction:?}?" | |
| 574 | ); | |
| 575 | let act = | |
| 576 | IambAction::Message(MessageAction::Unreact(Some(reaction), true)); | |
| 577 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 578 | let prompt = Box::new(prompt); | |
| 579 | ||
| 580 | return Err(UIError::NeedConfirm(prompt)); | |
| 581 | } | |
| 582 | }, | |
| 583 | None => None, | |
| 584 | }; | |
| 585 | ||
| 586 | let room = self.get_joined(&store.application.worker)?; | |
| 587 | let event_id = match &msg.event { | |
| 588 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 589 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 590 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 591 | MessageEvent::Local(..) => { | |
| 592 | let msg = "Cannot unreact to a local echo"; | |
| 593 | let err = UIError::Failure(msg.into()); | |
| 594 | ||
| 595 | return Err(err); | |
| 596 | }, | |
| 597 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 598 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 599 | MessageEvent::Redacted(_, _) => { | |
| 600 | let msg = "Cannot unreact to a redacted message"; | |
| 601 | let err = UIError::Failure(msg.into()); | |
| 602 | ||
| 603 | return Err(err); | |
| 604 | }, | |
| 605 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 606 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 607 | }; | |
| 608 | ||
| 609 | let reactions = match info.reactions.get(&event_id) { | |
| 610 | Some(r) => r, | |
| 611 | None => return Ok(None), | |
| 612 | }; | |
| 613 | ||
| 614 | let reactions = | |
| 615 | reactions.iter().filter_map(|(event_id, (reaction, user_id, _))| { | |
| 616 | if user_id != &settings.profile.user_id { | |
| 617 | return None; | |
| 618 | } | |
| 619 | ||
| 620 | if let Some(emoji) = &emoji { | |
| 621 | if emoji == reaction { | |
| 622 | return Some(event_id); | |
| 623 | } else { | |
| 624 | return None; | |
| 625 | } | |
| 626 | } else { | |
| 627 | return Some(event_id); | |
| 628 | } | |
| 629 | }); | |
| 630 | ||
| 631 | for reaction in reactions { | |
| 632 | let _ = room.redact(reaction, None, None).await.map_err(IambError::from)?; | |
| 633 | } | |
| 634 | ||
| 635 | Ok(None) | |
| 303 | let msg = self.scrollback.get(info).unwrap(); | |
| 304 | msg_unreact( | |
| 305 | msg, | |
| 306 | settings, | |
| 307 | info, | |
| 308 | &store.application.worker, | |
| 309 | self.id(), | |
| 310 | reaction, | |
| 311 | literal, | |
| 312 | ) | |
| 313 | .await | |
| 636 | 314 | }, |
| 637 | 315 | } |
| 638 | 316 | } |
| @@ -701,7 +379,7 @@ impl ChatState { | ||
| 701 | 379 | ) -> IambResult<EditInfo> { |
| 702 | 380 | let settings = &store.application.settings; |
| 703 | 381 | let tunables = &settings.tunables; |
| 704 | let room = self.get_joined(&store.application.worker)?; | |
| 382 | let room = get_joined(self.id(), &store.application.worker)?; | |
| 705 | 383 | let info = store.application.rooms.get_or_default(self.id().to_owned()); |
| 706 | 384 | |
| 707 | 385 | match act { |
| @@ -909,6 +587,11 @@ impl ChatState { | ||
| 909 | 587 | &self.room_id |
| 910 | 588 | } |
| 911 | 589 | |
| 590 | pub fn current_message(&self, store: &mut ProgramStore) -> Option<MessageId> { | |
| 591 | let info = store.application.rooms.get_or_default(self.room_id.clone()); | |
| 592 | self.scrollback.get_key(info).map(|key| key.id) | |
| 593 | } | |
| 594 | ||
| 912 | 595 | pub fn auto_toggle_focus( |
| 913 | 596 | &mut self, |
| 914 | 597 | act: &EditorAction, |
| @@ -935,20 +618,20 @@ impl ChatState { | ||
| 935 | 618 | } |
| 936 | 619 | } |
| 937 | 620 | |
| 938 | fn open_links(msg: &Message) -> UIError<IambInfo> { | |
| 939 | let mut links = if let Some(html) = &msg.html { | |
| 940 | html.get_links() | |
| 941 | } else { | |
| 942 | vec![] | |
| 621 | fn get_joined(id: &RoomId, worker: &Requester) -> Result<MatrixRoom, IambError> { | |
| 622 | let Some(room) = worker.client.get_room(id) else { | |
| 623 | return Err(IambError::NotJoined); | |
| 943 | 624 | }; |
| 944 | 625 | |
| 945 | if links.is_empty() { | |
| 946 | links = linkify::LinkFinder::new() | |
| 947 | .links(&msg.event.body()) | |
| 948 | .filter_map(|u| Url::parse(u.as_str()).ok()) | |
| 949 | .scan(TreeGenState { link_num: 0 }, |state, u| state.next_link_char().map(|c| (c, u))) | |
| 950 | .collect(); | |
| 626 | if room.state() == MatrixRoomState::Joined { | |
| 627 | Ok(room) | |
| 628 | } else { | |
| 629 | Err(IambError::NotJoined) | |
| 951 | 630 | } |
| 631 | } | |
| 632 | ||
| 633 | fn open_links(msg: &Message) -> UIError<IambInfo> { | |
| 634 | let links = msg.find_links(); | |
| 952 | 635 | |
| 953 | 636 | if links.is_empty() { |
| 954 | 637 | return IambError::NoAttachment.into(); |
| @@ -966,6 +649,391 @@ fn open_links(msg: &Message) -> UIError<IambInfo> { | ||
| 966 | 649 | UIError::NeedConfirm(Box::new(dialog)) |
| 967 | 650 | } |
| 968 | 651 | |
| 652 | pub async fn msg_download( | |
| 653 | ctx: ProgramContext, | |
| 654 | client: &Client, | |
| 655 | msg: &mut Message, | |
| 656 | settings: &ApplicationSettings, | |
| 657 | filename: Option<PathBuf>, | |
| 658 | flags: DownloadFlags, | |
| 659 | tunables: &TunableValues, | |
| 660 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 661 | if let MessageEvent::State(_) = &msg.event { | |
| 662 | let err = open_links(msg); | |
| 663 | return Err(err); | |
| 664 | } | |
| 665 | ||
| 666 | if let Some(msgtype) = msg.event.msgtype() { | |
| 667 | // A location message has no attachment to download, so `:open` | |
| 668 | // hands its `geo:` URI over to the system handler, which will | |
| 669 | // let the desktop open it with an application of its choosing. | |
| 670 | if let Some(geo_uri) = location_geo_uri(msgtype) { | |
| 671 | if !flags.contains(DownloadFlags::OPEN) { | |
| 672 | return Err(IambError::NoAttachment.into()); | |
| 673 | } | |
| 674 | ||
| 675 | let target = OsString::from(geo_uri.to_owned()); | |
| 676 | ||
| 677 | return match open_command(tunables.open_command.as_ref(), target) { | |
| 678 | Ok(_) => { | |
| 679 | Ok(vec![( | |
| 680 | Action::ShowInfoMessage(InfoMessage::from(format!("Opened {geo_uri}"))), | |
| 681 | ctx, | |
| 682 | )]) | |
| 683 | }, | |
| 684 | Err(err) => Err(err), | |
| 685 | }; | |
| 686 | } | |
| 687 | ||
| 688 | let media = client.media(); | |
| 689 | let mut filename = match (filename, &settings.dirs.downloads) { | |
| 690 | (Some(f), _) => f, | |
| 691 | (None, Some(downloads)) => downloads.clone(), | |
| 692 | (None, None) => return Err(IambError::NoDownloadDir.into()), | |
| 693 | }; | |
| 694 | let (source, msg_filename) = match msgtype { | |
| 695 | MessageType::Audio(c) => (c.source.clone(), c.filename()), | |
| 696 | MessageType::File(c) => (c.source.clone(), c.filename()), | |
| 697 | MessageType::Image(c) => (c.source.clone(), c.filename()), | |
| 698 | MessageType::Video(c) => (c.source.clone(), c.filename()), | |
| 699 | _ => { | |
| 700 | if !flags.contains(DownloadFlags::OPEN) { | |
| 701 | return Err(IambError::NoAttachment.into()); | |
| 702 | } | |
| 703 | let err = open_links(msg); | |
| 704 | return Err(err); | |
| 705 | }, | |
| 706 | }; | |
| 707 | if filename.is_dir() { | |
| 708 | filename.push(msg_filename.replace(std::path::MAIN_SEPARATOR_STR, "_")); | |
| 709 | } | |
| 710 | if filename.exists() && !flags.contains(DownloadFlags::FORCE) { | |
| 711 | // Find an incrementally suffixed filename, e.g. image-2.jpg -> image-3.jpg | |
| 712 | if let Some(stem) = filename.file_stem().and_then(OsStr::to_str) { | |
| 713 | let ext = filename.extension(); | |
| 714 | let mut filename_incr = filename.clone(); | |
| 715 | for n in 1..=1000 { | |
| 716 | if let Some(ext) = ext.and_then(OsStr::to_str) { | |
| 717 | filename_incr.set_file_name(format!("{stem}-{n}.{ext}")); | |
| 718 | } else { | |
| 719 | filename_incr.set_file_name(format!("{stem}-{n}")); | |
| 720 | } | |
| 721 | ||
| 722 | if !filename_incr.exists() { | |
| 723 | filename = filename_incr; | |
| 724 | break; | |
| 725 | } | |
| 726 | } | |
| 727 | } | |
| 728 | } | |
| 729 | if !filename.exists() || flags.contains(DownloadFlags::FORCE) { | |
| 730 | let req = MediaRequestParameters { source, format: MediaFormat::File }; | |
| 731 | ||
| 732 | let bytes = media.get_media_content(&req, true).await.map_err(IambError::from)?; | |
| 733 | ||
| 734 | fs::write(filename.as_path(), bytes.as_slice())?; | |
| 735 | ||
| 736 | msg.downloaded = true; | |
| 737 | } else if !flags.contains(DownloadFlags::OPEN) { | |
| 738 | let msg = format!( | |
| 739 | "The file {} already exists; add ! to end of command to overwrite it.", | |
| 740 | filename.display() | |
| 741 | ); | |
| 742 | let err = UIError::Failure(msg); | |
| 743 | ||
| 744 | return Err(err); | |
| 745 | } | |
| 746 | let info = if flags.contains(DownloadFlags::OPEN) { | |
| 747 | let target = filename.clone().into_os_string(); | |
| 748 | match open_command(settings.tunables.open_command.as_ref(), target) { | |
| 749 | Ok(_) => { | |
| 750 | InfoMessage::from(format!( | |
| 751 | "Attachment downloaded to {} and opened", | |
| 752 | filename.display() | |
| 753 | )) | |
| 754 | }, | |
| 755 | Err(err) => { | |
| 756 | return Err(err); | |
| 757 | }, | |
| 758 | } | |
| 759 | } else { | |
| 760 | InfoMessage::from(format!("Attachment downloaded to {}", filename.display())) | |
| 761 | }; | |
| 762 | ||
| 763 | return Ok(vec![(Action::ShowInfoMessage(info), ctx)]); | |
| 764 | } | |
| 765 | ||
| 766 | Err(IambError::NoAttachment.into()) | |
| 767 | } | |
| 768 | ||
| 769 | pub async fn msg_react( | |
| 770 | msg: &Message, | |
| 771 | settings: &ApplicationSettings, | |
| 772 | info: &RoomInfo, | |
| 773 | worker: &Requester, | |
| 774 | id: &RoomId, | |
| 775 | reaction: String, | |
| 776 | literal: bool, | |
| 777 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 778 | let emoji = if literal { | |
| 779 | reaction | |
| 780 | } else if let Some(emoji) = | |
| 781 | emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction)) | |
| 782 | { | |
| 783 | emoji.to_string() | |
| 784 | } else { | |
| 785 | let msg = format!( | |
| 786 | "{reaction:?} is not a known Emoji shortcode; do you want to react with exactly {reaction:?}?" | |
| 787 | ); | |
| 788 | let act = IambAction::Message(MessageAction::React(reaction, true)); | |
| 789 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 790 | let prompt = Box::new(prompt); | |
| 791 | ||
| 792 | return Err(UIError::NeedConfirm(prompt)); | |
| 793 | }; | |
| 794 | ||
| 795 | let room = get_joined(id, worker)?; | |
| 796 | let event_id = match &msg.event { | |
| 797 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 798 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 799 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 800 | MessageEvent::Local(..) => { | |
| 801 | // XXX: Implement reactions for local echos | |
| 802 | ||
| 803 | let msg = "Cannot react to a local echo"; | |
| 804 | let err = UIError::Failure(msg.into()); | |
| 805 | ||
| 806 | return Err(err); | |
| 807 | }, | |
| 808 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 809 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 810 | MessageEvent::Redacted(_, _) => { | |
| 811 | let msg = "Cannot react to a redacted message"; | |
| 812 | let err = UIError::Failure(msg.into()); | |
| 813 | ||
| 814 | return Err(err); | |
| 815 | }, | |
| 816 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 817 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 818 | }; | |
| 819 | ||
| 820 | if info.user_reactions_contains(&settings.profile.user_id, &event_id, &emoji) { | |
| 821 | let msg = format!("You’ve already reacted to this message with {emoji}"); | |
| 822 | let err = UIError::Failure(msg); | |
| 823 | ||
| 824 | return Err(err); | |
| 825 | } | |
| 826 | ||
| 827 | let reaction = Annotation::new(event_id, emoji); | |
| 828 | let msg = ReactionEventContent::new(reaction); | |
| 829 | ||
| 830 | room.send_queue().send(msg.into()).await.map_err(IambError::from)?; | |
| 831 | ||
| 832 | Ok(vec![]) | |
| 833 | } | |
| 834 | ||
| 835 | pub async fn msg_redact( | |
| 836 | msg: &Message, | |
| 837 | worker: &Requester, | |
| 838 | id: &RoomId, | |
| 839 | reason: Option<String>, | |
| 840 | skip_confirm: bool, | |
| 841 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 842 | if !skip_confirm { | |
| 843 | let msg = "Are you sure you want to redact this message?"; | |
| 844 | let act = IambAction::Message(MessageAction::Redact(reason, true)); | |
| 845 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 846 | let prompt = Box::new(prompt); | |
| 847 | ||
| 848 | return Err(UIError::NeedConfirm(prompt)); | |
| 849 | } | |
| 850 | ||
| 851 | let room = get_joined(id, worker)?; | |
| 852 | let event_id = match &msg.event { | |
| 853 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 854 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 855 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 856 | MessageEvent::Local(_, handle, _) => { | |
| 857 | let succeeded = handle | |
| 858 | .abort() | |
| 859 | .await | |
| 860 | .map_err(RoomSendQueueError::from) | |
| 861 | .map_err(IambError::from)?; | |
| 862 | ||
| 863 | if !succeeded { | |
| 864 | let msg = "local echo was already sent; please retry"; | |
| 865 | let err = UIError::Failure(msg.into()); | |
| 866 | ||
| 867 | return Err(err); | |
| 868 | } | |
| 869 | ||
| 870 | return Ok(vec![]); | |
| 871 | }, | |
| 872 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 873 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 874 | MessageEvent::Redacted(_, _) => { | |
| 875 | let msg = "Cannot redact already redacted message"; | |
| 876 | let err = UIError::Failure(msg.into()); | |
| 877 | ||
| 878 | return Err(err); | |
| 879 | }, | |
| 880 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 881 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 882 | }; | |
| 883 | ||
| 884 | let event_id = event_id.as_ref(); | |
| 885 | let reason = reason.as_deref(); | |
| 886 | let _ = room.redact(event_id, reason, None).await.map_err(IambError::from)?; | |
| 887 | ||
| 888 | Ok(vec![]) | |
| 889 | } | |
| 890 | ||
| 891 | pub async fn msg_unreact( | |
| 892 | msg: &Message, | |
| 893 | settings: &ApplicationSettings, | |
| 894 | info: &RoomInfo, | |
| 895 | worker: &Requester, | |
| 896 | id: &RoomId, | |
| 897 | reaction: Option<String>, | |
| 898 | literal: bool, | |
| 899 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 900 | let emoji = match reaction { | |
| 901 | reaction if literal => reaction, | |
| 902 | Some(reaction) => { | |
| 903 | if let Some(emoji) = | |
| 904 | emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction)) | |
| 905 | { | |
| 906 | Some(emoji.to_string()) | |
| 907 | } else { | |
| 908 | let msg = format!( | |
| 909 | "{reaction:?} is not a known Emoji shortcode; do you want to remove exactly {reaction:?}?" | |
| 910 | ); | |
| 911 | let act = IambAction::Message(MessageAction::Unreact(Some(reaction), true)); | |
| 912 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 913 | let prompt = Box::new(prompt); | |
| 914 | ||
| 915 | return Err(UIError::NeedConfirm(prompt)); | |
| 916 | } | |
| 917 | }, | |
| 918 | None => None, | |
| 919 | }; | |
| 920 | ||
| 921 | let room = get_joined(id, worker)?; | |
| 922 | let event_id = match &msg.event { | |
| 923 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 924 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 925 | MessageEvent::Original(ev, _) => ev.event_id.clone(), | |
| 926 | MessageEvent::Local(..) => { | |
| 927 | let msg = "Cannot unreact to a local echo"; | |
| 928 | let err = UIError::Failure(msg.into()); | |
| 929 | ||
| 930 | return Err(err); | |
| 931 | }, | |
| 932 | MessageEvent::State(ev) => ev.event_id().to_owned(), | |
| 933 | MessageEvent::Sticker(ev, ..) => ev.event_id.to_owned(), | |
| 934 | MessageEvent::Redacted(_, _) => { | |
| 935 | let msg = "Cannot unreact to a redacted message"; | |
| 936 | let err = UIError::Failure(msg.into()); | |
| 937 | ||
| 938 | return Err(err); | |
| 939 | }, | |
| 940 | MessageEvent::Poll(ev) => ev.event_id().to_owned(), | |
| 941 | MessageEvent::UnstablePoll(ev) => ev.event_id().to_owned(), | |
| 942 | }; | |
| 943 | ||
| 944 | let reactions = match info.reactions.get(&event_id) { | |
| 945 | Some(r) => r, | |
| 946 | None => return Ok(vec![]), | |
| 947 | }; | |
| 948 | ||
| 949 | let reactions = reactions.iter().filter_map(|(event_id, (reaction, user_id, _))| { | |
| 950 | if user_id != &settings.profile.user_id { | |
| 951 | return None; | |
| 952 | } | |
| 953 | ||
| 954 | if let Some(emoji) = &emoji { | |
| 955 | if emoji == reaction { | |
| 956 | return Some(event_id); | |
| 957 | } else { | |
| 958 | return None; | |
| 959 | } | |
| 960 | } else { | |
| 961 | return Some(event_id); | |
| 962 | } | |
| 963 | }); | |
| 964 | ||
| 965 | for reaction in reactions { | |
| 966 | let _ = room.redact(reaction, None, None).await.map_err(IambError::from)?; | |
| 967 | } | |
| 968 | ||
| 969 | Ok(vec![]) | |
| 970 | } | |
| 971 | ||
| 972 | pub async fn msg_pin( | |
| 973 | pin: bool, | |
| 974 | msg: &Message, | |
| 975 | id: &RoomId, | |
| 976 | worker: &Requester, | |
| 977 | settings: &ApplicationSettings, | |
| 978 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 979 | let event_id = match &msg.event { | |
| 980 | MessageEvent::Local(..) => { | |
| 981 | let msg = "Cannot pin a message that hasn't been sent yet"; | |
| 982 | return Err(UIError::Failure(msg.into())); | |
| 983 | }, | |
| 984 | MessageEvent::Redacted(..) | MessageEvent::EncryptedRedacted(_) if pin => { | |
| 985 | let msg = "Cannot pin a redacted message"; | |
| 986 | return Err(UIError::Failure(msg.into())); | |
| 987 | }, | |
| 988 | event => { | |
| 989 | event | |
| 990 | .event_id() | |
| 991 | .map(ToOwned::to_owned) | |
| 992 | .ok_or(IambError::NoSelectedMessage)? | |
| 993 | }, | |
| 994 | }; | |
| 995 | ||
| 996 | let room = get_joined(id, worker)?; | |
| 997 | ||
| 998 | let can_pin = room | |
| 999 | .power_levels() | |
| 1000 | .await | |
| 1001 | .map_err(matrix_sdk::Error::from) | |
| 1002 | .map_err(IambError::from)? | |
| 1003 | .user_can_send_state(&settings.profile.user_id, StateEventType::RoomPinnedEvents); | |
| 1004 | ||
| 1005 | if !can_pin { | |
| 1006 | return Err(IambError::InsufficientPermission.into()); | |
| 1007 | } | |
| 1008 | ||
| 1009 | // The state event holds the whole list, so rebuild it from the SDK's latest copy. | |
| 1010 | let mut pinned = room.pinned_event_ids().unwrap_or_default(); | |
| 1011 | let position = pinned.iter().position(|id| *id == event_id); | |
| 1012 | ||
| 1013 | match (pin, position) { | |
| 1014 | (true, Some(_)) => { | |
| 1015 | let msg = "This message is already pinned"; | |
| 1016 | return Err(UIError::Failure(msg.into())); | |
| 1017 | }, | |
| 1018 | (false, None) => { | |
| 1019 | let msg = "This message is not pinned"; | |
| 1020 | return Err(UIError::Failure(msg.into())); | |
| 1021 | }, | |
| 1022 | (true, None) => { | |
| 1023 | pinned.push(event_id); | |
| 1024 | }, | |
| 1025 | (false, Some(idx)) => { | |
| 1026 | pinned.remove(idx); | |
| 1027 | }, | |
| 1028 | } | |
| 1029 | ||
| 1030 | room.send_state_event(RoomPinnedEventsEventContent::new(pinned)) | |
| 1031 | .await | |
| 1032 | .map_err(IambError::from)?; | |
| 1033 | ||
| 1034 | Ok(vec![]) | |
| 1035 | } | |
| 1036 | ||
| 969 | 1037 | macro_rules! delegate { |
| 970 | 1038 | ($s: expr, $id: ident => $e: expr) => { |
| 971 | 1039 | match $s.focus { |
| @@ -992,8 +1060,8 @@ impl WindowOps<IambInfo> for ChatState { | ||
| 992 | 1060 | // XXX: I want each WindowSlot to have its own shared buffer, instead of each Room; need to |
| 993 | 1061 | // find a good way to pass that info here so that it can be part of the content id. |
| 994 | 1062 | let room_id = self.room_id.clone(); |
| 995 | let thread = self.thread().cloned(); | |
| 996 | let id = IambBufferId::Room(room_id.clone(), thread, RoomFocus::MessageBar); | |
| 1063 | let view = self.thread().cloned().into(); | |
| 1064 | let id = IambBufferId::Room(room_id.clone(), view, RoomFocus::MessageBar); | |
| 997 | 1065 | let ebuf = store.load_buffer(id); |
| 998 | 1066 | let tbox = TextBoxState::new(ebuf); |
| 999 | 1067 | |
| @@ -1062,9 +1130,9 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState { | ||
| 1062 | 1130 | // And now we can finally run the editor command. |
| 1063 | 1131 | match delegate!(self, w => w.editor_command(act, ctx, store)) { |
| 1064 | 1132 | res @ Ok(_) => res, |
| 1065 | Err(EditError::WrongBuffer(IambBufferId::Room(room_id, thread, focus))) | |
| 1133 | Err(EditError::WrongBuffer(IambBufferId::Room(room_id, view, focus))) | |
| 1066 | 1134 | if room_id == self.room_id && |
| 1067 | thread.as_ref() == self.thread() && | |
| 1135 | view == self.thread().into() && | |
| 1068 | 1136 | act.is_switchable(ctx) => |
| 1069 | 1137 | { |
| 1070 | 1138 | // Switch focus. |
| @@ -1598,7 +1666,7 @@ mod tests { | ||
| 1598 | 1666 | let room_id = TEST_ROOM1_ID.clone(); |
| 1599 | 1667 | let scrollback = ScrollbackState::new(room_id.clone(), None); |
| 1600 | 1668 | |
| 1601 | let id = IambBufferId::Room(room_id, None, RoomFocus::MessageBar); | |
| 1669 | let id = IambBufferId::Room(room_id, RoomView::Main, RoomFocus::MessageBar); | |
| 1602 | 1670 | let ebuf = store.load_buffer(id); |
| 1603 | 1671 | let mut tbox = TextBoxState::new(ebuf); |
| 1604 | 1672 | |
src/windows/room/message.rs added +656
| @@ -0,0 +1,656 @@ | ||
| 1 | use modalkit::editing::context::EditContext; | |
| 2 | use modalkit_ratatui::list::{List, ListCursor, ListItem, ListState}; | |
| 3 | use ratatui_image::sliced::{SignedPosition, SlicedImage}; | |
| 4 | ||
| 5 | use crate::prelude::*; | |
| 6 | use crate::preview::ImageStatus; | |
| 7 | use crate::util::space; | |
| 8 | use crate::windows::room::chat; | |
| 9 | use crate::windows::selected_style; | |
| 10 | ||
| 11 | /// State needed for rendering [MessageWidget]. | |
| 12 | pub(crate) struct MessageState { | |
| 13 | room: MatrixRoom, | |
| 14 | message_id: MessageId, | |
| 15 | list: ListState<MessageInfoItem, IambInfo>, | |
| 16 | } | |
| 17 | ||
| 18 | impl MessageState { | |
| 19 | pub fn new(room: MatrixRoom, message_id: MessageId) -> Self { | |
| 20 | let content = IambBufferId::Room( | |
| 21 | room.room_id().to_owned(), | |
| 22 | RoomView::Message(message_id.clone()), | |
| 23 | RoomFocus::Scrollback, | |
| 24 | ); | |
| 25 | let list = ListState::new(content, vec![]); | |
| 26 | ||
| 27 | MessageState { room, list, message_id } | |
| 28 | } | |
| 29 | ||
| 30 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { | |
| 31 | if let Some(room) = store.application.worker.client.get_room(self.room_id()) { | |
| 32 | self.room = room; | |
| 33 | } | |
| 34 | } | |
| 35 | ||
| 36 | pub fn room(&self) -> &MatrixRoom { | |
| 37 | &self.room | |
| 38 | } | |
| 39 | ||
| 40 | pub fn room_id(&self) -> &RoomId { | |
| 41 | self.room().room_id() | |
| 42 | } | |
| 43 | ||
| 44 | pub fn id(&self) -> MessageId { | |
| 45 | self.message_id.clone() | |
| 46 | } | |
| 47 | ||
| 48 | pub fn dup(&self, store: &mut ProgramStore) -> Self { | |
| 49 | MessageState { | |
| 50 | room: self.room.clone(), | |
| 51 | list: self.list.dup(store), | |
| 52 | message_id: self.message_id.clone(), | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | pub async fn message_command( | |
| 57 | &mut self, | |
| 58 | act: MessageAction, | |
| 59 | ctx: ProgramContext, | |
| 60 | store: &mut ProgramStore, | |
| 61 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 62 | let worker = &store.application.worker; | |
| 63 | ||
| 64 | let settings = &store.application.settings; | |
| 65 | let info = store.application.rooms.get_or_default(self.room_id().to_owned()); | |
| 66 | ||
| 67 | let msg = info.get_message(&self.id()).ok_or(IambError::NoSelectedMessage)?; | |
| 68 | ||
| 69 | match act { | |
| 70 | MessageAction::Download(filename, flags) => { | |
| 71 | let msg = info.get_message_mut(&self.id()).unwrap(); | |
| 72 | chat::msg_download( | |
| 73 | ctx, | |
| 74 | &store.application.worker.client, | |
| 75 | msg, | |
| 76 | settings, | |
| 77 | filename, | |
| 78 | flags, | |
| 79 | &store.application.settings.tunables, | |
| 80 | ) | |
| 81 | .await | |
| 82 | }, | |
| 83 | MessageAction::React(reaction, literal) => { | |
| 84 | chat::msg_react(msg, settings, info, worker, self.room_id(), reaction, literal) | |
| 85 | .await | |
| 86 | }, | |
| 87 | MessageAction::Redact(reason, skip_confirm) => { | |
| 88 | chat::msg_redact(msg, worker, self.room_id(), reason, skip_confirm).await | |
| 89 | }, | |
| 90 | MessageAction::Unreact(reaction, literal) => { | |
| 91 | chat::msg_unreact(msg, settings, info, worker, self.room_id(), reaction, literal) | |
| 92 | .await | |
| 93 | }, | |
| 94 | MessageAction::Pin => chat::msg_pin(true, msg, self.room_id(), worker, settings).await, | |
| 95 | MessageAction::Unpin => { | |
| 96 | chat::msg_pin(false, msg, self.room_id(), worker, settings).await | |
| 97 | }, | |
| 98 | MessageAction::Replied => { | |
| 99 | let Some(reply) = msg.reply_to() else { | |
| 100 | let msg = "Selected message is not a reply"; | |
| 101 | return Err(UIError::Failure(msg.into())); | |
| 102 | }; | |
| 103 | let act = Action::Window(WindowAction::Switch(OpenTarget::Application( | |
| 104 | IambId::Room(self.room_id().to_owned().into(), RoomView::Message(reply.into())), | |
| 105 | ))); | |
| 106 | ||
| 107 | Ok(vec![(act, ctx)]) | |
| 108 | }, | |
| 109 | MessageAction::Edit | MessageAction::Reply => { | |
| 110 | let msg = "Cannot write message in this view."; | |
| 111 | let err = UIError::Failure(msg.into()); | |
| 112 | ||
| 113 | Err(err) | |
| 114 | }, | |
| 115 | _ => Ok(vec![]), | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | impl TerminalCursor for MessageState { | |
| 121 | fn get_term_cursor(&self) -> Option<TermOffset> { | |
| 122 | self.list.get_term_cursor() | |
| 123 | } | |
| 124 | ||
| 125 | fn hide_term_cursor(&self) -> bool { | |
| 126 | self.list.hide_term_cursor() | |
| 127 | } | |
| 128 | } | |
| 129 | ||
| 130 | impl Deref for MessageState { | |
| 131 | type Target = ListState<MessageInfoItem, IambInfo>; | |
| 132 | ||
| 133 | fn deref(&self) -> &Self::Target { | |
| 134 | &self.list | |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | impl DerefMut for MessageState { | |
| 139 | fn deref_mut(&mut self) -> &mut Self::Target { | |
| 140 | &mut self.list | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | pub(crate) struct MessageWidget<'a> { | |
| 145 | focused: bool, | |
| 146 | store: &'a mut ProgramStore, | |
| 147 | } | |
| 148 | ||
| 149 | /// Used to temprarily store some settings that are changed for message rendering. | |
| 150 | struct SavedTunables { | |
| 151 | user_gutter_width: usize, | |
| 152 | read_receipt_display: bool, | |
| 153 | message_user_color: bool, | |
| 154 | reaction_display: bool, | |
| 155 | time_format: String, | |
| 156 | time_gutter_width: usize, | |
| 157 | } | |
| 158 | ||
| 159 | impl SavedTunables { | |
| 160 | fn setup(tunables: &mut TunableValues) -> Self { | |
| 161 | use std::mem::replace; | |
| 162 | ||
| 163 | let user_gutter_width = replace(&mut tunables.user_gutter_width, 2); | |
| 164 | let read_receipt_display = replace(&mut tunables.read_receipt_display, false); | |
| 165 | let message_user_color = replace(&mut tunables.message_user_color, false); | |
| 166 | let reaction_display = replace(&mut tunables.reaction_display, false); | |
| 167 | let time_format = replace(&mut tunables.time_format, "".into()); | |
| 168 | let time_gutter_width = replace(&mut tunables.time_gutter_width, 0); | |
| 169 | ||
| 170 | Self { | |
| 171 | user_gutter_width, | |
| 172 | read_receipt_display, | |
| 173 | message_user_color, | |
| 174 | reaction_display, | |
| 175 | time_format, | |
| 176 | time_gutter_width, | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | fn restore(self, tunables: &mut TunableValues) { | |
| 181 | let Self { | |
| 182 | user_gutter_width, | |
| 183 | read_receipt_display, | |
| 184 | message_user_color, | |
| 185 | reaction_display, | |
| 186 | time_format, | |
| 187 | time_gutter_width, | |
| 188 | } = self; | |
| 189 | ||
| 190 | tunables.user_gutter_width = user_gutter_width; | |
| 191 | tunables.read_receipt_display = read_receipt_display; | |
| 192 | tunables.message_user_color = message_user_color; | |
| 193 | tunables.reaction_display = reaction_display; | |
| 194 | tunables.time_format = time_format; | |
| 195 | tunables.time_gutter_width = time_gutter_width; | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 | impl<'a> MessageWidget<'a> { | |
| 200 | pub fn new(store: &'a mut ProgramStore) -> Self { | |
| 201 | MessageWidget { focused: false, store } | |
| 202 | } | |
| 203 | ||
| 204 | pub fn focus(mut self, focused: bool) -> Self { | |
| 205 | self.focused = focused; | |
| 206 | self | |
| 207 | } | |
| 208 | } | |
| 209 | ||
| 210 | impl StatefulWidget for MessageWidget<'_> { | |
| 211 | type State = MessageState; | |
| 212 | ||
| 213 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | |
| 214 | let ChatStore { rooms, worker, settings, previews, .. } = &mut self.store.application; | |
| 215 | let style = settings.theme.rooms.default; | |
| 216 | ||
| 217 | let saved_tunables = SavedTunables::setup(&mut settings.tunables); | |
| 218 | ||
| 219 | let info = rooms.get_or_default(state.room_id().to_owned()); | |
| 220 | let Some(msg) = info.get_message(&state.message_id) else { | |
| 221 | match &state.message_id { | |
| 222 | MessageId::Origin(event_id) => { | |
| 223 | self.store | |
| 224 | .application | |
| 225 | .need_load | |
| 226 | .need_message(state.room_id().to_owned(), event_id.to_owned()); | |
| 227 | ||
| 228 | Paragraph::new(Span::styled("Loading...", style)) | |
| 229 | .alignment(Alignment::Center) | |
| 230 | .render(area, buffer); | |
| 231 | return; | |
| 232 | }, | |
| 233 | MessageId::Local(_) => { | |
| 234 | Paragraph::new(Span::styled( | |
| 235 | "Local echo not found.", | |
| 236 | settings.theme.cmdbar.error, | |
| 237 | )) | |
| 238 | .alignment(Alignment::Center) | |
| 239 | .render(area, buffer); | |
| 240 | return; | |
| 241 | }, | |
| 242 | } | |
| 243 | }; | |
| 244 | ||
| 245 | // load image previews | |
| 246 | { | |
| 247 | if let Some(source) = msg.image_preview() { | |
| 248 | previews.load(source, PreviewKind::Message, worker); | |
| 249 | } | |
| 250 | let reply = msg | |
| 251 | .reply_to() | |
| 252 | .or_else(|| msg.thread_root()) | |
| 253 | .and_then(|e| info.get_event(&e)) | |
| 254 | .and_then(|msg| msg.image_preview()); | |
| 255 | if let Some(source) = reply { | |
| 256 | previews.load(source, PreviewKind::Message, worker); | |
| 257 | } | |
| 258 | let reactions = | |
| 259 | msg.event.event_id().and_then(|event_id| info.reactions.get(event_id)).map( | |
| 260 | |reactions| reactions.iter().filter_map(|(_, (_, _, source))| source.as_ref()), | |
| 261 | ); | |
| 262 | if let Some(reactions) = reactions { | |
| 263 | for source in reactions { | |
| 264 | previews.load(source, PreviewKind::Reaction, worker); | |
| 265 | } | |
| 266 | } | |
| 267 | } | |
| 268 | ||
| 269 | let (_, msg_previews) = msg.show_with_preview( | |
| 270 | // use self as prev to suppress user and date preview | |
| 271 | Some(msg), | |
| 272 | false, | |
| 273 | area.width as usize, | |
| 274 | info, | |
| 275 | settings, | |
| 276 | previews, | |
| 277 | ); | |
| 278 | ||
| 279 | // message | |
| 280 | let mut items = vec![ | |
| 281 | MessageInfoItem::Header(state.room_id().to_owned(), msg.sender.clone(), msg.timestamp), | |
| 282 | MessageInfoItem::Message(state.room_id().to_owned(), state.message_id.clone()), | |
| 283 | ]; | |
| 284 | ||
| 285 | // mentions | |
| 286 | if let Some(mentions) = msg.event.mentions() { | |
| 287 | items.extend(mentions.user_ids.iter().map(|user_id| { | |
| 288 | MessageInfoItem::User( | |
| 289 | "Mentions:".into(), | |
| 290 | state.room_id().to_owned(), | |
| 291 | user_id.to_owned(), | |
| 292 | ) | |
| 293 | })); | |
| 294 | } | |
| 295 | ||
| 296 | // links | |
| 297 | items.extend(msg.find_links().into_iter().map(|(c, url)| MessageInfoItem::Link(c, url))); | |
| 298 | ||
| 299 | // reactions | |
| 300 | // this uses the item index | |
| 301 | let mut reaction_previews = HashMap::new(); | |
| 302 | if saved_tunables.reaction_display && | |
| 303 | let Some(event_id) = state.message_id.as_origin() | |
| 304 | { | |
| 305 | for (key, users, source) in info.get_reactions(event_id) { | |
| 306 | if users.is_empty() { | |
| 307 | continue; | |
| 308 | } | |
| 309 | ||
| 310 | let proto = match source | |
| 311 | .as_ref() | |
| 312 | .and_then(|source| previews.get(source, PreviewKind::Reaction)) | |
| 313 | { | |
| 314 | Some(ImageStatus::Loaded(backend)) => Some(Some(backend)), | |
| 315 | // Use empty space as placeholder | |
| 316 | Some(ImageStatus::Queued(_)) | Some(ImageStatus::Downloading(_)) => Some(None), | |
| 317 | // Fall back to text | |
| 318 | None | Some(ImageStatus::Error(_)) => None, | |
| 319 | }; | |
| 320 | ||
| 321 | let short = emojis::get(key).and_then(|emoji| emoji.shortcode()); | |
| 322 | let (text, desc) = if proto.is_some() { | |
| 323 | (" ", Some(key)) | |
| 324 | } else if settings.tunables.reaction_shortcode_display { | |
| 325 | if let Some(short) = short { | |
| 326 | (short, None) | |
| 327 | } else { | |
| 328 | (key, None) | |
| 329 | } | |
| 330 | } else { | |
| 331 | (key, short) | |
| 332 | }; | |
| 333 | ||
| 334 | let content = if let Some(desc) = desc { | |
| 335 | format!("[{text} {}] ({desc})", users.len()) | |
| 336 | } else { | |
| 337 | format!("[{text} {}]", users.len()) | |
| 338 | }; | |
| 339 | ||
| 340 | if let Some(proto) = proto.flatten() { | |
| 341 | let proto = proto.clone(); | |
| 342 | reaction_previews.insert(items.len(), proto); | |
| 343 | } | |
| 344 | ||
| 345 | let section = Cow::from(content); | |
| 346 | items.extend(users.into_iter().map(|user_id| { | |
| 347 | MessageInfoItem::User( | |
| 348 | section.clone(), | |
| 349 | state.room_id().to_owned(), | |
| 350 | user_id.to_owned(), | |
| 351 | ) | |
| 352 | })); | |
| 353 | } | |
| 354 | } | |
| 355 | ||
| 356 | // receipts | |
| 357 | if saved_tunables.read_receipt_display && | |
| 358 | let Some(event_id) = msg.event.event_id() | |
| 359 | { | |
| 360 | let receipts = info | |
| 361 | .event_receipts | |
| 362 | .values() | |
| 363 | .filter_map(|receipts| receipts.get(event_id)) | |
| 364 | .flat_map(|receipts| receipts.iter()) | |
| 365 | .map(|user_id| { | |
| 366 | MessageInfoItem::User( | |
| 367 | "Last seen by:".into(), | |
| 368 | state.room_id().to_owned(), | |
| 369 | user_id.to_owned(), | |
| 370 | ) | |
| 371 | }); | |
| 372 | items.extend(receipts); | |
| 373 | } | |
| 374 | ||
| 375 | state.list.set(items); | |
| 376 | state.set_ignorecase(settings.tunables.ignorecase); | |
| 377 | ||
| 378 | List::new(self.store) | |
| 379 | .empty_message("This space is empty") | |
| 380 | .empty_alignment(Alignment::Center) | |
| 381 | .focus(self.focused) | |
| 382 | .style(style) | |
| 383 | .render(area, buffer, &mut state.list); | |
| 384 | ||
| 385 | // render image previews | |
| 386 | ||
| 387 | let viewctx = state.last_viewctx(); | |
| 388 | ||
| 389 | let mut y = 0; | |
| 390 | let mut prev_section = None; | |
| 391 | let mut scrolled_lines = 0; | |
| 392 | let mut previews = msg_previews; | |
| 393 | ||
| 394 | for (i, item) in state.get_items().iter().enumerate() { | |
| 395 | if let Some(proto) = reaction_previews.remove(&i) { | |
| 396 | previews.push((proto, 1, y)); | |
| 397 | } | |
| 398 | ||
| 399 | let curr_section = item.get_section(); | |
| 400 | if curr_section != prev_section { | |
| 401 | y += 1; | |
| 402 | prev_section = curr_section; | |
| 403 | } | |
| 404 | ||
| 405 | if i == viewctx.corner.position { | |
| 406 | scrolled_lines = y as usize + viewctx.corner.text_row; | |
| 407 | } | |
| 408 | ||
| 409 | y += item.show(false, viewctx, self.store).lines.len() as u16; | |
| 410 | } | |
| 411 | ||
| 412 | for (backend, msg_x, msg_y) in previews { | |
| 413 | let x = msg_x + area.left(); | |
| 414 | let y = msg_y as i16 - scrolled_lines as i16 + area.top() as i16; | |
| 415 | if backend.size().height as i16 + y >= area.top() as i16 { | |
| 416 | let hidden_lines = (area.top() as i16 - y).max(0); | |
| 417 | ||
| 418 | let position = SignedPosition { x: 0, y: -hidden_lines }; | |
| 419 | let image_widget = SlicedImage::new(&backend, position); | |
| 420 | let mut rect: Rect = backend.size().into(); | |
| 421 | rect.x = x; | |
| 422 | rect.y = (y + hidden_lines) as u16; | |
| 423 | ||
| 424 | rect.height -= hidden_lines as u16; | |
| 425 | ||
| 426 | let rect = rect.intersection(area); | |
| 427 | if !rect.is_empty() { | |
| 428 | image_widget.render(rect, buffer); | |
| 429 | } | |
| 430 | } | |
| 431 | } | |
| 432 | ||
| 433 | saved_tunables.restore(&mut self.store.application.settings.tunables); | |
| 434 | } | |
| 435 | } | |
| 436 | ||
| 437 | /// The different parts of the `:message` window | |
| 438 | #[derive(Debug, Clone)] | |
| 439 | pub(crate) enum MessageInfoItem { | |
| 440 | /// The message header | |
| 441 | Header(OwnedRoomId, OwnedUserId, MessageTimeStamp), | |
| 442 | ||
| 443 | /// The message itself | |
| 444 | Message(OwnedRoomId, MessageId), | |
| 445 | ||
| 446 | User(Cow<'static, str>, OwnedRoomId, OwnedUserId), | |
| 447 | Link(char, Url), | |
| 448 | } | |
| 449 | ||
| 450 | impl Display for MessageInfoItem { | |
| 451 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 452 | match self { | |
| 453 | MessageInfoItem::Message(_, message_id) => { | |
| 454 | // XXX: This should return the message text for yanking (or modify modalkit to not | |
| 455 | // use `ToString` here) | |
| 456 | write!(f, "{message_id}") | |
| 457 | }, | |
| 458 | MessageInfoItem::Header(_, user_id, _) | MessageInfoItem::User(_, _, user_id) => { | |
| 459 | write!(f, "{user_id}") | |
| 460 | }, | |
| 461 | MessageInfoItem::Link(_, url) => write!(f, "{url}"), | |
| 462 | } | |
| 463 | } | |
| 464 | } | |
| 465 | ||
| 466 | impl ListItem<IambInfo> for MessageInfoItem { | |
| 467 | type Section = Cow<'static, str>; | |
| 468 | ||
| 469 | fn show<'a>( | |
| 470 | &'a self, | |
| 471 | selected: bool, | |
| 472 | viewctx: &ViewportContext<ListCursor>, | |
| 473 | store: &'a ProgramStore, | |
| 474 | ) -> Text<'a> { | |
| 475 | let style = store.application.settings.theme.timeline.default; | |
| 476 | let style = selected_style(selected, style); | |
| 477 | match self { | |
| 478 | MessageInfoItem::Header(room_id, user_id, timestamp) => { | |
| 479 | let info = store.application.rooms.get(room_id); | |
| 480 | let settings = &store.application.settings; | |
| 481 | let width = viewctx.get_width(); | |
| 482 | ||
| 483 | let Span { content: mut user, style: user_style } = | |
| 484 | settings.get_user_span_maybe(user_id, info); | |
| 485 | ||
| 486 | let mut date = timestamp.as_datetime().format("%T %A, %B %d %Y").to_string(); | |
| 487 | ||
| 488 | // truncate if needed | |
| 489 | if user.len() > width { | |
| 490 | date.clear(); | |
| 491 | let user = user.to_mut(); | |
| 492 | std::mem::drop(user.drain(width.saturating_sub(2)..)); | |
| 493 | if width >= 2 { | |
| 494 | user.push_str(".."); | |
| 495 | } | |
| 496 | } else if user.len() + date.len() >= width { | |
| 497 | let date_width = width.saturating_sub(user.len() + 2); | |
| 498 | std::mem::drop(date.drain(..date.len().saturating_sub(date_width))); | |
| 499 | if date_width >= 2 { | |
| 500 | date.insert_str(0, ".."); | |
| 501 | } | |
| 502 | } | |
| 503 | ||
| 504 | let padding = width - user.len() - date.len(); | |
| 505 | let date_style = store.application.settings.theme.timeline.date; | |
| 506 | ||
| 507 | let line = Span::styled(user, style.patch(user_style)) + | |
| 508 | Span::raw(space(padding)) + | |
| 509 | Span::styled(date, date_style); | |
| 510 | line.into() | |
| 511 | }, | |
| 512 | MessageInfoItem::Message(room_id, message_id) => { | |
| 513 | let Some(info) = store.application.rooms.get(room_id) else { | |
| 514 | return Span::styled("Loading room...", style.fg(Color::Gray)).into(); | |
| 515 | }; | |
| 516 | let Some(msg) = info.get_message(message_id) else { | |
| 517 | return Span::styled("Loading message...", style.fg(Color::Gray)).into(); | |
| 518 | }; | |
| 519 | ||
| 520 | msg.show( | |
| 521 | // use self as prev to suppress user and date preview | |
| 522 | Some(msg), | |
| 523 | selected, | |
| 524 | viewctx.get_width(), | |
| 525 | info, | |
| 526 | &store.application.settings, | |
| 527 | &store.application.previews, | |
| 528 | ) | |
| 529 | }, | |
| 530 | // XXX: show reaction time | |
| 531 | MessageInfoItem::User(_, room_id, user_id) => { | |
| 532 | let info = store.application.rooms.get(room_id); | |
| 533 | let user = store.application.settings.get_user_span_maybe(user_id, info); | |
| 534 | let user_span = Span::styled(user.content.into_owned(), style.patch(user.style)); | |
| 535 | ||
| 536 | Text::from(Span::raw("- ") + user_span) | |
| 537 | }, | |
| 538 | MessageInfoItem::Link(c, url) => { | |
| 539 | Text::from(Span::styled(format!("[{c}] {url}"), style.bold())) | |
| 540 | }, | |
| 541 | } | |
| 542 | } | |
| 543 | ||
| 544 | fn get_word(&self) -> Option<String> { | |
| 545 | match self { | |
| 546 | MessageInfoItem::Message(..) => { | |
| 547 | // XXX: This could return a link to the replied-to item | |
| 548 | None | |
| 549 | }, | |
| 550 | MessageInfoItem::Header(_, user_id, _) | MessageInfoItem::User(.., user_id) => { | |
| 551 | Some(user_id.to_string()) | |
| 552 | }, | |
| 553 | MessageInfoItem::Link(.., url) => Some(url.to_string()), | |
| 554 | } | |
| 555 | } | |
| 556 | ||
| 557 | fn get_section(&self) -> Option<&Self::Section> { | |
| 558 | match self { | |
| 559 | MessageInfoItem::Header(..) | MessageInfoItem::Message(..) => None, | |
| 560 | MessageInfoItem::User(section, ..) => Some(section), | |
| 561 | MessageInfoItem::Link(..) => Some(&Cow::Borrowed("Links:")), | |
| 562 | } | |
| 563 | } | |
| 564 | } | |
| 565 | ||
| 566 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for MessageInfoItem { | |
| 567 | fn prompt( | |
| 568 | &mut self, | |
| 569 | act: &PromptAction, | |
| 570 | ctx: &ProgramContext, | |
| 571 | _: &mut ProgramStore, | |
| 572 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { | |
| 573 | match act { | |
| 574 | PromptAction::Submit => { | |
| 575 | match self { | |
| 576 | // XXX: Add action for [un-]react | |
| 577 | MessageInfoItem::Message(room_id, MessageId::Origin(event_id)) => { | |
| 578 | let id = IambId::Room( | |
| 579 | room_id.to_owned().into(), | |
| 580 | RoomView::Thread(event_id.to_owned()), | |
| 581 | ); | |
| 582 | let open = WindowAction::Switch(OpenTarget::Application(id)); | |
| 583 | Ok(vec![(open.into(), ctx.clone())]) | |
| 584 | }, | |
| 585 | MessageInfoItem::Message(_, MessageId::Local(_)) => { | |
| 586 | let msg = "Cannot create thread for local echo."; | |
| 587 | let err = EditError::Failure(msg.into()); | |
| 588 | ||
| 589 | return Err(err); | |
| 590 | }, | |
| 591 | MessageInfoItem::Header(..) | MessageInfoItem::User(..) => { | |
| 592 | // XXX: This should link to the user profile at some point | |
| 593 | Ok(vec![]) | |
| 594 | }, | |
| 595 | MessageInfoItem::Link(_, url) => { | |
| 596 | let act = IambAction::OpenLink(url.to_string()); | |
| 597 | Ok(vec![(act.into(), ctx.clone())]) | |
| 598 | }, | |
| 599 | } | |
| 600 | }, | |
| 601 | PromptAction::Abort(..) => { | |
| 602 | let msg = "Cannot abort a message."; | |
| 603 | let err = EditError::Failure(msg.into()); | |
| 604 | Err(err) | |
| 605 | }, | |
| 606 | PromptAction::Recall(..) => { | |
| 607 | let msg = "Cannot recall previous messages."; | |
| 608 | let err = EditError::Failure(msg.into()); | |
| 609 | Err(err) | |
| 610 | }, | |
| 611 | } | |
| 612 | } | |
| 613 | } | |
| 614 | ||
| 615 | #[cfg(test)] | |
| 616 | mod tests { | |
| 617 | use super::*; | |
| 618 | use crate::tests::*; | |
| 619 | ||
| 620 | #[tokio::test] | |
| 621 | async fn test_message_header_truncate() { | |
| 622 | let store = mock_store().await; | |
| 623 | let room_id = TEST_ROOM1_ID.clone(); | |
| 624 | let user_id = TEST_USER1.clone(); | |
| 625 | let msg_key = MSG1_KEY.clone(); | |
| 626 | let item = MessageInfoItem::Header(room_id, user_id.clone(), msg_key.ts); | |
| 627 | ||
| 628 | let mut viewctx = ViewportContext::new(); | |
| 629 | viewctx.dimensions.1 = 10; | |
| 630 | ||
| 631 | // Too narrow for anything: | |
| 632 | viewctx.dimensions.0 = 0; | |
| 633 | let text = item.show(false, &viewctx, &store); | |
| 634 | assert_eq!(text.width(), viewctx.get_width()); | |
| 635 | ||
| 636 | // One column: | |
| 637 | viewctx.dimensions.0 = 1; | |
| 638 | let text = item.show(false, &viewctx, &store); | |
| 639 | assert_eq!(text.width(), viewctx.get_width()); | |
| 640 | ||
| 641 | // Just wide enough for the username: | |
| 642 | viewctx.dimensions.0 = user_id.as_str().len(); | |
| 643 | let text = item.show(false, &viewctx, &store); | |
| 644 | assert_eq!(text.width(), viewctx.get_width()); | |
| 645 | ||
| 646 | // Just wide enough for the username and "..": | |
| 647 | viewctx.dimensions.0 = user_id.as_str().len() + 2; | |
| 648 | let text = item.show(false, &viewctx, &store); | |
| 649 | assert_eq!(text.width(), viewctx.get_width()); | |
| 650 | ||
| 651 | // Just wide enough for the username, ".." and some of the date: | |
| 652 | viewctx.dimensions.0 = user_id.as_str().len() + 2 + 4; | |
| 653 | let text = item.show(false, &viewctx, &store); | |
| 654 | assert_eq!(text.width(), viewctx.get_width()); | |
| 655 | } | |
| 656 | } | |
src/windows/room/mod.rs +90 −37
| @@ -13,15 +13,18 @@ use matrix_sdk::ruma::events::room::name::RoomNameEventContent; | ||
| 13 | 13 | use matrix_sdk::ruma::events::room::topic::RoomTopicEventContent; |
| 14 | 14 | use matrix_sdk::ruma::events::tag::TagInfo; |
| 15 | 15 | use matrix_sdk::ruma::room::{AllowRule, Restricted as JoinRestrictions}; |
| 16 | use modalkit::editing::context::EditContext; | |
| 16 | 17 | |
| 17 | 18 | use crate::base::{MemberUpdateAction, RoomField}; |
| 18 | 19 | use crate::config::EncryptionIndicatorLocation; |
| 19 | 20 | use crate::prelude::*; |
| 20 | 21 | use crate::windows::room::chat::ChatState; |
| 22 | use crate::windows::room::message::{MessageState, MessageWidget}; | |
| 21 | 23 | use crate::windows::room::not_joined::{NotJoined, NotJoinedState}; |
| 22 | 24 | use crate::windows::room::space::{Space, SpaceState}; |
| 23 | 25 | |
| 24 | 26 | mod chat; |
| 27 | mod message; | |
| 25 | 28 | mod not_joined; |
| 26 | 29 | mod scrollback; |
| 27 | 30 | mod space; |
| @@ -32,6 +35,7 @@ macro_rules! delegate { | ||
| 32 | 35 | RoomState::NotJoined($id) => $e, |
| 33 | 36 | RoomState::Chat($id) => $e, |
| 34 | 37 | RoomState::Space($id) => $e, |
| 38 | RoomState::Message($id) => $e, | |
| 35 | 39 | } |
| 36 | 40 | }; |
| 37 | 41 | } |
| @@ -64,16 +68,17 @@ fn hist_visibility_mode(name: impl Into<String>) -> IambResult<HistoryVisibility | ||
| 64 | 68 | } |
| 65 | 69 | |
| 66 | 70 | pub async fn room_command( |
| 67 | id: &RoomId, | |
| 71 | id: OwnedRoomId, | |
| 68 | 72 | act: RoomAction, |
| 69 | 73 | ctx: ProgramContext, |
| 70 | 74 | store: &mut ProgramStore, |
| 75 | focused_msg: Option<MessageId>, | |
| 71 | 76 | ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> { |
| 72 | 77 | let worker = &store.application.worker; |
| 73 | 78 | |
| 74 | 79 | match act { |
| 75 | 80 | RoomAction::Follow(cmd, dir) => { |
| 76 | let room = worker.client.get_room(id).ok_or(IambError::NotJoined)?; | |
| 81 | let room = worker.client.get_room(&id).ok_or(IambError::NotJoined)?; | |
| 77 | 82 | let room_id = match dir { |
| 78 | 83 | MoveDir1D::Next => { |
| 79 | 84 | let successor = room |
| @@ -89,14 +94,14 @@ pub async fn room_command( | ||
| 89 | 94 | }, |
| 90 | 95 | }; |
| 91 | 96 | |
| 92 | let id = IambId::Room(room_id.to_owned().into(), None); | |
| 97 | let id = IambId::Room(room_id.to_owned().into(), RoomView::Main); | |
| 93 | 98 | let target = OpenTarget::Application(id); |
| 94 | 99 | let act = cmd.switch(target); |
| 95 | 100 | |
| 96 | 101 | Ok(vec![(act, cmd.context.clone())]) |
| 97 | 102 | }, |
| 98 | 103 | RoomAction::InviteAccept => { |
| 99 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 104 | if let Some(room) = store.application.worker.client.get_room(&id) { | |
| 100 | 105 | room.join().await.map_err(IambError::from)?; |
| 101 | 106 | Ok(vec![]) |
| 102 | 107 | } else { |
| @@ -104,7 +109,7 @@ pub async fn room_command( | ||
| 104 | 109 | } |
| 105 | 110 | }, |
| 106 | 111 | RoomAction::InviteReject => { |
| 107 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 112 | if let Some(room) = store.application.worker.client.get_room(&id) { | |
| 108 | 113 | room.leave().await.map_err(IambError::from)?; |
| 109 | 114 | |
| 110 | 115 | Ok(vec![]) |
| @@ -113,7 +118,7 @@ pub async fn room_command( | ||
| 113 | 118 | } |
| 114 | 119 | }, |
| 115 | 120 | RoomAction::InviteSend(user) => { |
| 116 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 121 | if let Some(room) = store.application.worker.client.get_room(&id) { | |
| 117 | 122 | room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?; |
| 118 | 123 | |
| 119 | 124 | Ok(vec![]) |
| @@ -122,22 +127,22 @@ pub async fn room_command( | ||
| 122 | 127 | } |
| 123 | 128 | }, |
| 124 | 129 | RoomAction::KnockAccept(user) => { |
| 125 | let room = worker.client.get_room(id).ok_or(IambError::NotJoined)?; | |
| 130 | let room = worker.client.get_room(&id).ok_or(IambError::NotJoined)?; | |
| 126 | 131 | room.invite_user_by_id(&user).await.map_err(IambError::from)?; |
| 127 | 132 | Ok(vec![]) |
| 128 | 133 | }, |
| 129 | 134 | RoomAction::KnockReject(user, reason) => { |
| 130 | let room = worker.client.get_room(id).ok_or(IambError::NotJoined)?; | |
| 135 | let room = worker.client.get_room(&id).ok_or(IambError::NotJoined)?; | |
| 131 | 136 | room.kick_user(&user, reason.as_deref()).await.map_err(IambError::from)?; |
| 132 | 137 | Ok(vec![]) |
| 133 | 138 | }, |
| 134 | 139 | RoomAction::KnockBan(user, reason) => { |
| 135 | let room = worker.client.get_room(id).ok_or(IambError::NotJoined)?; | |
| 140 | let room = worker.client.get_room(&id).ok_or(IambError::NotJoined)?; | |
| 136 | 141 | room.ban_user(&user, reason.as_deref()).await.map_err(IambError::from)?; |
| 137 | 142 | Ok(vec![]) |
| 138 | 143 | }, |
| 139 | 144 | RoomAction::Leave(skip_confirm) => { |
| 140 | if let Some(room) = store.application.worker.client.get_room(id) { | |
| 145 | if let Some(room) = store.application.worker.client.get_room(&id) { | |
| 141 | 146 | if skip_confirm { |
| 142 | 147 | room.leave().await.map_err(IambError::from)?; |
| 143 | 148 | |
| @@ -155,7 +160,7 @@ pub async fn room_command( | ||
| 155 | 160 | } |
| 156 | 161 | }, |
| 157 | 162 | RoomAction::MemberUpdate(mua, user, reason, skip_confirm) => { |
| 158 | let Some(room) = store.application.worker.client.get_room(id) else { | |
| 163 | let Some(room) = store.application.worker.client.get_room(&id) else { | |
| 159 | 164 | return Err(IambError::NotJoined.into()); |
| 160 | 165 | }; |
| 161 | 166 | |
| @@ -213,7 +218,7 @@ pub async fn room_command( | ||
| 213 | 218 | Ok(vec![(act, cmd.context.clone())]) |
| 214 | 219 | }, |
| 215 | 220 | RoomAction::SetAccess(rule) => { |
| 216 | let Some(room) = store.application.worker.client.get_room(id) else { | |
| 221 | let Some(room) = store.application.worker.client.get_room(&id) else { | |
| 217 | 222 | return Err(IambError::NotJoined.into()); |
| 218 | 223 | }; |
| 219 | 224 | let rule = rule.into_join_rule(&store.application.worker.client).await?; |
| @@ -226,7 +231,7 @@ pub async fn room_command( | ||
| 226 | 231 | RoomAction::SetDirect(is_direct) => { |
| 227 | 232 | let room = store |
| 228 | 233 | .application |
| 229 | .get_joined_room(id) | |
| 234 | .get_joined_room(&id) | |
| 230 | 235 | .ok_or(UIError::Application(IambError::NotJoined))?; |
| 231 | 236 | |
| 232 | 237 | room.set_is_direct(is_direct).await.map_err(IambError::from)?; |
| @@ -236,7 +241,7 @@ pub async fn room_command( | ||
| 236 | 241 | RoomAction::SetUnread(is_unread) => { |
| 237 | 242 | let room = store |
| 238 | 243 | .application |
| 239 | .get_joined_room(id) | |
| 244 | .get_joined_room(&id) | |
| 240 | 245 | .ok_or(UIError::Application(IambError::NotJoined))?; |
| 241 | 246 | |
| 242 | 247 | room.set_unread_flag(is_unread).await.map_err(IambError::from)?; |
| @@ -245,7 +250,7 @@ pub async fn room_command( | ||
| 245 | 250 | let info = store.application.rooms.get_or_default(id.to_owned()); |
| 246 | 251 | |
| 247 | 252 | info.fully_read( |
| 248 | id.to_owned(), | |
| 253 | id, | |
| 249 | 254 | ReceiptThread::Main, |
| 250 | 255 | worker, |
| 251 | 256 | &store.application.settings, |
| @@ -258,7 +263,7 @@ pub async fn room_command( | ||
| 258 | 263 | RoomAction::Set(field, value) => { |
| 259 | 264 | let room = store |
| 260 | 265 | .application |
| 261 | .get_joined_room(id) | |
| 266 | .get_joined_room(&id) | |
| 262 | 267 | .ok_or(UIError::Application(IambError::NotJoined))?; |
| 263 | 268 | |
| 264 | 269 | match field { |
| @@ -287,7 +292,7 @@ pub async fn room_command( | ||
| 287 | 292 | let notifications = client.notification_settings().await; |
| 288 | 293 | |
| 289 | 294 | notifications |
| 290 | .set_room_notification_mode(id, mode) | |
| 295 | .set_room_notification_mode(&id, mode) | |
| 291 | 296 | .await |
| 292 | 297 | .map_err(IambError::from)?; |
| 293 | 298 | }, |
| @@ -379,7 +384,7 @@ pub async fn room_command( | ||
| 379 | 384 | RoomAction::Unset(field) => { |
| 380 | 385 | let room = store |
| 381 | 386 | .application |
| 382 | .get_joined_room(id) | |
| 387 | .get_joined_room(&id) | |
| 383 | 388 | .ok_or(UIError::Application(IambError::NotJoined))?; |
| 384 | 389 | |
| 385 | 390 | match field { |
| @@ -404,7 +409,7 @@ pub async fn room_command( | ||
| 404 | 409 | let notifications = client.notification_settings().await; |
| 405 | 410 | |
| 406 | 411 | notifications |
| 407 | .delete_user_defined_room_rules(id) | |
| 412 | .delete_user_defined_room_rules(&id) | |
| 408 | 413 | .await |
| 409 | 414 | .map_err(IambError::from)?; |
| 410 | 415 | }, |
| @@ -475,7 +480,7 @@ pub async fn room_command( | ||
| 475 | 480 | RoomAction::Show(field) => { |
| 476 | 481 | let room = store |
| 477 | 482 | .application |
| 478 | .get_joined_room(id) | |
| 483 | .get_joined_room(&id) | |
| 479 | 484 | .ok_or(UIError::Application(IambError::NotJoined))?; |
| 480 | 485 | |
| 481 | 486 | let msg = match field { |
| @@ -508,7 +513,7 @@ pub async fn room_command( | ||
| 508 | 513 | RoomField::NotificationMode => { |
| 509 | 514 | let client = &store.application.worker.client; |
| 510 | 515 | let notifications = client.notification_settings().await; |
| 511 | let mode = notifications.get_user_defined_room_notification_mode(id).await; | |
| 516 | let mode = notifications.get_user_defined_room_notification_mode(&id).await; | |
| 512 | 517 | |
| 513 | 518 | let level = match mode { |
| 514 | 519 | Some(RoomNotificationMode::Mute) => "mute", |
| @@ -612,7 +617,7 @@ pub async fn room_command( | ||
| 612 | 617 | Ok(vec![(act, ctx)]) |
| 613 | 618 | }, |
| 614 | 619 | RoomAction::Upgrade(new_version, additional_creators, false) => { |
| 615 | let room = worker.client.get_room(id).ok_or(IambError::NotJoined)?; | |
| 620 | let room = worker.client.get_room(&id).ok_or(IambError::NotJoined)?; | |
| 616 | 621 | let alias = room.canonical_alias(); |
| 617 | 622 | let name = alias.as_ref().map(|c| c.as_str()).unwrap_or_else(|| id.as_str()); |
| 618 | 623 | let msg = format!( |
| @@ -627,16 +632,27 @@ pub async fn room_command( | ||
| 627 | 632 | Err(UIError::NeedConfirm(prompt)) |
| 628 | 633 | }, |
| 629 | 634 | RoomAction::Upgrade(new_version, additional_creators, true) => { |
| 630 | let mut request = UpgradeRoomRequest::new(id.to_owned(), new_version); | |
| 635 | let mut request = UpgradeRoomRequest::new(id, new_version); | |
| 631 | 636 | request.additional_creators = additional_creators; |
| 632 | 637 | |
| 633 | 638 | let response = worker.client.send(request).await.map_err(IambError::from)?; |
| 634 | let id = IambId::Room(response.replacement_room.into(), None); | |
| 639 | let id = IambId::Room(response.replacement_room.into(), RoomView::Main); | |
| 635 | 640 | let target = OpenTarget::Application(id); |
| 636 | 641 | let act = WindowAction::Switch(target); |
| 637 | 642 | |
| 638 | 643 | Ok(vec![(act.into(), ctx)]) |
| 639 | 644 | }, |
| 645 | RoomAction::Message(cmd) => { | |
| 646 | let Some(msg_id) = focused_msg else { | |
| 647 | return Err(UIError::Failure("No message selected".into())); | |
| 648 | }; | |
| 649 | ||
| 650 | let id = IambId::Room(id.into(), RoomView::Message(msg_id)); | |
| 651 | let target = OpenTarget::Application(id); | |
| 652 | let act = cmd.switch(target); | |
| 653 | ||
| 654 | Ok(vec![(act, cmd.context.clone())]) | |
| 655 | }, | |
| 640 | 656 | } |
| 641 | 657 | } |
| 642 | 658 | |
| @@ -649,6 +665,7 @@ pub enum RoomState { | ||
| 649 | 665 | NotJoined(Box<NotJoinedState>), |
| 650 | 666 | Chat(Box<ChatState>), |
| 651 | 667 | Space(Box<SpaceState>), |
| 668 | Message(Box<MessageState>), | |
| 652 | 669 | } |
| 653 | 670 | |
| 654 | 671 | impl From<NotJoinedState> for RoomState { |
| @@ -669,12 +686,22 @@ impl From<SpaceState> for RoomState { | ||
| 669 | 686 | } |
| 670 | 687 | } |
| 671 | 688 | |
| 689 | impl From<MessageState> for RoomState { | |
| 690 | fn from(msg: MessageState) -> Self { | |
| 691 | RoomState::Message(Box::new(msg)) | |
| 692 | } | |
| 693 | } | |
| 694 | ||
| 672 | 695 | impl RoomState { |
| 673 | pub fn new(room: MatrixRoom, thread: Option<OwnedEventId>, store: &mut ProgramStore) -> Self { | |
| 696 | pub fn new(room: MatrixRoom, view: RoomView, store: &mut ProgramStore) -> Self { | |
| 674 | 697 | if room.is_space() { |
| 675 | 698 | SpaceState::new(room).into() |
| 676 | 699 | } else { |
| 677 | ChatState::new(room, thread, store).into() | |
| 700 | match view { | |
| 701 | RoomView::Main => ChatState::new(room, None, store).into(), | |
| 702 | RoomView::Thread(thread) => ChatState::new(room, Some(thread), store).into(), | |
| 703 | RoomView::Message(message) => MessageState::new(room, message).into(), | |
| 704 | } | |
| 678 | 705 | } |
| 679 | 706 | } |
| 680 | 707 | |
| @@ -684,11 +711,17 @@ impl RoomState { | ||
| 684 | 711 | |
| 685 | 712 | pub fn window_id(&self) -> IambId { |
| 686 | 713 | match self { |
| 687 | RoomState::NotJoined(nj) => IambId::Room(nj.alias().to_owned(), None), | |
| 714 | RoomState::NotJoined(nj) => IambId::Room(nj.alias().to_owned(), RoomView::Main), | |
| 688 | 715 | RoomState::Chat(chat) => { |
| 689 | IambId::Room(chat.id().to_owned().into(), chat.thread().cloned()) | |
| 716 | IambId::Room(chat.id().to_owned().into(), chat.thread().cloned().into()) | |
| 717 | }, | |
| 718 | RoomState::Space(space) => IambId::Room(space.id().to_owned().into(), RoomView::Main), | |
| 719 | RoomState::Message(msg) => { | |
| 720 | IambId::Room( | |
| 721 | msg.room_id().to_owned().into(), | |
| 722 | RoomView::Message(msg.id().to_owned()), | |
| 723 | ) | |
| 690 | 724 | }, |
| 691 | RoomState::Space(space) => IambId::Room(space.id().to_owned().into(), None), | |
| 692 | 725 | } |
| 693 | 726 | } |
| 694 | 727 | |
| @@ -697,6 +730,7 @@ impl RoomState { | ||
| 697 | 730 | RoomState::NotJoined(_) => None, |
| 698 | 731 | RoomState::Chat(chat) => Some(chat.room().state()), |
| 699 | 732 | RoomState::Space(space) => Some(space.room().state()), |
| 733 | RoomState::Message(msg) => Some(msg.room().state()), | |
| 700 | 734 | } |
| 701 | 735 | } |
| 702 | 736 | |
| @@ -721,6 +755,7 @@ impl RoomState { | ||
| 721 | 755 | }, |
| 722 | 756 | RoomState::Chat(chat) => chat.refresh_room(store), |
| 723 | 757 | RoomState::Space(space) => space.refresh_room(store), |
| 758 | RoomState::Message(msg) => msg.refresh_room(store), | |
| 724 | 759 | } |
| 725 | 760 | } |
| 726 | 761 | |
| @@ -799,11 +834,11 @@ impl RoomState { | ||
| 799 | 834 | act: MessageAction, |
| 800 | 835 | ctx: ProgramContext, |
| 801 | 836 | store: &mut ProgramStore, |
| 802 | ) -> IambResult<EditInfo> { | |
| 803 | if let RoomState::Chat(chat) = self { | |
| 804 | chat.message_command(act, ctx, store).await | |
| 805 | } else { | |
| 806 | Err(IambError::NoSelectedMessage.into()) | |
| 837 | ) -> IambResult<Vec<(Action<IambInfo>, EditContext)>> { | |
| 838 | match self { | |
| 839 | RoomState::Chat(chat) => chat.message_command(act, ctx, store).await, | |
| 840 | RoomState::Message(msg) => msg.message_command(act, ctx, store).await, | |
| 841 | _ => Err(IambError::NoSelectedMessage.into()), | |
| 807 | 842 | } |
| 808 | 843 | } |
| 809 | 844 | |
| @@ -877,6 +912,9 @@ impl RoomState { | ||
| 877 | 912 | { |
| 878 | 913 | spans.push(Span::styled("Thread in ", default_style)); |
| 879 | 914 | } |
| 915 | if let RoomState::Message(_) = self { | |
| 916 | spans.push("Message in ".into()); | |
| 917 | } | |
| 880 | 918 | |
| 881 | 919 | spans.push(Span::styled(title, title_style)); |
| 882 | 920 | |
| @@ -899,13 +937,17 @@ impl RoomState { | ||
| 899 | 937 | RoomState::Space(w) => store.application.get_room_title(w.id()).into(), |
| 900 | 938 | RoomState::Chat(w) => store.application.get_room_title(w.id()).into(), |
| 901 | 939 | RoomState::NotJoined(w) => w.get_tab_title(store), |
| 940 | RoomState::Message(w) => { | |
| 941 | let name = store.application.get_room_title(w.room_id()); | |
| 942 | ||
| 943 | Line::from(vec![Span::raw("Message in "), Span::raw(name)]) | |
| 944 | }, | |
| 902 | 945 | } |
| 903 | 946 | } |
| 904 | 947 | |
| 905 | 948 | pub fn focus_toggle(&mut self) { |
| 906 | match self { | |
| 907 | RoomState::Chat(chat) => chat.focus_toggle(), | |
| 908 | RoomState::NotJoined(_) | RoomState::Space(_) => return, | |
| 949 | if let RoomState::Chat(chat) = self { | |
| 950 | chat.focus_toggle(); | |
| 909 | 951 | } |
| 910 | 952 | } |
| 911 | 953 | |
| @@ -913,6 +955,7 @@ impl RoomState { | ||
| 913 | 955 | match self { |
| 914 | 956 | RoomState::Chat(chat) => Some(chat.room()), |
| 915 | 957 | RoomState::Space(space) => Some(space.room()), |
| 958 | RoomState::Message(msg) => Some(msg.room()), | |
| 916 | 959 | RoomState::NotJoined(_) => None, |
| 917 | 960 | } |
| 918 | 961 | } |
| @@ -922,6 +965,7 @@ impl RoomState { | ||
| 922 | 965 | RoomState::Chat(chat) => Some(chat.id()), |
| 923 | 966 | RoomState::Space(space) => Some(space.id()), |
| 924 | 967 | RoomState::NotJoined(nj) => nj.room_id(store), |
| 968 | RoomState::Message(msg) => Some(msg.room_id()), | |
| 925 | 969 | } |
| 926 | 970 | } |
| 927 | 971 | } |
| @@ -1006,6 +1050,9 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1006 | 1050 | RoomState::NotJoined(state) => { |
| 1007 | 1051 | NotJoined::new(store).render(area, buf, state); |
| 1008 | 1052 | }, |
| 1053 | RoomState::Message(msg) => { | |
| 1054 | MessageWidget::new(store).focus(focused).render(area, buf, msg) | |
| 1055 | }, | |
| 1009 | 1056 | RoomState::Space(space) => { |
| 1010 | 1057 | Space::new(store).focus(focused).render(area, buf, space); |
| 1011 | 1058 | }, |
| @@ -1017,6 +1064,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1017 | 1064 | RoomState::NotJoined(w) => RoomState::NotJoined(Box::new(w.dup())), |
| 1018 | 1065 | RoomState::Chat(chat) => RoomState::Chat(Box::new(chat.dup(store))), |
| 1019 | 1066 | RoomState::Space(space) => RoomState::Space(Box::new(space.dup(store))), |
| 1067 | RoomState::Message(msg) => RoomState::Message(Box::new(msg.dup(store))), | |
| 1020 | 1068 | } |
| 1021 | 1069 | } |
| 1022 | 1070 | |
| @@ -1025,6 +1073,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1025 | 1073 | RoomState::NotJoined(_) => true, |
| 1026 | 1074 | RoomState::Chat(chat) => chat.close(flags, store), |
| 1027 | 1075 | RoomState::Space(space) => space.close(flags, store), |
| 1076 | RoomState::Message(msg) => msg.close(flags, store), | |
| 1028 | 1077 | } |
| 1029 | 1078 | } |
| 1030 | 1079 | |
| @@ -1038,6 +1087,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1038 | 1087 | RoomState::NotJoined(_) => Err(EditError::ReadOnly.into()), |
| 1039 | 1088 | RoomState::Chat(chat) => chat.write(path, flags, store), |
| 1040 | 1089 | RoomState::Space(space) => space.write(path, flags, store), |
| 1090 | RoomState::Message(msg) => msg.write(path, flags, store), | |
| 1041 | 1091 | } |
| 1042 | 1092 | } |
| 1043 | 1093 | |
| @@ -1046,6 +1096,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1046 | 1096 | RoomState::NotJoined(_) => None, |
| 1047 | 1097 | RoomState::Chat(chat) => chat.get_completions(), |
| 1048 | 1098 | RoomState::Space(space) => space.get_completions(), |
| 1099 | RoomState::Message(msg) => msg.get_completions(), | |
| 1049 | 1100 | } |
| 1050 | 1101 | } |
| 1051 | 1102 | |
| @@ -1054,6 +1105,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1054 | 1105 | RoomState::NotJoined(_) => None, |
| 1055 | 1106 | RoomState::Chat(chat) => chat.get_cursor_word(style), |
| 1056 | 1107 | RoomState::Space(space) => space.get_cursor_word(style), |
| 1108 | RoomState::Message(msg) => msg.get_cursor_word(style), | |
| 1057 | 1109 | } |
| 1058 | 1110 | } |
| 1059 | 1111 | |
| @@ -1062,6 +1114,7 @@ impl WindowOps<IambInfo> for RoomState { | ||
| 1062 | 1114 | RoomState::NotJoined(_) => None, |
| 1063 | 1115 | RoomState::Chat(chat) => chat.get_selected_word(), |
| 1064 | 1116 | RoomState::Space(space) => space.get_selected_word(), |
| 1117 | RoomState::Message(msg) => msg.get_selected_word(), | |
| 1065 | 1118 | } |
| 1066 | 1119 | } |
| 1067 | 1120 | } |
src/windows/room/not_joined.rs +1 −1
| @@ -119,7 +119,7 @@ impl NotJoinedState { | ||
| 119 | 119 | |
| 120 | 120 | store.application.need_load.need_members(room.room_id().to_owned()); |
| 121 | 121 | |
| 122 | Some(RoomState::new(room, None, store)) | |
| 122 | Some(RoomState::new(room, RoomView::Main, store)) | |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | pub fn dup(&self) -> Self { |
src/windows/room/scrollback.rs +37 −14
| @@ -103,7 +103,8 @@ pub struct ScrollbackState { | ||
| 103 | 103 | |
| 104 | 104 | impl ScrollbackState { |
| 105 | 105 | pub fn new(room_id: OwnedRoomId, thread: Option<OwnedEventId>) -> ScrollbackState { |
| 106 | let id = IambBufferId::Room(room_id.to_owned(), thread.clone(), RoomFocus::Scrollback); | |
| 106 | let id = | |
| 107 | IambBufferId::Room(room_id.to_owned(), thread.clone().into(), RoomFocus::Scrollback); | |
| 107 | 108 | let cursor = MessageCursor::default(); |
| 108 | 109 | let viewctx = ViewportContext::default(); |
| 109 | 110 | let jumped = HistoryList::default(); |
| @@ -147,6 +148,16 @@ impl ScrollbackState { | ||
| 147 | 148 | .or_else(|| self.get_thread(info)?.last_key_value().map(|kv| kv.0.clone())) |
| 148 | 149 | } |
| 149 | 150 | |
| 151 | pub fn get<'a>(&self, info: &'a RoomInfo) -> Option<&'a Message> { | |
| 152 | let thread = self.get_thread(info); | |
| 153 | ||
| 154 | if let Some(k) = &self.cursor.timestamp { | |
| 155 | thread.and_then(|t| t.get(k)) | |
| 156 | } else { | |
| 157 | thread.and_then(|t| t.last_key_value()).map(|(_, v)| v) | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 150 | 161 | pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> { |
| 151 | 162 | let thread = self.get_thread_mut(info); |
| 152 | 163 | |
| @@ -254,8 +265,10 @@ impl ScrollbackState { | ||
| 254 | 265 | for (key, item) in thread.range(..=&idx).rev() { |
| 255 | 266 | let sel = selidx == key; |
| 256 | 267 | let prev = prevmsg(key, thread); |
| 257 | let len = | |
| 258 | item.show(prev, sel, &self.viewctx, info, settings, previews).lines.len(); | |
| 268 | let len = item | |
| 269 | .show(prev, sel, self.viewctx.get_width(), info, settings, previews) | |
| 270 | .lines | |
| 271 | .len(); | |
| 259 | 272 | |
| 260 | 273 | if key == &idx { |
| 261 | 274 | lines += len / 2; |
| @@ -278,8 +291,10 @@ impl ScrollbackState { | ||
| 278 | 291 | for (key, item) in thread.range(..=&idx).rev() { |
| 279 | 292 | let sel = key == selidx; |
| 280 | 293 | let prev = prevmsg(key, thread); |
| 281 | let len = | |
| 282 | item.show(prev, sel, &self.viewctx, info, settings, previews).lines.len(); | |
| 294 | let len = item | |
| 295 | .show(prev, sel, self.viewctx.get_width(), info, settings, previews) | |
| 296 | .lines | |
| 297 | .len(); | |
| 283 | 298 | |
| 284 | 299 | lines += len; |
| 285 | 300 | |
| @@ -338,7 +353,7 @@ impl ScrollbackState { | ||
| 338 | 353 | } |
| 339 | 354 | |
| 340 | 355 | lines += item |
| 341 | .show(prev, false, &self.viewctx, info, settings, previews) | |
| 356 | .show(prev, false, self.viewctx.get_width(), info, settings, previews) | |
| 342 | 357 | .height() |
| 343 | 358 | .max(1); |
| 344 | 359 | |
| @@ -1016,7 +1031,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1016 | 1031 | return Err(err); |
| 1017 | 1032 | }; |
| 1018 | 1033 | let room_id = self.room_id.clone(); |
| 1019 | let id = IambId::Room(room_id.into(), Some(root.to_owned())); | |
| 1034 | let id = IambId::Room(room_id.into(), RoomView::Thread(root.to_owned())); | |
| 1020 | 1035 | let open = WindowAction::Switch(OpenTarget::Application(id)); |
| 1021 | 1036 | Ok(vec![(open.into(), ctx.clone())]) |
| 1022 | 1037 | } |
| @@ -1074,7 +1089,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1074 | 1089 | for (key, item) in thread.range(..=&corner_key).rev() { |
| 1075 | 1090 | let sel = key == cursor_key; |
| 1076 | 1091 | let prev = prevmsg(key, thread); |
| 1077 | let txt = item.show(prev, sel, &self.viewctx, info, settings, previews); | |
| 1092 | let txt = | |
| 1093 | item.show(prev, sel, self.viewctx.get_width(), info, settings, previews); | |
| 1078 | 1094 | let len = txt.height().max(1); |
| 1079 | 1095 | let max = len.saturating_sub(1); |
| 1080 | 1096 | |
| @@ -1102,7 +1118,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1102 | 1118 | |
| 1103 | 1119 | for (key, item) in thread.range(&corner_key..) { |
| 1104 | 1120 | let sel = key == cursor_key; |
| 1105 | let txt = item.show(prev, sel, &self.viewctx, info, settings, previews); | |
| 1121 | let txt = | |
| 1122 | item.show(prev, sel, self.viewctx.get_width(), info, settings, previews); | |
| 1106 | 1123 | let len = txt.height().max(1); |
| 1107 | 1124 | let max = len.saturating_sub(1); |
| 1108 | 1125 | |
| @@ -1388,8 +1405,14 @@ impl StatefulWidget for Scrollback<'_> { | ||
| 1388 | 1405 | for (key, item) in thread.range(&corner_key..) { |
| 1389 | 1406 | let sel = key == cursor_key; |
| 1390 | 1407 | |
| 1391 | let (txt, mut msg_previews) = | |
| 1392 | item.show_with_preview(prev, foc && sel, &state.viewctx, info, settings, previews); | |
| 1408 | let (txt, mut msg_previews) = item.show_with_preview( | |
| 1409 | prev, | |
| 1410 | foc && sel, | |
| 1411 | state.viewctx.get_width(), | |
| 1412 | info, | |
| 1413 | settings, | |
| 1414 | previews, | |
| 1415 | ); | |
| 1393 | 1416 | |
| 1394 | 1417 | let incomplete_ok = !full || !sel; |
| 1395 | 1418 | |
| @@ -1466,11 +1489,11 @@ impl StatefulWidget for Scrollback<'_> { | ||
| 1466 | 1489 | // Render image previews after all text lines have been drawn, as the render might draw below the current |
| 1467 | 1490 | // line. |
| 1468 | 1491 | for (x, y, backend) in image_previews { |
| 1469 | if backend.size().height as i16 + y >= area.y as i16 { | |
| 1470 | let hidden_lines = (area.y as i16 - y).max(0); | |
| 1492 | if backend.size().height as i16 + y >= area.top() as i16 { | |
| 1493 | let hidden_lines = (area.top() as i16 - y).max(0); | |
| 1471 | 1494 | |
| 1472 | 1495 | let position = SignedPosition { x: 0, y: -hidden_lines }; |
| 1473 | let image_widget = SlicedImage::new(backend, position); | |
| 1496 | let image_widget = SlicedImage::new(&backend, position); | |
| 1474 | 1497 | let mut rect: Rect = backend.size().into(); |
| 1475 | 1498 | rect.x = x; |
| 1476 | 1499 | rect.y = (y + hidden_lines) as u16; |
src/windows/room/space.rs +1 −1
| @@ -21,7 +21,7 @@ pub struct SpaceState { | ||
| 21 | 21 | impl SpaceState { |
| 22 | 22 | pub fn new(room: MatrixRoom) -> Self { |
| 23 | 23 | let room_id = room.room_id().to_owned(); |
| 24 | let content = IambBufferId::Room(room_id.clone(), None, RoomFocus::Scrollback); | |
| 24 | let content = IambBufferId::Room(room_id.clone(), RoomView::Main, RoomFocus::Scrollback); | |
| 25 | 25 | let list = ListState::new(content, vec![]); |
| 26 | 26 | let last_fetch = None; |
| 27 | 27 | |
src/worker.rs −1
| @@ -92,7 +92,6 @@ use tracing::{Instrument as _, error, warn}; | ||
| 92 | 92 | |
| 93 | 93 | use crate::base::{CreateRoomFlags, CreateRoomType, EchoLocation, MessageNeed}; |
| 94 | 94 | use crate::config::ProxyUrl; |
| 95 | use crate::message::MessageId; | |
| 96 | 95 | use crate::notifications::register_notifications; |
| 97 | 96 | use crate::oauth::oauth_login; |
| 98 | 97 | use crate::prelude::*; |