feat: Return SENDER_IGNORED error in is_ignored_pdu

This commit is contained in:
timedout
2026-01-22 13:47:39 +00:00
committed by Jade Ellis
parent 60a3abe752
commit 8bc6e6ccca
2 changed files with 22 additions and 9 deletions
+21 -8
View File
@@ -1,7 +1,7 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp; use axum_client_ip::InsecureClientIp;
use conduwuit::{ use conduwuit::{
Err, Result, at, debug_warn, Err, Error, Result, at, debug_warn,
matrix::{ matrix::{
event::{Event, Matches}, event::{Event, Matches},
pdu::PduCount, pdu::PduCount,
@@ -26,7 +26,7 @@ use ruma::{
DeviceId, RoomId, UserId, DeviceId, RoomId, UserId,
api::{ api::{
Direction, Direction,
client::{filter::RoomEventFilter, message::get_message_events}, client::{error::ErrorKind, filter::RoomEventFilter, message::get_message_events},
}, },
events::{ events::{
AnyStateEvent, StateEventType, AnyStateEvent, StateEventType,
@@ -279,23 +279,30 @@ pub(crate) async fn ignored_filter(
is_ignored_pdu(services, pdu, user_id) is_ignored_pdu(services, pdu, user_id)
.await .await
.unwrap_or(true)
.eq(&false) .eq(&false)
.then_some(item) .then_some(item)
} }
/// Determine whether a PDU should be ignored for a given recipient user.
/// Returns True if this PDU should be ignored, returns False otherwise.
///
/// The error SenderIgnored is returned if the sender or the sender's server is
/// ignored by the relevant user. If the error cannot be returned to the user,
/// it should equate to a true value (i.e. ignored).
#[inline] #[inline]
pub(crate) async fn is_ignored_pdu<Pdu>( pub(crate) async fn is_ignored_pdu<Pdu>(
services: &Services, services: &Services,
event: &Pdu, event: &Pdu,
recipient_user: &UserId, recipient_user: &UserId,
) -> bool ) -> Result<bool>
where where
Pdu: Event + Send + Sync, Pdu: Event + Send + Sync,
{ {
// exclude Synapse's dummy events from bloating up response bodies. clients // exclude Synapse's dummy events from bloating up response bodies. clients
// don't need to see this. // don't need to see this.
if event.kind().to_cow_str() == "org.matrix.dummy_event" { if event.kind().to_cow_str() == "org.matrix.dummy_event" {
return true; return Ok(true);
} }
let sender_user = event.sender(); let sender_user = event.sender();
@@ -310,21 +317,27 @@ where
if !type_ignored { if !type_ignored {
// We cannot safely ignore this type // We cannot safely ignore this type
return false; return Ok(false);
} }
if server_ignored { if server_ignored {
// the sender's server is ignored, so ignore this event // the sender's server is ignored, so ignore this event
return true; return Err(Error::BadRequest(
ErrorKind::SenderIgnored { sender: None },
"The sender's server is ignored by this server.",
));
} }
if user_ignored && !services.config.send_messages_from_ignored_users_to_client { if user_ignored && !services.config.send_messages_from_ignored_users_to_client {
// the recipient of this PDU has the sender ignored, and we're not // the recipient of this PDU has the sender ignored, and we're not
// configured to send ignored messages to clients // configured to send ignored messages to clients
return true; return Err(Error::BadRequest(
ErrorKind::SenderIgnored { sender: Some(event.sender().to_owned()) },
"You have ignored this sender.",
));
} }
false Ok(false)
} }
#[inline] #[inline]
+1 -1
View File
@@ -29,7 +29,7 @@ pub(crate) async fn get_room_event_route(
let (mut event, visible) = try_join(event, visible).await?; let (mut event, visible) = try_join(event, visible).await?;
if !visible || is_ignored_pdu(services, &event, body.sender_user()).await { if !visible || is_ignored_pdu(services, &event, body.sender_user()).await? {
return Err!(Request(Forbidden("You don't have permission to view this event."))); return Err!(Request(Forbidden("You don't have permission to view this event.")));
} }