feat(pdu): copy top level redact to content and vice versa

This commit is contained in:
Matthias Ahouansou 2024-04-16 22:45:04 +01:00 committed by Valentin Lorentz
parent 92817213d5
commit 7a7c09785e
4 changed files with 41 additions and 23 deletions

View file

@ -671,7 +671,7 @@ pub async fn upgrade_room_route(
// "creator" key no longer exists in V11 rooms // "creator" key no longer exists in V11 rooms
create_event_content.remove("creator"); create_event_content.remove("creator");
} }
_ => unreachable!("Validity of room version already checked") _ => unreachable!("Validity of room version already checked"),
} }
create_event_content.insert( create_event_content.insert(
"room_version".into(), "room_version".into(),

View file

@ -181,11 +181,7 @@ impl Service {
RoomVersionId::V11, RoomVersionId::V11,
]; ];
// Experimental, partially supported room versions // Experimental, partially supported room versions
let unstable_room_versions = vec![ let unstable_room_versions = vec![RoomVersionId::V3, RoomVersionId::V4, RoomVersionId::V5];
RoomVersionId::V3,
RoomVersionId::V4,
RoomVersionId::V5,
];
let mut s = Self { let mut s = Self {
db, db,

View file

@ -2,7 +2,8 @@ use crate::Error;
use ruma::{ use ruma::{
canonical_json::redact_content_in_place, canonical_json::redact_content_in_place,
events::{ events::{
room::member::RoomMemberEventContent, space::child::HierarchySpaceChildEvent, room::{member::RoomMemberEventContent, redaction::RoomRedactionEventContent},
space::child::HierarchySpaceChildEvent,
AnyEphemeralRoomEvent, AnyMessageLikeEvent, AnyStateEvent, AnyStrippedStateEvent, AnyEphemeralRoomEvent, AnyMessageLikeEvent, AnyStateEvent, AnyStrippedStateEvent,
AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, StateEvent, TimelineEventType, AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, StateEvent, TimelineEventType,
}, },
@ -25,7 +26,7 @@ pub struct EventHash {
pub sha256: String, pub sha256: String,
} }
#[derive(Clone, Deserialize, Serialize, Debug)] #[derive(Clone, Deserialize, Debug, Serialize)]
pub struct PduEvent { pub struct PduEvent {
pub event_id: Arc<EventId>, pub event_id: Arc<EventId>,
pub room_id: OwnedRoomId, pub room_id: OwnedRoomId,
@ -96,10 +97,31 @@ impl PduEvent {
Ok(()) Ok(())
} }
pub fn copy_redacts(&self) -> (Option<Arc<EventId>>, Box<RawJsonValue>) {
if self.kind == TimelineEventType::RoomRedaction {
if let Ok(mut content) =
serde_json::from_str::<RoomRedactionEventContent>(self.content.get())
{
if let Some(redacts) = content.redacts {
return (Some(redacts.into()), self.content.clone());
} else if let Some(redacts) = self.redacts.clone() {
content.redacts = Some(redacts.into());
return (
self.redacts.clone(),
to_raw_value(&content).expect("Must be valid, we only added redacts field"),
);
}
}
}
(self.redacts.clone(), self.content.clone())
}
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn to_sync_room_event(&self) -> Raw<AnySyncTimelineEvent> { pub fn to_sync_room_event(&self) -> Raw<AnySyncTimelineEvent> {
let (redacts, content) = self.copy_redacts();
let mut json = json!({ let mut json = json!({
"content": self.content, "content": content,
"type": self.kind, "type": self.kind,
"event_id": self.event_id, "event_id": self.event_id,
"sender": self.sender, "sender": self.sender,
@ -112,7 +134,7 @@ impl PduEvent {
if let Some(state_key) = &self.state_key { if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key); json["state_key"] = json!(state_key);
} }
if let Some(redacts) = &self.redacts { if let Some(redacts) = &redacts {
json["redacts"] = json!(redacts); json["redacts"] = json!(redacts);
} }
@ -146,8 +168,9 @@ impl PduEvent {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn to_room_event(&self) -> Raw<AnyTimelineEvent> { pub fn to_room_event(&self) -> Raw<AnyTimelineEvent> {
let (redacts, content) = self.copy_redacts();
let mut json = json!({ let mut json = json!({
"content": self.content, "content": content,
"type": self.kind, "type": self.kind,
"event_id": self.event_id, "event_id": self.event_id,
"sender": self.sender, "sender": self.sender,
@ -161,7 +184,7 @@ impl PduEvent {
if let Some(state_key) = &self.state_key { if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key); json["state_key"] = json!(state_key);
} }
if let Some(redacts) = &self.redacts { if let Some(redacts) = &redacts {
json["redacts"] = json!(redacts); json["redacts"] = json!(redacts);
} }
@ -170,8 +193,9 @@ impl PduEvent {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn to_message_like_event(&self) -> Raw<AnyMessageLikeEvent> { pub fn to_message_like_event(&self) -> Raw<AnyMessageLikeEvent> {
let (redacts, content) = self.copy_redacts();
let mut json = json!({ let mut json = json!({
"content": self.content, "content": content,
"type": self.kind, "type": self.kind,
"event_id": self.event_id, "event_id": self.event_id,
"sender": self.sender, "sender": self.sender,
@ -185,7 +209,7 @@ impl PduEvent {
if let Some(state_key) = &self.state_key { if let Some(state_key) = &self.state_key {
json["state_key"] = json!(state_key); json["state_key"] = json!(state_key);
} }
if let Some(redacts) = &self.redacts { if let Some(redacts) = &redacts {
json["redacts"] = json!(redacts); json["redacts"] = json!(redacts);
} }

View file

@ -399,15 +399,16 @@ impl Service {
} }
} }
RoomVersionId::V11 => { RoomVersionId::V11 => {
let content = serde_json::from_str::<RoomRedactionEventContent>(pdu.content.get()) let content =
.map_err(|_| { serde_json::from_str::<RoomRedactionEventContent>(pdu.content.get())
Error::bad_database("Invalid content in redaction pdu.") .map_err(|_| {
})?; Error::bad_database("Invalid content in redaction pdu.")
})?;
if let Some(redact_id) = &content.redacts { if let Some(redact_id) = &content.redacts {
self.redact_pdu(redact_id, pdu)?; self.redact_pdu(redact_id, pdu)?;
} }
} }
_ => unreachable!("Validity of room version already checked") _ => unreachable!("Validity of room version already checked"),
}; };
} }
TimelineEventType::SpaceChild => { TimelineEventType::SpaceChild => {
@ -1015,10 +1016,7 @@ impl Service {
let mut pdu = self let mut pdu = self
.get_pdu_from_id(&pdu_id)? .get_pdu_from_id(&pdu_id)?
.ok_or_else(|| Error::bad_database("PDU ID points to invalid PDU."))?; .ok_or_else(|| Error::bad_database("PDU ID points to invalid PDU."))?;
let room_version_id = services() let room_version_id = services().rooms.state.get_room_version(&pdu.room_id)?;
.rooms
.state
.get_room_version(&pdu.room_id)?;
pdu.redact(room_version_id, reason)?; pdu.redact(room_version_id, reason)?;
self.replace_pdu( self.replace_pdu(
&pdu_id, &pdu_id,