Skip to content

Commit

Permalink
fixup! timeline: use a TimelineItemId to react to a timeline item
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Oct 15, 2024
1 parent 43cd680 commit 0b1bb10
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
12 changes: 0 additions & 12 deletions crates/matrix-sdk-ui/src/timeline/event_item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,6 @@ pub enum TimelineEventItemId {
EventId(OwnedEventId),
}

impl From<OwnedEventId> for TimelineEventItemId {
fn from(value: OwnedEventId) -> Self {
Self::EventId(value)
}
}

impl From<OwnedTransactionId> for TimelineEventItemId {
fn from(value: OwnedTransactionId) -> Self {
Self::TransactionId(value)
}
}

/// An handle that usually allows to perform an action on a timeline event.
///
/// If the item represents a remote item, then the event id is usually
Expand Down
27 changes: 16 additions & 11 deletions crates/matrix-sdk-ui/src/timeline/tests/reactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tokio::time::timeout;

use crate::timeline::{
controller::TimelineEnd, event_item::RemoteEventOrigin, tests::TestTimeline, ReactionStatus,
TimelineItem,
TimelineEventItemId, TimelineItem,
};

const REACTION_KEY: &str = "👍";
Expand Down Expand Up @@ -84,7 +84,10 @@ async fn test_add_reaction_on_non_existent_event() {
let mut stream = timeline.subscribe().await;

let event_id = EventId::parse("$nonexisting_unique_id").unwrap();
timeline.toggle_reaction_local(&event_id.into(), REACTION_KEY).await.unwrap_err();
timeline
.toggle_reaction_local(&TimelineEventItemId::EventId(event_id), REACTION_KEY)
.await
.unwrap_err();

assert!(stream.next().now_or_never().is_none());
}
Expand All @@ -93,10 +96,10 @@ async fn test_add_reaction_on_non_existent_event() {
async fn test_add_reaction_success() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;
let (event_id, item_pos) = send_first_message(&timeline, &mut stream).await;
let (item_id, event_id, item_pos) = send_first_message(&timeline, &mut stream).await;

// If I toggle a reaction on an event which didn't have any…
timeline.toggle_reaction_local(&event_id.clone().into(), REACTION_KEY).await.unwrap();
timeline.toggle_reaction_local(&item_id, REACTION_KEY).await.unwrap();

// The timeline item is updated, with a local echo for the reaction.
assert_reaction_is_updated!(stream, &event_id, item_pos, false);
Expand Down Expand Up @@ -124,7 +127,7 @@ async fn test_redact_reaction_success() {
let f = &timeline.factory;

let mut stream = timeline.subscribe().await;
let (event_id, item_pos) = send_first_message(&timeline, &mut stream).await;
let (item_id, event_id, item_pos) = send_first_message(&timeline, &mut stream).await;

// A reaction is added by sync.
let reaction_id = event_id!("$reaction_id");
Expand All @@ -136,7 +139,7 @@ async fn test_redact_reaction_success() {
assert_reaction_is_updated!(stream, &event_id, item_pos, true);

// Toggling the reaction locally…
timeline.toggle_reaction_local(&event_id.clone().into(), REACTION_KEY).await.unwrap();
timeline.toggle_reaction_local(&item_id, REACTION_KEY).await.unwrap();

// Will immediately redact it on the item.
let event = assert_item_update!(stream, &event_id, item_pos);
Expand Down Expand Up @@ -167,12 +170,12 @@ async fn test_redact_reaction_success() {
async fn test_reactions_store_timestamp() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;
let (event_id, msg_pos) = send_first_message(&timeline, &mut stream).await;
let (item_id, event_id, msg_pos) = send_first_message(&timeline, &mut stream).await;

// Creating a reaction adds a valid timestamp.
let timestamp_before = MilliSecondsSinceUnixEpoch::now();

timeline.toggle_reaction_local(&event_id.clone().into(), REACTION_KEY).await.unwrap();
timeline.toggle_reaction_local(&item_id, REACTION_KEY).await.unwrap();

let event = assert_reaction_is_updated!(stream, &event_id, msg_pos, false);
let reactions = event.reactions().get(&REACTION_KEY.to_owned()).unwrap();
Expand Down Expand Up @@ -217,15 +220,17 @@ async fn test_initial_reaction_timestamp_is_stored() {
async fn send_first_message(
timeline: &TestTimeline,
stream: &mut (impl Stream<Item = VectorDiff<Arc<TimelineItem>>> + Unpin),
) -> (OwnedEventId, usize) {
) -> (TimelineEventItemId, OwnedEventId, usize) {
timeline.handle_live_event(timeline.factory.text_msg("I want you to react").sender(&BOB)).await;

let item = assert_next_matches!(*stream, VectorDiff::PushBack { value } => value);
let event_id = item.as_event().unwrap().as_remote().unwrap().event_id.clone();
let event_item = item.as_event().unwrap();
let item_id = event_item.identifier();
let event_id = event_item.event_id().unwrap().to_owned();
let position = timeline.len().await - 1;

let day_divider = assert_next_matches!(*stream, VectorDiff::PushFront { value } => value);
assert!(day_divider.is_day_divider());

(event_id, position)
(item_id, event_id, position)
}
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ async fn test_focused_timeline_local_echoes() {
assert_pending!(timeline_stream);

// Add a reaction to the focused event, which will cause a local echo to happen.
timeline.toggle_reaction(&target_event.to_owned().into(), "✨").await.unwrap();
timeline.toggle_reaction(&event_item.identifier(), "✨").await.unwrap();

// We immediately get the local echo for the reaction.
let item = assert_next_matches_with_timeout!(timeline_stream, VectorDiff::Set { index: 1, value: item } => item);
Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-ui/tests/integration/timeline/reactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn test_abort_before_being_sent() {

assert_let!(Some(VectorDiff::PushBack { value: first }) = stream.next().await);
let item = first.as_event().unwrap();
let item_id = item.event_id().unwrap().to_owned().into();
let item_id = item.identifier();
assert_eq!(item.content().as_message().unwrap().body(), "hello");

assert_let!(Some(VectorDiff::PushFront { value: day_divider }) = stream.next().await);
Expand Down Expand Up @@ -214,11 +214,11 @@ async fn test_redact_failed() {
let _response = client.sync_once(Default::default()).await.unwrap();
server.reset().await;

let event_id = assert_next_matches_with_timeout!(stream, VectorDiff::PushBack { value: item } => {
let item_id = assert_next_matches_with_timeout!(stream, VectorDiff::PushBack { value: item } => {
let item = item.as_event().unwrap();
assert_eq!(item.content().as_message().unwrap().body(), "hello");
assert!(item.reactions().is_empty());
item.event_id().unwrap().to_owned()
item.identifier()
});

assert_next_matches_with_timeout!(stream, VectorDiff::Set { index: 0, value: item } => {
Expand All @@ -241,7 +241,7 @@ async fn test_redact_failed() {
.await;

// We toggle the reaction, which fails with an error.
timeline.toggle_reaction(&event_id.into(), "😆").await.unwrap_err();
timeline.toggle_reaction(&item_id, "😆").await.unwrap_err();

// The local echo is removed (assuming the redaction works)…
assert_next_matches_with_timeout!(stream, VectorDiff::Set { index: 1, value: item } => {
Expand Down Expand Up @@ -314,7 +314,7 @@ async fn test_local_reaction_to_local_echo() {
assert!(item.is_local_echo());
assert_eq!(item.content().as_message().unwrap().body(), "lol");
assert!(item.reactions().is_empty());
item.transaction_id().unwrap().to_owned().into()
item.identifier()
});

// Good ol' day divider.
Expand Down
14 changes: 7 additions & 7 deletions testing/matrix-sdk-integration-testing/src/tests/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,13 @@ async fn test_toggling_reaction() -> Result<()> {
diff.apply(&mut items);
}

let message_position = items
let (message_position, item_id) = items
.iter()
.enumerate()
.find_map(|(i, item)| (item.as_event()?.event_id()? == event_id).then_some(i))
.find_map(|(i, item)| {
let item = item.as_event()?;
(item.event_id()? == event_id).then_some((i, item.identifier()))
})
.expect("couldn't find the final position for the event id");

let reaction_key = "👍".to_owned();
Expand All @@ -161,10 +164,7 @@ async fn test_toggling_reaction() -> Result<()> {
debug!("Starting the toggle reaction tests…");

// Add the reaction.
timeline
.toggle_reaction(&event_id.clone().into(), &reaction_key)
.await
.expect("toggling reaction");
timeline.toggle_reaction(&item_id, &reaction_key).await.expect("toggling reaction");

// Local echo is added.
{
Expand Down Expand Up @@ -193,7 +193,7 @@ async fn test_toggling_reaction() -> Result<()> {

// Redact the reaction.
timeline
.toggle_reaction(&event_id.clone().into(), &reaction_key)
.toggle_reaction(&item_id, &reaction_key)
.await
.expect("toggling reaction the second time");

Expand Down

0 comments on commit 0b1bb10

Please sign in to comment.