Can check if rooms are muted

This commit is contained in:
2025-12-01 09:12:58 +01:00
parent 7acb0cbafa
commit 57797e933a
3 changed files with 39 additions and 11 deletions

View File

@@ -4,6 +4,9 @@ use crate::controllers::matrix::matrix_media_controller;
use crate::extractors::matrix_client_extractor::MatrixClientExtractor; use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::{HttpRequest, HttpResponse, web};
use futures_util::{StreamExt, stream}; use futures_util::{StreamExt, stream};
use matrix_sdk::notification_settings::{
IsEncrypted, IsOneToOne, NotificationSettings, RoomNotificationMode,
};
use matrix_sdk::room::ParentSpace; use matrix_sdk::room::ParentSpace;
use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId}; use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId};
use matrix_sdk::{Room, RoomMemberships}; use matrix_sdk::{Room, RoomMemberships};
@@ -17,11 +20,12 @@ pub struct APIRoomInfo {
is_space: bool, is_space: bool,
parents: Vec<OwnedRoomId>, parents: Vec<OwnedRoomId>,
number_unread_messages: u64, number_unread_messages: u64,
notifications: RoomNotificationMode,
latest_event: Option<APIEvent>, latest_event: Option<APIEvent>,
} }
impl APIRoomInfo { impl APIRoomInfo {
async fn from_room(r: &Room) -> anyhow::Result<Self> { async fn from_room(r: &Room, notif: &NotificationSettings) -> anyhow::Result<Self> {
// Get parent spaces // Get parent spaces
let parent_spaces = r let parent_spaces = r
.parent_spaces() .parent_spaces()
@@ -39,19 +43,34 @@ impl APIRoomInfo {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(Self { let members = r
id: r.room_id().to_owned(),
name: r.name(),
members: r
.members(RoomMemberships::ACTIVE) .members(RoomMemberships::ACTIVE)
.await? .await?
.into_iter() .into_iter()
.map(|r| r.user_id().to_owned()) .map(|r| r.user_id().to_owned())
.collect::<Vec<_>>(), .collect::<Vec<_>>();
let notifications = notif
.get_user_defined_room_notification_mode(r.room_id())
.await
.unwrap_or(
notif
.get_default_room_notification_mode(
IsEncrypted::from(r.encryption_state().is_encrypted()),
IsOneToOne::from(members.len() == 2),
)
.await,
);
Ok(Self {
id: r.room_id().to_owned(),
name: r.name(),
members,
avatar: r.avatar_url(), avatar: r.avatar_url(),
is_space: r.is_space(), is_space: r.is_space(),
parents: parent_spaces, parents: parent_spaces,
number_unread_messages: r.unread_notification_counts().notification_count, number_unread_messages: r.unread_notification_counts().notification_count,
notifications,
latest_event: get_events(r, 1, None).await?.events.into_iter().next(), latest_event: get_events(r, 1, None).await?.events.into_iter().next(),
}) })
} }
@@ -59,8 +78,9 @@ impl APIRoomInfo {
/// Get the list of joined rooms of the user /// Get the list of joined rooms of the user
pub async fn joined_rooms(client: MatrixClientExtractor) -> HttpResult { pub async fn joined_rooms(client: MatrixClientExtractor) -> HttpResult {
let notifs = client.client.client.notification_settings().await;
let list = stream::iter(client.client.client.joined_rooms()) let list = stream::iter(client.client.client.joined_rooms())
.then(async |room| APIRoomInfo::from_room(&room).await) .then(async |room| APIRoomInfo::from_room(&room, &notifs).await)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.await .await
.into_iter() .into_iter()
@@ -71,8 +91,10 @@ pub async fn joined_rooms(client: MatrixClientExtractor) -> HttpResult {
/// Get joined spaces rooms of user /// Get joined spaces rooms of user
pub async fn get_joined_spaces(client: MatrixClientExtractor) -> HttpResult { pub async fn get_joined_spaces(client: MatrixClientExtractor) -> HttpResult {
let notifs = client.client.client.notification_settings().await;
let list = stream::iter(client.client.client.joined_space_rooms()) let list = stream::iter(client.client.client.joined_space_rooms())
.then(async |room| APIRoomInfo::from_room(&room).await) .then(async |room| APIRoomInfo::from_room(&room, &notifs).await)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.await .await
.into_iter() .into_iter()
@@ -91,9 +113,11 @@ pub async fn single_room_info(
client: MatrixClientExtractor, client: MatrixClientExtractor,
path: web::Path<RoomIdInPath>, path: web::Path<RoomIdInPath>,
) -> HttpResult { ) -> HttpResult {
let notifs = client.client.client.notification_settings().await;
Ok(match client.client.client.get_room(&path.room_id) { Ok(match client.client.client.get_room(&path.room_id) {
None => HttpResponse::NotFound().json("Room not found"), None => HttpResponse::NotFound().json("Room not found"),
Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r).await?), Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r, &notifs).await?),
}) })
} }

View File

@@ -11,6 +11,7 @@ export interface Room {
is_space?: boolean; is_space?: boolean;
parents: string[]; parents: string[];
number_unread_messages: number; number_unread_messages: number;
notifications: "AllMessages" | "MentionsAndKeywordsOnly" | "Mute";
latest_event?: MatrixEvent; latest_event?: MatrixEvent;
} }

View File

@@ -72,7 +72,10 @@ function _MainMessageWidget(p: {
}, [space, p.rooms]); }, [space, p.rooms]);
const unreadRooms = React.useMemo( const unreadRooms = React.useMemo(
() => p.rooms.filter((r) => r.number_unread_messages > 0).length, () =>
p.rooms.filter(
(r) => r.number_unread_messages > 0 && r.notifications === "AllMessages"
).length,
[p.rooms] [p.rooms]
); );