Skip to content

Commit 65208a3

Browse files
author
dragonfly1033
committed
fixup! fixup! fixup! fixup! fixup! fixup! test(sdk): Add integration test for search
1 parent 4db3fdd commit 65208a3

File tree

8 files changed

+47
-103
lines changed

8 files changed

+47
-103
lines changed

crates/matrix-sdk-search/src/error.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! The event cache is an abstraction layer, sitting between the Rust SDK and a
16-
//! final client, that acts as a global observer of all the rooms, gathering and
17-
//! inferring some extra useful information about each room. In particular, this
18-
//! doesn't require subscribing to a specific room to get access to this
19-
//! information.
20-
//!
21-
//! It's intended to be fast, robust and easy to maintain, having learned from
22-
//! previous endeavours at implementing middle to high level features elsewhere
23-
//! in the SDK, notably in the UI's Timeline object.
24-
//!
25-
//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26-
//! details about the historical reasons that led us to start writing this.
27-
2815
use tantivy::{
2916
directory::error::OpenDirectoryError as TantivyOpenDirectoryError,
3017
query::QueryParserError as TantivyQueryParserError,

crates/matrix-sdk-search/src/index.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! The event cache is an abstraction layer, sitting between the Rust SDK and a
16-
//! final client, that acts as a global observer of all the rooms, gathering and
17-
//! inferring some extra useful information about each room. In particular, this
18-
//! doesn't require subscribing to a specific room to get access to this
19-
//! information.
20-
//!
21-
//! It's intended to be fast, robust and easy to maintain, having learned from
22-
//! previous endeavours at implementing middle to high level features elsewhere
23-
//! in the SDK, notably in the UI's Timeline object.
24-
//!
25-
//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26-
//! details about the historical reasons that led us to start writing this.
27-
2815
use std::{fmt, fs, path::Path, sync::Arc};
2916

30-
use ruma::{OwnedEventId, OwnedRoomId, RoomId, events::AnyMessageLikeEvent};
17+
use ruma::{OwnedEventId, OwnedRoomId, RoomId, events::AnySyncMessageLikeEvent};
3118
use tantivy::{
3219
Index, IndexReader, TantivyDocument,
3320
collector::TopDocs,
@@ -135,8 +122,8 @@ impl RoomIndex {
135122
RoomIndex::new_with(index, schema, room_id)
136123
}
137124

138-
/// Handle [`AnyMessageLikeEvent`]
139-
pub fn handle_event(&mut self, event: AnyMessageLikeEvent) -> Result<(), IndexError> {
125+
/// Handle [`AnySyncMessageLikeEvent`]
126+
pub fn handle_event(&mut self, event: AnySyncMessageLikeEvent) -> Result<(), IndexError> {
140127
match self.schema.handle_event(event)? {
141128
RoomIndexOperation::Add(document) => self.writer.add_document(document)?,
142129
};
@@ -222,7 +209,7 @@ mod tests {
222209
.event_id(event_id!("$event_id:localhost"))
223210
.room(room_id)
224211
.sender(user_id!("@user_id:localhost"))
225-
.into_any_message_like_event();
212+
.into_any_sync_message_like_event();
226213

227214
index.handle_event(event).expect("failed to add event: {res:?}");
228215
}
@@ -243,7 +230,7 @@ mod tests {
243230
.event_id(event_id_1)
244231
.room(room_id)
245232
.sender(user_id!("@user_id:localhost"))
246-
.into_any_message_like_event(),
233+
.into_any_sync_message_like_event(),
247234
)?;
248235

249236
index.handle_event(
@@ -252,7 +239,7 @@ mod tests {
252239
.event_id(event_id_2)
253240
.room(room_id)
254241
.sender(user_id!("@user_id:localhost"))
255-
.into_any_message_like_event(),
242+
.into_any_sync_message_like_event(),
256243
)?;
257244

258245
index.handle_event(
@@ -261,7 +248,7 @@ mod tests {
261248
.event_id(event_id_3)
262249
.room(room_id)
263250
.sender(user_id!("@user_id:localhost"))
264-
.into_any_message_like_event(),
251+
.into_any_sync_message_like_event(),
265252
)?;
266253

267254
index.commit_and_reload()?;

crates/matrix-sdk-search/src/schema.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! The event cache is an abstraction layer, sitting between the Rust SDK and a
16-
//! final client, that acts as a global observer of all the rooms, gathering and
17-
//! inferring some extra useful information about each room. In particular, this
18-
//! doesn't require subscribing to a specific room to get access to this
19-
//! information.
20-
//!
21-
//! It's intended to be fast, robust and easy to maintain, having learned from
22-
//! previous endeavours at implementing middle to high level features elsewhere
23-
//! in the SDK, notably in the UI's Timeline object.
24-
//!
25-
//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26-
//! details about the historical reasons that led us to start writing this.
27-
2815
use ruma::events::{
29-
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeEventContent, RedactContent,
30-
RedactedMessageLikeEventContent, room::message::MessageType,
16+
AnySyncMessageLikeEvent, MessageLikeEventContent, RedactContent,
17+
RedactedMessageLikeEventContent, SyncMessageLikeEvent, room::message::MessageType,
3118
};
3219
use tantivy::{
3320
DateTime, TantivyDocument, doc,
@@ -44,7 +31,10 @@ pub(crate) trait MatrixSearchIndexSchema {
4431
fn default_search_fields(&self) -> Vec<Field>;
4532
fn primary_key(&self) -> Field;
4633
fn as_tantivy_schema(&self) -> Schema;
47-
fn handle_event(&self, event: AnyMessageLikeEvent) -> Result<RoomIndexOperation, IndexError>;
34+
fn handle_event(
35+
&self,
36+
event: AnySyncMessageLikeEvent,
37+
) -> Result<RoomIndexOperation, IndexError>;
4838
}
4939

5040
#[derive(Debug, Clone)]
@@ -58,11 +48,12 @@ pub(crate) struct RoomMessageSchema {
5848
}
5949

6050
impl RoomMessageSchema {
61-
/// Given an [`AnyMessageLikeEvent`] and a function to convert the content
62-
/// into a String to be indexed, return a [`TantivyDocument`] to index.
51+
/// Given an [`AnySyncMessageLikeEvent`] and a function to convert the
52+
/// content into a String to be indexed, return a [`TantivyDocument`] to
53+
/// index.
6354
fn make_doc<C: MessageLikeEventContent + RedactContent, F>(
6455
&self,
65-
event: MessageLikeEvent<C>,
56+
event: SyncMessageLikeEvent<C>,
6657
get_body_from_content: F,
6758
) -> Result<TantivyDocument, IndexError>
6859
where
@@ -122,18 +113,21 @@ impl MatrixSearchIndexSchema for RoomMessageSchema {
122113
self.inner.clone()
123114
}
124115

125-
fn handle_event(&self, event: AnyMessageLikeEvent) -> Result<RoomIndexOperation, IndexError> {
116+
fn handle_event(
117+
&self,
118+
event: AnySyncMessageLikeEvent,
119+
) -> Result<RoomIndexOperation, IndexError> {
126120
match event {
127121
// m.room.message behaviour
128-
AnyMessageLikeEvent::RoomMessage(event) => self
122+
AnySyncMessageLikeEvent::RoomMessage(event) => self
129123
.make_doc(event, |content| match &content.msgtype {
130124
MessageType::Text(content) => Ok(content.body.clone()),
131125
_ => Err(IndexError::MessageTypeNotSupported),
132126
})
133127
.map(RoomIndexOperation::Add),
134128

135129
// new MSC-1767 m.message behaviour
136-
AnyMessageLikeEvent::Message(event) => self
130+
AnySyncMessageLikeEvent::Message(event) => self
137131
.make_doc(event, |content| {
138132
content.text.find_plain().ok_or(IndexError::EmptyMessage).map(|v| v.to_owned())
139133
})

crates/matrix-sdk-search/src/writer.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! The event cache is an abstraction layer, sitting between the Rust SDK and a
16-
//! final client, that acts as a global observer of all the rooms, gathering and
17-
//! inferring some extra useful information about each room. In particular, this
18-
//! doesn't require subscribing to a specific room to get access to this
19-
//! information.
20-
//!
21-
//! It's intended to be fast, robust and easy to maintain, having learned from
22-
//! previous endeavours at implementing middle to high level features elsewhere
23-
//! in the SDK, notably in the UI's Timeline object.
24-
//!
25-
//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26-
//! details about the historical reasons that led us to start writing this.
27-
2815
use tantivy::{IndexWriter, TantivyDocument, TantivyError};
2916

3017
use crate::{OpStamp, error::IndexError};

crates/matrix-sdk/src/client/search.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! The event cache is an abstraction layer, sitting between the Rust SDK and a
16-
//! final client, that acts as a global observer of all the rooms, gathering and
17-
//! inferring some extra useful information about each room. In particular, this
18-
//! doesn't require subscribing to a specific room to get access to this
19-
//! information.
20-
//!
21-
//! It's intended to be fast, robust and easy to maintain, having learned from
22-
//! previous endeavours at implementing middle to high level features elsewhere
23-
//! in the SDK, notably in the UI's Timeline object.
24-
//!
25-
//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26-
//! details about the historical reasons that led us to start writing this.
27-
2815
use std::{collections::hash_map::HashMap, path::PathBuf, sync::Arc};
2916

3017
use matrix_sdk_search::{error::IndexError, index::RoomIndex};
31-
use ruma::{events::AnyMessageLikeEvent, OwnedEventId, OwnedRoomId, RoomId};
18+
use ruma::{events::AnySyncMessageLikeEvent, OwnedEventId, OwnedRoomId, RoomId};
3219
use tokio::sync::{Mutex, MutexGuard};
3320
use tracing::{debug, error};
3421

@@ -85,14 +72,14 @@ impl SearchIndexGuard<'_> {
8572
Ok(index)
8673
}
8774

88-
/// Handle an [`AnyMessageLikeEvent`] in the [`RoomIndex`] of a given
75+
/// Handle an [`AnySyncMessageLikeEvent`] in the [`RoomIndex`] of a given
8976
/// [`RoomId`]
9077
///
9178
/// This which will add/remove/edit an event in the index based on the
9279
/// event type.
9380
pub(crate) fn handle_event(
9481
&mut self,
95-
event: AnyMessageLikeEvent,
82+
event: AnySyncMessageLikeEvent,
9683
room_id: &RoomId,
9784
) -> Result<(), IndexError> {
9885
if !self.index_map.contains_key(room_id) {

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use tokio::sync::{
4343
broadcast::{Receiver, Sender},
4444
mpsc, Notify, RwLock,
4545
};
46-
use tracing::{instrument, trace, warn};
46+
use tracing::{debug, instrument, trace, warn};
4747

4848
use super::{
4949
AutoShrinkChannelPayload, EventsOrigin, Result, RoomEventCacheGenericUpdate,
@@ -544,7 +544,7 @@ impl RoomEventCacheInner {
544544

545545
#[cfg(feature = "experimental-search")]
546546
let Some(room) = self.weak_room.get() else {
547-
warn!("Couldn't get room while handling timeline");
547+
debug!("Couldn't get room while handling timeline");
548548
return Ok(());
549549
};
550550

@@ -647,7 +647,7 @@ mod private {
647647
};
648648
use matrix_sdk_common::executor::spawn;
649649
#[cfg(feature = "experimental-search")]
650-
use ruma::events::AnyMessageLikeEvent;
650+
use ruma::events::{AnyMessageLikeEvent, AnySyncMessageLikeEvent};
651651
use ruma::{
652652
events::{
653653
relation::RelationType, room::redaction::SyncRoomRedactionEvent, AnySyncTimelineEvent,
@@ -1455,14 +1455,14 @@ mod private {
14551455
}
14561456

14571457
#[cfg(feature = "experimental-search")]
1458-
fn parse_timeline_event(&self, event: &TimelineEvent) -> Option<AnyMessageLikeEvent> {
1458+
fn parse_timeline_event(&self, event: &TimelineEvent) -> Option<AnySyncMessageLikeEvent> {
14591459
let maybe_try_event = match &event.kind {
1460-
TimelineEventKind::Decrypted(d) => Some(d.event.deserialize()),
1460+
TimelineEventKind::Decrypted(d) => {
1461+
Some(d.event.deserialize().map(AnyMessageLikeEvent::into))
1462+
}
14611463
TimelineEventKind::PlainText { event } => match event.deserialize() {
14621464
Ok(event_obj) => match event_obj {
1463-
AnySyncTimelineEvent::MessageLike(sync_event) => {
1464-
Some(Ok(sync_event.into_full_event(self.room.clone())))
1465-
}
1465+
AnySyncTimelineEvent::MessageLike(sync_event) => Some(Ok(sync_event)),
14661466
AnySyncTimelineEvent::State(_) => None,
14671467
},
14681468
Err(e) => Some(Err(e)),
@@ -1668,9 +1668,9 @@ mod private {
16681668

16691669
// It is a `m.room.redaction`! We can deserialize it entirely.
16701670

1671-
let Ok(AnySyncTimelineEvent::MessageLike(
1672-
ruma::events::AnySyncMessageLikeEvent::RoomRedaction(redaction),
1673-
)) = raw_event.deserialize()
1671+
let Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomRedaction(
1672+
redaction,
1673+
))) = raw_event.deserialize()
16741674
else {
16751675
return Ok(());
16761676
};

crates/matrix-sdk/src/room/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ use reply::Reply;
6464
#[cfg(feature = "unstable-msc4274")]
6565
use ruma::events::room::message::GalleryItemType;
6666
#[cfg(feature = "experimental-search")]
67-
use ruma::events::AnyMessageLikeEvent;
67+
use ruma::events::AnySyncMessageLikeEvent;
6868
#[cfg(feature = "e2e-encryption")]
6969
use ruma::events::{
70-
room::encrypted::OriginalSyncRoomEncryptedEvent, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
71-
SyncMessageLikeEvent,
70+
room::encrypted::OriginalSyncRoomEncryptedEvent, AnySyncTimelineEvent, SyncMessageLikeEvent,
7271
};
7372
use ruma::{
7473
api::client::{
@@ -3678,12 +3677,15 @@ impl Room {
36783677
opts.send(self, event_id).await
36793678
}
36803679

3681-
/// Handle an [`AnyMessageLikeEvent`] in this room's [`RoomIndex`].
3680+
/// Handle an [`AnySyncMessageLikeEvent`] in this room's [`RoomIndex`].
36823681
///
36833682
/// This which will add/remove/edit an event in the index based on the
36843683
/// event type.
36853684
#[cfg(feature = "experimental-search")]
3686-
pub(crate) async fn index_event(&self, event: AnyMessageLikeEvent) -> Result<(), IndexError> {
3685+
pub(crate) async fn index_event(
3686+
&self,
3687+
event: AnySyncMessageLikeEvent,
3688+
) -> Result<(), IndexError> {
36873689
self.client.search_index().lock().await.handle_event(event, self.room_id())
36883690
}
36893691

testing/matrix-sdk-test/src/event_factory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use ruma::{
2828
OwnedRoomId, OwnedTransactionId, OwnedUserId, OwnedVoipId, RoomId, RoomVersionId,
2929
TransactionId, UInt, UserId, VoipVersionId,
3030
events::{
31-
AnyMessageLikeEvent, AnyStateEvent, AnySyncMessageLikeEvent, AnySyncStateEvent,
32-
AnySyncTimelineEvent, AnyTimelineEvent, BundledMessageLikeRelations, False, Mentions,
31+
AnyStateEvent, AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent,
32+
AnyTimelineEvent, BundledMessageLikeRelations, False, Mentions,
3333
RedactedMessageLikeEventContent, RedactedStateEventContent, StateEventContent,
3434
StaticEventContent,
3535
beacon::BeaconEventContent,
@@ -321,7 +321,7 @@ where
321321
self.into_raw()
322322
}
323323

324-
pub fn into_any_message_like_event(self) -> AnyMessageLikeEvent {
324+
pub fn into_any_sync_message_like_event(self) -> AnySyncMessageLikeEvent {
325325
self.into_raw().deserialize().expect("expected message like event")
326326
}
327327

0 commit comments

Comments
 (0)