diff --git a/crates/matrix-sdk-ui/src/timeline/controller/aggregations.rs b/crates/matrix-sdk-ui/src/timeline/controller/aggregations.rs index 65cc1bfa40a..5b558353c42 100644 --- a/crates/matrix-sdk-ui/src/timeline/controller/aggregations.rs +++ b/crates/matrix-sdk-ui/src/timeline/controller/aggregations.rs @@ -39,7 +39,6 @@ use std::{borrow::Cow, collections::HashMap, sync::Arc}; -use as_variant::as_variant; use matrix_sdk::{check_validity_of_replacement_events, deserialized_responses::EncryptionInfo}; use ruma::{ MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedTransactionId, OwnedUserId, @@ -183,18 +182,16 @@ pub(crate) struct Aggregation { fn poll_state_from_item<'a>( event: &'a mut Cow<'_, EventTimelineItem>, ) -> Result<&'a mut PollState, AggregationError> { - if event.content().is_poll() { - // It was a poll! Now return the state as mutable. - let state = as_variant!( - event.to_mut().content_mut(), - TimelineItemContent::MsgLike(MsgLikeContent { kind: MsgLikeKind::Poll(s), ..}) => s - ) - .expect("it was a poll just above"); + let content = event.to_mut().content_mut(); + + if let TimelineItemContent::MsgLike(MsgLikeContent { kind: MsgLikeKind::Poll(state), .. }) = + content + { Ok(state) } else { Err(AggregationError::InvalidType { expected: "a poll".to_owned(), - actual: event.content().debug_string().to_owned(), + actual: content.debug_string().to_owned(), }) } } @@ -203,18 +200,18 @@ fn poll_state_from_item<'a>( fn live_location_state_from_item<'a>( event: &'a mut Cow<'_, EventTimelineItem>, ) -> Result<&'a mut LiveLocationState, AggregationError> { - if event.content().is_live_location() { - // It was a live location! Now return the state as mutable. - let state = event - .to_mut() - .content_mut() - .as_live_location_state_mut() - .expect("it was a live location just above"); + let content = event.to_mut().content_mut(); + + if let TimelineItemContent::MsgLike(MsgLikeContent { + kind: MsgLikeKind::LiveLocation(state), + .. + }) = content + { Ok(state) } else { Err(AggregationError::InvalidType { expected: "a live location".to_owned(), - actual: event.content().debug_string().to_owned(), + actual: content.debug_string().to_owned(), }) } } @@ -223,13 +220,16 @@ fn live_location_state_from_item<'a>( fn rtc_notification_declinations_from_item<'a>( event: &'a mut Cow<'_, EventTimelineItem>, ) -> Result<&'a mut Vec, AggregationError> { - let debug_string = event.content().debug_string().to_owned(); - event.to_mut().content_mut().as_rtc_notification_mut().ok_or_else(|| { - AggregationError::InvalidType { + let content = event.to_mut().content_mut(); + + if let TimelineItemContent::RtcNotification { declined_by, .. } = content { + Ok(declined_by) + } else { + Err(AggregationError::InvalidType { expected: "an rtc notification".to_owned(), - actual: debug_string, - } - }) + actual: content.debug_string().to_owned(), + }) + } } impl Aggregation { diff --git a/crates/matrix-sdk-ui/src/timeline/event_item/content/mod.rs b/crates/matrix-sdk-ui/src/timeline/event_item/content/mod.rs index c0e2e8126a6..c65d974bd3d 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_item/content/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_item/content/mod.rs @@ -223,38 +223,6 @@ impl TimelineItemContent { }) => state) } - /// If `self` is of the [`MsgLike`][Self::MsgLike] variant with a - /// [`LiveLocation`][MsgLikeKind::LiveLocation] kind, return the inner - /// [`LiveLocationState`] mutably. - pub(in crate::timeline) fn as_live_location_state_mut( - &mut self, - ) -> Option<&mut LiveLocationState> { - as_variant!(self, Self::MsgLike(MsgLikeContent { - kind: MsgLikeKind::LiveLocation(state), - .. - }) => state) - } - - pub(in crate::timeline) fn as_rtc_notification_mut(&mut self) -> Option<&mut Vec> { - if let TimelineItemContent::RtcNotification { declined_by, .. } = self { - Some(declined_by) - } else { - None - } - } - - /// Check whether this item's content is a - /// [`LiveLocation`][MsgLikeKind::LiveLocation]. - pub fn is_live_location(&self) -> bool { - matches!(self, Self::MsgLike(MsgLikeContent { kind: MsgLikeKind::LiveLocation(_), .. })) - } - - /// Check whether this item's content is an - /// [`RtcNotification`][Self::RtcNotification]. - pub fn is_rtc_notification(&self) -> bool { - matches!(self, Self::RtcNotification { .. }) - } - /// If `self` is of the [`MsgLike`][Self::MsgLike] variant, return the /// inner [`Message`]. pub fn as_message(&self) -> Option<&Message> { diff --git a/crates/matrix-sdk-ui/src/timeline/tests/shields.rs b/crates/matrix-sdk-ui/src/timeline/tests/shields.rs index 42c054e3439..25654aaa477 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/shields.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/shields.rs @@ -22,7 +22,8 @@ use ruma::{ use stream_assert::{assert_next_matches, assert_pending}; use crate::timeline::{ - EventSendState, TimelineEventShieldState, TimelineEventShieldStateCode, + EventSendState, MsgLikeContent, MsgLikeKind, TimelineEventShieldState, + TimelineEventShieldStateCode, TimelineItemContent, tests::{TestTimeline, TestTimelineBuilder}, }; @@ -156,7 +157,13 @@ async fn test_live_location_no_sent_in_clear_shield() { // No beacons yet → shield should be None. let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value); - assert!(item.content().is_live_location(), "timeline item should be a live location"); + assert!( + matches!( + item.content(), + TimelineItemContent::MsgLike(MsgLikeContent { kind: MsgLikeKind::LiveLocation(_), .. }) + ), + "timeline item should be a live location" + ); let shield = item.get_shield(false); assert_eq!(shield, TimelineEventShieldState::None); diff --git a/crates/matrix-sdk-ui/tests/integration/timeline/rtc.rs b/crates/matrix-sdk-ui/tests/integration/timeline/rtc.rs index 793b8ea256c..93ebb973f08 100644 --- a/crates/matrix-sdk-ui/tests/integration/timeline/rtc.rs +++ b/crates/matrix-sdk-ui/tests/integration/timeline/rtc.rs @@ -52,7 +52,7 @@ async fn test_decline_call() { assert_let!(VectorDiff::PushBack { value: message } = &timeline_updates[0]); let event_item = message.as_event().unwrap(); - assert!(event_item.content().is_rtc_notification()); + assert!(matches!(event_item.content(), TimelineItemContent::RtcNotification { .. })); assert_let!( TimelineItemContent::RtcNotification { call_intent: _, declined_by } = event_item.content() ); @@ -116,7 +116,7 @@ async fn test_multiple_decline_call() { assert_let!(VectorDiff::PushBack { value: message } = &timeline_updates[0]); let event_item = message.as_event().unwrap(); - assert!(event_item.content().is_rtc_notification()); + assert!(matches!(event_item.content(), TimelineItemContent::RtcNotification { .. })); assert_let!( TimelineItemContent::RtcNotification { call_intent: _, declined_by } = event_item.content() );