simplify client event endpoint

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-02-06 10:23:17 +00:00 committed by strawberry
parent 565837ad75
commit 31ab84e928
3 changed files with 71 additions and 53 deletions

View file

@ -1,6 +1,6 @@
use axum::extract::State;
use conduwuit::{
at, is_equal_to,
at,
utils::{
result::{FlatOk, LogErr},
stream::{BroadbandExt, TryIgnore, WidebandExt},
@ -30,7 +30,7 @@ use service::{
use crate::Ruma;
/// list of safe and common non-state events to ignore if the user is ignored
const IGNORED_MESSAGE_TYPES: &[TimelineEventType; 17] = &[
const IGNORED_MESSAGE_TYPES: &[TimelineEventType] = &[
Audio,
CallInvite,
Emote,
@ -225,34 +225,50 @@ async fn get_member_event(
.ok()
}
#[inline]
pub(crate) async fn ignored_filter(
services: &Services,
item: PdusIterItem,
user_id: &UserId,
) -> Option<PdusIterItem> {
let (_, pdu) = &item;
let (_, ref pdu) = item;
is_ignored_pdu(services, pdu, user_id)
.await
.eq(&false)
.then_some(item)
}
#[inline]
pub(crate) async fn is_ignored_pdu(
services: &Services,
pdu: &PduEvent,
user_id: &UserId,
) -> bool {
// exclude Synapse's dummy events from bloating up response bodies. clients
// don't need to see this.
if pdu.kind.to_cow_str() == "org.matrix.dummy_event" {
return None;
return true;
}
if IGNORED_MESSAGE_TYPES.binary_search(&pdu.kind).is_ok()
&& (services.users.user_is_ignored(&pdu.sender, user_id).await
|| services
.server
.config
.forbidden_remote_server_names
.iter()
.any(is_equal_to!(pdu.sender().server_name())))
let ignored_type = IGNORED_MESSAGE_TYPES.binary_search(&pdu.kind).is_ok();
let ignored_server = services
.server
.config
.forbidden_remote_server_names
.contains(pdu.sender().server_name());
if ignored_type
&& (ignored_server || services.users.user_is_ignored(&pdu.sender, user_id).await)
{
return None;
return true;
}
Some(item)
false
}
#[inline]
pub(crate) async fn visibility_filter(
services: &Services,
item: PdusIterItem,
@ -268,7 +284,16 @@ pub(crate) async fn visibility_filter(
.then_some(item)
}
#[inline]
pub(crate) fn event_filter(item: PdusIterItem, filter: &RoomEventFilter) -> Option<PdusIterItem> {
let (_, pdu) = &item;
pdu.matches(filter).then_some(item)
}
#[cfg_attr(debug_assertions, conduwuit::ctor)]
fn _is_sorted() {
debug_assert!(
IGNORED_MESSAGE_TYPES.is_sorted(),
"IGNORED_MESSAGE_TYPES must be sorted by the developer"
);
}

View file

@ -1,52 +1,44 @@
use axum::extract::State;
use conduwuit::{err, Err, Event, Result};
use futures::{try_join, FutureExt, TryFutureExt};
use futures::{future::try_join, FutureExt, TryFutureExt};
use ruma::api::client::room::get_room_event;
use crate::{client::ignored_filter, Ruma};
use crate::{client::is_ignored_pdu, Ruma};
/// # `GET /_matrix/client/r0/rooms/{roomId}/event/{eventId}`
///
/// Gets a single event.
pub(crate) async fn get_room_event_route(
State(services): State<crate::State>,
State(ref services): State<crate::State>,
ref body: Ruma<get_room_event::v3::Request>,
) -> Result<get_room_event::v3::Response> {
let event_id = &body.event_id;
let room_id = &body.room_id;
let event = services
.rooms
.timeline
.get_pdu(&body.event_id)
.map_err(|_| err!(Request(NotFound("Event {} not found.", &body.event_id))));
let token = services
.rooms
.timeline
.get_pdu_count(&body.event_id)
.map_err(|_| err!(Request(NotFound("Event not found."))));
.get_pdu(event_id)
.map_err(|_| err!(Request(NotFound("Event {} not found.", event_id))));
let visible = services
.rooms
.state_accessor
.user_can_see_event(body.sender_user(), &body.room_id, &body.event_id)
.user_can_see_event(body.sender_user(), room_id, event_id)
.map(Ok);
let (token, mut event, visible) = try_join!(token, event, visible)?;
let (mut event, visible) = try_join(event, visible).await?;
if !visible
|| ignored_filter(&services, (token, event.clone()), body.sender_user())
.await
.is_none()
{
if !visible || is_ignored_pdu(services, &event, body.sender_user()).await {
return Err!(Request(Forbidden("You don't have permission to view this event.")));
}
if event.event_id() != &body.event_id || event.room_id() != body.room_id {
return Err!(Request(NotFound("Event not found")));
}
debug_assert!(
event.event_id() == event_id && event.room_id() == room_id,
"Fetched PDU must match requested"
);
event.add_age().ok();
let event = event.to_room_event();
Ok(get_room_event::v3::Response { event })
Ok(get_room_event::v3::Response { event: event.to_room_event() })
}