reorg PduEvent strip tools and callsites

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-03-30 21:19:47 +00:00
parent dc6e9e74d9
commit bee4c6255a
13 changed files with 152 additions and 71 deletions

View file

@ -182,7 +182,7 @@ pub(crate) async fn get_context_route(
.await;
Ok(get_context::v3::Response {
event: base_event.map(at!(1)).as_ref().map(PduEvent::to_room_event),
event: base_event.map(at!(1)).map(PduEvent::into_room_event),
start: events_before
.last()
@ -201,13 +201,13 @@ pub(crate) async fn get_context_route(
events_before: events_before
.into_iter()
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.collect(),
events_after: events_after
.into_iter()
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.collect(),
state,

View file

@ -157,7 +157,7 @@ pub(crate) async fn get_message_events_route(
let chunk = events
.into_iter()
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.collect();
Ok(get_message_events::v3::Response {

View file

@ -40,5 +40,5 @@ pub(crate) async fn get_room_event_route(
event.add_age().ok();
Ok(get_room_event::v3::Response { event: event.to_room_event() })
Ok(get_room_event::v3::Response { event: event.into_room_event() })
}

View file

@ -55,7 +55,7 @@ pub(crate) async fn room_initial_sync_route(
chunk: events
.into_iter()
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.collect(),
};

View file

@ -143,7 +143,7 @@ async fn category_room_events(
.map(at!(2))
.flatten()
.stream()
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.map(|result| SearchResult {
rank: None,
result: Some(result),

View file

@ -461,7 +461,7 @@ async fn handle_left_room(
events: Vec::new(),
},
state: RoomState {
events: vec![event.to_sync_state_event()],
events: vec![event.into_sync_state_event()],
},
}));
}
@ -546,7 +546,7 @@ async fn handle_left_room(
continue;
}
left_state_events.push(pdu.to_sync_state_event());
left_state_events.push(pdu.into_sync_state_event());
}
}
@ -865,8 +865,8 @@ async fn load_joined_room(
},
state: RoomState {
events: state_events
.iter()
.map(PduEvent::to_sync_state_event)
.into_iter()
.map(PduEvent::into_sync_state_event)
.collect(),
},
ephemeral: Ephemeral { events: edus },

View file

@ -6,7 +6,7 @@ use std::{
use axum::extract::State;
use conduwuit::{
Error, PduCount, Result, debug, error, extract_variant,
Error, PduCount, PduEvent, Result, debug, error, extract_variant,
utils::{
BoolExt, IterStream, ReadyExt, TryFutureExtExt,
math::{ruma_from_usize, usize_from_ruma, usize_from_u64_truncated},
@ -634,7 +634,7 @@ pub(crate) async fn sync_events_v4_route(
.state_accessor
.room_state_get(room_id, &state.0, &state.1)
.await
.map(|s| s.to_sync_state_event())
.map(PduEvent::into_sync_state_event)
.ok()
})
.collect()

View file

@ -6,7 +6,7 @@ use std::{
use axum::extract::State;
use conduwuit::{
Error, Result, TypeStateKey, debug, error, extract_variant, trace,
Error, PduEvent, Result, TypeStateKey, debug, error, extract_variant, trace,
utils::{
BoolExt, IterStream, ReadyExt, TryFutureExtExt,
math::{ruma_from_usize, usize_from_ruma},
@ -507,7 +507,7 @@ async fn process_rooms(
.state_accessor
.room_state_get(room_id, &state.0, &state.1)
.await
.map(|s| s.to_sync_state_event())
.map(PduEvent::into_sync_state_event)
.ok()
})
.collect()

View file

@ -53,7 +53,7 @@ pub(crate) async fn get_threads_route(
chunk: threads
.into_iter()
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.map(PduEvent::into_room_event)
.collect(),
})
}

View file

@ -10,35 +10,18 @@ use serde_json::{json, value::Value as JsonValue};
use crate::implement;
#[must_use]
#[implement(super::Pdu)]
pub fn to_sync_room_event(&self) -> Raw<AnySyncTimelineEvent> {
let (redacts, content) = self.copy_redacts();
let mut json = json!({
"content": content,
"type": self.kind,
"event_id": self.event_id,
"sender": self.sender,
"origin_server_ts": self.origin_server_ts,
});
if let Some(unsigned) = &self.unsigned {
json["unsigned"] = json!(unsigned);
}
if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key);
}
if let Some(redacts) = &redacts {
json["redacts"] = json!(redacts);
}
serde_json::from_value(json).expect("Raw::from_value always works")
}
/// This only works for events that are also AnyRoomEvents.
#[must_use]
#[implement(super::Pdu)]
pub fn to_any_event(&self) -> Raw<AnyEphemeralRoomEvent> {
pub fn into_any_event(self) -> Raw<AnyEphemeralRoomEvent> {
serde_json::from_value(self.into_any_event_value()).expect("Raw::from_value always works")
}
/// This only works for events that are also AnyRoomEvents.
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_any_event_value(self) -> JsonValue {
let (redacts, content) = self.copy_redacts();
let mut json = json!({
"content": content,
@ -59,12 +42,24 @@ pub fn to_any_event(&self) -> Raw<AnyEphemeralRoomEvent> {
json["redacts"] = json!(redacts);
}
serde_json::from_value(json).expect("Raw::from_value always works")
json
}
#[must_use]
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_room_event(self) -> Raw<AnyTimelineEvent> { self.to_room_event() }
#[implement(super::Pdu)]
#[must_use]
pub fn to_room_event(&self) -> Raw<AnyTimelineEvent> {
serde_json::from_value(self.to_room_event_value()).expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn to_room_event_value(&self) -> JsonValue {
let (redacts, content) = self.copy_redacts();
let mut json = json!({
"content": content,
@ -85,12 +80,25 @@ pub fn to_room_event(&self) -> Raw<AnyTimelineEvent> {
json["redacts"] = json!(redacts);
}
serde_json::from_value(json).expect("Raw::from_value always works")
json
}
#[must_use]
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_message_like_event(self) -> Raw<AnyMessageLikeEvent> { self.to_message_like_event() }
#[implement(super::Pdu)]
#[must_use]
pub fn to_message_like_event(&self) -> Raw<AnyMessageLikeEvent> {
serde_json::from_value(self.to_message_like_event_value())
.expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn to_message_like_event_value(&self) -> JsonValue {
let (redacts, content) = self.copy_redacts();
let mut json = json!({
"content": content,
@ -111,11 +119,55 @@ pub fn to_message_like_event(&self) -> Raw<AnyMessageLikeEvent> {
json["redacts"] = json!(redacts);
}
serde_json::from_value(json).expect("Raw::from_value always works")
json
}
#[must_use]
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_sync_room_event(self) -> Raw<AnySyncTimelineEvent> { self.to_sync_room_event() }
#[implement(super::Pdu)]
#[must_use]
pub fn to_sync_room_event(&self) -> Raw<AnySyncTimelineEvent> {
serde_json::from_value(self.to_sync_room_event_value()).expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn to_sync_room_event_value(&self) -> JsonValue {
let (redacts, content) = self.copy_redacts();
let mut json = json!({
"content": content,
"type": self.kind,
"event_id": self.event_id,
"sender": self.sender,
"origin_server_ts": self.origin_server_ts,
});
if let Some(unsigned) = &self.unsigned {
json["unsigned"] = json!(unsigned);
}
if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key);
}
if let Some(redacts) = &redacts {
json["redacts"] = json!(redacts);
}
json
}
#[implement(super::Pdu)]
#[must_use]
pub fn into_state_event(self) -> Raw<AnyStateEvent> {
serde_json::from_value(self.into_state_event_value()).expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_state_event_value(self) -> JsonValue {
let mut json = json!({
"content": self.content,
@ -134,15 +186,17 @@ pub fn into_state_event_value(self) -> JsonValue {
json
}
#[must_use]
#[implement(super::Pdu)]
pub fn into_state_event(self) -> Raw<AnyStateEvent> {
serde_json::from_value(self.into_state_event_value()).expect("Raw::from_value always works")
#[must_use]
pub fn into_sync_state_event(self) -> Raw<AnySyncStateEvent> {
serde_json::from_value(self.into_sync_state_event_value())
.expect("Raw::from_value always works")
}
#[must_use]
#[implement(super::Pdu)]
pub fn to_sync_state_event(&self) -> Raw<AnySyncStateEvent> {
#[must_use]
#[inline]
pub fn into_sync_state_event_value(self) -> JsonValue {
let mut json = json!({
"content": self.content,
"type": self.kind,
@ -156,39 +210,65 @@ pub fn to_sync_state_event(&self) -> Raw<AnySyncStateEvent> {
json["unsigned"] = json!(unsigned);
}
serde_json::from_value(json).expect("Raw::from_value always works")
json
}
#[must_use]
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_stripped_state_event(self) -> Raw<AnyStrippedStateEvent> {
self.to_stripped_state_event()
}
#[implement(super::Pdu)]
#[must_use]
pub fn to_stripped_state_event(&self) -> Raw<AnyStrippedStateEvent> {
let json = json!({
serde_json::from_value(self.to_stripped_state_event_value())
.expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn to_stripped_state_event_value(&self) -> JsonValue {
json!({
"content": self.content,
"type": self.kind,
"sender": self.sender,
"state_key": self.state_key,
});
serde_json::from_value(json).expect("Raw::from_value always works")
})
}
#[must_use]
#[implement(super::Pdu)]
pub fn to_stripped_spacechild_state_event(&self) -> Raw<HierarchySpaceChildEvent> {
let json = json!({
#[must_use]
pub fn into_stripped_spacechild_state_event(self) -> Raw<HierarchySpaceChildEvent> {
serde_json::from_value(self.into_stripped_spacechild_state_event_value())
.expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_stripped_spacechild_state_event_value(self) -> JsonValue {
json!({
"content": self.content,
"type": self.kind,
"sender": self.sender,
"state_key": self.state_key,
"origin_server_ts": self.origin_server_ts,
});
serde_json::from_value(json).expect("Raw::from_value always works")
})
}
#[must_use]
#[implement(super::Pdu)]
#[must_use]
pub fn into_member_event(self) -> Raw<StateEvent<RoomMemberEventContent>> {
serde_json::from_value(self.into_member_event_value()).expect("Raw::from_value always works")
}
#[implement(super::Pdu)]
#[must_use]
#[inline]
pub fn into_member_event_value(self) -> JsonValue {
let mut json = json!({
"content": self.content,
"type": self.kind,
@ -204,5 +284,5 @@ pub fn into_member_event(self) -> Raw<StateEvent<RoomMemberEventContent>> {
json["unsigned"] = json!(unsigned);
}
serde_json::from_value(json).expect("Raw::from_value always works")
json
}

View file

@ -6,7 +6,7 @@ use std::{fmt::Write, sync::Arc};
use async_trait::async_trait;
use conduwuit::{
Err, Error, Result, implement,
Err, Error, PduEvent, Result, implement,
utils::{
IterStream,
future::BoolExt,
@ -267,11 +267,12 @@ fn get_stripped_space_child_events<'a>(
}
if RoomId::parse(&state_key).is_ok() {
return Some(pdu.to_stripped_spacechild_state_event());
return Some(pdu);
}
None
})
.map(PduEvent::into_stripped_spacechild_state_event)
}
/// Gets the summary of a space using either local or remote (federation)

View file

@ -341,7 +341,7 @@ impl Service {
.await
.into_iter()
.filter_map(Result::ok)
.map(|e| e.to_stripped_state_event())
.map(PduEvent::into_stripped_state_event)
.chain(once(event.to_stripped_state_event()))
.collect()
}

View file

@ -697,7 +697,7 @@ impl Service {
match event {
| SendingEvent::Pdu(pdu_id) => {
if let Ok(pdu) = self.services.timeline.get_pdu_from_id(pdu_id).await {
pdu_jsons.push(pdu.to_room_event());
pdu_jsons.push(pdu.into_room_event());
}
},
| SendingEvent::Edu(edu) =>