Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependent events: followup #3760

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ matrix-sdk-crypto = { workspace = true, optional = true }
matrix-sdk-store-encryption = { workspace = true }
matrix-sdk-test = { workspace = true, optional = true }
once_cell = { workspace = true }
ruma = { workspace = true, features = ["canonical-json", "unstable-msc3381", "unstable-msc2867"] }
ruma = { workspace = true, features = ["canonical-json", "unstable-msc3381", "unstable-msc2867", "rand"] }
serde = { workspace = true, features = ["rc"] }
serde_json = { workspace = true }
tokio = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-base/src/store/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use super::{DependentQueuedEventKind, DynStateStore, ServerCapabilities};
use crate::{
deserialized_responses::MemberEvent,
media::{MediaFormat, MediaRequest, MediaThumbnailSettings},
store::{Result, SerializableEventContent, StateStoreExt},
store::{traits::ChildTransactionId, Result, SerializableEventContent, StateStoreExt},
RoomInfo, RoomMemberships, RoomState, StateChanges, StateStoreDataKey, StateStoreDataValue,
};

Expand Down Expand Up @@ -1498,7 +1498,7 @@ impl StateStoreIntegrationTests for DynStateStore {
assert!(self.list_dependent_send_queue_events(room_id).await.unwrap().is_empty());

// Save a redaction for that event.
let child_txn = TransactionId::new();
let child_txn = ChildTransactionId::new();
self.save_dependent_send_queue_event(
room_id,
&txn0,
Expand Down Expand Up @@ -1551,7 +1551,7 @@ impl StateStoreIntegrationTests for DynStateStore {
self.save_dependent_send_queue_event(
room_id,
&txn0,
TransactionId::new(),
ChildTransactionId::new(),
DependentQueuedEventKind::Redact,
)
.await
Expand All @@ -1561,7 +1561,7 @@ impl StateStoreIntegrationTests for DynStateStore {
self.save_dependent_send_queue_event(
room_id,
&txn1,
TransactionId::new(),
ChildTransactionId::new(),
DependentQueuedEventKind::Edit {
new_content: SerializableEventContent::new(
&RoomMessageEventContent::text_plain("edit").into(),
Expand Down
11 changes: 7 additions & 4 deletions crates/matrix-sdk-base/src/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ use ruma::{
use tracing::{debug, instrument, trace, warn};

use super::{
traits::{ComposerDraft, QueuedEvent, SerializableEventContent, ServerCapabilities},
traits::{
ChildTransactionId, ComposerDraft, QueuedEvent, SerializableEventContent,
ServerCapabilities,
},
DependentQueuedEvent, DependentQueuedEventKind, Result, RoomInfo, StateChanges, StateStore,
StoreError,
};
Expand Down Expand Up @@ -1004,7 +1007,7 @@ impl StateStore for MemoryStore {
&self,
room: &RoomId,
parent_transaction_id: &TransactionId,
own_transaction_id: OwnedTransactionId,
own_transaction_id: ChildTransactionId,
content: DependentQueuedEventKind,
) -> Result<(), Self::Error> {
self.dependent_send_queue_events.write().unwrap().entry(room.to_owned()).or_default().push(
Expand Down Expand Up @@ -1037,11 +1040,11 @@ impl StateStore for MemoryStore {
async fn remove_dependent_send_queue_event(
&self,
room: &RoomId,
txn_id: &TransactionId,
txn_id: &ChildTransactionId,
) -> Result<bool, Self::Error> {
let mut dependent_send_queue_events = self.dependent_send_queue_events.write().unwrap();
let dependents = dependent_send_queue_events.entry(room.to_owned()).or_default();
if let Some(pos) = dependents.iter().position(|item| item.own_transaction_id == txn_id) {
if let Some(pos) = dependents.iter().position(|item| item.own_transaction_id == *txn_id) {
dependents.remove(pos);
Ok(true)
} else {
Expand Down
7 changes: 4 additions & 3 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ pub use self::integration_tests::StateStoreIntegrationTests;
pub use self::{
memory_store::MemoryStore,
traits::{
ComposerDraft, ComposerDraftType, DependentQueuedEvent, DependentQueuedEventKind,
DynStateStore, IntoStateStore, QueuedEvent, SerializableEventContent, ServerCapabilities,
StateStore, StateStoreDataKey, StateStoreDataValue, StateStoreExt,
ChildTransactionId, ComposerDraft, ComposerDraftType, DependentQueuedEvent,
DependentQueuedEventKind, DynStateStore, IntoStateStore, QueuedEvent,
SerializableEventContent, ServerCapabilities, StateStore, StateStoreDataKey,
StateStoreDataValue, StateStoreExt,
},
};

Expand Down
50 changes: 45 additions & 5 deletions crates/matrix-sdk-base/src/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
borrow::Borrow,
collections::{BTreeMap, BTreeSet},
fmt,
ops::Deref,
sync::Arc,
};

Expand Down Expand Up @@ -460,7 +461,7 @@ pub trait StateStore: AsyncTraitDeps {
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: OwnedTransactionId,
own_txn_id: ChildTransactionId,
content: DependentQueuedEventKind,
) -> Result<(), Self::Error>;

Expand All @@ -481,7 +482,7 @@ pub trait StateStore: AsyncTraitDeps {
async fn remove_dependent_send_queue_event(
&self,
room: &RoomId,
own_txn_id: &TransactionId,
own_txn_id: &ChildTransactionId,
) -> Result<bool, Self::Error>;

/// List all the dependent send queue events.
Expand Down Expand Up @@ -769,7 +770,7 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: OwnedTransactionId,
own_txn_id: ChildTransactionId,
content: DependentQueuedEventKind,
) -> Result<(), Self::Error> {
self.0
Expand All @@ -793,7 +794,7 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
async fn remove_dependent_send_queue_event(
&self,
room_id: &RoomId,
own_txn_id: &TransactionId,
own_txn_id: &ChildTransactionId,
) -> Result<bool, Self::Error> {
self.0.remove_dependent_send_queue_event(room_id, own_txn_id).await.map_err(Into::into)
}
Expand Down Expand Up @@ -1254,6 +1255,45 @@ pub enum DependentQueuedEventKind {
Redact,
}

/// A transaction id identifying a [`DependentQueuedEvent`] rather than its
/// parent [`QueuedEvent`].
///
/// This thin wrapper adds some safety to some APIs, making it possible to
/// distinguish between the parent's `TransactionId` and the dependent event's
/// own `TransactionId`.
#[repr(transparent)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ChildTransactionId(OwnedTransactionId);

impl ChildTransactionId {
/// Returns a new [`ChildTransactionId`].
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(TransactionId::new())
}
}

impl Deref for ChildTransactionId {
type Target = TransactionId;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<String> for ChildTransactionId {
fn from(val: String) -> Self {
Self(val.into())
}
}

impl From<ChildTransactionId> for OwnedTransactionId {
fn from(val: ChildTransactionId) -> Self {
val.0
}
}

/// An event to be sent, depending on a [`QueuedEvent`] to be sent first.
///
/// Depending on whether the event has been sent or not, this will either update
Expand All @@ -1264,7 +1304,7 @@ pub struct DependentQueuedEvent {
/// Unique identifier for this dependent queued event.
///
/// Useful for deletion.
pub own_transaction_id: OwnedTransactionId,
pub own_transaction_id: ChildTransactionId,

/// The kind of user intent.
pub kind: DependentQueuedEventKind,
Expand Down
11 changes: 6 additions & 5 deletions crates/matrix-sdk-indexeddb/src/state_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ use matrix_sdk_base::{
deserialized_responses::RawAnySyncOrStrippedState,
media::{MediaRequest, UniqueKey},
store::{
ComposerDraft, DependentQueuedEvent, DependentQueuedEventKind, QueuedEvent,
SerializableEventContent, ServerCapabilities, StateChanges, StateStore, StoreError,
ChildTransactionId, ComposerDraft, DependentQueuedEvent, DependentQueuedEventKind,
QueuedEvent, SerializableEventContent, ServerCapabilities, StateChanges, StateStore,
StoreError,
},
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, RoomState, StateStoreDataKey,
StateStoreDataValue,
Expand Down Expand Up @@ -1576,7 +1577,7 @@ impl_state_store!({
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: OwnedTransactionId,
own_txn_id: ChildTransactionId,
content: DependentQueuedEventKind,
) -> Result<()> {
let encoded_key = self.encode_key(keys::DEPENDENT_SEND_QUEUE, room_id);
Expand Down Expand Up @@ -1655,7 +1656,7 @@ impl_state_store!({
async fn remove_dependent_send_queue_event(
&self,
room_id: &RoomId,
txn_id: &TransactionId,
txn_id: &ChildTransactionId,
) -> Result<bool> {
let encoded_key = self.encode_key(keys::DEPENDENT_SEND_QUEUE, room_id);

Expand All @@ -1670,7 +1671,7 @@ impl_state_store!({
// Reload the previous vector for this room.
if let Some(val) = obj.get(&encoded_key)?.await? {
let mut prev = self.deserialize_value::<Vec<DependentQueuedEvent>>(&val)?;
if let Some(pos) = prev.iter().position(|item| item.own_transaction_id == txn_id) {
if let Some(pos) = prev.iter().position(|item| item.own_transaction_id == *txn_id) {
prev.remove(pos);

if prev.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-sqlite/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use matrix_sdk_base::{
deserialized_responses::{RawAnySyncOrStrippedState, SyncOrStrippedState},
media::{MediaRequest, UniqueKey},
store::{
migration_helpers::RoomInfoV1, DependentQueuedEvent, DependentQueuedEventKind, QueuedEvent,
SerializableEventContent,
migration_helpers::RoomInfoV1, ChildTransactionId, DependentQueuedEvent,
DependentQueuedEventKind, QueuedEvent, SerializableEventContent,
},
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, RoomState, StateChanges, StateStore,
StateStoreDataKey, StateStoreDataValue,
Expand Down Expand Up @@ -1863,7 +1863,7 @@ impl StateStore for SqliteStateStore {
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: OwnedTransactionId,
own_txn_id: ChildTransactionId,
content: DependentQueuedEventKind,
) -> Result<()> {
let room_id = self.encode_key(keys::DEPENDENTS_SEND_QUEUE, room_id);
Expand Down Expand Up @@ -1913,7 +1913,7 @@ impl StateStore for SqliteStateStore {
async fn remove_dependent_send_queue_event(
&self,
room_id: &RoomId,
txn_id: &TransactionId,
txn_id: &ChildTransactionId,
) -> Result<bool> {
let room_id = self.encode_key(keys::DEPENDENTS_SEND_QUEUE, room_id);

Expand Down
Loading
Loading