diff --git a/crates/matrix-sdk-ui/src/timeline/util.rs b/crates/matrix-sdk-ui/src/timeline/algorithms.rs similarity index 77% rename from crates/matrix-sdk-ui/src/timeline/util.rs rename to crates/matrix-sdk-ui/src/timeline/algorithms.rs index 2bb9c449456..edf3464bee5 100644 --- a/crates/matrix-sdk-ui/src/timeline/util.rs +++ b/crates/matrix-sdk-ui/src/timeline/algorithms.rs @@ -14,9 +14,8 @@ use std::{ops::Deref, sync::Arc}; -use chrono::{Datelike, Local, TimeZone}; use imbl::Vector; -use ruma::{EventId, MilliSecondsSinceUnixEpoch}; +use ruma::EventId; #[cfg(doc)] use super::controller::TimelineMetadata; @@ -113,40 +112,3 @@ pub(super) fn rfind_event_by_item_id<'a>( TimelineEventItemId::EventId(event_id) => rfind_event_by_id(items, event_id), } } - -/// Result of comparing events position in the timeline. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(super) enum RelativePosition { - /// Event B is after (more recent than) event A. - After, - /// They are the same event. - Same, - /// Event B is before (older than) event A. - Before, -} - -#[derive(Debug, PartialEq)] -pub(super) struct Date { - year: i32, - month: u32, - day: u32, -} - -impl Date { - pub fn is_same_month_as(&self, date: Date) -> bool { - self.year == date.year && self.month == date.month - } -} - -/// Converts a timestamp since Unix Epoch to a year, month and day. -pub(super) fn timestamp_to_date(ts: MilliSecondsSinceUnixEpoch) -> Date { - let datetime = Local - .timestamp_millis_opt(ts.0.into()) - // Only returns `None` if date is after Dec 31, 262143 BCE. - .single() - // Fallback to the current date to avoid issues with malicious - // homeservers. - .unwrap_or_else(Local::now); - - Date { year: datetime.year(), month: datetime.month(), day: datetime.day() } -} diff --git a/crates/matrix-sdk-ui/src/timeline/controller/mod.rs b/crates/matrix-sdk-ui/src/timeline/controller/mod.rs index faa4305ea7e..066aecab3c9 100644 --- a/crates/matrix-sdk-ui/src/timeline/controller/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/controller/mod.rs @@ -64,28 +64,29 @@ pub(super) use self::{ }, }; use super::{ + algorithms::{rfind_event_by_id, rfind_event_item}, event_handler::TimelineEventKind, event_item::{ReactionStatus, RemoteEventOrigin}, item::TimelineUniqueId, traits::{Decryptor, RoomDataProvider}, - util::{rfind_event_by_id, rfind_event_item, RelativePosition}, DateDividerMode, Error, EventSendState, EventTimelineItem, InReplyToDetails, Message, PaginationError, Profile, ReactionInfo, RepliedToEvent, TimelineDetails, TimelineEventItemId, TimelineFocus, TimelineItem, TimelineItemContent, TimelineItemKind, }; use crate::{ timeline::{ + algorithms::rfind_event_by_item_id, date_dividers::DateDividerAdjuster, event_item::EventTimelineItemKind, pinned_events_loader::{PinnedEventsLoader, PinnedEventsLoaderError}, reactions::FullReactionKey, - util::rfind_event_by_item_id, TimelineEventFilterFn, }, unable_to_decrypt_hook::UtdHookManager, }; mod observable_items; +mod read_receipts; mod state; /// Data associated to the current timeline focus. @@ -1648,3 +1649,14 @@ async fn fetch_replied_to_event( }; Ok(res) } + +/// Result of comparing events position in the timeline. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(in crate::timeline) enum RelativePosition { + /// Event B is after (more recent than) event A. + After, + /// They are the same event. + Same, + /// Event B is before (older than) event A. + Before, +} diff --git a/crates/matrix-sdk-ui/src/timeline/read_receipts.rs b/crates/matrix-sdk-ui/src/timeline/controller/read_receipts.rs similarity index 98% rename from crates/matrix-sdk-ui/src/timeline/read_receipts.rs rename to crates/matrix-sdk-ui/src/timeline/controller/read_receipts.rs index 00964de20be..6c3b232f562 100644 --- a/crates/matrix-sdk-ui/src/timeline/read_receipts.rs +++ b/crates/matrix-sdk-ui/src/timeline/controller/read_receipts.rs @@ -25,14 +25,10 @@ use tokio_stream::wrappers::WatchStream; use tracing::{debug, error, warn}; use super::{ - controller::{ - AllRemoteEvents, FullEventMeta, ObservableItemsTransaction, TimelineMetadata, - TimelineState, TimelineStateTransaction, - }, - traits::RoomDataProvider, - util::{rfind_event_by_id, RelativePosition}, - TimelineItem, + rfind_event_by_id, AllRemoteEvents, FullEventMeta, ObservableItemsTransaction, + RelativePosition, RoomDataProvider, TimelineMetadata, TimelineState, }; +use crate::timeline::{controller::TimelineStateTransaction, TimelineItem}; /// In-memory caches for read receipts. #[derive(Clone, Debug, Default)] diff --git a/crates/matrix-sdk-ui/src/timeline/controller/state.rs b/crates/matrix-sdk-ui/src/timeline/controller/state.rs index dffda89f6ce..2f91777777c 100644 --- a/crates/matrix-sdk-ui/src/timeline/controller/state.rs +++ b/crates/matrix-sdk-ui/src/timeline/controller/state.rs @@ -49,11 +49,13 @@ use super::{ AllRemoteEvents, ObservableItems, ObservableItemsTransaction, ObservableItemsTransactionEntry, }, - DateDividerMode, HandleManyEventsResult, TimelineFocusKind, TimelineSettings, + read_receipts::ReadReceipts, + DateDividerMode, HandleManyEventsResult, RelativePosition, TimelineFocusKind, TimelineSettings, }; use crate::{ events::SyncTimelineEventWithoutContent, timeline::{ + algorithms::rfind_event_by_id, date_dividers::DateDividerAdjuster, event_handler::{ Flow, HandleEventResult, TimelineEventContext, TimelineEventHandler, TimelineEventKind, @@ -62,9 +64,7 @@ use crate::{ event_item::{PollState, RemoteEventOrigin, ResponseData}, item::TimelineUniqueId, reactions::Reactions, - read_receipts::ReadReceipts, traits::RoomDataProvider, - util::{rfind_event_by_id, RelativePosition}, Profile, TimelineItem, TimelineItemKind, }, unable_to_decrypt_hook::UtdHookManager, @@ -1058,7 +1058,7 @@ pub(in crate::timeline) struct TimelineMetadata { /// Read receipts related state. /// /// TODO: move this over to the event cache (see also #3058). - pub read_receipts: ReadReceipts, + pub(in crate::timeline::controller) read_receipts: ReadReceipts, } /// Maximum number of stash pending edits. @@ -1110,7 +1110,7 @@ impl TimelineMetadata { /// known. /// /// Returns `None` if none of the two events could be found in the timeline. - pub fn compare_events_positions( + pub(in crate::timeline) fn compare_events_positions( &self, event_a: &EventId, event_b: &EventId, diff --git a/crates/matrix-sdk-ui/src/timeline/date_dividers.rs b/crates/matrix-sdk-ui/src/timeline/date_dividers.rs index 09f903b7b86..55ac269433c 100644 --- a/crates/matrix-sdk-ui/src/timeline/date_dividers.rs +++ b/crates/matrix-sdk-ui/src/timeline/date_dividers.rs @@ -17,15 +17,41 @@ use std::{fmt::Display, sync::Arc}; +use chrono::{Datelike, Local, TimeZone}; use ruma::MilliSecondsSinceUnixEpoch; use tracing::{error, event_enabled, instrument, trace, warn, Level}; use super::{ controller::{ObservableItemsTransaction, TimelineMetadata}, - util::timestamp_to_date, DateDividerMode, TimelineItem, TimelineItemKind, VirtualTimelineItem, }; +#[derive(Debug, PartialEq)] +struct Date { + year: i32, + month: u32, + day: u32, +} + +impl Date { + fn is_same_month_as(&self, date: Date) -> bool { + self.year == date.year && self.month == date.month + } +} + +/// Converts a timestamp since Unix Epoch to a year, month and day. +fn timestamp_to_date(ts: MilliSecondsSinceUnixEpoch) -> Date { + let datetime = Local + .timestamp_millis_opt(ts.0.into()) + // Only returns `None` if date is after Dec 31, 262143 BCE. + .single() + // Fallback to the current date to avoid issues with malicious + // homeservers. + .unwrap_or_else(Local::now); + + Date { year: datetime.year(), month: datetime.month(), day: datetime.day() } +} + /// Algorithm ensuring that date dividers are adjusted correctly, according to /// new items that have been inserted. pub(super) struct DateDividerAdjuster { @@ -618,8 +644,8 @@ mod tests { use super::{super::controller::ObservableItems, DateDividerAdjuster}; use crate::timeline::{ controller::TimelineMetadata, + date_dividers::timestamp_to_date, event_item::{EventTimelineItemKind, RemoteEventTimelineItem}, - util::timestamp_to_date, DateDividerMode, EventTimelineItem, TimelineItemContent, VirtualTimelineItem, }; diff --git a/crates/matrix-sdk-ui/src/timeline/event_handler.rs b/crates/matrix-sdk-ui/src/timeline/event_handler.rs index a1262a78a2c..8cdcf1256ee 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_handler.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_handler.rs @@ -51,6 +51,7 @@ use ruma::{ use tracing::{debug, error, field::debug, info, instrument, trace, warn}; use super::{ + algorithms::{rfind_event_by_id, rfind_event_item}, controller::{ ObservableItemsTransaction, ObservableItemsTransactionEntry, PendingEdit, PendingEditKind, TimelineMetadata, TimelineStateTransaction, @@ -64,7 +65,6 @@ use super::{ }, reactions::{FullReactionKey, PendingReaction}, traits::RoomDataProvider, - util::{rfind_event_by_id, rfind_event_item}, EventTimelineItem, InReplyToDetails, OtherState, RepliedToEvent, Sticker, TimelineDetails, TimelineItem, TimelineItemContent, }; diff --git a/crates/matrix-sdk-ui/src/timeline/mod.rs b/crates/matrix-sdk-ui/src/timeline/mod.rs index df2ce390a79..7219925278a 100644 --- a/crates/matrix-sdk-ui/src/timeline/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/mod.rs @@ -18,6 +18,7 @@ use std::{fs, path::PathBuf, pin::Pin, sync::Arc, task::Poll}; +use algorithms::rfind_event_by_item_id; use event_item::{extract_room_msg_edit_content, TimelineItemHandle}; use eyeball_im::VectorDiff; use futures_core::Stream; @@ -53,10 +54,13 @@ use ruma::{ }; use thiserror::Error; use tracing::{error, instrument, trace, warn}; -use util::rfind_event_by_item_id; +use self::{ + algorithms::rfind_event_by_id, controller::TimelineController, futures::SendAttachment, +}; use crate::timeline::pinned_events_loader::PinnedEventsRoom; +mod algorithms; mod builder; mod controller; mod date_dividers; @@ -69,12 +73,10 @@ mod item; mod pagination; mod pinned_events_loader; mod reactions; -mod read_receipts; #[cfg(test)] mod tests; mod to_device; mod traits; -mod util; mod virtual_item; pub use self::{ @@ -94,7 +96,6 @@ pub use self::{ traits::RoomExt, virtual_item::VirtualTimelineItem, }; -use self::{controller::TimelineController, futures::SendAttachment, util::rfind_event_by_id}; /// Information needed to reply to an event. #[derive(Debug, Clone)] diff --git a/crates/matrix-sdk-ui/src/timeline/tests/mod.rs b/crates/matrix-sdk-ui/src/timeline/tests/mod.rs index eb6601c167d..13f974f1621 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/mod.rs @@ -61,11 +61,11 @@ use ruma::{ use tokio::sync::RwLock; use super::{ + algorithms::rfind_event_by_item_id, controller::{TimelineNewItemPosition, TimelineSettings}, event_handler::TimelineEventKind, event_item::RemoteEventOrigin, traits::RoomDataProvider, - util::rfind_event_by_item_id, EventTimelineItem, Profile, TimelineController, TimelineEventItemId, TimelineFocus, TimelineItem, }; diff --git a/crates/matrix-sdk/src/send_queue.rs b/crates/matrix-sdk/src/send_queue/mod.rs similarity index 100% rename from crates/matrix-sdk/src/send_queue.rs rename to crates/matrix-sdk/src/send_queue/mod.rs