mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-30 12:27:33 +00:00
Abstract Pdu filter matching into trait Event.
Abstract Pdu unsigned accessors into trait Event. Abstract Pdu relation related into trait Event. Abstract PDU content into trait Event. Move event_id utils from pdu to event. Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
1 parent
222e89f6fe
commit
af7dfb31bc
56 files changed
+666
-492
No files matched your search
@@ -0,0 +1,101 @@
|
||||
use ruma::api::client::filter::{RoomEventFilter, UrlFilter};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::Event;
|
||||
use crate::is_equal_to;
|
||||
|
||||
pub trait Matches<E: Event> {
|
||||
fn matches(&self, event: &E) -> bool;
|
||||
}
|
||||
|
||||
impl<E: Event> Matches<E> for &RoomEventFilter {
|
||||
#[inline]
|
||||
fn matches(&self, event: &E) -> bool {
|
||||
if !matches_sender(event, self) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !matches_room(event, self) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !matches_type(event, self) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !matches_url(event, self) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn matches_room<E: Event>(event: &E, filter: &RoomEventFilter) -> bool {
|
||||
if filter
|
||||
.not_rooms
|
||||
.iter()
|
||||
.any(is_equal_to!(event.room_id()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(rooms) = filter.rooms.as_ref() {
|
||||
if !rooms.iter().any(is_equal_to!(event.room_id())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn matches_sender<E: Event>(event: &E, filter: &RoomEventFilter) -> bool {
|
||||
if filter
|
||||
.not_senders
|
||||
.iter()
|
||||
.any(is_equal_to!(event.sender()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(senders) = filter.senders.as_ref() {
|
||||
if !senders.iter().any(is_equal_to!(event.sender())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn matches_type<E: Event>(event: &E, filter: &RoomEventFilter) -> bool {
|
||||
let kind = event.kind().to_cow_str();
|
||||
|
||||
if filter.not_types.iter().any(is_equal_to!(&kind)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(types) = filter.types.as_ref() {
|
||||
if !types.iter().any(is_equal_to!(&kind)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn matches_url<E: Event>(event: &E, filter: &RoomEventFilter) -> bool {
|
||||
let Some(url_filter) = filter.url_filter.as_ref() else {
|
||||
return true;
|
||||
};
|
||||
|
||||
//TODO: might be better to use Ruma's Raw rather than serde here
|
||||
let url = event
|
||||
.get_content_as_value()
|
||||
.get("url")
|
||||
.is_some_and(Value::is_string);
|
||||
|
||||
match url_filter {
|
||||
| UrlFilter::EventsWithUrl => url,
|
||||
| UrlFilter::EventsWithoutUrl => !url,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use ruma::{CanonicalJsonObject, OwnedEventId, RoomVersionId};
|
||||
use serde_json::value::RawValue as RawJsonValue;
|
||||
|
||||
use crate::{Result, err};
|
||||
|
||||
/// Generates a correct eventId for the incoming pdu.
|
||||
///
|
||||
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String,
|
||||
/// CanonicalJsonValue>`.
|
||||
pub fn gen_event_id_canonical_json(
|
||||
pdu: &RawJsonValue,
|
||||
room_version_id: &RoomVersionId,
|
||||
) -> Result<(OwnedEventId, CanonicalJsonObject)> {
|
||||
let value: CanonicalJsonObject = serde_json::from_str(pdu.get())
|
||||
.map_err(|e| err!(BadServerResponse(warn!("Error parsing incoming event: {e:?}"))))?;
|
||||
|
||||
let event_id = gen_event_id(&value, room_version_id)?;
|
||||
|
||||
Ok((event_id, value))
|
||||
}
|
||||
|
||||
/// Generates a correct eventId for the incoming pdu.
|
||||
pub fn gen_event_id(
|
||||
value: &CanonicalJsonObject,
|
||||
room_version_id: &RoomVersionId,
|
||||
) -> Result<OwnedEventId> {
|
||||
let reference_hash = ruma::signatures::reference_hash(value, room_version_id)?;
|
||||
let event_id: OwnedEventId = format!("${reference_hash}").try_into()?;
|
||||
|
||||
Ok(event_id)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use ruma::events::relation::RelationType;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::Event;
|
||||
|
||||
pub trait RelationTypeEqual<E: Event> {
|
||||
fn relation_type_equal(&self, event: &E) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct ExtractRelatesToEventId {
|
||||
#[serde(rename = "m.relates_to")]
|
||||
relates_to: ExtractRelType,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct ExtractRelType {
|
||||
rel_type: RelationType,
|
||||
}
|
||||
|
||||
impl<E: Event> RelationTypeEqual<E> for RelationType {
|
||||
fn relation_type_equal(&self, event: &E) -> bool {
|
||||
event
|
||||
.get_content()
|
||||
.map(|c: ExtractRelatesToEventId| c.relates_to.rel_type)
|
||||
.is_ok_and(|r| r == *self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use serde::Deserialize;
|
||||
use serde_json::value::Value as JsonValue;
|
||||
|
||||
use super::Event;
|
||||
use crate::{Result, err, is_true};
|
||||
|
||||
pub(super) fn contains_unsigned_property<F, E>(event: &E, property: &str, is_type: F) -> bool
|
||||
where
|
||||
F: FnOnce(&JsonValue) -> bool,
|
||||
E: Event,
|
||||
{
|
||||
get_unsigned_as_value(event)
|
||||
.get(property)
|
||||
.map(is_type)
|
||||
.is_some_and(is_true!())
|
||||
}
|
||||
|
||||
pub(super) fn get_unsigned_property<T, E>(event: &E, property: &str) -> Result<T>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
E: Event,
|
||||
{
|
||||
get_unsigned_as_value(event)
|
||||
.get_mut(property)
|
||||
.map(JsonValue::take)
|
||||
.map(serde_json::from_value)
|
||||
.ok_or(err!(Request(NotFound("property not found in unsigned object"))))?
|
||||
.map_err(|e| err!(Database("Failed to deserialize unsigned.{property} into type: {e}")))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub(super) fn get_unsigned_as_value<E>(event: &E) -> JsonValue
|
||||
where
|
||||
E: Event,
|
||||
{
|
||||
get_unsigned::<JsonValue, E>(event).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub(super) fn get_unsigned<T, E>(event: &E) -> Result<T>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
E: Event,
|
||||
{
|
||||
event
|
||||
.unsigned()
|
||||
.as_ref()
|
||||
.map(|raw| raw.get())
|
||||
.map(serde_json::from_str)
|
||||
.ok_or(err!(Request(NotFound("\"unsigned\" property not found in pdu"))))?
|
||||
.map_err(|e| err!(Database("Failed to deserialize \"unsigned\" into value: {e}")))
|
||||
}
|
||||
Reference in new issue
Block a user