mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-09 16:34:32 -04:00
feat(indexeddb): add IndexedDB-backed impl for EventCacheStore::save_event
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
committed by
Benjamin Bouvier
parent
e862ded147
commit
4e0dab959a
@@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user