| 1 | use chatsvc::{MessageWindow, WindowDirection}; |
| 2 | use chrono::{DateTime, Utc}; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::engine::{SyncEngine, sorted}; |
| 6 | use crate::error::Result; |
| 7 | use crate::mapping::message_record; |
| 8 | use crate::remote::Remote; |
| 9 | |
| 10 | pub const WINDOW_PAGE_SIZE: usize = 20; |
| 11 | |
| 12 | #[derive(Debug, Clone, Default)] |
| 13 | pub struct WindowPage { |
| 14 | pub records: Vec<MessageRecord>, |
| 15 | pub older_cursor: Option<String>, |
| 16 | pub newer_cursor: Option<String>, |
| 17 | pub oldest: Option<DateTime<Utc>>, |
| 18 | pub newest: Option<DateTime<Utc>>, |
| 19 | } |
| 20 | |
| 21 | impl<R: Remote> SyncEngine<R> { |
| 22 | pub async fn fetch_chat_message( |
| 23 | &self, |
| 24 | chat_id: &str, |
| 25 | message_id: &str, |
| 26 | ) -> Result<Option<MessageRecord>> { |
| 27 | let message = self.remote.chat_message(chat_id, message_id).await?; |
| 28 | Ok(message_record(chat_id, &message)) |
| 29 | } |
| 30 | |
| 31 | pub async fn load_window(&self, chat_id: &str, anchor_id: &str) -> Result<WindowPage> { |
| 32 | self.window_page(chat_id, anchor_id, WindowDirection::Bidirectional) |
| 33 | .await |
| 34 | } |
| 35 | |
| 36 | pub async fn load_window_page( |
| 37 | &self, |
| 38 | chat_id: &str, |
| 39 | cursor: &str, |
| 40 | direction: WindowDirection, |
| 41 | ) -> Result<WindowPage> { |
| 42 | self.window_page(chat_id, cursor, direction).await |
| 43 | } |
| 44 | |
| 45 | async fn window_page( |
| 46 | &self, |
| 47 | chat_id: &str, |
| 48 | position: &str, |
| 49 | direction: WindowDirection, |
| 50 | ) -> Result<WindowPage> { |
| 51 | let MessageWindow { |
| 52 | message_ids, |
| 53 | older_cursor, |
| 54 | newer_cursor, |
| 55 | } = self |
| 56 | .remote |
| 57 | .chat_message_window(chat_id, position, direction, WINDOW_PAGE_SIZE) |
| 58 | .await?; |
| 59 | let mut records = Vec::with_capacity(message_ids.len()); |
| 60 | for answer in self |
| 61 | .remote |
| 62 | .chat_messages_by_id(chat_id, &message_ids) |
| 63 | .await? |
| 64 | { |
| 65 | if let Some(record) = answer |
| 66 | .ok() |
| 67 | .and_then(|message| message_record(chat_id, &message)) |
| 68 | { |
| 69 | records.push(record); |
| 70 | } |
| 71 | } |
| 72 | let mut records = sorted(records); |
| 73 | self.apply_chat_emotions(chat_id, &mut records).await; |
| 74 | self.ingest(chat_id, records.clone())?; |
| 75 | self.announce(chat_id, !records.is_empty()); |
| 76 | Ok(WindowPage { |
| 77 | oldest: records.first().map(|record| record.created_at), |
| 78 | newest: records.last().map(|record| record.created_at), |
| 79 | records, |
| 80 | older_cursor, |
| 81 | newer_cursor, |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 87 | pub enum JumpPlan { |
| 88 | InPlace, |
| 89 | LeaveWindow, |
| 90 | LoadWindow, |
| 91 | } |
| 92 | |
| 93 | pub fn jump_plan( |
| 94 | window: Option<(DateTime<Utc>, DateTime<Utc>)>, |
| 95 | target_created_at: Option<DateTime<Utc>>, |
| 96 | oldest_loaded: Option<DateTime<Utc>>, |
| 97 | ) -> JumpPlan { |
| 98 | let Some(created_at) = target_created_at else { |
| 99 | return JumpPlan::LoadWindow; |
| 100 | }; |
| 101 | if window.is_some_and(|(oldest, newest)| oldest <= created_at && created_at <= newest) { |
| 102 | JumpPlan::InPlace |
| 103 | } else if oldest_loaded.is_none_or(|oldest_loaded| created_at >= oldest_loaded) { |
| 104 | JumpPlan::LeaveWindow |
| 105 | } else { |
| 106 | JumpPlan::LoadWindow |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn without_islands( |
| 111 | mut records: Vec<MessageRecord>, |
| 112 | oldest_loaded: Option<DateTime<Utc>>, |
| 113 | ) -> Vec<MessageRecord> { |
| 114 | if let Some(oldest_loaded) = oldest_loaded { |
| 115 | records.retain(|record| record.created_at >= oldest_loaded); |
| 116 | } |
| 117 | records |
| 118 | } |
| 119 | |
| 120 | #[cfg(test)] |
| 121 | mod tests { |
| 122 | use chrono::TimeZone; |
| 123 | |
| 124 | use super::*; |
| 125 | |
| 126 | fn at(minute: u32) -> DateTime<Utc> { |
| 127 | Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap() |
| 128 | } |
| 129 | |
| 130 | fn record(minute: u32) -> MessageRecord { |
| 131 | MessageRecord { |
| 132 | message_id: minute.to_string(), |
| 133 | created_at: at(minute), |
| 134 | ..MessageRecord::default() |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn jump_plan_and_island_filter() { |
| 140 | assert_eq!(jump_plan(None, None, Some(at(5))), JumpPlan::LoadWindow); |
| 141 | assert_eq!( |
| 142 | jump_plan(None, Some(at(4)), Some(at(5))), |
| 143 | JumpPlan::LoadWindow |
| 144 | ); |
| 145 | assert_eq!( |
| 146 | jump_plan(None, Some(at(5)), Some(at(5))), |
| 147 | JumpPlan::LeaveWindow |
| 148 | ); |
| 149 | assert_eq!(jump_plan(None, Some(at(1)), None), JumpPlan::LeaveWindow); |
| 150 | let window = Some((at(10), at(20))); |
| 151 | assert_eq!( |
| 152 | jump_plan(window, Some(at(15)), Some(at(30))), |
| 153 | JumpPlan::InPlace |
| 154 | ); |
| 155 | assert_eq!( |
| 156 | jump_plan(window, Some(at(25)), Some(at(5))), |
| 157 | JumpPlan::LeaveWindow |
| 158 | ); |
| 159 | assert_eq!( |
| 160 | jump_plan(window, Some(at(9)), Some(at(30))), |
| 161 | JumpPlan::LoadWindow |
| 162 | ); |
| 163 | let kept = without_islands(vec![record(1), record(5), record(6)], Some(at(5))); |
| 164 | let ids: Vec<&str> = kept |
| 165 | .iter() |
| 166 | .map(|record| record.message_id.as_str()) |
| 167 | .collect(); |
| 168 | assert_eq!(ids, ["5", "6"]); |
| 169 | assert_eq!(without_islands(vec![record(1)], None).len(), 1); |
| 170 | } |
| 171 | } |