refactor(stores): get rid of the temporary compute_filter_strings now that Ruma has been updated

This was a local fix for a bug in Ruma, that has been fixed upstream since then, so we can get rid of the workaround now.
This commit is contained in:
Benjamin Bouvier
2025-10-01 18:50:23 +02:00
committed by GitHub
parent 681b22142f
commit 37ee5d5075
3 changed files with 16 additions and 29 deletions

View File

@@ -28,9 +28,7 @@ use matrix_sdk_common::{
use ruma::{EventId, OwnedEventId, RoomId, events::relation::RelationType, time::Instant};
use tracing::error;
use super::{
EventCacheStore, EventCacheStoreError, Result, compute_filters_string, extract_event_relation,
};
use super::{EventCacheStore, EventCacheStoreError, Result, extract_event_relation};
use crate::event_cache::{Event, Gap};
/// In-memory, non-persistent implementation of the `EventCacheStore`.
@@ -194,14 +192,13 @@ impl EventCacheStore for MemoryStore {
) -> Result<Vec<(Event, Option<Position>)>, Self::Error> {
let inner = self.inner.read().unwrap();
let filters = compute_filters_string(filters);
let related_events = inner
.events
.items(room_id)
.filter_map(|(event, pos)| {
// Must have a relation.
let (related_to, rel_type) = extract_event_relation(event.raw())?;
let rel_type = RelationType::from(rel_type.as_str());
// Must relate to the target item.
if related_to != event_id {

View File

@@ -31,11 +31,7 @@ use matrix_sdk_common::cross_process_lock::{
CrossProcessLock, CrossProcessLockError, CrossProcessLockGuard, TryLock,
};
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
use ruma::{
OwnedEventId,
events::{AnySyncTimelineEvent, relation::RelationType},
serde::Raw,
};
use ruma::{OwnedEventId, events::AnySyncTimelineEvent, serde::Raw};
use tracing::trace;
#[cfg(any(test, feature = "testing"))]
@@ -225,18 +221,3 @@ pub fn extract_event_relation(event: &Raw<AnySyncTimelineEvent>) -> Option<(Owne
}
}
}
/// Compute the list of string filters to be applied when looking for an event's
/// relations.
// TODO: get Ruma fix from https://github.com/ruma/ruma/pull/2052, and get rid of this function
// then.
pub fn compute_filters_string(filters: Option<&[RelationType]>) -> Option<Vec<String>> {
filters.map(|filter| {
filter
.iter()
.map(|f| {
if *f == RelationType::Replacement { "m.replace".to_owned() } else { f.to_string() }
})
.collect()
})
}

View File

@@ -21,7 +21,7 @@ use deadpool_sqlite::{Object as SqliteAsyncConn, Pool as SqlitePool, Runtime};
use matrix_sdk_base::{
deserialized_responses::TimelineEvent,
event_cache::{
store::{compute_filters_string, extract_event_relation, EventCacheStore},
store::{extract_event_relation, EventCacheStore},
Event, Gap,
},
linked_chunk::{
@@ -1390,7 +1390,7 @@ fn find_event_relations_transaction(
Ok(related)
};
let related = if let Some(filters) = compute_filters_string(filters.as_deref()) {
let related = if let Some(filters) = filters {
let question_marks = repeat_vars(filters.len());
let query = format!(
"SELECT events.content, event_chunks.chunk_id, event_chunks.position
@@ -1399,7 +1399,16 @@ fn find_event_relations_transaction(
WHERE relates_to = ? AND room_id = ? AND rel_type IN ({question_marks})"
);
let filters: Vec<_> = filters.iter().map(|f| f.to_sql().unwrap()).collect();
// First the filters need to be stringified; because `.to_sql()` will borrow
// from them, they also need to be stringified onto the stack, so as to
// get a stable address (to avoid returning a temporary reference in the
// map closure below).
let filter_strings: Vec<_> = filters.iter().map(|f| f.to_string()).collect();
let filters_params: Vec<_> = filter_strings
.iter()
.map(|f| f.to_sql().expect("converting a string to SQL should work"))
.collect();
let parameters = params_from_iter(
[
hashed_linked_chunk_id.to_sql().expect(
@@ -1414,7 +1423,7 @@ fn find_event_relations_transaction(
.expect("We should be able to convert a room ID to a SQLite value"),
]
.into_iter()
.chain(filters),
.chain(filters_params),
);
let mut transaction = txn.prepare(&query)?;