Skip to content

Commit

Permalink
fixup! Try moving pinned events related logic to the `PinnedEventsRoo…
Browse files Browse the repository at this point in the history
…m` trait, expose it from `RoomInfo`
  • Loading branch information
jmartinesp committed Aug 2, 2024
1 parent 8863981 commit 237ee9a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
39 changes: 21 additions & 18 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,11 +949,6 @@ impl Room {
self.inner.read().recency_stamp
}

/// Get the list of event ids for pinned events in this room.
pub fn pinned_event_ids(&self) -> Vec<OwnedEventId> {
self.inner.get().base_info.pinned_events.map(|content| content.pinned).unwrap_or_default()
}

/// Get a `Stream` of loaded pinned events for this room.
/// If no pinned events are found a single empty `Vec` will be returned.
pub fn pinned_event_ids_stream(&self) -> impl Stream<Item = Vec<OwnedEventId>> {
Expand All @@ -962,19 +957,9 @@ impl Room {
.map(|i| i.base_info.pinned_events.map(|c| c.pinned).unwrap_or_default())
}

/// Checks if an `EventId` is currently pinned.
/// It avoids having to clone the whole list of event ids to check a single
/// value.
///
/// Returns `true` if the provided `event_id` is pinned, `false` otherwise.
pub fn is_pinned_event(&self, event_id: &EventId) -> bool {
self.inner
.read()
.base_info
.pinned_events
.as_ref()
.map(|p| p.pinned.contains(&event_id.to_owned()))
.unwrap_or_default()
/// Returns the current pinned event ids for this room.
pub fn pinned_event_ids(&self) -> Vec<OwnedEventId> {
self.inner.read().pinned_event_ids()
}
}

Expand Down Expand Up @@ -1495,6 +1480,24 @@ impl RoomInfo {
pub(crate) fn update_recency_stamp(&mut self, stamp: u64) {
self.recency_stamp = Some(stamp);
}

/// Returns the current pinned event ids for this room.
pub fn pinned_event_ids(&self) -> Vec<OwnedEventId> {
self.base_info.pinned_events.clone().map(|c| c.pinned).unwrap_or_default()
}

/// Checks if an `EventId` is currently pinned.
/// It avoids having to clone the whole list of event ids to check a single
/// value.
///
/// Returns `true` if the provided `event_id` is pinned, `false` otherwise.
pub fn is_pinned_event(&self, event_id: &EventId) -> bool {
self.base_info
.pinned_events
.as_ref()
.map(|p| p.pinned.contains(&event_id.to_owned()))
.unwrap_or_default()
}
}

#[cfg(feature = "experimental-sliding-sync")]
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 @@ -348,7 +348,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
// events in an event-focused timeline.
let can_add_to_live = match self.live_timeline_updates_type {
LiveTimelineUpdatesAllowed::PinnedEvents => {
room_data_provider.room_is_pinned_event_id(event_id)
room_data_provider.is_pinned_event(event_id)
}
LiveTimelineUpdatesAllowed::All => true,
LiveTimelineUpdatesAllowed::None => false,
Expand Down
2 changes: 2 additions & 0 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ use ruma::{
use thiserror::Error;
use tracing::{error, instrument, trace, warn};

use crate::timeline::pinned_events_loader::PinnedEventsRoom;

mod builder;
mod day_dividers;
mod error;
Expand Down
25 changes: 14 additions & 11 deletions crates/matrix-sdk-ui/src/timeline/pinned_events_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl PinnedEventsLoader {
pub async fn load_events(&self) -> Result<Vec<SyncTimelineEvent>, PinnedEventsLoaderError> {
let pinned_event_ids: Vec<OwnedEventId> = self
.room
.room_pinned_event_ids()
.pinned_event_ids()
.into_iter()
.rev()
.take(self.max_events_to_load)
Expand Down Expand Up @@ -71,7 +71,7 @@ impl PinnedEventsLoader {
.await
.map_err(|_| PinnedEventsLoaderError::SemaphoreNotAcquired)?;
let ret = provider
.room_event(&id)
.event(&id)
.await
.map_err(|_| PinnedEventsLoaderError::EventNotFound(id.to_owned()));
drop(permit);
Expand Down Expand Up @@ -118,7 +118,7 @@ impl PinnedEventsLoader {
for ev in events {
let ev = ev.into();
if let Some(ev_id) = ev.event_id() {
if self.room.room_is_pinned_event_id(&ev_id) {
if self.room.is_pinned_event(&ev_id) {
to_update.push(ev);
}
}
Expand All @@ -137,31 +137,34 @@ impl PinnedEventsLoader {
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait PinnedEventsRoom: SendOutsideWasm + SyncOutsideWasm {
/// Load a single room event.
async fn room_event(&self, event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError>;
async fn event(&self, event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError>;

/// Get the pinned event ids for a room.
fn room_pinned_event_ids(&self) -> Vec<OwnedEventId>;
fn pinned_event_ids(&self) -> Vec<OwnedEventId>;

/// Checks whether an event id is pinned in this room.
fn room_is_pinned_event_id(&self, event_id: &EventId) -> bool;
///
/// It avoids having to clone the whole list of event ids to check a single
/// value.
fn is_pinned_event(&self, event_id: &EventId) -> bool;
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl PinnedEventsRoom for Room {
async fn room_event(&self, event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError> {
async fn event(&self, event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError> {
self.event(event_id)
.await
.map(|e| e.into())
.map_err(|err| PaginatorError::SdkError(Box::new(err)))
}

fn room_pinned_event_ids(&self) -> Vec<OwnedEventId> {
self.pinned_event_ids()
fn pinned_event_ids(&self) -> Vec<OwnedEventId> {
self.clone_info().pinned_event_ids()
}

fn room_is_pinned_event_id(&self, event_id: &EventId) -> bool {
self.is_pinned_event(event_id)
fn is_pinned_event(&self, event_id: &EventId) -> bool {
self.subscribe_info().read().is_pinned_event(event_id)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-ui/src/timeline/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ impl PaginableRoom for TestRoomDataProvider {
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl PinnedEventsRoom for TestRoomDataProvider {
async fn room_event(&self, _event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError> {
async fn event(&self, _event_id: &EventId) -> Result<SyncTimelineEvent, PaginatorError> {
unimplemented!();
}

fn room_pinned_event_ids(&self) -> Vec<OwnedEventId> {
fn pinned_event_ids(&self) -> Vec<OwnedEventId> {
unimplemented!();
}

fn room_is_pinned_event_id(&self, _event_id: &EventId) -> bool {
fn is_pinned_event(&self, _event_id: &EventId) -> bool {
unimplemented!();
}
}
Expand Down

0 comments on commit 237ee9a

Please sign in to comment.