#![cfg(any(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
#[cfg(feature = "e2e-encryption")]
use ruma::events::{
call::{invite::SyncCallInviteEvent, notify::SyncCallNotifyEvent},
poll::unstable_start::SyncUnstablePollStartEvent,
relation::RelationType,
room::message::SyncRoomMessageEvent,
AnySyncMessageLikeEvent, AnySyncTimelineEvent,
};
use ruma::{
events::{
room::{
member::{MembershipState, SyncRoomMemberEvent},
power_levels::RoomPowerLevels,
},
sticker::SyncStickerEvent,
AnySyncStateEvent,
},
MxcUri, OwnedEventId, UserId,
};
use serde::{Deserialize, Serialize};
use crate::MinimalRoomMemberEvent;
#[cfg(feature = "e2e-encryption")]
#[derive(Debug)]
pub enum PossibleLatestEvent<'a> {
YesRoomMessage(&'a SyncRoomMessageEvent),
YesSticker(&'a SyncStickerEvent),
YesPoll(&'a SyncUnstablePollStartEvent),
YesCallInvite(&'a SyncCallInviteEvent),
YesCallNotify(&'a SyncCallNotifyEvent),
YesKnockedStateEvent(&'a SyncRoomMemberEvent),
NoUnsupportedEventType,
NoUnsupportedMessageLikeType,
NoEncrypted,
}
#[cfg(feature = "e2e-encryption")]
pub fn is_suitable_for_latest_event<'a>(
event: &'a AnySyncTimelineEvent,
power_levels_info: Option<(&'a UserId, &'a RoomPowerLevels)>,
) -> PossibleLatestEvent<'a> {
match event {
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(message)) => {
if let Some(original_message) = message.as_original() {
let is_replacement =
original_message.content.relates_to.as_ref().is_some_and(|relates_to| {
if let Some(relation_type) = relates_to.rel_type() {
relation_type == RelationType::Replacement
} else {
false
}
});
if is_replacement {
PossibleLatestEvent::NoUnsupportedMessageLikeType
} else {
PossibleLatestEvent::YesRoomMessage(message)
}
} else {
PossibleLatestEvent::YesRoomMessage(message)
}
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::UnstablePollStart(poll)) => {
PossibleLatestEvent::YesPoll(poll)
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallInvite(invite)) => {
PossibleLatestEvent::YesCallInvite(invite)
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallNotify(notify)) => {
PossibleLatestEvent::YesCallNotify(notify)
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::Sticker(sticker)) => {
PossibleLatestEvent::YesSticker(sticker)
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(_)) => {
PossibleLatestEvent::NoEncrypted
}
AnySyncTimelineEvent::MessageLike(_) => PossibleLatestEvent::NoUnsupportedMessageLikeType,
AnySyncTimelineEvent::State(state) => {
if let AnySyncStateEvent::RoomMember(member) = state {
if matches!(member.membership(), MembershipState::Knock) {
let can_accept_or_decline_knocks = match power_levels_info {
Some((own_user_id, room_power_levels)) => {
room_power_levels.user_can_invite(own_user_id)
|| room_power_levels.user_can_kick(own_user_id)
}
_ => false,
};
if can_accept_or_decline_knocks {
return PossibleLatestEvent::YesKnockedStateEvent(member);
}
}
}
PossibleLatestEvent::NoUnsupportedEventType
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct LatestEvent {
event: SyncTimelineEvent,
#[serde(skip_serializing_if = "Option::is_none")]
sender_profile: Option<MinimalRoomMemberEvent>,
#[serde(skip_serializing_if = "Option::is_none")]
sender_name_is_ambiguous: Option<bool>,
}
#[derive(Deserialize)]
struct SerializedLatestEvent {
event: SyncTimelineEvent,
#[serde(skip_serializing_if = "Option::is_none")]
sender_profile: Option<MinimalRoomMemberEvent>,
#[serde(skip_serializing_if = "Option::is_none")]
sender_name_is_ambiguous: Option<bool>,
}
impl<'de> Deserialize<'de> for LatestEvent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw: Box<serde_json::value::RawValue> = Box::deserialize(deserializer)?;
let mut variant_errors = Vec::new();
match serde_json::from_str::<SerializedLatestEvent>(raw.get()) {
Ok(value) => {
return Ok(LatestEvent {
event: value.event,
sender_profile: value.sender_profile,
sender_name_is_ambiguous: value.sender_name_is_ambiguous,
});
}
Err(err) => variant_errors.push(err),
}
match serde_json::from_str::<SyncTimelineEvent>(raw.get()) {
Ok(value) => {
return Ok(LatestEvent {
event: value,
sender_profile: None,
sender_name_is_ambiguous: None,
});
}
Err(err) => variant_errors.push(err),
}
Err(serde::de::Error::custom(
format!("data did not match any variant of serialized LatestEvent (using serde_json). Observed errors: {variant_errors:?}")
))
}
}
impl LatestEvent {
pub fn new(event: SyncTimelineEvent) -> Self {
Self { event, sender_profile: None, sender_name_is_ambiguous: None }
}
pub fn new_with_sender_details(
event: SyncTimelineEvent,
sender_profile: Option<MinimalRoomMemberEvent>,
sender_name_is_ambiguous: Option<bool>,
) -> Self {
Self { event, sender_profile, sender_name_is_ambiguous }
}
pub fn into_event(self) -> SyncTimelineEvent {
self.event
}
pub fn event(&self) -> &SyncTimelineEvent {
&self.event
}
pub fn event_mut(&mut self) -> &mut SyncTimelineEvent {
&mut self.event
}
pub fn event_id(&self) -> Option<OwnedEventId> {
self.event.event_id()
}
pub fn has_sender_profile(&self) -> bool {
self.sender_profile.is_some()
}
pub fn sender_display_name(&self) -> Option<&str> {
self.sender_profile.as_ref().and_then(|profile| {
profile.as_original().and_then(|event| event.content.displayname.as_deref())
})
}
pub fn sender_name_ambiguous(&self) -> Option<bool> {
self.sender_name_is_ambiguous
}
pub fn sender_avatar_url(&self) -> Option<&MxcUri> {
self.sender_profile.as_ref().and_then(|profile| {
profile.as_original().and_then(|event| event.content.avatar_url.as_deref())
})
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use ruma::{
events::{
call::{
invite::{CallInviteEventContent, SyncCallInviteEvent},
notify::{
ApplicationType, CallNotifyEventContent, NotifyType, SyncCallNotifyEvent,
},
SessionDescription,
},
poll::{
unstable_response::{
SyncUnstablePollResponseEvent, UnstablePollResponseEventContent,
},
unstable_start::{
NewUnstablePollStartEventContent, SyncUnstablePollStartEvent,
UnstablePollAnswer, UnstablePollStartContentBlock,
},
},
relation::Replacement,
room::{
encrypted::{
EncryptedEventScheme, OlmV1Curve25519AesSha2Content, RoomEncryptedEventContent,
SyncRoomEncryptedEvent,
},
message::{
ImageMessageEventContent, MessageType, RedactedRoomMessageEventContent,
Relation, RoomMessageEventContent, SyncRoomMessageEvent,
},
topic::{RoomTopicEventContent, SyncRoomTopicEvent},
ImageInfo, MediaSource,
},
sticker::{StickerEventContent, SyncStickerEvent},
AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent, EmptyStateKey,
Mentions, MessageLikeUnsigned, OriginalSyncMessageLikeEvent, OriginalSyncStateEvent,
RedactedSyncMessageLikeEvent, RedactedUnsigned, StateUnsigned, SyncMessageLikeEvent,
UnsignedRoomRedactionEvent,
},
owned_event_id, owned_mxc_uri, owned_user_id,
serde::Raw,
MilliSecondsSinceUnixEpoch, UInt, VoipVersionId,
};
use serde_json::json;
use crate::latest_event::{is_suitable_for_latest_event, LatestEvent, PossibleLatestEvent};
#[test]
fn test_room_messages_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncRoomMessageEvent::Original(OriginalSyncMessageLikeEvent {
content: RoomMessageEventContent::new(MessageType::Image(
ImageMessageEventContent::new(
"".to_owned(),
MediaSource::Plain(owned_mxc_uri!("mxc://example.com/1")),
),
)),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_let!(
PossibleLatestEvent::YesRoomMessage(SyncMessageLikeEvent::Original(m)) =
is_suitable_for_latest_event(&event, None)
);
assert_eq!(m.content.msgtype.msgtype(), "m.image");
}
#[test]
fn test_polls_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::UnstablePollStart(
SyncUnstablePollStartEvent::Original(OriginalSyncMessageLikeEvent {
content: NewUnstablePollStartEventContent::new(UnstablePollStartContentBlock::new(
"do you like rust?",
vec![UnstablePollAnswer::new("id", "yes")].try_into().unwrap(),
))
.into(),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_let!(
PossibleLatestEvent::YesPoll(SyncMessageLikeEvent::Original(m)) =
is_suitable_for_latest_event(&event, None)
);
assert_eq!(m.content.poll_start().question.text, "do you like rust?");
}
#[test]
fn test_call_invites_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallInvite(
SyncCallInviteEvent::Original(OriginalSyncMessageLikeEvent {
content: CallInviteEventContent::new(
"call_id".into(),
UInt::new(123).unwrap(),
SessionDescription::new("".into(), "".into()),
VoipVersionId::V1,
),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_let!(
PossibleLatestEvent::YesCallInvite(SyncMessageLikeEvent::Original(_)) =
is_suitable_for_latest_event(&event, None)
);
}
#[test]
fn test_call_notifications_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallNotify(
SyncCallNotifyEvent::Original(OriginalSyncMessageLikeEvent {
content: CallNotifyEventContent::new(
"call_id".into(),
ApplicationType::Call,
NotifyType::Ring,
Mentions::new(),
),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_let!(
PossibleLatestEvent::YesCallNotify(SyncMessageLikeEvent::Original(_)) =
is_suitable_for_latest_event(&event, None)
);
}
#[test]
fn test_stickers_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::Sticker(
SyncStickerEvent::Original(OriginalSyncMessageLikeEvent {
content: StickerEventContent::new(
"sticker!".to_owned(),
ImageInfo::new(),
owned_mxc_uri!("mxc://example.com/1"),
),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::YesSticker(SyncStickerEvent::Original(_))
);
}
#[test]
fn test_different_types_of_messagelike_are_unsuitable() {
let event =
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::UnstablePollResponse(
SyncUnstablePollResponseEvent::Original(OriginalSyncMessageLikeEvent {
content: UnstablePollResponseEventContent::new(
vec![String::from("option1")],
owned_event_id!("$1"),
),
event_id: owned_event_id!("$2"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::NoUnsupportedMessageLikeType
);
}
#[test]
fn test_redacted_messages_are_suitable() {
let room_redaction_event: UnsignedRoomRedactionEvent = serde_json::from_value(json!({
"content": {},
"event_id": "$redaction",
"sender": "@x:y.za",
"origin_server_ts": 223543,
"unsigned": { "reason": "foo" }
}))
.unwrap();
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncRoomMessageEvent::Redacted(RedactedSyncMessageLikeEvent {
content: RedactedRoomMessageEventContent::new(),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: RedactedUnsigned::new(room_redaction_event),
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::YesRoomMessage(SyncMessageLikeEvent::Redacted(_))
);
}
#[test]
fn test_encrypted_messages_are_unsuitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(
SyncRoomEncryptedEvent::Original(OriginalSyncMessageLikeEvent {
content: RoomEncryptedEventContent::new(
EncryptedEventScheme::OlmV1Curve25519AesSha2(
OlmV1Curve25519AesSha2Content::new(BTreeMap::new(), "".to_owned()),
),
None,
),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::NoEncrypted
);
}
#[test]
fn test_state_events_are_unsuitable() {
let event = AnySyncTimelineEvent::State(AnySyncStateEvent::RoomTopic(
SyncRoomTopicEvent::Original(OriginalSyncStateEvent {
content: RoomTopicEventContent::new("".to_owned()),
event_id: owned_event_id!("$1"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: StateUnsigned::new(),
state_key: EmptyStateKey,
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::NoUnsupportedEventType
);
}
#[test]
fn test_replacement_events_are_unsuitable() {
let mut event_content = RoomMessageEventContent::text_plain("Bye bye, world!");
event_content.relates_to = Some(Relation::Replacement(Replacement::new(
owned_event_id!("$1"),
RoomMessageEventContent::text_plain("Hello, world!").into(),
)));
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncRoomMessageEvent::Original(OriginalSyncMessageLikeEvent {
content: event_content,
event_id: owned_event_id!("$2"),
sender: owned_user_id!("@a:b.c"),
origin_server_ts: MilliSecondsSinceUnixEpoch(UInt::new(2123).unwrap()),
unsigned: MessageLikeUnsigned::new(),
}),
));
assert_matches!(
is_suitable_for_latest_event(&event, None),
PossibleLatestEvent::NoUnsupportedMessageLikeType
);
}
#[test]
fn test_deserialize_latest_event() {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct TestStruct {
latest_event: LatestEvent,
}
let event = SyncTimelineEvent::new(
Raw::from_json_string(json!({ "event_id": "$1" }).to_string()).unwrap(),
);
let initial = TestStruct {
latest_event: LatestEvent {
event: event.clone(),
sender_profile: None,
sender_name_is_ambiguous: None,
},
};
let serialized = serde_json::to_value(&initial).unwrap();
assert_eq!(
serialized,
json!({
"latest_event": {
"event": {
"kind": {
"PlainText": {
"event": {
"event_id": "$1"
}
}
}
},
}
})
);
let deserialized: TestStruct = serde_json::from_value(serialized).unwrap();
assert_eq!(deserialized.latest_event.event().event_id().unwrap(), "$1");
assert!(deserialized.latest_event.sender_profile.is_none());
assert!(deserialized.latest_event.sender_name_is_ambiguous.is_none());
let serialized = json!({
"latest_event": {
"event": {
"encryption_info": null,
"event": {
"event_id": "$1"
}
},
}
});
let deserialized: TestStruct = serde_json::from_value(serialized).unwrap();
assert_eq!(deserialized.latest_event.event().event_id().unwrap(), "$1");
assert!(deserialized.latest_event.sender_profile.is_none());
assert!(deserialized.latest_event.sender_name_is_ambiguous.is_none());
let serialized = json!({
"latest_event": event
});
let deserialized: TestStruct = serde_json::from_value(serialized).unwrap();
assert_eq!(deserialized.latest_event.event().event_id().unwrap(), "$1");
assert!(deserialized.latest_event.sender_profile.is_none());
assert!(deserialized.latest_event.sender_name_is_ambiguous.is_none());
}
}