Skip to content

Commit

Permalink
ui: inline SlidingSyncRoomExt::timeline into its own caller
Browse files Browse the repository at this point in the history
It's only used in test code, so it's not worth exposing to the SlidingSyncRoom object (and the Room already has such a timeline function, if needs be).
  • Loading branch information
bnjbvr committed Jan 30, 2024
1 parent fd26cbc commit 1c1ecf0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
5 changes: 3 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ impl TimelineBuilder {
}

/// Add initial events to the timeline.
///
/// TODO: remove this, the EventGraph should hold the events data in the
/// first place, and we'd provide an existing EventGraph to the
/// TimelineBuilder.
pub(crate) async fn events(
pub async fn events(
mut self,
prev_token: Option<String>,
events: Vector<SyncTimelineEvent>,
Expand All @@ -75,7 +76,7 @@ impl TimelineBuilder {

/// Enable tracking of the fully-read marker and the read receipts on the
/// timeline.
pub(crate) fn track_read_marker_and_receipts(mut self) -> Self {
pub fn track_read_marker_and_receipts(mut self) -> Self {
self.settings.track_read_receipts = true;
self
}
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ impl From<&Annotation> for AnnotationKey {
}

impl Timeline {
pub(crate) fn builder(room: &Room) -> TimelineBuilder {
/// Create a new [`TimelineBuilder`] for the given room.
pub fn builder(room: &Room) -> TimelineBuilder {
TimelineBuilder::new(room)
}

Expand Down
30 changes: 2 additions & 28 deletions crates/matrix-sdk-ui/src/timeline/sliding_sync_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@

use async_trait::async_trait;
use matrix_sdk::SlidingSyncRoom;
use tracing::{error, instrument};
use tracing::instrument;

use super::{EventTimelineItem, Timeline, TimelineBuilder};
use crate::event_graph::Result;
use super::EventTimelineItem;

#[async_trait]
pub trait SlidingSyncRoomExt {
/// Get a `Timeline` for this room.
// TODO(bnjbvr): remove from this trait.
async fn timeline(&self) -> Result<Option<Timeline>>;

/// Get the latest timeline item of this room, if it is already cached.
///
/// Use `Timeline::latest_event` instead if you already have a timeline for
Expand All @@ -34,14 +29,6 @@ pub trait SlidingSyncRoomExt {

#[async_trait]
impl SlidingSyncRoomExt for SlidingSyncRoom {
async fn timeline(&self) -> Result<Option<Timeline>> {
if let Some(builder) = sliding_sync_timeline_builder(self).await {
Ok(Some(builder.track_read_marker_and_receipts().build().await?))
} else {
Ok(None)
}
}

/// Get a timeline item representing the latest event in this room.
/// This method wraps latest_event, converting the event into an
/// EventTimelineItem.
Expand All @@ -52,19 +39,6 @@ impl SlidingSyncRoomExt for SlidingSyncRoom {
}
}

async fn sliding_sync_timeline_builder(room: &SlidingSyncRoom) -> Option<TimelineBuilder> {
let room_id = room.room_id();
match room.client().get_room(room_id) {
Some(r) => {
Some(Timeline::builder(&r).events(room.prev_batch(), room.timeline_queue()).await)
}
None => {
error!(?room_id, "Room not found in client. Can't provide a timeline for it");
None
}
}
}

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
Expand Down
29 changes: 18 additions & 11 deletions crates/matrix-sdk-ui/tests/integration/timeline/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::{pin::Pin, sync::Arc};

use anyhow::{Context, Result};
use anyhow::{Context as _, Result};
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::{Vector, VectorDiff};
Expand All @@ -23,8 +23,9 @@ use matrix_sdk::{
SlidingSync, SlidingSyncList, SlidingSyncListBuilder, SlidingSyncMode, UpdateSummary,
};
use matrix_sdk_test::async_test;
use matrix_sdk_ui::timeline::{
SlidingSyncRoomExt, TimelineItem, TimelineItemKind, VirtualTimelineItem,
use matrix_sdk_ui::{
timeline::{TimelineItem, TimelineItemKind, VirtualTimelineItem},
Timeline,
};
use ruma::{room_id, user_id, RoomId};
use serde_json::json;
Expand Down Expand Up @@ -248,15 +249,21 @@ async fn timeline_test_helper(
sliding_sync: &SlidingSync,
room_id: &RoomId,
) -> Result<(Vector<Arc<TimelineItem>>, impl Stream<Item = VectorDiff<Arc<TimelineItem>>>)> {
Ok(sliding_sync
.get_room(room_id)
let sliding_sync_room = sliding_sync.get_room(room_id).await.unwrap();

let room_id = sliding_sync_room.room_id();
let sdk_room = sliding_sync_room.client().get_room(room_id).ok_or_else(|| {
anyhow::anyhow!("Room {room_id} not found in client. Can't provide a timeline for it")
})?;

let timeline = Timeline::builder(&sdk_room)
.events(sliding_sync_room.prev_batch(), sliding_sync_room.timeline_queue())
.await
.unwrap()
.timeline()
.await?
.context("`timeline`")?
.subscribe()
.await)
.track_read_marker_and_receipts()
.build()
.await?;

Ok(timeline.subscribe().await)
}

struct SlidingSyncMatcher;
Expand Down

0 comments on commit 1c1ecf0

Please sign in to comment.