move contains_url_filter helper to utils::filter

The plan is to move all of the per-event checks into the
pdu_event_allowed function.
This commit is contained in:
Benjamin Lee 2024-05-02 18:46:58 -07:00
parent ecb0a55511
commit a11550cdf1
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
2 changed files with 32 additions and 19 deletions

View file

@ -6,13 +6,12 @@ use std::{
use ruma::{
api::client::{
error::ErrorKind,
filter::{RoomEventFilter, UrlFilter},
message::{get_message_events, send_message_event},
},
events::{MessageLikeEventType, StateEventType},
RoomId, UserId,
};
use serde_json::{from_str, Value};
use serde_json::from_str;
use crate::{
service::{pdu::PduBuilder, rooms::timeline::PduCount},
@ -176,7 +175,7 @@ pub(crate) async fn get_message_events_route(
.timeline
.pdus_after(sender_user, &body.room_id, from)?
.filter_map(Result::ok) // Filter out buggy events
.filter(|(_, pdu)| contains_url_filter(pdu, &body.filter))
.filter(|(_, pdu)| filter.pdu_event_allowed(pdu))
.filter(|(_, pdu)| visibility_filter(pdu, sender_user, &body.room_id))
.take_while(|&(k, _)| Some(k) != to) // Stop at `to`
.take(limit)
@ -222,7 +221,7 @@ pub(crate) async fn get_message_events_route(
.timeline
.pdus_until(sender_user, &body.room_id, from)?
.filter_map(Result::ok) // Filter out buggy events
.filter(|(_, pdu)| contains_url_filter(pdu, &body.filter))
.filter(|(_, pdu)| filter.pdu_event_allowed(pdu))
.filter(|(_, pdu)| visibility_filter(pdu, sender_user, &body.room_id))
.take_while(|&(k, _)| Some(k) != to) // Stop at `to`
.take(limit)
@ -291,16 +290,3 @@ fn visibility_filter(pdu: &PduEvent, user_id: &UserId, room_id: &RoomId) -> bool
.user_can_see_event(user_id, room_id, &pdu.event_id)
.unwrap_or(false)
}
fn contains_url_filter(pdu: &PduEvent, filter: &RoomEventFilter) -> bool {
if filter.url_filter.is_none() {
return true;
}
let content: Value = from_str(pdu.content.get()).unwrap();
match filter.url_filter {
Some(UrlFilter::EventsWithoutUrl) => !content["url"].is_string(),
Some(UrlFilter::EventsWithUrl) => content["url"].is_string(),
None => true,
}
}