refactor(sdk): LatestEvent::_room_id becomes _weak_room: WeakRoom.

This patch replaces `OwnedRoomId` by `WeakRoom` in `LatestEvent`. Apart
from simplifying a couple of method' signatures, it also opens the road
for storing the `LatestEventValue` in `RoomInfo`.
This commit is contained in:
Ivan Enderlin
2025-08-27 14:28:08 +02:00
parent 951d22ac24
commit f3eeb82b0b
2 changed files with 14 additions and 34 deletions

View File

@@ -22,8 +22,7 @@ use ruma::{
room::{member::MembershipState, message::MessageType, power_levels::RoomPowerLevels},
AnyMessageLikeEventContent, AnySyncStateEvent, AnySyncTimelineEvent, SyncStateEvent,
},
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedTransactionId, RoomId,
TransactionId, UserId,
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedTransactionId, TransactionId, UserId,
};
use serde::{Deserialize, Serialize};
use tracing::error;
@@ -36,7 +35,7 @@ use crate::{event_cache::RoomEventCache, room::WeakRoom, send_queue::RoomSendQue
#[derive(Debug)]
pub(super) struct LatestEvent {
/// The room owning this latest event.
_room_id: OwnedRoomId,
_weak_room: WeakRoom,
/// The thread (if any) owning this latest event.
_thread_id: Option<OwnedEventId>,
@@ -52,13 +51,12 @@ pub(super) struct LatestEvent {
impl LatestEvent {
pub(super) async fn new(
room_id: &RoomId,
weak_room: &WeakRoom,
thread_id: Option<&EventId>,
room_event_cache: &RoomEventCache,
weak_room: &WeakRoom,
) -> Self {
Self {
_room_id: room_id.to_owned(),
_weak_room: weak_room.clone(),
_thread_id: thread_id.map(ToOwned::to_owned),
buffer_of_values_for_local_events: LatestEventValuesForLocalEvents::new(),
current_value: SharedObservable::new_async(
@@ -203,7 +201,7 @@ mod tests_latest_event {
let (room_event_cache, _) = event_cache.for_room(room_id).await.unwrap();
let mut latest_event = LatestEvent::new(room_id, None, &room_event_cache, &weak_room).await;
let mut latest_event = LatestEvent::new(&weak_room, None, &room_event_cache).await;
// First off, check the default value is `None`!
assert_matches!(latest_event.current_value.get().await, LatestEventValue::None);
@@ -268,8 +266,7 @@ mod tests_latest_event {
let send_queue = client.send_queue();
let room_send_queue = send_queue.for_room(room);
let mut latest_event =
LatestEvent::new(&room_id, None, &room_event_cache, &weak_room).await;
let mut latest_event = LatestEvent::new(&weak_room, None, &room_event_cache).await;
// First, let's create a `LatestEventValue` from the event cache. It must work.
{

View File

@@ -290,9 +290,7 @@ impl RegisteredRooms {
if room_latest_event.per_thread.contains_key(thread_id).not() {
room_latest_event.per_thread.insert(
thread_id.to_owned(),
room_latest_event
.create_latest_event_for(room_id, Some(thread_id))
.await,
room_latest_event.create_latest_event_for(Some(thread_id)).await,
);
}
}
@@ -464,40 +462,25 @@ impl RoomLatestEvents {
};
Ok(Some(Self {
for_the_room: Self::create_latest_event_for_inner(
room_id,
None,
&room_event_cache,
&weak_room,
)
.await,
for_the_room: Self::create_latest_event_for_inner(&weak_room, None, &room_event_cache)
.await,
per_thread: HashMap::new(),
weak_room,
room_event_cache,
}))
}
async fn create_latest_event_for(
&self,
room_id: &RoomId,
thread_id: Option<&EventId>,
) -> LatestEvent {
Self::create_latest_event_for_inner(
room_id,
thread_id,
&self.room_event_cache,
&self.weak_room,
)
.await
async fn create_latest_event_for(&self, thread_id: Option<&EventId>) -> LatestEvent {
Self::create_latest_event_for_inner(&self.weak_room, thread_id, &self.room_event_cache)
.await
}
async fn create_latest_event_for_inner(
room_id: &RoomId,
weak_room: &WeakRoom,
thread_id: Option<&EventId>,
room_event_cache: &RoomEventCache,
weak_room: &WeakRoom,
) -> LatestEvent {
LatestEvent::new(room_id, thread_id, room_event_cache, weak_room).await
LatestEvent::new(weak_room, thread_id, room_event_cache).await
}
/// Get the [`LatestEvent`] for the room.