use std::{borrow::Borrow, collections::BTreeMap};
use ruma::{
api::client::{
push::get_notifications::v3::Notification,
sync::sync_events::{
v3::{
DeviceLists, Ephemeral, GlobalAccountData, InvitedRoom, Presence, RoomAccountData,
State, ToDevice,
},
UnreadNotificationsCount as RumaUnreadNotificationsCount,
},
},
events::{
room::member::{
MembershipState, RoomMemberEvent, RoomMemberEventContent, StrippedRoomMemberEvent,
SyncRoomMemberEvent,
},
AnySyncTimelineEvent, AnyTimelineEvent,
},
serde::Raw,
DeviceKeyAlgorithm, EventId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId,
OwnedRoomId, OwnedUserId, UserId,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AmbiguityChange {
pub member_ambiguous: bool,
pub disambiguated_member: Option<OwnedUserId>,
pub ambiguated_member: Option<OwnedUserId>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AmbiguityChanges {
pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum VerificationState {
Trusted,
Untrusted,
UnknownDevice,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum AlgorithmInfo {
MegolmV1AesSha2 {
curve25519_key: String,
sender_claimed_keys: BTreeMap<DeviceKeyAlgorithm, String>,
},
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EncryptionInfo {
pub sender: OwnedUserId,
pub sender_device: OwnedDeviceId,
pub algorithm_info: AlgorithmInfo,
pub verification_state: VerificationState,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SyncTimelineEvent {
pub event: Raw<AnySyncTimelineEvent>,
pub encryption_info: Option<EncryptionInfo>,
}
impl SyncTimelineEvent {
pub fn event_id(&self) -> Option<OwnedEventId> {
self.event.get_field::<OwnedEventId>("event_id").ok().flatten()
}
}
impl From<Raw<AnySyncTimelineEvent>> for SyncTimelineEvent {
fn from(inner: Raw<AnySyncTimelineEvent>) -> Self {
Self { encryption_info: None, event: inner }
}
}
impl From<TimelineEvent> for SyncTimelineEvent {
fn from(o: TimelineEvent) -> Self {
Self { encryption_info: o.encryption_info, event: o.event.cast() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SyncResponse {
pub next_batch: String,
pub rooms: Rooms,
pub presence: Presence,
pub account_data: GlobalAccountData,
pub to_device: ToDevice,
pub device_lists: DeviceLists,
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, u64>,
pub ambiguity_changes: AmbiguityChanges,
pub notifications: BTreeMap<OwnedRoomId, Vec<Notification>>,
}
impl SyncResponse {
pub fn new(next_batch: String) -> Self {
Self { next_batch, ..Default::default() }
}
}
#[derive(Clone, Debug)]
pub struct TimelineEvent {
pub event: Raw<AnyTimelineEvent>,
pub encryption_info: Option<EncryptionInfo>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Rooms {
pub leave: BTreeMap<OwnedRoomId, LeftRoom>,
pub join: BTreeMap<OwnedRoomId, JoinedRoom>,
pub invite: BTreeMap<OwnedRoomId, InvitedRoom>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct JoinedRoom {
pub unread_notifications: UnreadNotificationsCount,
pub timeline: Timeline,
pub state: State,
pub account_data: RoomAccountData,
pub ephemeral: Ephemeral,
}
impl JoinedRoom {
pub fn new(
timeline: Timeline,
state: State,
account_data: RoomAccountData,
ephemeral: Ephemeral,
unread_notifications: UnreadNotificationsCount,
) -> Self {
Self { unread_notifications, timeline, state, account_data, ephemeral }
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct UnreadNotificationsCount {
pub highlight_count: u64,
pub notification_count: u64,
}
impl From<RumaUnreadNotificationsCount> for UnreadNotificationsCount {
fn from(notifications: RumaUnreadNotificationsCount) -> Self {
Self {
highlight_count: notifications.highlight_count.map(|c| c.into()).unwrap_or(0),
notification_count: notifications.notification_count.map(|c| c.into()).unwrap_or(0),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LeftRoom {
pub timeline: Timeline,
pub state: State,
pub account_data: RoomAccountData,
}
impl LeftRoom {
pub fn new(timeline: Timeline, state: State, account_data: RoomAccountData) -> Self {
Self { timeline, state, account_data }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Timeline {
pub limited: bool,
pub prev_batch: Option<String>,
pub events: Vec<SyncTimelineEvent>,
}
impl Timeline {
pub fn new(limited: bool, prev_batch: Option<String>) -> Self {
Self { limited, prev_batch, ..Default::default() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct TimelineSlice {
pub start: String,
pub end: Option<String>,
pub limited: bool,
pub events: Vec<SyncTimelineEvent>,
pub sync: bool,
}
impl TimelineSlice {
pub fn new(
events: Vec<SyncTimelineEvent>,
start: String,
end: Option<String>,
limited: bool,
sync: bool,
) -> Self {
Self { start, end, events, limited, sync }
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MemberEvent {
Sync(SyncRoomMemberEvent),
Stripped(StrippedRoomMemberEvent),
}
impl MemberEvent {
pub fn original_content(&self) -> Option<&RoomMemberEventContent> {
match self {
MemberEvent::Sync(e) => e.as_original().map(|e| &e.content),
MemberEvent::Stripped(e) => Some(&e.content),
}
}
pub fn sender(&self) -> &UserId {
match self {
MemberEvent::Sync(e) => e.sender(),
MemberEvent::Stripped(e) => e.sender.borrow(),
}
}
pub fn event_id(&self) -> Option<&EventId> {
match self {
MemberEvent::Sync(e) => Some(e.event_id()),
MemberEvent::Stripped(_) => None,
}
}
pub fn origin_server_ts(&self) -> Option<MilliSecondsSinceUnixEpoch> {
match self {
MemberEvent::Sync(e) => Some(e.origin_server_ts()),
MemberEvent::Stripped(_) => None,
}
}
pub fn membership(&self) -> &MembershipState {
match self {
MemberEvent::Sync(e) => e.membership(),
MemberEvent::Stripped(e) => &e.content.membership,
}
}
pub fn user_id(&self) -> &UserId {
match self {
MemberEvent::Sync(e) => e.state_key(),
MemberEvent::Stripped(e) => &e.state_key,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct MembersResponse {
pub chunk: Vec<RoomMemberEvent>,
pub ambiguity_changes: AmbiguityChanges,
}
#[cfg(test)]
mod tests {
use ruma::{
event_id,
events::{
room::message::RoomMessageEventContent, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
MessageLikeUnsigned, OriginalMessageLikeEvent, SyncMessageLikeEvent,
},
room_id,
serde::Raw,
user_id, MilliSecondsSinceUnixEpoch,
};
use super::{SyncTimelineEvent, TimelineEvent};
#[test]
fn room_event_to_sync_room_event() {
let content = RoomMessageEventContent::text_plain("foobar");
let event = OriginalMessageLikeEvent {
content,
event_id: event_id!("$xxxxx:example.org").to_owned(),
room_id: room_id!("!someroom:example.com").to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
sender: user_id!("@carl:example.com").to_owned(),
unsigned: MessageLikeUnsigned::default(),
};
let room_event =
TimelineEvent { event: Raw::new(&event).unwrap().cast(), encryption_info: None };
let converted_room_event: SyncTimelineEvent = room_event.into();
let converted_event: AnySyncTimelineEvent =
converted_room_event.event.deserialize().unwrap();
let sync_event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncMessageLikeEvent::Original(event.into()),
));
assert_eq!(converted_event.event_id(), sync_event.event_id());
assert_eq!(converted_event.sender(), sync_event.sender());
}
}