feat(indexeddb): add IndexedDB-backed impl for EventCacheStore::save_event

Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
Michael Goldenberg
2025-07-15 12:26:36 -04:00
committed by Benjamin Bouvier
parent e862ded147
commit 4e0dab959a
3 changed files with 33 additions and 6 deletions

View File

@@ -762,6 +762,13 @@ macro_rules! event_cache_store_integration_tests {
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_find_event().await;
}
#[async_test]
async fn test_save_event() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_save_event().await;
}
}
};
}

View File

@@ -31,14 +31,14 @@ use matrix_sdk_base::{
timer,
};
use ruma::{events::relation::RelationType, EventId, MxcUri, OwnedEventId, RoomId};
use tracing::{instrument, trace};
use tracing::{error, instrument, trace};
use web_sys::IdbTransactionMode;
use crate::event_cache_store::{
migrations::current::keys,
serializer::IndexeddbEventCacheStoreSerializer,
transaction::{IndexeddbEventCacheStoreTransaction, IndexeddbEventCacheStoreTransactionError},
types::{ChunkType, InBandEvent},
types::{ChunkType, InBandEvent, OutOfBandEvent},
};
mod builder;
@@ -493,10 +493,20 @@ impl_event_cache_store! {
event: Event,
) -> Result<(), IndexeddbEventCacheStoreError> {
let _timer = timer!("method");
self.memory_store
.save_event(room_id, event)
.await
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
let Some(event_id) = event.event_id() else {
error!(%room_id, "Trying to save an event with no ID");
return Ok(());
};
let transaction =
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readwrite)?;
let event = match transaction.get_event_by_id(room_id, &event_id).await? {
Some(mut inner) => inner.with_content(event),
None => types::Event::OutOfBand(OutOfBandEvent { content: event, position: () }),
};
transaction.put_event(room_id, &event).await?;
transaction.commit().await?;
Ok(())
}
#[instrument(skip_all)]

View File

@@ -94,6 +94,16 @@ impl Event {
Event::OutOfBand(e) => e.relation(),
}
}
/// Sets the content of the underlying [`GenericEvent`] and returns
/// the mutated [`Event`]
pub fn with_content(mut self, content: TimelineEvent) -> Self {
match self {
Event::InBand(ref mut i) => i.content = content,
Event::OutOfBand(ref mut o) => o.content = content,
}
self
}
}
/// A generic representation of an