respect filter.limit in the /messages endpoint

As far as I can tell, 'filter.limit' and the 'limit' query parameter are
completely redundant. I've moved the 'take(limit)' call until after
filtering, to ensure that we can return up to 'limit' events even when
some are rejected by the filter. In a future commit, I will add a global
limit on loaded events to avoid DoS.
This commit is contained in:
Benjamin Lee 2024-05-02 18:30:39 -07:00
parent 404d5fae6c
commit 93ad93a36b
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4

View file

@ -9,7 +9,7 @@ use ruma::{
message::{get_message_events, send_message_event},
},
events::{StateEventType, TimelineEventType},
uint,
uint, UInt,
};
use crate::{
@ -177,6 +177,7 @@ pub(crate) async fn get_message_events_route(
let limit = body
.limit
.min(body.filter.limit.unwrap_or(UInt::MAX))
.min(uint!(100))
.try_into()
.expect("0-100 should fit in usize");
@ -193,7 +194,6 @@ pub(crate) async fn get_message_events_route(
.rooms
.timeline
.pdus_after(sender_user, &body.room_id, from)?
.take(limit)
.filter_map(Result::ok)
.filter(|(_, pdu)| {
services()
@ -207,6 +207,7 @@ pub(crate) async fn get_message_events_route(
.unwrap_or(false)
})
.take_while(|&(k, _)| Some(k) != to)
.take(limit)
.collect();
for (_, event) in &events_after {
@ -249,7 +250,6 @@ pub(crate) async fn get_message_events_route(
.rooms
.timeline
.pdus_until(sender_user, &body.room_id, from)?
.take(limit)
.filter_map(Result::ok)
.filter(|(_, pdu)| {
services()
@ -263,6 +263,7 @@ pub(crate) async fn get_message_events_route(
.unwrap_or(false)
})
.take_while(|&(k, _)| Some(k) != to)
.take(limit)
.collect();
for (_, event) in &events_before {