sdk: Add RemoteEventTimelineItem::latest_edit_json

… and rename raw to original_json to disambiguate.
This commit is contained in:
Jonas Platte
2023-04-05 09:13:04 +02:00
committed by Jonas Platte
parent 2c38c6c371
commit 3ac6b10daa
6 changed files with 53 additions and 25 deletions

View File

@@ -305,7 +305,7 @@ impl EventTimelineItem {
}
pub fn raw(&self) -> Option<String> {
self.0.raw().map(|r| r.json().get().to_owned())
self.0.original_json().map(|r| r.json().get().to_owned())
}
pub fn local_send_state(&self) -> Option<EventSendState> {

View File

@@ -381,14 +381,19 @@ impl<'a> TimelineEventHandler<'a> {
}
};
let content = TimelineItemContent::Message(Message {
let new_content = TimelineItemContent::Message(Message {
msgtype: replacement.new_content,
in_reply_to: msg.in_reply_to.clone(),
edited: true,
});
let edit_json = match &self.flow {
Flow::Local { .. } => None,
Flow::Remote { raw_event, .. } => Some(raw_event.clone()),
};
trace!("Applying edit");
Some(event_item.with_content(content))
Some(event_item.apply_edit(new_content, edit_json))
});
}

View File

@@ -169,18 +169,24 @@ impl EventTimelineItem {
///
/// Returns `None` if this event hasn't been echoed back by the server
/// yet.
pub fn raw(&self) -> Option<&Raw<AnySyncTimelineEvent>> {
pub fn original_json(&self) -> Option<&Raw<AnySyncTimelineEvent>> {
match self {
Self::Local(_local_event) => None,
Self::Remote(remote_event) => Some(remote_event.raw()),
Self::Remote(remote_event) => Some(remote_event.original_json()),
}
}
/// Clone the current event item, and update its `content`.
pub(super) fn with_content(&self, content: TimelineItemContent) -> Self {
/// Clone the current event item, and apply an edit to it.
pub(super) fn apply_edit(
&self,
new_content: TimelineItemContent,
edit_json: Option<Raw<AnySyncTimelineEvent>>,
) -> Self {
match self {
Self::Local(local_event) => Self::Local(local_event.with_content(content)),
Self::Remote(remote_event) => Self::Remote(remote_event.with_content(content)),
Self::Local(local_event) => Self::Local(local_event.with_content(new_content)),
Self::Remote(remote_event) => {
Self::Remote(remote_event.apply_edit(new_content, edit_json))
}
}
}

View File

@@ -36,8 +36,13 @@ pub struct RemoteEventTimelineItem {
is_own: bool,
/// Encryption information.
encryption_info: Option<EncryptionInfo>,
// FIXME: Expose the raw JSON of aggregated events somehow
raw: Raw<AnySyncTimelineEvent>,
/// JSON of the original event.
///
/// If the message is edited, this *won't* change, instead
/// `latest_edit_json` will be updated.
original_json: Raw<AnySyncTimelineEvent>,
/// JSON of the latest edit to this item.
latest_edit_json: Option<Raw<AnySyncTimelineEvent>>,
/// Whether the item should be highlighted in the timeline.
is_highlighted: bool,
}
@@ -54,7 +59,7 @@ impl RemoteEventTimelineItem {
read_receipts: IndexMap<OwnedUserId, Receipt>,
is_own: bool,
encryption_info: Option<EncryptionInfo>,
raw: Raw<AnySyncTimelineEvent>,
original_json: Raw<AnySyncTimelineEvent>,
is_highlighted: bool,
) -> Self {
Self {
@@ -67,7 +72,8 @@ impl RemoteEventTimelineItem {
read_receipts,
is_own,
encryption_info,
raw,
original_json,
latest_edit_json: None,
is_highlighted,
}
}
@@ -125,8 +131,13 @@ impl RemoteEventTimelineItem {
}
/// Get the raw JSON representation of the primary event.
pub fn raw(&self) -> &Raw<AnySyncTimelineEvent> {
&self.raw
pub fn original_json(&self) -> &Raw<AnySyncTimelineEvent> {
&self.original_json
}
/// Get the raw JSON representation of the latest edit, if any.
pub fn latest_edit_json(&self) -> Option<&Raw<AnySyncTimelineEvent>> {
self.latest_edit_json.as_ref()
}
/// Whether the event should be highlighted in the timeline.
@@ -159,8 +170,15 @@ impl RemoteEventTimelineItem {
}
/// Clone the current event item, and update its `content`.
pub(in crate::room::timeline) fn with_content(&self, content: TimelineItemContent) -> Self {
Self { content, ..self.clone() }
pub(in crate::room::timeline) fn apply_edit(
&self,
content: TimelineItemContent,
edit_json: Option<Raw<AnySyncTimelineEvent>>,
) -> Self {
// If the edit is local (is not a full event yet), `edit_json` will be
// `None`, in that case retain the existing value of `latest_edit_json`
let latest_edit_json = edit_json.or_else(|| self.latest_edit_json.clone());
Self { content, latest_edit_json, ..self.clone() }
}
/// Clone the current event item, and update its `sender_profile`.

View File

@@ -395,7 +395,7 @@ impl<P: RoomDataProvider> TimelineInner<P> {
tracing::Span::current().record("event_id", debug(remote_event.event_id()));
let raw = remote_event.raw().cast_ref();
let raw = remote_event.original_json().cast_ref();
match olm_machine.decrypt_room_event(raw, room_id).await {
Ok(event) => {
trace!("Successfully decrypted event that previously failed to decrypt");
@@ -694,12 +694,11 @@ async fn fetch_replied_to_event(
return details;
};
let event_item = item
.with_content(TimelineItemContent::Message(message.with_in_reply_to(InReplyToDetails {
event_id: in_reply_to.to_owned(),
details: TimelineDetails::Pending,
})))
.into();
let reply = message.with_in_reply_to(InReplyToDetails {
event_id: in_reply_to.to_owned(),
details: TimelineDetails::Pending,
});
let event_item = item.apply_edit(TimelineItemContent::Message(reply), None).into();
state.items.set(index, Arc::new(TimelineItem::Event(event_item)));
// Don't hold the state lock while the network request is made

View File

@@ -130,7 +130,7 @@ async fn edit() {
assert_eq!(item.timestamp(), MilliSecondsSinceUnixEpoch(uint!(152038280)));
assert!(item.event_id().is_some());
assert!(!item.is_own());
assert!(item.raw().is_some());
assert!(item.original_json().is_some());
let msg = assert_matches!(item.content(), TimelineItemContent::Message(msg) => msg);
assert_matches!(msg.msgtype(), MessageType::Text(_));