Skip to content

Commit dd14df0

Browse files
committed
refactor(ffi): simplify FFI layer and use only EmbeddedEventDetails there for both thread latest event + replied-to event item
1 parent c426138 commit dd14df0

File tree

3 files changed

+37
-54
lines changed

3 files changed

+37
-54
lines changed

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use matrix_sdk_ui::timeline::{
3636
TimelineUniqueId as SdkTimelineUniqueId,
3737
};
3838
use mime::Mime;
39-
use reply::{InReplyToDetails, RepliedToEventDetails};
39+
use reply::{EmbeddedEventDetails, InReplyToDetails};
4040
use ruma::{
4141
events::{
4242
location::{AssetType as RumaAssetType, LocationContent, ZoomLevel},
@@ -689,7 +689,7 @@ impl Timeline {
689689
match replied_to {
690690
Ok(Some(replied_to)) => Ok(Arc::new(InReplyToDetails::new(
691691
event_id_str,
692-
RepliedToEventDetails::Ready {
692+
EmbeddedEventDetails::Ready {
693693
content: replied_to.content().clone().into(),
694694
sender: replied_to.sender().to_string(),
695695
sender_profile: replied_to.sender_profile().into(),
@@ -698,12 +698,12 @@ impl Timeline {
698698

699699
Ok(None) => Ok(Arc::new(InReplyToDetails::new(
700700
event_id_str,
701-
RepliedToEventDetails::Error { message: "unsupported event".to_owned() },
701+
EmbeddedEventDetails::Error { message: "unsupported event".to_owned() },
702702
))),
703703

704704
Err(e) => Ok(Arc::new(InReplyToDetails::new(
705705
event_id_str,
706-
RepliedToEventDetails::Error { message: e.to_string() },
706+
EmbeddedEventDetails::Error { message: e.to_string() },
707707
))),
708708
}
709709
}

bindings/matrix-sdk-ffi/src/timeline/msg_like.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
use std::{collections::HashMap, sync::Arc};
1616

1717
use matrix_sdk::crypto::types::events::UtdCause;
18-
use matrix_sdk_ui::timeline::TimelineDetails;
1918
use ruma::events::{room::MediaSource as RumaMediaSource, EventContent};
2019

2120
use super::{
22-
content::{Reaction, TimelineItemContent},
23-
reply::InReplyToDetails,
21+
content::Reaction,
22+
reply::{EmbeddedEventDetails, InReplyToDetails},
2423
};
2524
use crate::{
2625
error::ClientError,
2726
ruma::{ImageInfo, MediaSource, MediaSourceExt, Mentions, MessageType, PollKind},
28-
timeline::{content::ReactionSenderData, ProfileDetails},
27+
timeline::content::ReactionSenderData,
2928
utils::Timestamp,
3029
};
3130

@@ -241,42 +240,22 @@ pub struct PollAnswer {
241240

242241
#[derive(Clone, uniffi::Object)]
243242
pub struct ThreadSummary {
244-
pub latest_event: ThreadSummaryLatestEventDetails,
243+
pub latest_event: EmbeddedEventDetails,
245244
pub num_replies: usize,
246245
}
247246

248247
#[matrix_sdk_ffi_macros::export]
249248
impl ThreadSummary {
250-
pub fn latest_event(&self) -> ThreadSummaryLatestEventDetails {
249+
pub fn latest_event(&self) -> EmbeddedEventDetails {
251250
self.latest_event.clone()
252251
}
253252
}
254253

255-
#[derive(Clone, uniffi::Enum)]
256-
#[allow(clippy::large_enum_variant)]
257-
// TODO: unify with the FFI replied-to-event-details
258-
pub enum ThreadSummaryLatestEventDetails {
259-
Unavailable,
260-
Pending,
261-
Ready { sender: String, sender_profile: ProfileDetails, content: TimelineItemContent },
262-
Error { message: String },
263-
}
264-
265254
impl From<matrix_sdk_ui::timeline::ThreadSummary> for ThreadSummary {
266255
fn from(value: matrix_sdk_ui::timeline::ThreadSummary) -> Self {
267-
let latest_event = match value.latest_event {
268-
TimelineDetails::Unavailable => ThreadSummaryLatestEventDetails::Unavailable,
269-
TimelineDetails::Pending => ThreadSummaryLatestEventDetails::Pending,
270-
TimelineDetails::Ready(event) => ThreadSummaryLatestEventDetails::Ready {
271-
content: event.content.into(),
272-
sender: event.sender.to_string(),
273-
sender_profile: (&event.sender_profile).into(),
274-
},
275-
TimelineDetails::Error(message) => {
276-
ThreadSummaryLatestEventDetails::Error { message: message.to_string() }
277-
}
278-
};
279-
280-
Self { latest_event, num_replies: value.num_replies }
256+
Self {
257+
latest_event: EmbeddedEventDetails::from(value.latest_event),
258+
num_replies: value.num_replies,
259+
}
281260
}
282261
}

bindings/matrix-sdk-ffi/src/timeline/reply.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use matrix_sdk_ui::timeline::TimelineDetails;
15+
use matrix_sdk_ui::timeline::{EmbeddedEvent, TimelineDetails};
1616

1717
use super::{content::TimelineItemContent, ProfileDetails};
1818

1919
#[derive(Clone, uniffi::Object)]
2020
pub struct InReplyToDetails {
2121
event_id: String,
22-
event: RepliedToEventDetails,
22+
event: EmbeddedEventDetails,
2323
}
2424

2525
impl InReplyToDetails {
26-
pub(crate) fn new(event_id: String, event: RepliedToEventDetails) -> Self {
26+
pub(crate) fn new(event_id: String, event: EmbeddedEventDetails) -> Self {
2727
Self { event_id, event }
2828
}
2929
}
@@ -34,36 +34,40 @@ impl InReplyToDetails {
3434
self.event_id.clone()
3535
}
3636

37-
pub fn event(&self) -> RepliedToEventDetails {
37+
pub fn event(&self) -> EmbeddedEventDetails {
3838
self.event.clone()
3939
}
4040
}
4141

4242
impl From<matrix_sdk_ui::timeline::InReplyToDetails> for InReplyToDetails {
4343
fn from(inner: matrix_sdk_ui::timeline::InReplyToDetails) -> Self {
44-
let event_id = inner.event_id.to_string();
45-
let event = match &inner.event {
46-
TimelineDetails::Unavailable => RepliedToEventDetails::Unavailable,
47-
TimelineDetails::Pending => RepliedToEventDetails::Pending,
48-
TimelineDetails::Ready(event) => RepliedToEventDetails::Ready {
49-
content: event.content().clone().into(),
50-
sender: event.sender().to_string(),
51-
sender_profile: event.sender_profile().into(),
52-
},
53-
TimelineDetails::Error(err) => {
54-
RepliedToEventDetails::Error { message: err.to_string() }
55-
}
56-
};
57-
58-
Self { event_id, event }
44+
Self { event_id: inner.event_id.to_string(), event: inner.event.into() }
5945
}
6046
}
6147

6248
#[derive(Clone, uniffi::Enum)]
6349
#[allow(clippy::large_enum_variant)]
64-
pub enum RepliedToEventDetails {
50+
pub enum EmbeddedEventDetails {
6551
Unavailable,
6652
Pending,
6753
Ready { content: TimelineItemContent, sender: String, sender_profile: ProfileDetails },
6854
Error { message: String },
6955
}
56+
57+
impl From<TimelineDetails<Box<EmbeddedEvent>>> for EmbeddedEventDetails {
58+
fn from(event: TimelineDetails<Box<EmbeddedEvent>>) -> Self {
59+
match event {
60+
TimelineDetails::Unavailable => EmbeddedEventDetails::Unavailable,
61+
TimelineDetails::Pending => EmbeddedEventDetails::Pending,
62+
TimelineDetails::Ready(event) => {
63+
let sender_profile = event.sender_profile().into();
64+
EmbeddedEventDetails::Ready {
65+
content: event.content.into(),
66+
sender: event.sender.to_string(),
67+
sender_profile,
68+
}
69+
}
70+
TimelineDetails::Error(err) => EmbeddedEventDetails::Error { message: err.to_string() },
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)