Better unred counts

This commit is contained in:
tezlm 2024-01-17 17:36:37 -08:00
parent ec43c4a47b
commit 8204f73d50
Signed by: tezlm
GPG key ID: 649733FCD94AFBBA
3 changed files with 58 additions and 12 deletions

View file

@ -11,10 +11,10 @@ pub mod v3;
#[cfg(feature = "unstable-msc3575")]
pub mod v4;
/// Unread notifications count.
/// Unread notifications count in threads.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnreadNotificationsCount {
pub struct UnreadCountThread {
/// The last acked/read event or message
#[serde(skip_serializing_if = "Option::is_none")]
pub last_ack: Option<OwnedEventId>,
@ -38,8 +38,37 @@ pub struct UnreadNotificationsCount {
pub messages: Option<UInt>,
}
impl UnreadNotificationsCount {
/// Creates an empty `UnreadNotificationsCount`.
/// Unread notifications count in rooms.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnreadCountRoom {
/// The sum of all threaded `mention_user`s
#[serde(skip_serializing_if = "Option::is_none")]
pub mention_user: Option<UInt>,
/// The sum of all threaded `mention_bulk`s
#[serde(skip_serializing_if = "Option::is_none")]
pub mention_bulk: Option<UInt>,
/// The sum of all threaded `notify`s
#[serde(skip_serializing_if = "Option::is_none")]
pub notify: Option<UInt>,
/// The sum of all threaded `message`s
#[serde(skip_serializing_if = "Option::is_none")]
pub messages: Option<UInt>,
/// The total number of threads that don't have an `last_ack` yet
#[serde(skip_serializing_if = "Option::is_none")]
pub threads_unseen: Option<UInt>,
/// The total number of unread threads where `messages > 0` / `last_ack` is no longer the latest message
#[serde(skip_serializing_if = "Option::is_none")]
pub threads_unread: Option<UInt>,
}
impl UnreadCountThread {
/// Creates an empty `UnreadCountThread`.
pub fn new() -> Self {
Default::default()
}
@ -54,6 +83,23 @@ impl UnreadNotificationsCount {
}
}
impl UnreadCountRoom {
/// Creates an empty `UnreadCountRoom`.
pub fn new() -> Self {
Default::default()
}
/// Returns true if there are no notification count updates.
pub fn is_empty(&self) -> bool {
self.mention_user.is_none()
&& self.mention_bulk.is_none()
&& self.notify.is_none()
&& self.messages.is_none()
&& self.threads_unseen.is_none()
&& self.threads_unread.is_none()
}
}
/// Information on E2E device updates.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]

View file

@ -19,7 +19,7 @@ use ruma_events::{
};
use serde::{Deserialize, Serialize};
use super::{DeviceLists, UnreadNotificationsCount};
use super::{DeviceLists, UnreadCountRoom};
use crate::filter::FilterDefinition;
const METADATA: Metadata = metadata! {
@ -250,8 +250,8 @@ pub struct JoinedRoom {
///
/// [unread notifications]: https://spec.matrix.org/latest/client-server-api/#receiving-notifications
/// [`RoomEventFilter`]: crate::filter::RoomEventFilter
#[serde(default, skip_serializing_if = "UnreadNotificationsCount::is_empty")]
pub unread_notifications: UnreadNotificationsCount,
#[serde(default, skip_serializing_if = "UnreadCountRoom::is_empty")]
pub unread_notifications: UnreadCountRoom,
/// Counts of [unread notifications] for threads in this room.
///
@ -262,7 +262,7 @@ pub struct JoinedRoom {
/// [unread notifications]: https://spec.matrix.org/latest/client-server-api/#receiving-notifications
/// [`RoomEventFilter`]: crate::filter::RoomEventFilter
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub unread_thread_notifications: BTreeMap<OwnedEventId, UnreadNotificationsCount>,
pub unread_thread_notifications: BTreeMap<OwnedEventId, UnreadCountRoom>,
/// The timeline of messages and state changes in the room.
#[serde(default, skip_serializing_if = "Timeline::is_empty")]

View file

@ -21,7 +21,7 @@ use ruma_events::{
};
use serde::{de::Error as _, Deserialize, Serialize};
use super::{DeviceLists, UnreadNotificationsCount};
use super::{DeviceLists, UnreadCountRoom, UnreadCountThread};
const METADATA: Metadata = metadata! {
method: POST,
@ -424,8 +424,8 @@ pub struct SlidingSyncRoom {
pub invite_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
/// Counts of unread notifications for this room.
#[serde(default, skip_serializing_if = "UnreadNotificationsCount::is_empty")]
pub unreads: UnreadNotificationsCount,
#[serde(default, skip_serializing_if = "UnreadCountRoom::is_empty")]
pub unreads: UnreadCountRoom,
/// Counts of unread notifications for threads in this room.
///
@ -433,7 +433,7 @@ pub struct SlidingSyncRoom {
///
/// Only set if `unread_thread_notifications` was set to `true` in the [`RoomEventFilter`].
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub unreads_threaded: BTreeMap<OwnedEventId, UnreadNotificationsCount>,
pub unreads_threaded: BTreeMap<OwnedEventId, UnreadCountThread>,
/// The timeline of messages and state changes in the room.
#[serde(default, skip_serializing_if = "Vec::is_empty")]