Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unable-to-decrypt timeline item support #1140

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ mod uniffi_types {
SlidingSyncView, SlidingSyncViewBuilder, StoppableSpawn, UnreadNotificationsCount,
},
timeline::{
EmoteMessageContent, EventTimelineItem, FormattedBody, ImageInfo, ImageMessageContent,
InsertAtData, Message, MessageFormat, MessageType, NoticeMessageContent, Reaction,
TextMessageContent, ThumbnailInfo, TimelineChange, TimelineDiff, TimelineItem,
TimelineItemContent, TimelineKey, UpdateAtData, VirtualTimelineItem,
EmoteMessageContent, EncryptedMessage, EventTimelineItem, FormattedBody, ImageInfo,
ImageMessageContent, InsertAtData, Message, MessageFormat, MessageType,
NoticeMessageContent, Reaction, TextMessageContent, ThumbnailInfo, TimelineChange,
TimelineDiff, TimelineItem, TimelineItemContent, TimelineKey, UpdateAtData,
VirtualTimelineItem,
},
};
}
34 changes: 34 additions & 0 deletions bindings/matrix-sdk-ffi/src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,25 @@ impl TimelineItemContent {
unwrap_or_clone_arc_into_variant!(self, .0, C::Message(msg) => Arc::new(Message(msg)))
}

pub fn as_unable_to_decrypt(&self) -> Option<EncryptedMessage> {
use matrix_sdk::room::timeline::{EncryptedMessage as E, TimelineItemContent as C};

match &self.0 {
C::UnableToDecrypt(utd) => Some(match utd {
E::OlmV1Curve25519AesSha2 { sender_key } => {
let sender_key = sender_key.clone();
EncryptedMessage::OlmV1Curve25519AesSha2 { sender_key }
}
E::MegolmV1AesSha2 { session_id, .. } => {
let session_id = session_id.clone();
EncryptedMessage::MegolmV1AesSha2 { session_id }
}
E::Unknown => EncryptedMessage::Unknown,
}),
_ => None,
}
}

pub fn is_redacted_message(&self) -> bool {
use matrix_sdk::room::timeline::TimelineItemContent as C;
matches!(self.0, C::RedactedMessage)
Expand Down Expand Up @@ -374,6 +393,21 @@ impl From<&matrix_sdk::ruma::events::room::ImageInfo> for ImageInfo {
}
}

#[derive(Clone, uniffi::Enum)]
pub enum EncryptedMessage {
OlmV1Curve25519AesSha2 {
/// The Curve25519 key of the sender.
sender_key: String,
},
// Other fields not included because UniFFI doesn't have the concept of
// deprecated fields right now.
MegolmV1AesSha2 {
/// The ID of the session used to encrypt the message.
session_id: String,
},
Unknown,
}

#[derive(Clone, uniffi::Record)]
pub struct Reaction {
pub key: String,
Expand Down
42 changes: 35 additions & 7 deletions crates/matrix-sdk/src/room/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use ruma::{
events::{
reaction::ReactionEventContent,
room::{
message::{Relation, Replacement, RoomMessageEventContent},
encrypted::{self, RoomEncryptedEventContent},
message::{self, Replacement, RoomMessageEventContent},
redaction::{
OriginalSyncRoomRedactionEvent, RoomRedactionEventContent, SyncRoomRedactionEvent,
},
Expand Down Expand Up @@ -216,6 +217,7 @@ impl<'a> TimelineEventHandler<'a> {
TimelineEventKind::Message { content } => match content {
AnyMessageLikeEventContent::Reaction(c) => self.handle_reaction(c),
AnyMessageLikeEventContent::RoomMessage(c) => self.handle_room_message(c),
AnyMessageLikeEventContent::RoomEncrypted(c) => self.handle_room_encrypted(c),
// TODO
_ => {}
},
Expand All @@ -236,7 +238,7 @@ impl<'a> TimelineEventHandler<'a> {

fn handle_room_message(&mut self, content: RoomMessageEventContent) {
match content.relates_to {
Some(Relation::Replacement(re)) => {
Some(message::Relation::Replacement(re)) => {
self.handle_room_message_edit(re);
}
_ => {
Expand All @@ -252,7 +254,7 @@ impl<'a> TimelineEventHandler<'a> {
if self.meta.sender != item.sender() {
info!(
%event_id, original_sender = %item.sender(), edit_sender = %self.meta.sender,
"Event tries to edit another user's timeline item, discarding"
"Edit event applies to another user's timeline item, discarding"
);
return None;
}
Expand All @@ -262,7 +264,14 @@ impl<'a> TimelineEventHandler<'a> {
TimelineItemContent::RedactedMessage => {
info!(
%event_id,
"Event tries to edit a non-editable timeline item, discarding"
"Edit event applies to a redacted message, discarding"
);
return None;
}
TimelineItemContent::UnableToDecrypt(_) => {
info!(
%event_id,
"Edit event applies to event that couldn't be decrypted, discarding"
);
return None;
}
Expand Down Expand Up @@ -313,6 +322,18 @@ impl<'a> TimelineEventHandler<'a> {
}
}

fn handle_room_encrypted(&mut self, c: RoomEncryptedEventContent) {
match c.relates_to {
Some(encrypted::Relation::Replacement(_) | encrypted::Relation::Annotation(_)) => {
// Do nothing for these, as they would not produce a new
// timeline item when decrypted either
Comment on lines +328 to +329
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand on this? Is this about an encrypted event, that we can't decrypt, being a replacement that is an edit.

The assumption here is that edits only update timeline items but don't produce new ones, right? If that's so, then this sounds correct.

Copy link
Collaborator Author

@jplatte jplatte Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. I was wondering whether we should show a UTD item for an edit that can't be decrypted. this branch also includes "annotations", i.e. reactions which are currently never encrypted so maybe that variant shouldn't exist at all 🤔

}
_ => {
self.add(NewEventTimelineItem::unable_to_decrypt(c));
}
}
}

// Redacted redactions are no-ops (unfortunately)
fn handle_redaction(&mut self, redacts: OwnedEventId, _content: RoomRedactionEventContent) {
let mut did_update = false;
Expand Down Expand Up @@ -490,12 +511,12 @@ struct NewEventTimelineItem {
impl NewEventTimelineItem {
// These constructors could also be `From` implementations, but that would
// allow users to call them directly, which should not be supported
pub(crate) fn message(c: RoomMessageEventContent, relations: Option<Relations>) -> Self {
fn message(c: RoomMessageEventContent, relations: Option<Relations>) -> Self {
let edited = relations.as_ref().map_or(false, |r| r.replace.is_some());
let content = TimelineItemContent::Message(Message {
msgtype: c.msgtype,
in_reply_to: c.relates_to.and_then(|rel| match rel {
Relation::Reply { in_reply_to } => Some(in_reply_to.event_id),
message::Relation::Reply { in_reply_to } => Some(in_reply_to.event_id),
_ => None,
}),
edited,
Expand All @@ -507,7 +528,14 @@ impl NewEventTimelineItem {
Self { content, reactions }
}

pub(crate) fn redacted_message() -> Self {
fn unable_to_decrypt(content: RoomEncryptedEventContent) -> Self {
Self {
content: TimelineItemContent::UnableToDecrypt(content.into()),
reactions: BundledReactions::default(),
}
}

fn redacted_message() -> Self {
Self {
content: TimelineItemContent::RedactedMessage,
reactions: BundledReactions::default(),
Expand Down
56 changes: 53 additions & 3 deletions crates/matrix-sdk/src/room/timeline/event_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ use ruma::events::room::message::{OriginalSyncRoomMessageEvent, Relation};
use ruma::{
events::{
relation::{AnnotationChunk, AnnotationType},
room::message::MessageType,
room::{
encrypted::{EncryptedEventScheme, MegolmV1AesSha2Content, RoomEncryptedEventContent},
message::MessageType,
},
AnySyncTimelineEvent,
},
serde::Raw,
uint, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedTransactionId, OwnedUserId,
TransactionId, UInt, UserId,
uint, EventId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedTransactionId,
OwnedUserId, TransactionId, UInt, UserId,
};

/// An item in the timeline that represents at least one event.
Expand Down Expand Up @@ -282,6 +285,9 @@ pub enum TimelineItemContent {

/// A redacted message.
RedactedMessage,

/// An `m.room.encrypted` event that could not be decrypted.
UnableToDecrypt(EncryptedMessage),
}

/// An `m.room.message` event or extensible event, including edits.
Expand Down Expand Up @@ -312,6 +318,50 @@ impl Message {
}
}

/// Metadata about an `m.room.encrypted` event that could not be decrypted.
#[derive(Clone, Debug)]
pub enum EncryptedMessage {
/// Metadata about an event using the `m.olm.v1.curve25519-aes-sha2`
/// algorithm.
OlmV1Curve25519AesSha2 {
/// The Curve25519 key of the sender.
sender_key: String,
},
/// Metadata about an event using the `m.megolm.v1.aes-sha2` algorithm.
MegolmV1AesSha2 {
/// The Curve25519 key of the sender.
#[deprecated = "this field still needs to be sent but should not be used when received"]
#[doc(hidden)] // Included for Debug formatting only
sender_key: String,

/// The ID of the sending device.
#[deprecated = "this field still needs to be sent but should not be used when received"]
#[doc(hidden)] // Included for Debug formatting only
device_id: OwnedDeviceId,

/// The ID of the session used to encrypt the message.
session_id: String,
},
/// No metadata because the event uses an unknown algorithm.
Unknown,
}

impl From<RoomEncryptedEventContent> for EncryptedMessage {
fn from(c: RoomEncryptedEventContent) -> Self {
match c.scheme {
EncryptedEventScheme::OlmV1Curve25519AesSha2(s) => {
Self::OlmV1Curve25519AesSha2 { sender_key: s.sender_key }
}
#[allow(deprecated)]
EncryptedEventScheme::MegolmV1AesSha2(s) => {
let MegolmV1AesSha2Content { sender_key, device_id, session_id, .. } = s;
Self::MegolmV1AesSha2 { sender_key, device_id, session_id }
}
_ => Self::Unknown,
}
}
}

#[derive(Clone, Debug)]
pub struct BundledReactions {
/// Whether all reactions are known, or some may be missing.
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk/src/room/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ mod virtual_item;

pub use self::{
event_item::{
EventTimelineItem, Message, PaginationOutcome, ReactionDetails, TimelineDetails,
TimelineItemContent, TimelineKey,
EncryptedMessage, EventTimelineItem, Message, PaginationOutcome, ReactionDetails,
TimelineDetails, TimelineItemContent, TimelineKey,
},
virtual_item::VirtualTimelineItem,
};
Expand Down