mirror of
https://github.com/element-hq/synapse.git
synced 2026-05-24 15:15:22 +00:00
Small refactor to ensure we call 'validate' for all formats
This commit is contained in:
@@ -68,6 +68,7 @@
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use anyhow::Error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::events::{json_object::JsonObject, signatures::Signatures, unsigned::Unsigned};
|
||||
@@ -128,6 +129,15 @@ impl FormattedEvent {
|
||||
common_fields: Arc::clone(&self.common_fields),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> Result<(), Error> {
|
||||
match &*self.specific_fields {
|
||||
EventFormatEnum::V1(format) => format.validate(&self.common_fields),
|
||||
EventFormatEnum::V2V3(format) => format.validate(&self.common_fields),
|
||||
EventFormatEnum::V4(format) => format.validate(&self.common_fields),
|
||||
EventFormatEnum::VMSC4242(format) => format.validate(&self.common_fields),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> FormattedEvent<E>
|
||||
|
||||
@@ -25,8 +25,11 @@
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use anyhow::Error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::events::formats::EventCommonFields;
|
||||
|
||||
/// Version-specific fields for room versions 1 and 2.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EventFormatV1 {
|
||||
@@ -37,6 +40,10 @@ pub struct EventFormatV1 {
|
||||
}
|
||||
|
||||
impl EventFormatV1 {
|
||||
pub fn validate(&self, _common_fields: &EventCommonFields) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn auth_event_ids(&self) -> Vec<String> {
|
||||
self.auth_events.iter().map(|(id, _)| id.clone()).collect()
|
||||
}
|
||||
|
||||
@@ -525,23 +525,14 @@ fn depythonize_event_dict(
|
||||
}
|
||||
EventFormatVersions::ROOM_V3 | EventFormatVersions::ROOM_V4_PLUS => {
|
||||
let event_format: FormattedEvent<EventFormatV2V3> = depythonize(event_dict)?;
|
||||
event_format
|
||||
.specific_fields
|
||||
.validate(&event_format.common_fields)?;
|
||||
event_format.into()
|
||||
}
|
||||
EventFormatVersions::ROOM_V11_HYDRA_PLUS => {
|
||||
let event_format: FormattedEvent<EventFormatV4> = depythonize(event_dict)?;
|
||||
event_format
|
||||
.specific_fields
|
||||
.validate(&event_format.common_fields)?;
|
||||
event_format.into()
|
||||
}
|
||||
EventFormatVersions::ROOM_VMSC4242 => {
|
||||
let event_format: FormattedEvent<EventFormatVMSC4242> = depythonize(event_dict)?;
|
||||
event_format
|
||||
.specific_fields
|
||||
.validate(&event_format.common_fields)?;
|
||||
event_format.into()
|
||||
}
|
||||
_ => {
|
||||
@@ -552,6 +543,8 @@ fn depythonize_event_dict(
|
||||
}
|
||||
};
|
||||
|
||||
formatted_event.validate()?;
|
||||
|
||||
Ok(formatted_event)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user