Skip to content

Commit 32e9626

Browse files
authored
feat(ffi): expose event types and content (#6387)
Signed-off-by: Philippe Bertin <pbertin@teladochealth.com>
1 parent e376670 commit 32e9626

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ All notable changes to this project will be documented in this file.
4141

4242
### Features
4343

44+
- Expose `event_type_raw` and `latest_content_raw()` on `EventTimelineItem`,
45+
allowing clients to access the raw event type string and content JSON for
46+
custom event handling without pattern-matching through nested enums.
47+
([#6387](https://github.com/matrix-org/matrix-rust-sdk/pull/6387))
4448
- Expose sync v2 API through FFI via `Client.sync_v2()` and
4549
`Client.sync_once_v2()`, enabling mobile clients to sync without
4650
requiring Sliding Sync support on the homeserver. `Client.sync_v2()`

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ pub struct EventTimelineItem {
10211021
is_own: bool,
10221022
is_editable: bool,
10231023
content: TimelineItemContent,
1024+
/// The raw Matrix event type string (e.g. `"m.room.message"`), or `None`
1025+
/// when the original type is not available (e.g. redacted events).
1026+
event_type_raw: Option<String>,
10241027
timestamp: Timestamp,
10251028
local_send_state: Option<EventSendState>,
10261029
local_created_at: Option<u64>,
@@ -1046,6 +1049,7 @@ impl From<matrix_sdk_ui::timeline::EventTimelineItem> for EventTimelineItem {
10461049
is_own: item.is_own(),
10471050
is_editable: item.is_editable(),
10481051
content: item.content().clone().into(),
1052+
event_type_raw: item.content().event_type_str(),
10491053
timestamp: item.timestamp().into(),
10501054
local_send_state: item.send_state().map(|s| s.into()),
10511055
local_created_at: item.local_created_at().map(|t| t.0.into()),
@@ -1323,6 +1327,16 @@ impl LazyTimelineItemProvider {
13231327
fn contains_only_emojis(&self) -> bool {
13241328
self.0.contains_only_emojis()
13251329
}
1330+
1331+
/// Returns the JSON string of the event's `content` field from the latest
1332+
/// version (including edits). Returns `None` for local echoes that haven't
1333+
/// been echoed back by the server yet.
1334+
fn latest_content_raw(&self) -> Option<String> {
1335+
let raw = self.0.latest_json()?;
1336+
let value: serde_json::Value = serde_json::from_str(raw.json().get()).ok()?;
1337+
let content = value.get("content")?;
1338+
serde_json::to_string(content).ok()
1339+
}
13261340
}
13271341

13281342
/// Mimic the [`UiLatestEventValue`] type.

crates/matrix-sdk-ui/src/timeline/event_item/content/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ pub enum TimelineItemContent {
122122
}
123123

124124
impl TimelineItemContent {
125+
/// Returns the raw Matrix event type string (e.g. `"m.room.message"`),
126+
/// or `None` when the original type is not available (e.g. redacted
127+
/// events).
128+
pub fn event_type_str(&self) -> Option<String> {
129+
match self {
130+
Self::MsgLike(msg) => Some(match &msg.kind {
131+
MsgLikeKind::Message(_) => MessageLikeEventType::RoomMessage.to_string(),
132+
MsgLikeKind::Sticker(_) => MessageLikeEventType::Sticker.to_string(),
133+
MsgLikeKind::Poll(_) => MessageLikeEventType::PollStart.to_string(),
134+
MsgLikeKind::Redacted => return None,
135+
MsgLikeKind::UnableToDecrypt(_) => MessageLikeEventType::RoomEncrypted.to_string(),
136+
MsgLikeKind::Other(other) => other.event_type().to_string(),
137+
MsgLikeKind::LiveLocation(_) => StateEventType::BeaconInfo.to_string(),
138+
}),
139+
Self::MembershipChange(_) | Self::ProfileChange(_) => {
140+
Some(StateEventType::RoomMember.to_string())
141+
}
142+
Self::OtherState(state) => Some(state.content().event_type().to_string()),
143+
Self::FailedToParseMessageLike { event_type, .. } => Some(event_type.to_string()),
144+
Self::FailedToParseState { event_type, .. } => Some(event_type.to_string()),
145+
Self::CallInvite => Some(MessageLikeEventType::CallInvite.to_string()),
146+
Self::RtcNotification => Some(MessageLikeEventType::RtcNotification.to_string()),
147+
}
148+
}
149+
125150
pub fn as_msglike(&self) -> Option<&MsgLikeContent> {
126151
as_variant!(self, TimelineItemContent::MsgLike)
127152
}

0 commit comments

Comments
 (0)