diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index f6198c4d..f54825a3 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -624,6 +624,9 @@ impl BaseClient { summary.mark_members_missing(); } + let notification_count = room_info.unread_notifications.into(); + summary.update_notification_count(notification_count); + // TODO should we store this? let ephemeral = Ephemeral { events: room_info @@ -636,7 +639,13 @@ impl BaseClient { rooms.join.insert( room_id, - JoinedRoom::new(timeline, state, account_data, ephemeral), + JoinedRoom::new( + timeline, + state, + account_data, + ephemeral, + notification_count, + ), ); changes.add_room(summary); diff --git a/matrix_sdk_base/src/responses.rs b/matrix_sdk_base/src/responses.rs index 936f559f..f913b12c 100644 --- a/matrix_sdk_base/src/responses.rs +++ b/matrix_sdk_base/src/responses.rs @@ -2,7 +2,9 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use matrix_sdk_common::{ - api::r0::sync::sync_events::DeviceLists, + api::r0::sync::sync_events::{ + DeviceLists, UnreadNotificationsCount as RumaUnreadNotificationsCount, + }, events::{ presence::PresenceEvent, AnyBasicEvent, AnySyncEphemeralRoomEvent, AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent, @@ -74,8 +76,8 @@ pub struct Rooms { /// Updates to joined rooms. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct JoinedRoom { - // /// Counts of unread notifications for this room. - // pub unread_notifications: UnreadNotificationsCount, + /// Counts of unread notifications for this room. + pub unread_notifications: UnreadNotificationsCount, /// The timeline of messages and state changes in the room. pub timeline: Timeline, /// Updates to the state, between the time indicated by the `since` parameter, and the start @@ -95,12 +97,35 @@ impl JoinedRoom { state: State, account_data: AccountData, ephemeral: Ephemeral, + unread_notifications: UnreadNotificationsCount, ) -> Self { Self { timeline, state, account_data, ephemeral, + unread_notifications, + } + } +} + +/// Counts of unread notifications for a room. +#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)] +pub struct UnreadNotificationsCount { + /// The number of unread notifications for this room with the highlight flag set. + highlight_count: u64, + /// The total number of unread notifications for this room. + notification_count: u64, +} + +impl From 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), } } } diff --git a/matrix_sdk_base/src/store.rs b/matrix_sdk_base/src/store.rs index 18f06b08..6e2e7cd6 100644 --- a/matrix_sdk_base/src/store.rs +++ b/matrix_sdk_base/src/store.rs @@ -39,7 +39,7 @@ pub struct Store { presence: Tree, } -use crate::Session; +use crate::{responses::UnreadNotificationsCount, Session}; #[derive(Debug, Default)] pub struct StateChanges { @@ -171,6 +171,7 @@ impl Room { canonical_alias: None, avatar_url: None, topic: None, + notification_counts: Default::default(), })), } } @@ -402,6 +403,7 @@ pub struct InnerSummary { avatar_url: Option, topic: Option, + notification_counts: UnreadNotificationsCount, summary: SomeSummary, members_synced: bool, @@ -462,6 +464,10 @@ impl InnerSummary { self.encryption.is_some() } + pub fn update_notification_count(&mut self, notification_counts: UnreadNotificationsCount) { + self.notification_counts = notification_counts; + } + pub(crate) fn update(&mut self, summary: &RumaSummary) -> bool { let mut changed = false;