mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2025-12-24 00:01:03 -05:00
feat(base): LatestEventValue::LocalHasBeenSent gains an event_id field.
This patch adds the `event_id: OwnedEventId` field to `LatestEventValue::LocalHasBeenSent`.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! The Latest Event basic types.
|
||||
|
||||
use matrix_sdk_common::deserialized_responses::TimelineEvent;
|
||||
use ruma::MilliSecondsSinceUnixEpoch;
|
||||
use ruma::{MilliSecondsSinceUnixEpoch, OwnedEventId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::store::SerializableEventContent;
|
||||
@@ -21,7 +21,13 @@ pub enum LatestEventValue {
|
||||
|
||||
/// The latest event represents a local event that has been sent
|
||||
/// successfully. It should come quickly as a [`Self::Remote`].
|
||||
LocalHasBeenSent(LocalLatestEventValue),
|
||||
LocalHasBeenSent {
|
||||
/// ID of the sent event.
|
||||
event_id: OwnedEventId,
|
||||
|
||||
/// Value, as for other [`Self::Local*`] variants.
|
||||
value: LocalLatestEventValue,
|
||||
},
|
||||
|
||||
/// The latest event represents a local event that cannot be sent, either
|
||||
/// because a previous local event, or this local event cannot be sent.
|
||||
@@ -46,7 +52,7 @@ impl LatestEventValue {
|
||||
Self::None => None,
|
||||
Self::Remote(remote_latest_event_value) => remote_latest_event_value.timestamp(),
|
||||
Self::LocalIsSending(LocalLatestEventValue { timestamp, .. })
|
||||
| Self::LocalHasBeenSent(LocalLatestEventValue { timestamp, .. })
|
||||
| Self::LocalHasBeenSent { value: LocalLatestEventValue { timestamp, .. }, .. }
|
||||
| Self::LocalCannotBeSent(LocalLatestEventValue { timestamp, .. }) => Some(*timestamp),
|
||||
}
|
||||
}
|
||||
@@ -58,9 +64,9 @@ impl LatestEventValue {
|
||||
/// [`LocalCannotBeSent`]: LatestEventValue::LocalCannotBeSent
|
||||
pub fn is_local(&self) -> bool {
|
||||
match self {
|
||||
Self::LocalIsSending(_) | Self::LocalHasBeenSent(_) | Self::LocalCannotBeSent(_) => {
|
||||
true
|
||||
}
|
||||
Self::LocalIsSending(_)
|
||||
| Self::LocalHasBeenSent { .. }
|
||||
| Self::LocalCannotBeSent(_) => true,
|
||||
Self::None | Self::Remote(_) => false,
|
||||
}
|
||||
}
|
||||
@@ -73,7 +79,7 @@ impl LatestEventValue {
|
||||
pub fn is_unsent(&self) -> bool {
|
||||
match self {
|
||||
Self::LocalIsSending(_) | Self::LocalCannotBeSent(_) => true,
|
||||
Self::LocalHasBeenSent(_) | Self::Remote(_) | Self::None => false,
|
||||
Self::LocalHasBeenSent { .. } | Self::Remote(_) | Self::None => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +110,7 @@ mod tests_latest_event_value {
|
||||
use ruma::{
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
events::{AnyMessageLikeEventContent, room::message::RoomMessageEventContent},
|
||||
owned_event_id,
|
||||
serde::Raw,
|
||||
uint,
|
||||
};
|
||||
@@ -154,13 +161,16 @@ mod tests_latest_event_value {
|
||||
|
||||
#[test]
|
||||
fn test_timestamp_with_local_has_been_sent() {
|
||||
let value = LatestEventValue::LocalHasBeenSent(LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
});
|
||||
let value = LatestEventValue::LocalHasBeenSent {
|
||||
event_id: owned_event_id!("$ev0"),
|
||||
value: LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
},
|
||||
};
|
||||
|
||||
assert_eq!(value.timestamp(), Some(MilliSecondsSinceUnixEpoch(uint!(42))));
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ mod tests {
|
||||
use ruma::{
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
events::{AnyMessageLikeEventContent, room::message::RoomMessageEventContent},
|
||||
room_id,
|
||||
owned_event_id, room_id,
|
||||
serde::Raw,
|
||||
uint,
|
||||
};
|
||||
@@ -114,13 +114,16 @@ mod tests {
|
||||
}
|
||||
|
||||
fn local_has_been_sent() -> LatestEventValue {
|
||||
LatestEventValue::LocalHasBeenSent(LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
})
|
||||
LatestEventValue::LocalHasBeenSent {
|
||||
event_id: owned_event_id!("$ev0"),
|
||||
value: LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn local_cannot_be_sent() -> LatestEventValue {
|
||||
|
||||
@@ -161,7 +161,7 @@ impl LatestEventValue {
|
||||
}
|
||||
}
|
||||
BaseLatestEventValue::LocalIsSending(ref local_value)
|
||||
| BaseLatestEventValue::LocalHasBeenSent(ref local_value)
|
||||
| BaseLatestEventValue::LocalHasBeenSent { value: ref local_value, .. }
|
||||
| BaseLatestEventValue::LocalCannotBeSent(ref local_value) => {
|
||||
let LocalLatestEventValue { timestamp, content: serialized_content } = local_value;
|
||||
|
||||
@@ -187,7 +187,7 @@ impl LatestEventValue {
|
||||
BaseLatestEventValue::LocalIsSending(_) => {
|
||||
LatestEventValueLocalState::IsSending
|
||||
}
|
||||
BaseLatestEventValue::LocalHasBeenSent(_) => {
|
||||
BaseLatestEventValue::LocalHasBeenSent { .. } => {
|
||||
LatestEventValueLocalState::HasBeenSent
|
||||
}
|
||||
BaseLatestEventValue::LocalCannotBeSent(_) => {
|
||||
@@ -313,13 +313,16 @@ mod tests {
|
||||
let client = server.client_builder().build().await;
|
||||
let room = server.sync_room(&client, JoinedRoomBuilder::new(room_id!("!r0"))).await;
|
||||
|
||||
let base_value = BaseLatestEventValue::LocalHasBeenSent(LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
});
|
||||
let base_value = BaseLatestEventValue::LocalHasBeenSent {
|
||||
event_id: event_id!("$ev0").to_owned(),
|
||||
value: LocalLatestEventValue {
|
||||
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
|
||||
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
|
||||
RoomMessageEventContent::text_plain("raclette"),
|
||||
))
|
||||
.unwrap(),
|
||||
},
|
||||
};
|
||||
let value =
|
||||
LatestEventValue::from_base_latest_event_value(base_value, &room, &client).await;
|
||||
|
||||
|
||||
@@ -532,7 +532,7 @@ mod tests_latest_event {
|
||||
|
||||
assert_matches!(
|
||||
latest_event.current_value.get().await,
|
||||
LatestEventValue::LocalHasBeenSent(_)
|
||||
LatestEventValue::LocalHasBeenSent { .. }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -760,7 +760,7 @@ impl LatestEventValueBuilder {
|
||||
// “is sending”. Then, remove the calculated `LatestEventValue` from the buffer of
|
||||
// values. Finally, return the last `LatestEventValue` or calculate a new
|
||||
// one.
|
||||
RoomSendQueueUpdate::SentEvent { transaction_id, .. } => {
|
||||
RoomSendQueueUpdate::SentEvent { transaction_id, event_id } => {
|
||||
if let Some(position) =
|
||||
buffer_of_values_for_local_events.mark_is_sending_after(transaction_id)
|
||||
{
|
||||
@@ -774,8 +774,8 @@ impl LatestEventValueBuilder {
|
||||
LatestEventValue::LocalIsSending(local_value)
|
||||
| LatestEventValue::LocalCannotBeSent(local_value)
|
||||
// Technically impossible, but it's not harmful to handle this that way.
|
||||
| LatestEventValue::LocalHasBeenSent(local_value ) => {
|
||||
return Some(LatestEventValue::LocalHasBeenSent(local_value));
|
||||
| LatestEventValue::LocalHasBeenSent { value: local_value, .. } => {
|
||||
return Some(LatestEventValue::LocalHasBeenSent { event_id: event_id.clone(), value: local_value });
|
||||
}
|
||||
LatestEventValue::Remote(_) | LatestEventValue::None => unreachable!("Impossible to get a remote `LatestEventValue`"),
|
||||
}
|
||||
@@ -1566,6 +1566,7 @@ mod tests_latest_event_values_for_local_events {
|
||||
use ruma::{
|
||||
MilliSecondsSinceUnixEpoch, OwnedTransactionId,
|
||||
events::{AnyMessageLikeEventContent, room::message::RoomMessageEventContent},
|
||||
owned_event_id,
|
||||
serde::Raw,
|
||||
};
|
||||
use serde_json::json;
|
||||
@@ -1673,7 +1674,10 @@ mod tests_latest_event_values_for_local_events {
|
||||
);
|
||||
buffer.push(
|
||||
OwnedTransactionId::from("txnid1"),
|
||||
LatestEventValue::LocalHasBeenSent(local_room_message("raclette")),
|
||||
LatestEventValue::LocalHasBeenSent {
|
||||
event_id: owned_event_id!("$ev0"),
|
||||
value: local_room_message("raclette"),
|
||||
},
|
||||
);
|
||||
|
||||
// no panic.
|
||||
@@ -1701,7 +1705,10 @@ mod tests_latest_event_values_for_local_events {
|
||||
|
||||
buffer.push(
|
||||
OwnedTransactionId::from("txnid"),
|
||||
LatestEventValue::LocalHasBeenSent(local_room_message("gruyère")),
|
||||
LatestEventValue::LocalHasBeenSent {
|
||||
event_id: owned_event_id!("$ev0"),
|
||||
value: local_room_message("gruyère"),
|
||||
},
|
||||
);
|
||||
|
||||
let LocalLatestEventValue { content: new_content, .. } = local_room_message("comté");
|
||||
@@ -1935,6 +1942,28 @@ mod tests_latest_event_value_builder {
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
( $latest_event_value:expr, $pattern:path {
|
||||
$local_value:ident with body = $body:expr
|
||||
$( , $field:ident => $more:block )*
|
||||
} ) => {
|
||||
assert_matches!(
|
||||
$latest_event_value,
|
||||
Some( $pattern { $local_value, $( $field, )* .. }) => {
|
||||
assert_matches!(
|
||||
$local_value .content.deserialize().unwrap(),
|
||||
AnyMessageLikeEventContent::RoomMessage(message_content) => {
|
||||
assert_eq!(message_content.body(), $body);
|
||||
|
||||
$({
|
||||
let $field = $field;
|
||||
$more
|
||||
})*
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
@@ -2347,15 +2376,21 @@ mod tests_latest_event_value_builder {
|
||||
// hasn't changed, this is still this event, but the status has changed to
|
||||
// `LocalHasBeenSent`.
|
||||
{
|
||||
let expected_event_id = event_id!("$ev1").to_owned();
|
||||
let update = RoomSendQueueUpdate::SentEvent {
|
||||
transaction_id: transaction_id_1,
|
||||
event_id: event_id!("$ev1").to_owned(),
|
||||
event_id: expected_event_id.clone(),
|
||||
};
|
||||
|
||||
// The `LatestEventValue` hasn't changed.
|
||||
assert_local_value_matches_room_message_with_body!(
|
||||
LatestEventValueBuilder::new_local(&update, &mut buffer, &room_event_cache, user_id, None).await,
|
||||
LatestEventValue::LocalHasBeenSent => with body = "B"
|
||||
LatestEventValue::LocalHasBeenSent {
|
||||
value with body = "B",
|
||||
event_id => {
|
||||
assert_eq!(event_id, expected_event_id);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert!(buffer.buffer.is_empty());
|
||||
|
||||
Reference in New Issue
Block a user