Skip to content

Commit

Permalink
make_reply_event: don't require the event cache to be enabled for the…
Browse files Browse the repository at this point in the history
… API to work
  • Loading branch information
bnjbvr committed Jul 22, 2024
1 parent ed2ab3f commit 5242f64
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
26 changes: 15 additions & 11 deletions crates/matrix-sdk/src/room/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ruma::{
EventId, RoomId, UserId,
};
use thiserror::Error;
use tracing::{instrument, warn};
use tracing::{debug, instrument, trace, warn};

use crate::Room;

Expand Down Expand Up @@ -98,17 +98,21 @@ trait EventSource {
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<'a> EventSource for &'a Room {
async fn get_event(&self, event_id: &EventId) -> Result<SyncTimelineEvent, EditError> {
let (event_cache, _drop_handles) =
self.event_cache().await.map_err(|err| EditError::Fetch(Box::new(err.into())))?;

match event_cache.event(event_id).await {
Some(ev) => Ok(ev),
None => self
.event(event_id)
.await
.map(Into::into)
.map_err(|err| EditError::Fetch(Box::new(err))),
match self.event_cache().await {
Ok((event_cache, _drop_handles)) => {
if let Some(event) = event_cache.event(event_id).await {
return Ok(event);
}
// Fallthrough: try with /event.
}

Err(err) => {
debug!("error when getting the event cache: {err}");
}
}

trace!("trying with /event now");
self.event(event_id).await.map(Into::into).map_err(|err| EditError::Fetch(Box::new(err)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/tests/integration/event_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async fn test_must_explicitly_subscribe() {

let room_id = room_id!("!omelette:fromage.fr");

// Make sure the client is aware of the room.
{
// Make sure the client is aware of the room.
let mut sync_builder = SyncResponseBuilder::new();
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));
let response_body = sync_builder.build_json_sync_response();
Expand Down
61 changes: 58 additions & 3 deletions crates/matrix-sdk/tests/integration/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::{
use futures_util::future::join_all;
use matrix_sdk::{
config::SyncSettings,
room::{Receipts, ReportedContentScore, RoomMemberRole},
room::{edit::EditedContent, Receipts, ReportedContentScore, RoomMemberRole},
test_utils::events::EventFactory,
};
use matrix_sdk_base::RoomState;
use matrix_sdk_test::{
Expand All @@ -16,7 +17,11 @@ use matrix_sdk_test::{
use ruma::{
api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType},
assign, event_id,
events::{receipt::ReceiptThread, room::message::RoomMessageEventContent, TimelineEventType},
events::{
receipt::ReceiptThread,
room::message::{RoomMessageEventContent, RoomMessageEventContentWithoutRelation},
TimelineEventType,
},
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, TransactionId,
};
use serde_json::{json, Value};
Expand All @@ -25,7 +30,10 @@ use wiremock::{
Mock, ResponseTemplate,
};

use crate::{logged_in_client_with_server, mock_encryption_state, mock_sync, synced_client};
use crate::{
logged_in_client_with_server, mock_encryption_state, mock_sync, mock_sync_with_new_room,
synced_client,
};

#[async_test]
async fn test_invite_user_by_id() {
Expand Down Expand Up @@ -713,3 +721,50 @@ async fn test_call_notifications_notify_for_rooms() {

room.send_call_notification_if_needed().await.unwrap();
}

#[async_test]
async fn test_make_reply_event_doesnt_require_event_cache() {
// Even if we don't have enabled the event cache, we'll resort to using the
// /event query to get details on an event.

let (client, server) = logged_in_client_with_server().await;

let event_id = event_id!("$1");
let resp_event_id = event_id!("$resp");
let room_id = room_id!("!galette:saucisse.bzh");

let f = EventFactory::new();

let raw_original_event = f
.text_msg("hi")
.event_id(event_id)
.sender(client.user_id().unwrap())
.room(room_id)
.into_raw_timeline();

mock_sync_with_new_room(
|builder| {
builder.add_joined_room(JoinedRoomBuilder::new(room_id));
},
&client,
&server,
room_id,
)
.await;

Mock::given(method("GET"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/event/"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(raw_original_event.json()))
.expect(1)
.named("/event")
.mount(&server)
.await;

let new_content = RoomMessageEventContentWithoutRelation::text_plain("uh i mean bonjour");

let room = client.get_room(room_id).unwrap();

// make_edit_event works, even if the event cache hasn't been enabled.
room.make_edit_event(resp_event_id, EditedContent::RoomMessage(new_content)).await.unwrap();
}

0 comments on commit 5242f64

Please sign in to comment.