use std::{collections::BTreeMap, fmt};
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use ruma::{
api::client::{
push::get_notifications::v3::Notification,
sync::sync_events::{
v3::InvitedRoom, UnreadNotificationsCount as RumaUnreadNotificationsCount,
},
},
events::{
presence::PresenceEvent, AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent,
AnySyncEphemeralRoomEvent, AnySyncStateEvent, AnyToDeviceEvent,
},
serde::Raw,
OwnedRoomId,
};
use serde::{Deserialize, Serialize};
use crate::{
debug::{
DebugInvitedRoom, DebugListOfRawEvents, DebugListOfRawEventsNoId, DebugNotificationMap,
},
deserialized_responses::AmbiguityChanges,
};
#[derive(Clone, Default)]
pub struct SyncResponse {
pub rooms: Rooms,
pub presence: Vec<Raw<PresenceEvent>>,
pub account_data: Vec<Raw<AnyGlobalAccountDataEvent>>,
pub to_device: Vec<Raw<AnyToDeviceEvent>>,
pub ambiguity_changes: AmbiguityChanges,
pub notifications: BTreeMap<OwnedRoomId, Vec<Notification>>,
}
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for SyncResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SyncResponse")
.field("rooms", &self.rooms)
.field("account_data", &DebugListOfRawEventsNoId(&self.account_data))
.field("to_device", &DebugListOfRawEventsNoId(&self.to_device))
.field("ambiguity_changes", &self.ambiguity_changes)
.field("notifications", &DebugNotificationMap(&self.notifications))
.finish_non_exhaustive()
}
}
#[derive(Clone, Default)]
pub struct Rooms {
pub leave: BTreeMap<OwnedRoomId, LeftRoom>,
pub join: BTreeMap<OwnedRoomId, JoinedRoom>,
pub invite: BTreeMap<OwnedRoomId, InvitedRoom>,
}
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for Rooms {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Rooms")
.field("leave", &self.leave)
.field("join", &self.join)
.field("invite", &DebugInvitedRooms(&self.invite))
.finish()
}
}
#[derive(Clone, Default)]
pub struct JoinedRoom {
pub unread_notifications: UnreadNotificationsCount,
pub timeline: Timeline,
pub state: Vec<Raw<AnySyncStateEvent>>,
pub account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
pub ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
}
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for JoinedRoom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JoinedRoom")
.field("unread_notifications", &self.unread_notifications)
.field("timeline", &self.timeline)
.field("state", &DebugListOfRawEvents(&self.state))
.field("account_data", &DebugListOfRawEventsNoId(&self.account_data))
.field("ephemeral", &self.ephemeral)
.finish()
}
}
impl JoinedRoom {
pub(crate) fn new(
timeline: Timeline,
state: Vec<Raw<AnySyncStateEvent>>,
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
unread_notifications: UnreadNotificationsCount,
) -> Self {
Self { unread_notifications, timeline, state, account_data, ephemeral }
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
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)]
pub struct LeftRoom {
pub timeline: Timeline,
pub state: Vec<Raw<AnySyncStateEvent>>,
pub account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
}
impl LeftRoom {
pub(crate) fn new(
timeline: Timeline,
state: Vec<Raw<AnySyncStateEvent>>,
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
) -> Self {
Self { timeline, state, account_data }
}
}
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for LeftRoom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JoinedRoom")
.field("timeline", &self.timeline)
.field("state", &DebugListOfRawEvents(&self.state))
.field("account_data", &DebugListOfRawEventsNoId(&self.account_data))
.finish()
}
}
#[derive(Clone, Debug, Default)]
pub struct Timeline {
pub limited: bool,
pub prev_batch: Option<String>,
pub events: Vec<SyncTimelineEvent>,
}
impl Timeline {
pub(crate) fn new(limited: bool, prev_batch: Option<String>) -> Self {
Self { limited, prev_batch, ..Default::default() }
}
}
struct DebugInvitedRooms<'a>(&'a BTreeMap<OwnedRoomId, InvitedRoom>);
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for DebugInvitedRooms<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter().map(|(k, v)| (k, DebugInvitedRoom(v)))).finish()
}
}