Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): some code motion #4493

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() }
}
16 changes: 14 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-ui/src/timeline/controller/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 28 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/date_dividers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
Expand Down
9 changes: 5 additions & 4 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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::{
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/timeline/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
Loading