Commit 2ac71da9a6
Unregistered key
Layout: unified · split
src/base.rs +77 −2
| @@ -45,6 +45,7 @@ use matrix_sdk::{ | ||
| 45 | 45 | RoomMessageEventContent, |
| 46 | 46 | RoomMessageEventContentWithoutRelation, |
| 47 | 47 | }, |
| 48 | room::redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent}, | |
| 48 | 49 | tag::{TagName, Tags}, |
| 49 | 50 | MessageLikeEvent, |
| 50 | 51 | }, |
| @@ -54,6 +55,7 @@ use matrix_sdk::{ | ||
| 54 | 55 | OwnedRoomId, |
| 55 | 56 | OwnedUserId, |
| 56 | 57 | RoomId, |
| 58 | RoomVersionId, | |
| 57 | 59 | UserId, |
| 58 | 60 | }, |
| 59 | 61 | RoomState as MatrixRoomState, |
| @@ -729,7 +731,7 @@ pub struct RoomInfo { | ||
| 729 | 731 | pub keys: HashMap<OwnedEventId, EventLocation>, |
| 730 | 732 | |
| 731 | 733 | /// The messages loaded for this room. |
| 732 | pub messages: Messages, | |
| 734 | messages: Messages, | |
| 733 | 735 | |
| 734 | 736 | /// A map of read markers to display on different events. |
| 735 | 737 | pub event_receipts: HashMap<OwnedEventId, HashSet<OwnedUserId>>, |
| @@ -745,7 +747,7 @@ pub struct RoomInfo { | ||
| 745 | 747 | pub reactions: HashMap<OwnedEventId, MessageReactions>, |
| 746 | 748 | |
| 747 | 749 | /// A map of message identifiers to thread replies. |
| 748 | pub threads: HashMap<OwnedEventId, Messages>, | |
| 750 | threads: HashMap<OwnedEventId, Messages>, | |
| 749 | 751 | |
| 750 | 752 | /// Whether the scrollback for this room is currently being fetched. |
| 751 | 753 | pub fetching: bool, |
| @@ -767,6 +769,48 @@ pub struct RoomInfo { | ||
| 767 | 769 | } |
| 768 | 770 | |
| 769 | 771 | impl RoomInfo { |
| 772 | pub fn get_thread(&self, root: Option<&EventId>) -> Option<&Messages> { | |
| 773 | if let Some(thread_root) = root { | |
| 774 | self.threads.get(thread_root) | |
| 775 | } else { | |
| 776 | Some(&self.messages) | |
| 777 | } | |
| 778 | } | |
| 779 | ||
| 780 | pub fn get_thread_mut(&mut self, root: Option<OwnedEventId>) -> &mut Messages { | |
| 781 | if let Some(thread_root) = root { | |
| 782 | self.threads.entry(thread_root).or_default() | |
| 783 | } else { | |
| 784 | &mut self.messages | |
| 785 | } | |
| 786 | } | |
| 787 | ||
| 788 | /// Get the event for the last message in a thread (or the thread root if there are no | |
| 789 | /// in-thread replies yet). | |
| 790 | /// | |
| 791 | /// This returns `None` if the event identifier isn't in the room. | |
| 792 | pub fn get_thread_last<'a>( | |
| 793 | &'a self, | |
| 794 | thread_root: &OwnedEventId, | |
| 795 | ) -> Option<&'a OriginalRoomMessageEvent> { | |
| 796 | let last = self.threads.get(thread_root).and_then(|t| Some(t.last_key_value()?.1)); | |
| 797 | ||
| 798 | let msg = if let Some(last) = last { | |
| 799 | &last.event | |
| 800 | } else if let EventLocation::Message(_, key) = self.keys.get(thread_root)? { | |
| 801 | let msg = self.messages.get(key)?; | |
| 802 | &msg.event | |
| 803 | } else { | |
| 804 | return None; | |
| 805 | }; | |
| 806 | ||
| 807 | if let MessageEvent::Original(ev) = &msg { | |
| 808 | Some(ev) | |
| 809 | } else { | |
| 810 | None | |
| 811 | } | |
| 812 | } | |
| 813 | ||
| 770 | 814 | /// Get the reactions and their counts for a message. |
| 771 | 815 | pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize)> { |
| 772 | 816 | if let Some(reacts) = self.reactions.get(event_id) { |
| @@ -801,6 +845,37 @@ impl RoomInfo { | ||
| 801 | 845 | self.messages.get_mut(self.keys.get(event_id)?.to_message_key()?) |
| 802 | 846 | } |
| 803 | 847 | |
| 848 | pub fn redact(&mut self, ev: OriginalSyncRoomRedactionEvent, room_version: &RoomVersionId) { | |
| 849 | let Some(redacts) = &ev.redacts else { | |
| 850 | return; | |
| 851 | }; | |
| 852 | ||
| 853 | match self.keys.get(redacts) { | |
| 854 | None => return, | |
| 855 | Some(EventLocation::Message(None, key)) => { | |
| 856 | if let Some(msg) = self.messages.get_mut(key) { | |
| 857 | let ev = SyncRoomRedactionEvent::Original(ev); | |
| 858 | msg.redact(ev, room_version); | |
| 859 | } | |
| 860 | }, | |
| 861 | Some(EventLocation::Message(Some(root), key)) => { | |
| 862 | if let Some(thread) = self.threads.get_mut(root) { | |
| 863 | if let Some(msg) = thread.get_mut(key) { | |
| 864 | let ev = SyncRoomRedactionEvent::Original(ev); | |
| 865 | msg.redact(ev, room_version); | |
| 866 | } | |
| 867 | } | |
| 868 | }, | |
| 869 | Some(EventLocation::Reaction(event_id)) => { | |
| 870 | if let Some(reactions) = self.reactions.get_mut(event_id) { | |
| 871 | reactions.remove(redacts); | |
| 872 | } | |
| 873 | ||
| 874 | self.keys.remove(redacts); | |
| 875 | }, | |
| 876 | } | |
| 877 | } | |
| 878 | ||
| 804 | 879 | /// Insert a reaction to a message. |
| 805 | 880 | pub fn insert_reaction(&mut self, react: ReactionEvent) { |
| 806 | 881 | match react { |
src/message/mod.rs +105 −82
| @@ -638,6 +638,106 @@ impl<'a> MessageFormatter<'a> { | ||
| 638 | 638 | self.push_spans(line, style, text); |
| 639 | 639 | } |
| 640 | 640 | } |
| 641 | ||
| 642 | fn push_in_reply( | |
| 643 | &mut self, | |
| 644 | msg: &'a Message, | |
| 645 | style: Style, | |
| 646 | text: &mut Text<'a>, | |
| 647 | info: &'a RoomInfo, | |
| 648 | ) { | |
| 649 | let width = self.width(); | |
| 650 | let w = width.saturating_sub(2); | |
| 651 | let shortcodes = self.settings.tunables.message_shortcode_display; | |
| 652 | let mut replied = msg.show_msg(w, style, true, shortcodes); | |
| 653 | let mut sender = msg.sender_span(info, self.settings); | |
| 654 | let sender_width = UnicodeWidthStr::width(sender.content.as_ref()); | |
| 655 | let trailing = w.saturating_sub(sender_width + 1); | |
| 656 | ||
| 657 | sender.style = sender.style.patch(style); | |
| 658 | ||
| 659 | self.push_spans( | |
| 660 | Line::from(vec![ | |
| 661 | Span::styled(" ", style), | |
| 662 | Span::styled(THICK_VERTICAL, style), | |
| 663 | sender, | |
| 664 | Span::styled(":", style), | |
| 665 | space_span(trailing, style), | |
| 666 | ]), | |
| 667 | style, | |
| 668 | text, | |
| 669 | ); | |
| 670 | ||
| 671 | for line in replied.lines.iter_mut() { | |
| 672 | line.spans.insert(0, Span::styled(THICK_VERTICAL, style)); | |
| 673 | line.spans.insert(0, Span::styled(" ", style)); | |
| 674 | } | |
| 675 | ||
| 676 | self.push_text(replied, style, text); | |
| 677 | } | |
| 678 | ||
| 679 | fn push_reactions(&mut self, counts: Vec<(&'a str, usize)>, style: Style, text: &mut Text<'a>) { | |
| 680 | let mut emojis = printer::TextPrinter::new(self.width(), style, false, false); | |
| 681 | let mut reactions = 0; | |
| 682 | ||
| 683 | for (key, count) in counts { | |
| 684 | if reactions != 0 { | |
| 685 | emojis.push_str(" ", style); | |
| 686 | } | |
| 687 | ||
| 688 | let name = if self.settings.tunables.reaction_shortcode_display { | |
| 689 | if let Some(emoji) = emojis::get(key) { | |
| 690 | if let Some(short) = emoji.shortcode() { | |
| 691 | short | |
| 692 | } else { | |
| 693 | // No ASCII shortcode name to show. | |
| 694 | continue; | |
| 695 | } | |
| 696 | } else if key.chars().all(|c| c.is_ascii_alphanumeric()) { | |
| 697 | key | |
| 698 | } else { | |
| 699 | // Not an Emoji or a printable ASCII string. | |
| 700 | continue; | |
| 701 | } | |
| 702 | } else { | |
| 703 | key | |
| 704 | }; | |
| 705 | ||
| 706 | emojis.push_str("[", style); | |
| 707 | emojis.push_str(name, style); | |
| 708 | emojis.push_str(" ", style); | |
| 709 | emojis.push_span_nobreak(Span::styled(count.to_string(), style)); | |
| 710 | emojis.push_str("]", style); | |
| 711 | ||
| 712 | reactions += 1; | |
| 713 | } | |
| 714 | ||
| 715 | if reactions > 0 { | |
| 716 | self.push_text(emojis.finish(), style, text); | |
| 717 | } | |
| 718 | } | |
| 719 | ||
| 720 | fn push_thread_reply_count(&mut self, len: usize, text: &mut Text<'a>) { | |
| 721 | if len == 0 { | |
| 722 | return; | |
| 723 | } | |
| 724 | ||
| 725 | // If we have threaded replies to this message, show how many. | |
| 726 | let plural = len != 1; | |
| 727 | let style = Style::default(); | |
| 728 | let mut threaded = | |
| 729 | printer::TextPrinter::new(self.width(), style, false, false).literal(true); | |
| 730 | let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD)); | |
| 731 | threaded.push_str(" \u{2937} ", style); | |
| 732 | threaded.push_span_nobreak(len); | |
| 733 | if plural { | |
| 734 | threaded.push_str(" replies in thread", style); | |
| 735 | } else { | |
| 736 | threaded.push_str(" reply in thread", style); | |
| 737 | } | |
| 738 | ||
| 739 | self.push_text(threaded.finish(), style, text); | |
| 740 | } | |
| 641 | 741 | } |
| 642 | 742 | |
| 643 | 743 | pub enum ImageStatus { |
| @@ -835,33 +935,7 @@ impl Message { | ||
| 835 | 935 | .and_then(|e| info.get_event(&e)); |
| 836 | 936 | |
| 837 | 937 | if let Some(r) = &reply { |
| 838 | let w = width.saturating_sub(2); | |
| 839 | let mut replied = | |
| 840 | r.show_msg(w, style, true, settings.tunables.message_shortcode_display); | |
| 841 | let mut sender = r.sender_span(info, settings); | |
| 842 | let sender_width = UnicodeWidthStr::width(sender.content.as_ref()); | |
| 843 | let trailing = w.saturating_sub(sender_width + 1); | |
| 844 | ||
| 845 | sender.style = sender.style.patch(style); | |
| 846 | ||
| 847 | fmt.push_spans( | |
| 848 | Line::from(vec![ | |
| 849 | Span::styled(" ", style), | |
| 850 | Span::styled(THICK_VERTICAL, style), | |
| 851 | sender, | |
| 852 | Span::styled(":", style), | |
| 853 | space_span(trailing, style), | |
| 854 | ]), | |
| 855 | style, | |
| 856 | &mut text, | |
| 857 | ); | |
| 858 | ||
| 859 | for line in replied.lines.iter_mut() { | |
| 860 | line.spans.insert(0, Span::styled(THICK_VERTICAL, style)); | |
| 861 | line.spans.insert(0, Span::styled(" ", style)); | |
| 862 | } | |
| 863 | ||
| 864 | fmt.push_text(replied, style, &mut text); | |
| 938 | fmt.push_in_reply(r, style, &mut text, info); | |
| 865 | 939 | } |
| 866 | 940 | |
| 867 | 941 | // Now show the message contents, and the inlined reply if we couldn't find it above. |
| @@ -879,63 +953,12 @@ impl Message { | ||
| 879 | 953 | } |
| 880 | 954 | |
| 881 | 955 | if settings.tunables.reaction_display { |
| 882 | // Pass false for emoji_shortcodes parameter because we handle shortcodes ourselves | |
| 883 | // before pushing to the printer. | |
| 884 | let mut emojis = printer::TextPrinter::new(width, style, false, false); | |
| 885 | let mut reactions = 0; | |
| 886 | ||
| 887 | for (key, count) in info.get_reactions(self.event.event_id()).into_iter() { | |
| 888 | if reactions != 0 { | |
| 889 | emojis.push_str(" ", style); | |
| 890 | } | |
| 891 | ||
| 892 | let name = if settings.tunables.reaction_shortcode_display { | |
| 893 | if let Some(emoji) = emojis::get(key) { | |
| 894 | if let Some(short) = emoji.shortcode() { | |
| 895 | short | |
| 896 | } else { | |
| 897 | // No ASCII shortcode name to show. | |
| 898 | continue; | |
| 899 | } | |
| 900 | } else if key.chars().all(|c| c.is_ascii_alphanumeric()) { | |
| 901 | key | |
| 902 | } else { | |
| 903 | // Not an Emoji or a printable ASCII string. | |
| 904 | continue; | |
| 905 | } | |
| 906 | } else { | |
| 907 | key | |
| 908 | }; | |
| 909 | ||
| 910 | emojis.push_str("[", style); | |
| 911 | emojis.push_str(name, style); | |
| 912 | emojis.push_str(" ", style); | |
| 913 | emojis.push_span_nobreak(Span::styled(count.to_string(), style)); | |
| 914 | emojis.push_str("]", style); | |
| 915 | ||
| 916 | reactions += 1; | |
| 917 | } | |
| 918 | ||
| 919 | if reactions > 0 { | |
| 920 | fmt.push_text(emojis.finish(), style, &mut text); | |
| 921 | } | |
| 956 | let reactions = info.get_reactions(self.event.event_id()); | |
| 957 | fmt.push_reactions(reactions, style, &mut text); | |
| 922 | 958 | } |
| 923 | 959 | |
| 924 | if let Some(thread) = info.threads.get(self.event.event_id()) { | |
| 925 | // If we have threaded replies to this message, show how many. | |
| 926 | let len = thread.len(); | |
| 927 | ||
| 928 | if len > 0 { | |
| 929 | let style = Style::default(); | |
| 930 | let emoji_shortcodes = settings.tunables.message_shortcode_display; | |
| 931 | let mut threaded = | |
| 932 | printer::TextPrinter::new(width, style, false, emoji_shortcodes).literal(true); | |
| 933 | let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD)); | |
| 934 | threaded.push_str(" \u{2937} ", style); | |
| 935 | threaded.push_span_nobreak(len); | |
| 936 | threaded.push_str(" replies in thread", style); | |
| 937 | fmt.push_text(threaded.finish(), style, &mut text); | |
| 938 | } | |
| 960 | if let Some(thread) = info.get_thread(Some(self.event.event_id())) { | |
| 961 | fmt.push_thread_reply_count(thread.len(), &mut text); | |
| 939 | 962 | } |
| 940 | 963 | |
| 941 | 964 | text |
src/tests.rs +6 −20
| @@ -21,7 +21,7 @@ use tracing::Level; | ||
| 21 | 21 | use url::Url; |
| 22 | 22 | |
| 23 | 23 | use crate::{ |
| 24 | base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo}, | |
| 24 | base::{ChatStore, EventLocation, ProgramStore, RoomInfo}, | |
| 25 | 25 | config::{ |
| 26 | 26 | user_color, |
| 27 | 27 | user_style_from_color, |
| @@ -148,25 +148,11 @@ pub fn mock_messages() -> Messages { | ||
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | pub fn mock_room() -> RoomInfo { |
| 151 | RoomInfo { | |
| 152 | name: Some("Watercooler Discussion".into()), | |
| 153 | tags: None, | |
| 154 | ||
| 155 | keys: mock_keys(), | |
| 156 | messages: mock_messages(), | |
| 157 | threads: HashMap::default(), | |
| 158 | ||
| 159 | event_receipts: HashMap::new(), | |
| 160 | user_receipts: HashMap::new(), | |
| 161 | reactions: HashMap::new(), | |
| 162 | ||
| 163 | fetching: false, | |
| 164 | fetch_id: RoomFetchStatus::NotStarted, | |
| 165 | fetch_last: None, | |
| 166 | users_typing: None, | |
| 167 | display_names: HashMap::new(), | |
| 168 | draw_last: None, | |
| 169 | } | |
| 151 | let mut room = RoomInfo::default(); | |
| 152 | room.name = Some("Watercooler Discussion".into()); | |
| 153 | room.keys = mock_keys(); | |
| 154 | *room.get_thread_mut(None) = mock_messages(); | |
| 155 | room | |
| 170 | 156 | } |
| 171 | 157 | |
| 172 | 158 | pub fn mock_dirs() -> DirectoryValues { |
src/windows/room/chat.rs +23 −48
| @@ -28,7 +28,6 @@ use matrix_sdk::{ | ||
| 28 | 28 | RoomMessageEventContent, |
| 29 | 29 | TextMessageEventContent, |
| 30 | 30 | }, |
| 31 | EventId, | |
| 32 | 31 | OwnedEventId, |
| 33 | 32 | OwnedRoomId, |
| 34 | 33 | RoomId, |
| @@ -72,7 +71,6 @@ use modalkit::prelude::*; | ||
| 72 | 71 | |
| 73 | 72 | use crate::base::{ |
| 74 | 73 | DownloadFlags, |
| 75 | EventLocation, | |
| 76 | 74 | IambAction, |
| 77 | 75 | IambBufferId, |
| 78 | 76 | IambError, |
| @@ -148,32 +146,10 @@ impl ChatState { | ||
| 148 | 146 | } |
| 149 | 147 | } |
| 150 | 148 | |
| 151 | fn get_thread_last<'a>( | |
| 152 | &self, | |
| 153 | thread_root: &OwnedEventId, | |
| 154 | info: &'a RoomInfo, | |
| 155 | ) -> Option<&'a OriginalRoomMessageEvent> { | |
| 156 | let last = info.threads.get(thread_root).and_then(|t| Some(t.last_key_value()?.1)); | |
| 157 | ||
| 158 | let msg = if let Some(last) = last { | |
| 159 | &last.event | |
| 160 | } else if let EventLocation::Message(_, key) = info.keys.get(thread_root)? { | |
| 161 | let msg = info.messages.get(key)?; | |
| 162 | &msg.event | |
| 163 | } else { | |
| 164 | return None; | |
| 165 | }; | |
| 166 | ||
| 167 | if let MessageEvent::Original(ev) = &msg { | |
| 168 | Some(ev) | |
| 169 | } else { | |
| 170 | None | |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 174 | 149 | fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> { |
| 150 | let thread = self.scrollback.get_thread(info)?; | |
| 175 | 151 | let key = self.reply_to.as_ref()?; |
| 176 | let msg = info.messages.get(key)?; | |
| 152 | let msg = thread.get(key)?; | |
| 177 | 153 | |
| 178 | 154 | if let MessageEvent::Original(ev) = &msg.event { |
| 179 | 155 | Some(ev) |
| @@ -205,10 +181,7 @@ impl ChatState { | ||
| 205 | 181 | let settings = &store.application.settings; |
| 206 | 182 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 207 | 183 | |
| 208 | let msg = self | |
| 209 | .scrollback | |
| 210 | .get_mut(&mut info.messages) | |
| 211 | .ok_or(IambError::NoSelectedMessage)?; | |
| 184 | let msg = self.scrollback.get_mut(info).ok_or(IambError::NoSelectedMessage)?; | |
| 212 | 185 | |
| 213 | 186 | match act { |
| 214 | 187 | MessageAction::Cancel(skip_confirm) => { |
| @@ -441,11 +414,11 @@ impl ChatState { | ||
| 441 | 414 | }, |
| 442 | 415 | MessageAction::Unreact(emoji) => { |
| 443 | 416 | let room = self.get_joined(&store.application.worker)?; |
| 444 | let event_id: &EventId = match &msg.event { | |
| 445 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.as_ref(), | |
| 446 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.as_ref(), | |
| 447 | MessageEvent::Original(ev) => ev.event_id.as_ref(), | |
| 448 | MessageEvent::Local(event_id, _) => event_id.as_ref(), | |
| 417 | let event_id = match &msg.event { | |
| 418 | MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), | |
| 419 | MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(), | |
| 420 | MessageEvent::Original(ev) => ev.event_id.clone(), | |
| 421 | MessageEvent::Local(event_id, _) => event_id.clone(), | |
| 449 | 422 | MessageEvent::Redacted(_) => { |
| 450 | 423 | let msg = "Cannot unreact to a redacted message"; |
| 451 | 424 | let err = UIError::Failure(msg.into()); |
| @@ -454,7 +427,7 @@ impl ChatState { | ||
| 454 | 427 | }, |
| 455 | 428 | }; |
| 456 | 429 | |
| 457 | let reactions = match info.reactions.get(event_id) { | |
| 430 | let reactions = match info.reactions.get(&event_id) { | |
| 458 | 431 | Some(r) => r, |
| 459 | 432 | None => return Ok(None), |
| 460 | 433 | }; |
| @@ -518,7 +491,7 @@ impl ChatState { | ||
| 518 | 491 | } else if let Some(thread_root) = self.scrollback.thread() { |
| 519 | 492 | if let Some(m) = self.get_reply_to(info) { |
| 520 | 493 | msg = msg.make_for_thread(m, ReplyWithinThread::Yes, AddMentions::No); |
| 521 | } else if let Some(m) = self.get_thread_last(thread_root, info) { | |
| 494 | } else if let Some(m) = info.get_thread_last(thread_root) { | |
| 522 | 495 | msg = msg.make_for_thread(m, ReplyWithinThread::No, AddMentions::No); |
| 523 | 496 | } else { |
| 524 | 497 | // Internal state is wonky? |
| @@ -884,19 +857,21 @@ impl<'a> StatefulWidget for Chat<'a> { | ||
| 884 | 857 | |
| 885 | 858 | fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { |
| 886 | 859 | // Determine whether we have a description to show for the message bar. |
| 887 | let desc_spans = match (&state.editing, &state.reply_to) { | |
| 888 | (None, None) => None, | |
| 889 | (Some(_), None) => Some(Line::from("Editing message")), | |
| 890 | (editing, Some(_)) => { | |
| 891 | state.reply_to.as_ref().and_then(|k| { | |
| 892 | let room = self.store.application.rooms.get(state.id())?; | |
| 893 | let msg = room.messages.get(k)?; | |
| 860 | let desc_spans = match (&state.editing, &state.reply_to, state.thread()) { | |
| 861 | (None, None, None) => None, | |
| 862 | (None, None, Some(_)) => Some(Line::from("Replying in thread")), | |
| 863 | (Some(_), None, None) => Some(Line::from("Editing message")), | |
| 864 | (Some(_), None, Some(_)) => Some(Line::from("Editing message in thread")), | |
| 865 | (editing, Some(_), thread) => { | |
| 866 | self.store.application.rooms.get(state.id()).and_then(|room| { | |
| 867 | let msg = state.get_reply_to(room)?; | |
| 894 | 868 | let user = |
| 895 | 869 | self.store.application.settings.get_user_span(msg.sender.as_ref(), room); |
| 896 | let prefix = if editing.is_some() { | |
| 897 | Span::from("Editing reply to ") | |
| 898 | } else { | |
| 899 | Span::from("Replying to ") | |
| 870 | let prefix = match (editing.is_some(), thread.is_some()) { | |
| 871 | (true, false) => Span::from("Editing reply to "), | |
| 872 | (true, true) => Span::from("Editing reply in thread to "), | |
| 873 | (false, false) => Span::from("Replying to "), | |
| 874 | (false, true) => Span::from("Replying in thread to "), | |
| 900 | 875 | }; |
| 901 | 876 | let spans = Line::from(vec![prefix, user]); |
| 902 | 877 | |
src/windows/room/scrollback.rs +49 −55
| @@ -1,5 +1,4 @@ | ||
| 1 | 1 | //! Message scrollback |
| 2 | ||
| 3 | 2 | use ratatui_image::Image; |
| 4 | 3 | use regex::Regex; |
| 5 | 4 | |
| @@ -59,6 +58,11 @@ use crate::{ | ||
| 59 | 58 | message::{Message, MessageCursor, MessageKey, Messages}, |
| 60 | 59 | }; |
| 61 | 60 | |
| 61 | fn no_msgs() -> EditError<IambInfo> { | |
| 62 | let msg = "No messages to select."; | |
| 63 | EditError::Failure(msg.to_string()) | |
| 64 | } | |
| 65 | ||
| 62 | 66 | fn nth_key_before(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey { |
| 63 | 67 | let mut end = &pos; |
| 64 | 68 | let iter = thread.range(..=&pos).rev().enumerate(); |
| @@ -159,14 +163,16 @@ impl ScrollbackState { | ||
| 159 | 163 | self.cursor |
| 160 | 164 | .timestamp |
| 161 | 165 | .clone() |
| 162 | .or_else(|| self.get_thread(info).last_key_value().map(|kv| kv.0.clone())) | |
| 166 | .or_else(|| self.get_thread(info)?.last_key_value().map(|kv| kv.0.clone())) | |
| 163 | 167 | } |
| 164 | 168 | |
| 165 | pub fn get_mut<'a>(&mut self, messages: &'a mut Messages) -> Option<&'a mut Message> { | |
| 169 | pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> { | |
| 170 | let thread = self.get_thread_mut(info); | |
| 171 | ||
| 166 | 172 | if let Some(k) = &self.cursor.timestamp { |
| 167 | messages.get_mut(k) | |
| 173 | thread.get_mut(k) | |
| 168 | 174 | } else { |
| 169 | messages.last_entry().map(|o| o.into_mut()) | |
| 175 | thread.last_entry().map(|o| o.into_mut()) | |
| 170 | 176 | } |
| 171 | 177 | } |
| 172 | 178 | |
| @@ -174,20 +180,12 @@ impl ScrollbackState { | ||
| 174 | 180 | self.thread.as_ref() |
| 175 | 181 | } |
| 176 | 182 | |
| 177 | pub fn get_thread<'a>(&self, info: &'a RoomInfo) -> &'a Messages { | |
| 178 | if let Some(thread_root) = self.thread.as_ref() { | |
| 179 | info.threads.get(thread_root).unwrap_or(&info.messages) | |
| 180 | } else { | |
| 181 | &info.messages | |
| 182 | } | |
| 183 | pub fn get_thread<'a>(&self, info: &'a RoomInfo) -> Option<&'a Messages> { | |
| 184 | info.get_thread(self.thread.as_deref()) | |
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | pub fn get_thread_mut<'a>(&self, info: &'a mut RoomInfo) -> &'a mut Messages { |
| 186 | if let Some(thread_root) = self.thread.as_ref() { | |
| 187 | info.threads.get_mut(thread_root).unwrap_or(&mut info.messages) | |
| 188 | } else { | |
| 189 | &mut info.messages | |
| 190 | } | |
| 188 | info.get_thread_mut(self.thread.clone()) | |
| 191 | 189 | } |
| 192 | 190 | |
| 193 | 191 | pub fn messages<'a>( |
| @@ -195,7 +193,10 @@ impl ScrollbackState { | ||
| 195 | 193 | range: EditRange<MessageCursor>, |
| 196 | 194 | info: &'a RoomInfo, |
| 197 | 195 | ) -> impl Iterator<Item = (&'a MessageKey, &'a Message)> { |
| 198 | let thread = self.get_thread(info); | |
| 196 | let Some(thread) = self.get_thread(info) else { | |
| 197 | return Default::default(); | |
| 198 | }; | |
| 199 | ||
| 199 | 200 | let start = range.start.to_key(thread); |
| 200 | 201 | let end = range.end.to_key(thread); |
| 201 | 202 | |
| @@ -223,7 +224,7 @@ impl ScrollbackState { | ||
| 223 | 224 | _ => {}, |
| 224 | 225 | } |
| 225 | 226 | |
| 226 | let first_key = self.get_thread(info).first_key_value().map(|(k, _)| k); | |
| 227 | let first_key = self.get_thread(info).and_then(|t| t.first_key_value()).map(|(k, _)| k); | |
| 227 | 228 | let at_top = first_key == self.viewctx.corner.timestamp.as_ref(); |
| 228 | 229 | |
| 229 | 230 | match (at_top, self.thread.as_ref()) { |
| @@ -254,7 +255,10 @@ impl ScrollbackState { | ||
| 254 | 255 | info: &RoomInfo, |
| 255 | 256 | settings: &ApplicationSettings, |
| 256 | 257 | ) { |
| 257 | let thread = self.get_thread(info); | |
| 258 | let Some(thread) = self.get_thread(info) else { | |
| 259 | return; | |
| 260 | }; | |
| 261 | ||
| 258 | 262 | let selidx = if let Some(key) = self.cursor.to_key(thread) { |
| 259 | 263 | key |
| 260 | 264 | } else { |
| @@ -319,7 +323,9 @@ impl ScrollbackState { | ||
| 319 | 323 | } |
| 320 | 324 | |
| 321 | 325 | fn shift_cursor(&mut self, info: &RoomInfo, settings: &ApplicationSettings) { |
| 322 | let thread = self.get_thread(info); | |
| 326 | let Some(thread) = self.get_thread(info) else { | |
| 327 | return; | |
| 328 | }; | |
| 323 | 329 | |
| 324 | 330 | let last_key = if let Some(k) = thread.last_key_value() { |
| 325 | 331 | k.0 |
| @@ -389,7 +395,7 @@ impl ScrollbackState { | ||
| 389 | 395 | MoveType::BufferLineOffset => None, |
| 390 | 396 | MoveType::BufferLinePercent => None, |
| 391 | 397 | MoveType::BufferPos(MovePosition::Beginning) => { |
| 392 | let start = self.get_thread(info).first_key_value()?.0.clone(); | |
| 398 | let start = self.get_thread(info)?.first_key_value()?.0.clone(); | |
| 393 | 399 | |
| 394 | 400 | Some(start.into()) |
| 395 | 401 | }, |
| @@ -402,7 +408,7 @@ impl ScrollbackState { | ||
| 402 | 408 | MoveType::ParagraphBegin(dir) | |
| 403 | 409 | MoveType::SectionBegin(dir) | |
| 404 | 410 | MoveType::SectionEnd(dir) => { |
| 405 | let thread = self.get_thread(info); | |
| 411 | let thread = self.get_thread(info)?; | |
| 406 | 412 | |
| 407 | 413 | match dir { |
| 408 | 414 | MoveDir1D::Previous => nth_before(pos, count, thread).into(), |
| @@ -455,14 +461,14 @@ impl ScrollbackState { | ||
| 455 | 461 | RangeType::XmlTag => None, |
| 456 | 462 | |
| 457 | 463 | RangeType::Buffer => { |
| 458 | let thread = self.get_thread(info); | |
| 464 | let thread = self.get_thread(info)?; | |
| 459 | 465 | let start = thread.first_key_value()?.0.clone(); |
| 460 | 466 | let end = thread.last_key_value()?.0.clone(); |
| 461 | 467 | |
| 462 | 468 | Some(EditRange::inclusive(start.into(), end.into(), TargetShape::LineWise)) |
| 463 | 469 | }, |
| 464 | 470 | RangeType::Line | RangeType::Paragraph | RangeType::Sentence => { |
| 465 | let thread = self.get_thread(info); | |
| 471 | let thread = self.get_thread(info)?; | |
| 466 | 472 | let count = ctx.resolve(count); |
| 467 | 473 | |
| 468 | 474 | if count == 0 { |
| @@ -496,7 +502,7 @@ impl ScrollbackState { | ||
| 496 | 502 | mut count: usize, |
| 497 | 503 | info: &RoomInfo, |
| 498 | 504 | ) -> Option<MessageCursor> { |
| 499 | let thread = self.get_thread(info); | |
| 505 | let thread = self.get_thread(info)?; | |
| 500 | 506 | let mut mc = None; |
| 501 | 507 | |
| 502 | 508 | for (key, msg) in thread.range(&start..) { |
| @@ -524,9 +530,12 @@ impl ScrollbackState { | ||
| 524 | 530 | mut count: usize, |
| 525 | 531 | info: &RoomInfo, |
| 526 | 532 | ) -> (Option<MessageCursor>, bool) { |
| 527 | let thread = self.get_thread(info); | |
| 528 | 533 | let mut mc = None; |
| 529 | 534 | |
| 535 | let Some(thread) = self.get_thread(info) else { | |
| 536 | return (None, false); | |
| 537 | }; | |
| 538 | ||
| 530 | 539 | for (key, msg) in thread.range(..&end).rev() { |
| 531 | 540 | if count == 0 { |
| 532 | 541 | break; |
| @@ -614,14 +623,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 614 | 623 | store: &mut ProgramStore, |
| 615 | 624 | ) -> EditResult<EditInfo, IambInfo> { |
| 616 | 625 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 617 | let thread = self.get_thread(info); | |
| 618 | let key = if let Some(k) = self.cursor.to_key(thread) { | |
| 619 | k.clone() | |
| 620 | } else { | |
| 621 | let msg = "No messages to select."; | |
| 622 | let err = EditError::Failure(msg.to_string()); | |
| 623 | return Err(err); | |
| 624 | }; | |
| 626 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 627 | let key = self.cursor.to_key(thread).ok_or_else(no_msgs)?.clone(); | |
| 625 | 628 | |
| 626 | 629 | match operation { |
| 627 | 630 | EditAction::Motion => { |
| @@ -644,7 +647,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 644 | 647 | EditTarget::CharJump(mark) | EditTarget::LineJump(mark) => { |
| 645 | 648 | let mark = ctx.resolve(mark); |
| 646 | 649 | let cursor = store.cursors.get_mark(self.id.clone(), mark)?; |
| 647 | let thread = self.get_thread(info); | |
| 648 | 650 | |
| 649 | 651 | if let Some(mc) = MessageCursor::from_cursor(&cursor, thread) { |
| 650 | 652 | Some(mc) |
| @@ -723,7 +725,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 723 | 725 | EditTarget::CharJump(mark) | EditTarget::LineJump(mark) => { |
| 724 | 726 | let mark = ctx.resolve(mark); |
| 725 | 727 | let cursor = store.cursors.get_mark(self.id.clone(), mark)?; |
| 726 | let thread = self.get_thread(info); | |
| 727 | 728 | |
| 728 | 729 | if let Some(c) = MessageCursor::from_cursor(&cursor, thread) { |
| 729 | 730 | self._range_to(c).into() |
| @@ -821,18 +822,11 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 821 | 822 | store: &mut ProgramStore, |
| 822 | 823 | ) -> EditResult<EditInfo, IambInfo> { |
| 823 | 824 | let info = store.application.get_room_info(self.room_id.clone()); |
| 824 | let thread = self.get_thread(info); | |
| 825 | ||
| 826 | if let Some(cursor) = self.cursor.to_cursor(thread) { | |
| 827 | store.cursors.set_mark(self.id.clone(), name, cursor); | |
| 828 | ||
| 829 | Ok(None) | |
| 830 | } else { | |
| 831 | let msg = "Failed to set mark for message"; | |
| 832 | let err = EditError::Failure(msg.into()); | |
| 825 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 826 | let cursor = self.cursor.to_cursor(thread).ok_or_else(no_msgs)?; | |
| 827 | store.cursors.set_mark(self.id.clone(), name, cursor); | |
| 833 | 828 | |
| 834 | Err(err) | |
| 835 | } | |
| 829 | Ok(None) | |
| 836 | 830 | } |
| 837 | 831 | |
| 838 | 832 | fn complete( |
| @@ -884,6 +878,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 884 | 878 | store: &mut ProgramStore, |
| 885 | 879 | ) -> EditResult<EditInfo, IambInfo> { |
| 886 | 880 | let info = store.application.get_room_info(self.room_id.clone()); |
| 881 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 887 | 882 | |
| 888 | 883 | match act { |
| 889 | 884 | CursorAction::Close(_) => Ok(None), |
| @@ -901,8 +896,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 901 | 896 | self.push_jump(); |
| 902 | 897 | } |
| 903 | 898 | |
| 904 | let thread = self.get_thread(info); | |
| 905 | ||
| 906 | 899 | if let Some(mc) = MessageCursor::from_cursor(ngroup.leader.cursor(), thread) { |
| 907 | 900 | self.cursor = mc; |
| 908 | 901 | |
| @@ -916,7 +909,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 916 | 909 | }, |
| 917 | 910 | CursorAction::Save(_) => { |
| 918 | 911 | let reg = ctx.get_register().unwrap_or(Register::UnnamedCursorGroup); |
| 919 | let thread = self.get_thread(info); | |
| 920 | 912 | |
| 921 | 913 | // Lists don't have groups; override any previously saved group. |
| 922 | 914 | let cursor = self.cursor.to_cursor(thread).ok_or_else(|| { |
| @@ -1019,7 +1011,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1019 | 1011 | store: &mut ProgramStore, |
| 1020 | 1012 | ) -> EditResult<Vec<(Action<IambInfo>, ProgramContext)>, IambInfo> { |
| 1021 | 1013 | let info = store.application.get_room_info(self.room_id.clone()); |
| 1022 | let thread = self.get_thread(info); | |
| 1014 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 1023 | 1015 | |
| 1024 | 1016 | let Some(key) = self.cursor.to_key(thread) else { |
| 1025 | 1017 | let msg = "No message currently selected"; |
| @@ -1068,7 +1060,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1068 | 1060 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 1069 | 1061 | let settings = &store.application.settings; |
| 1070 | 1062 | let mut corner = self.viewctx.corner.clone(); |
| 1071 | let thread = self.get_thread(info); | |
| 1063 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 1072 | 1064 | |
| 1073 | 1065 | let last_key = if let Some(k) = thread.last_key_value() { |
| 1074 | 1066 | k.0 |
| @@ -1182,7 +1174,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState { | ||
| 1182 | 1174 | Axis::Vertical => { |
| 1183 | 1175 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| 1184 | 1176 | let settings = &store.application.settings; |
| 1185 | let thread = self.get_thread(info); | |
| 1177 | let thread = self.get_thread(info).ok_or_else(no_msgs)?; | |
| 1186 | 1178 | |
| 1187 | 1179 | if let Some(key) = self.cursor.to_key(thread).cloned() { |
| 1188 | 1180 | self.scrollview(key, pos, info, settings); |
| @@ -1315,12 +1307,14 @@ impl<'a> StatefulWidget for Scrollback<'a> { | ||
| 1315 | 1307 | return; |
| 1316 | 1308 | } |
| 1317 | 1309 | |
| 1310 | let Some(thread) = state.get_thread(info) else { | |
| 1311 | return; | |
| 1312 | }; | |
| 1313 | ||
| 1318 | 1314 | if state.cursor.timestamp < state.viewctx.corner.timestamp { |
| 1319 | 1315 | state.viewctx.corner = state.cursor.clone(); |
| 1320 | 1316 | } |
| 1321 | 1317 | |
| 1322 | let thread = state.get_thread(info); | |
| 1323 | ||
| 1324 | 1318 | let cursor = &state.cursor; |
| 1325 | 1319 | let cursor_key = if let Some(k) = cursor.to_key(thread) { |
| 1326 | 1320 | k |
src/worker.rs +2 −31
| @@ -52,7 +52,7 @@ use matrix_sdk::{ | ||
| 52 | 52 | member::OriginalSyncRoomMemberEvent, |
| 53 | 53 | message::{MessageType, RoomMessageEventContent}, |
| 54 | 54 | name::RoomNameEventContent, |
| 55 | redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent}, | |
| 55 | redaction::OriginalSyncRoomRedactionEvent, | |
| 56 | 56 | }, |
| 57 | 57 | tag::Tags, |
| 58 | 58 | typing::SyncTypingEvent, |
| @@ -94,7 +94,6 @@ use crate::{ | ||
| 94 | 94 | ChatStore, |
| 95 | 95 | CreateRoomFlags, |
| 96 | 96 | CreateRoomType, |
| 97 | EventLocation, | |
| 98 | 97 | IambError, |
| 99 | 98 | IambResult, |
| 100 | 99 | ProgramStore, |
| @@ -1063,35 +1062,7 @@ impl ClientWorker { | ||
| 1063 | 1062 | |
| 1064 | 1063 | let mut locked = store.lock().await; |
| 1065 | 1064 | let info = locked.application.get_room_info(room_id.to_owned()); |
| 1066 | ||
| 1067 | let Some(redacts) = &ev.redacts else { | |
| 1068 | return; | |
| 1069 | }; | |
| 1070 | ||
| 1071 | match info.keys.get(redacts) { | |
| 1072 | None => return, | |
| 1073 | Some(EventLocation::Message(None, key)) => { | |
| 1074 | if let Some(msg) = info.messages.get_mut(key) { | |
| 1075 | let ev = SyncRoomRedactionEvent::Original(ev); | |
| 1076 | msg.redact(ev, room_version); | |
| 1077 | } | |
| 1078 | }, | |
| 1079 | Some(EventLocation::Message(Some(root), key)) => { | |
| 1080 | if let Some(thread) = info.threads.get_mut(root) { | |
| 1081 | if let Some(msg) = thread.get_mut(key) { | |
| 1082 | let ev = SyncRoomRedactionEvent::Original(ev); | |
| 1083 | msg.redact(ev, room_version); | |
| 1084 | } | |
| 1085 | } | |
| 1086 | }, | |
| 1087 | Some(EventLocation::Reaction(event_id)) => { | |
| 1088 | if let Some(reactions) = info.reactions.get_mut(event_id) { | |
| 1089 | reactions.remove(redacts); | |
| 1090 | } | |
| 1091 | ||
| 1092 | info.keys.remove(redacts); | |
| 1093 | }, | |
| 1094 | } | |
| 1065 | info.redact(ev, room_version); | |
| 1095 | 1066 | } |
| 1096 | 1067 | }, |
| 1097 | 1068 | ); |