Skip to content

Commit ada68e1

Browse files
mgoldenbergbnjbvr
authored andcommitted
feat(indexeddb): add IndexedDB-backed impl for EventCacheStore::find_event_relations
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent c9137f0 commit ada68e1

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/integration_tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ macro_rules! event_cache_store_integration_tests {
763763
event_cache_store.test_find_event().await;
764764
}
765765

766+
#[async_test]
767+
async fn test_find_event_relations() {
768+
let event_cache_store =
769+
get_event_cache_store().await.unwrap().into_event_cache_store();
770+
event_cache_store.test_find_event_relations().await;
771+
}
772+
766773
#[async_test]
767774
async fn test_save_event() {
768775
let event_cache_store =

crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,32 @@ impl_event_cache_store! {
480480
filters: Option<&[RelationType]>,
481481
) -> Result<Vec<(Event, Option<Position>)>, IndexeddbEventCacheStoreError> {
482482
let _timer = timer!("method");
483-
self.memory_store
484-
.find_event_relations(room_id, event_id, filters)
485-
.await
486-
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
483+
484+
let transaction =
485+
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readonly)?;
486+
487+
let mut related_events = Vec::new();
488+
match filters {
489+
Some(relation_types) if !relation_types.is_empty() => {
490+
for relation_type in relation_types {
491+
let relation = (event_id.to_owned(), relation_type.clone());
492+
let events = transaction.get_events_by_relation(room_id, &relation).await?;
493+
for event in events {
494+
let position = event.position().map(Into::into);
495+
related_events.push((event.into(), position));
496+
}
497+
}
498+
}
499+
_ => {
500+
for event in
501+
transaction.get_events_by_related_event(room_id, &event_id.to_owned()).await?
502+
{
503+
let position = event.position().map(Into::into);
504+
related_events.push((event.into(), position));
505+
}
506+
}
507+
}
508+
Ok(related_events)
487509
}
488510

489511
#[instrument(skip(self, event))]

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,23 @@ pub type IndexedEventPositionIndex = usize;
417417
#[derive(Debug, Serialize, Deserialize)]
418418
pub struct IndexedEventRelationKey(IndexedRoomId, IndexedEventId, IndexedRelationType);
419419

420+
impl IndexedEventRelationKey {
421+
/// Returns an identical key, but with the related event field updated to
422+
/// the given related event. This is helpful when searching for all
423+
/// events which are related to the given event.
424+
pub fn with_related_event_id(
425+
&self,
426+
related_event_id: &OwnedEventId,
427+
serializer: &IndexeddbSerializer,
428+
) -> Self {
429+
let room_id = self.0.clone();
430+
let related_event_id =
431+
serializer.encode_key_as_string(keys::EVENTS_RELATION_RELATED_EVENTS, related_event_id);
432+
let relation_type = self.2.clone();
433+
Self(room_id, related_event_id, relation_type)
434+
}
435+
}
436+
420437
impl IndexedKey<Event> for IndexedEventRelationKey {
421438
const INDEX: Option<&'static str> = Some(keys::EVENTS_RELATION);
422439

crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,32 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
631631
self.get_events_count_by_position(room_id, range).await
632632
}
633633

634+
/// Query IndexedDB for events that match the given relation range in the
635+
/// given room.
636+
pub async fn get_events_by_relation(
637+
&self,
638+
room_id: &RoomId,
639+
range: impl Into<IndexedKeyRange<&(OwnedEventId, RelationType)>>,
640+
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
641+
let range = range.into().encoded(room_id, self.serializer.inner());
642+
self.get_items_by_key::<Event, IndexedEventRelationKey>(room_id, range).await
643+
}
644+
645+
/// Query IndexedDB for events that are related to the given event in the
646+
/// given room.
647+
pub async fn get_events_by_related_event(
648+
&self,
649+
room_id: &RoomId,
650+
related_event_id: &OwnedEventId,
651+
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
652+
let lower = IndexedEventRelationKey::lower_key(room_id, self.serializer.inner())
653+
.with_related_event_id(related_event_id, self.serializer.inner());
654+
let upper = IndexedEventRelationKey::upper_key(room_id, self.serializer.inner())
655+
.with_related_event_id(related_event_id, self.serializer.inner());
656+
let range = IndexedKeyRange::Bound(lower, upper);
657+
self.get_items_by_key::<Event, IndexedEventRelationKey>(room_id, range).await
658+
}
659+
634660
/// Puts an event in the given room. If an event with the same key already
635661
/// exists, it will be overwritten.
636662
pub async fn put_event(

0 commit comments

Comments
 (0)