Commit 2ac71da9a6

2ac71da9a676d15d8572d7c1d9692658cd46bf24

parent: 1e9b6cc271

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-03-24 02:20 UTC
committer: <noreply@github.com>

Fix entering thread view when there's no messages yet (#224)

Layout: unified · split

src/base.rs +77 −2
@@ -45,6 +45,7 @@ use matrix_sdk::{
4545 RoomMessageEventContent,
4646 RoomMessageEventContentWithoutRelation,
4747 },
48 room::redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
4849 tag::{TagName, Tags},
4950 MessageLikeEvent,
5051 },
@@ -54,6 +55,7 @@ use matrix_sdk::{
5455 OwnedRoomId,
5556 OwnedUserId,
5657 RoomId,
58 RoomVersionId,
5759 UserId,
5860 },
5961 RoomState as MatrixRoomState,
@@ -729,7 +731,7 @@ pub struct RoomInfo {
729731 pub keys: HashMap<OwnedEventId, EventLocation>,
730732
731733 /// The messages loaded for this room.
732 pub messages: Messages,
734 messages: Messages,
733735
734736 /// A map of read markers to display on different events.
735737 pub event_receipts: HashMap<OwnedEventId, HashSet<OwnedUserId>>,
@@ -745,7 +747,7 @@ pub struct RoomInfo {
745747 pub reactions: HashMap<OwnedEventId, MessageReactions>,
746748
747749 /// A map of message identifiers to thread replies.
748 pub threads: HashMap<OwnedEventId, Messages>,
750 threads: HashMap<OwnedEventId, Messages>,
749751
750752 /// Whether the scrollback for this room is currently being fetched.
751753 pub fetching: bool,
@@ -767,6 +769,48 @@ pub struct RoomInfo {
767769}
768770
769771impl 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
770814 /// Get the reactions and their counts for a message.
771815 pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize)> {
772816 if let Some(reacts) = self.reactions.get(event_id) {
@@ -801,6 +845,37 @@ impl RoomInfo {
801845 self.messages.get_mut(self.keys.get(event_id)?.to_message_key()?)
802846 }
803847
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
804879 /// Insert a reaction to a message.
805880 pub fn insert_reaction(&mut self, react: ReactionEvent) {
806881 match react {
src/message/mod.rs +105 −82
@@ -638,6 +638,106 @@ impl<'a> MessageFormatter<'a> {
638638 self.push_spans(line, style, text);
639639 }
640640 }
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 }
641741}
642742
643743pub enum ImageStatus {
@@ -835,33 +935,7 @@ impl Message {
835935 .and_then(|e| info.get_event(&e));
836936
837937 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);
865939 }
866940
867941 // Now show the message contents, and the inlined reply if we couldn't find it above.
@@ -879,63 +953,12 @@ impl Message {
879953 }
880954
881955 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);
922958 }
923959
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);
939962 }
940963
941964 text
src/tests.rs +6 −20
@@ -21,7 +21,7 @@ use tracing::Level;
2121use url::Url;
2222
2323use crate::{
24 base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo},
24 base::{ChatStore, EventLocation, ProgramStore, RoomInfo},
2525 config::{
2626 user_color,
2727 user_style_from_color,
@@ -148,25 +148,11 @@ pub fn mock_messages() -> Messages {
148148}
149149
150150pub 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
170156}
171157
172158pub fn mock_dirs() -> DirectoryValues {
src/windows/room/chat.rs +23 −48
@@ -28,7 +28,6 @@ use matrix_sdk::{
2828 RoomMessageEventContent,
2929 TextMessageEventContent,
3030 },
31 EventId,
3231 OwnedEventId,
3332 OwnedRoomId,
3433 RoomId,
@@ -72,7 +71,6 @@ use modalkit::prelude::*;
7271
7372use crate::base::{
7473 DownloadFlags,
75 EventLocation,
7674 IambAction,
7775 IambBufferId,
7876 IambError,
@@ -148,32 +146,10 @@ impl ChatState {
148146 }
149147 }
150148
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
174149 fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
150 let thread = self.scrollback.get_thread(info)?;
175151 let key = self.reply_to.as_ref()?;
176 let msg = info.messages.get(key)?;
152 let msg = thread.get(key)?;
177153
178154 if let MessageEvent::Original(ev) = &msg.event {
179155 Some(ev)
@@ -205,10 +181,7 @@ impl ChatState {
205181 let settings = &store.application.settings;
206182 let info = store.application.rooms.get_or_default(self.room_id.clone());
207183
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)?;
212185
213186 match act {
214187 MessageAction::Cancel(skip_confirm) => {
@@ -441,11 +414,11 @@ impl ChatState {
441414 },
442415 MessageAction::Unreact(emoji) => {
443416 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(),
449422 MessageEvent::Redacted(_) => {
450423 let msg = "Cannot unreact to a redacted message";
451424 let err = UIError::Failure(msg.into());
@@ -454,7 +427,7 @@ impl ChatState {
454427 },
455428 };
456429
457 let reactions = match info.reactions.get(event_id) {
430 let reactions = match info.reactions.get(&event_id) {
458431 Some(r) => r,
459432 None => return Ok(None),
460433 };
@@ -518,7 +491,7 @@ impl ChatState {
518491 } else if let Some(thread_root) = self.scrollback.thread() {
519492 if let Some(m) = self.get_reply_to(info) {
520493 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) {
522495 msg = msg.make_for_thread(m, ReplyWithinThread::No, AddMentions::No);
523496 } else {
524497 // Internal state is wonky?
@@ -884,19 +857,21 @@ impl<'a> StatefulWidget for Chat<'a> {
884857
885858 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
886859 // 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)?;
894868 let user =
895869 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 "),
900875 };
901876 let spans = Line::from(vec![prefix, user]);
902877
src/windows/room/scrollback.rs +49 −55
@@ -1,5 +1,4 @@
11//! Message scrollback
2
32use ratatui_image::Image;
43use regex::Regex;
54
@@ -59,6 +58,11 @@ use crate::{
5958 message::{Message, MessageCursor, MessageKey, Messages},
6059};
6160
61fn no_msgs() -> EditError<IambInfo> {
62 let msg = "No messages to select.";
63 EditError::Failure(msg.to_string())
64}
65
6266fn nth_key_before(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey {
6367 let mut end = &pos;
6468 let iter = thread.range(..=&pos).rev().enumerate();
@@ -159,14 +163,16 @@ impl ScrollbackState {
159163 self.cursor
160164 .timestamp
161165 .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()))
163167 }
164168
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
166172 if let Some(k) = &self.cursor.timestamp {
167 messages.get_mut(k)
173 thread.get_mut(k)
168174 } else {
169 messages.last_entry().map(|o| o.into_mut())
175 thread.last_entry().map(|o| o.into_mut())
170176 }
171177 }
172178
@@ -174,20 +180,12 @@ impl ScrollbackState {
174180 self.thread.as_ref()
175181 }
176182
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())
183185 }
184186
185187 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())
191189 }
192190
193191 pub fn messages<'a>(
@@ -195,7 +193,10 @@ impl ScrollbackState {
195193 range: EditRange<MessageCursor>,
196194 info: &'a RoomInfo,
197195 ) -> 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
199200 let start = range.start.to_key(thread);
200201 let end = range.end.to_key(thread);
201202
@@ -223,7 +224,7 @@ impl ScrollbackState {
223224 _ => {},
224225 }
225226
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);
227228 let at_top = first_key == self.viewctx.corner.timestamp.as_ref();
228229
229230 match (at_top, self.thread.as_ref()) {
@@ -254,7 +255,10 @@ impl ScrollbackState {
254255 info: &RoomInfo,
255256 settings: &ApplicationSettings,
256257 ) {
257 let thread = self.get_thread(info);
258 let Some(thread) = self.get_thread(info) else {
259 return;
260 };
261
258262 let selidx = if let Some(key) = self.cursor.to_key(thread) {
259263 key
260264 } else {
@@ -319,7 +323,9 @@ impl ScrollbackState {
319323 }
320324
321325 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 };
323329
324330 let last_key = if let Some(k) = thread.last_key_value() {
325331 k.0
@@ -389,7 +395,7 @@ impl ScrollbackState {
389395 MoveType::BufferLineOffset => None,
390396 MoveType::BufferLinePercent => None,
391397 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();
393399
394400 Some(start.into())
395401 },
@@ -402,7 +408,7 @@ impl ScrollbackState {
402408 MoveType::ParagraphBegin(dir) |
403409 MoveType::SectionBegin(dir) |
404410 MoveType::SectionEnd(dir) => {
405 let thread = self.get_thread(info);
411 let thread = self.get_thread(info)?;
406412
407413 match dir {
408414 MoveDir1D::Previous => nth_before(pos, count, thread).into(),
@@ -455,14 +461,14 @@ impl ScrollbackState {
455461 RangeType::XmlTag => None,
456462
457463 RangeType::Buffer => {
458 let thread = self.get_thread(info);
464 let thread = self.get_thread(info)?;
459465 let start = thread.first_key_value()?.0.clone();
460466 let end = thread.last_key_value()?.0.clone();
461467
462468 Some(EditRange::inclusive(start.into(), end.into(), TargetShape::LineWise))
463469 },
464470 RangeType::Line | RangeType::Paragraph | RangeType::Sentence => {
465 let thread = self.get_thread(info);
471 let thread = self.get_thread(info)?;
466472 let count = ctx.resolve(count);
467473
468474 if count == 0 {
@@ -496,7 +502,7 @@ impl ScrollbackState {
496502 mut count: usize,
497503 info: &RoomInfo,
498504 ) -> Option<MessageCursor> {
499 let thread = self.get_thread(info);
505 let thread = self.get_thread(info)?;
500506 let mut mc = None;
501507
502508 for (key, msg) in thread.range(&start..) {
@@ -524,9 +530,12 @@ impl ScrollbackState {
524530 mut count: usize,
525531 info: &RoomInfo,
526532 ) -> (Option<MessageCursor>, bool) {
527 let thread = self.get_thread(info);
528533 let mut mc = None;
529534
535 let Some(thread) = self.get_thread(info) else {
536 return (None, false);
537 };
538
530539 for (key, msg) in thread.range(..&end).rev() {
531540 if count == 0 {
532541 break;
@@ -614,14 +623,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
614623 store: &mut ProgramStore,
615624 ) -> EditResult<EditInfo, IambInfo> {
616625 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();
625628
626629 match operation {
627630 EditAction::Motion => {
@@ -644,7 +647,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
644647 EditTarget::CharJump(mark) | EditTarget::LineJump(mark) => {
645648 let mark = ctx.resolve(mark);
646649 let cursor = store.cursors.get_mark(self.id.clone(), mark)?;
647 let thread = self.get_thread(info);
648650
649651 if let Some(mc) = MessageCursor::from_cursor(&cursor, thread) {
650652 Some(mc)
@@ -723,7 +725,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
723725 EditTarget::CharJump(mark) | EditTarget::LineJump(mark) => {
724726 let mark = ctx.resolve(mark);
725727 let cursor = store.cursors.get_mark(self.id.clone(), mark)?;
726 let thread = self.get_thread(info);
727728
728729 if let Some(c) = MessageCursor::from_cursor(&cursor, thread) {
729730 self._range_to(c).into()
@@ -821,18 +822,11 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
821822 store: &mut ProgramStore,
822823 ) -> EditResult<EditInfo, IambInfo> {
823824 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);
833828
834 Err(err)
835 }
829 Ok(None)
836830 }
837831
838832 fn complete(
@@ -884,6 +878,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
884878 store: &mut ProgramStore,
885879 ) -> EditResult<EditInfo, IambInfo> {
886880 let info = store.application.get_room_info(self.room_id.clone());
881 let thread = self.get_thread(info).ok_or_else(no_msgs)?;
887882
888883 match act {
889884 CursorAction::Close(_) => Ok(None),
@@ -901,8 +896,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
901896 self.push_jump();
902897 }
903898
904 let thread = self.get_thread(info);
905
906899 if let Some(mc) = MessageCursor::from_cursor(ngroup.leader.cursor(), thread) {
907900 self.cursor = mc;
908901
@@ -916,7 +909,6 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
916909 },
917910 CursorAction::Save(_) => {
918911 let reg = ctx.get_register().unwrap_or(Register::UnnamedCursorGroup);
919 let thread = self.get_thread(info);
920912
921913 // Lists don't have groups; override any previously saved group.
922914 let cursor = self.cursor.to_cursor(thread).ok_or_else(|| {
@@ -1019,7 +1011,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
10191011 store: &mut ProgramStore,
10201012 ) -> EditResult<Vec<(Action<IambInfo>, ProgramContext)>, IambInfo> {
10211013 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)?;
10231015
10241016 let Some(key) = self.cursor.to_key(thread) else {
10251017 let msg = "No message currently selected";
@@ -1068,7 +1060,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
10681060 let info = store.application.rooms.get_or_default(self.room_id.clone());
10691061 let settings = &store.application.settings;
10701062 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)?;
10721064
10731065 let last_key = if let Some(k) = thread.last_key_value() {
10741066 k.0
@@ -1182,7 +1174,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
11821174 Axis::Vertical => {
11831175 let info = store.application.rooms.get_or_default(self.room_id.clone());
11841176 let settings = &store.application.settings;
1185 let thread = self.get_thread(info);
1177 let thread = self.get_thread(info).ok_or_else(no_msgs)?;
11861178
11871179 if let Some(key) = self.cursor.to_key(thread).cloned() {
11881180 self.scrollview(key, pos, info, settings);
@@ -1315,12 +1307,14 @@ impl<'a> StatefulWidget for Scrollback<'a> {
13151307 return;
13161308 }
13171309
1310 let Some(thread) = state.get_thread(info) else {
1311 return;
1312 };
1313
13181314 if state.cursor.timestamp < state.viewctx.corner.timestamp {
13191315 state.viewctx.corner = state.cursor.clone();
13201316 }
13211317
1322 let thread = state.get_thread(info);
1323
13241318 let cursor = &state.cursor;
13251319 let cursor_key = if let Some(k) = cursor.to_key(thread) {
13261320 k
src/worker.rs +2 −31
@@ -52,7 +52,7 @@ use matrix_sdk::{
5252 member::OriginalSyncRoomMemberEvent,
5353 message::{MessageType, RoomMessageEventContent},
5454 name::RoomNameEventContent,
55 redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
55 redaction::OriginalSyncRoomRedactionEvent,
5656 },
5757 tag::Tags,
5858 typing::SyncTypingEvent,
@@ -94,7 +94,6 @@ use crate::{
9494 ChatStore,
9595 CreateRoomFlags,
9696 CreateRoomType,
97 EventLocation,
9897 IambError,
9998 IambResult,
10099 ProgramStore,
@@ -1063,35 +1062,7 @@ impl ClientWorker {
10631062
10641063 let mut locked = store.lock().await;
10651064 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);
10951066 }
10961067 },
10971068 );