Skip to content

Commit 4e0dab9

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

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
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
@@ -762,6 +762,13 @@ macro_rules! event_cache_store_integration_tests {
762762
get_event_cache_store().await.unwrap().into_event_cache_store();
763763
event_cache_store.test_find_event().await;
764764
}
765+
766+
#[async_test]
767+
async fn test_save_event() {
768+
let event_cache_store =
769+
get_event_cache_store().await.unwrap().into_event_cache_store();
770+
event_cache_store.test_save_event().await;
771+
}
765772
}
766773
};
767774
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ use matrix_sdk_base::{
3131
timer,
3232
};
3333
use ruma::{events::relation::RelationType, EventId, MxcUri, OwnedEventId, RoomId};
34-
use tracing::{instrument, trace};
34+
use tracing::{error, instrument, trace};
3535
use web_sys::IdbTransactionMode;
3636

3737
use crate::event_cache_store::{
3838
migrations::current::keys,
3939
serializer::IndexeddbEventCacheStoreSerializer,
4040
transaction::{IndexeddbEventCacheStoreTransaction, IndexeddbEventCacheStoreTransactionError},
41-
types::{ChunkType, InBandEvent},
41+
types::{ChunkType, InBandEvent, OutOfBandEvent},
4242
};
4343

4444
mod builder;
@@ -493,10 +493,20 @@ impl_event_cache_store! {
493493
event: Event,
494494
) -> Result<(), IndexeddbEventCacheStoreError> {
495495
let _timer = timer!("method");
496-
self.memory_store
497-
.save_event(room_id, event)
498-
.await
499-
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
496+
497+
let Some(event_id) = event.event_id() else {
498+
error!(%room_id, "Trying to save an event with no ID");
499+
return Ok(());
500+
};
501+
let transaction =
502+
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readwrite)?;
503+
let event = match transaction.get_event_by_id(room_id, &event_id).await? {
504+
Some(mut inner) => inner.with_content(event),
505+
None => types::Event::OutOfBand(OutOfBandEvent { content: event, position: () }),
506+
};
507+
transaction.put_event(room_id, &event).await?;
508+
transaction.commit().await?;
509+
Ok(())
500510
}
501511

502512
#[instrument(skip_all)]

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ impl Event {
9494
Event::OutOfBand(e) => e.relation(),
9595
}
9696
}
97+
98+
/// Sets the content of the underlying [`GenericEvent`] and returns
99+
/// the mutated [`Event`]
100+
pub fn with_content(mut self, content: TimelineEvent) -> Self {
101+
match self {
102+
Event::InBand(ref mut i) => i.content = content,
103+
Event::OutOfBand(ref mut o) => o.content = content,
104+
}
105+
self
106+
}
97107
}
98108

99109
/// A generic representation of an

0 commit comments

Comments
 (0)