Upgrade Ruma to present. The following are intentionally benign for activation in a later commit: - Hydra backports not default. - Room version 12 not default. - Room version 12 not listed as stable. Do not enable them manually or you can brick your database. Signed-off-by: Jason Volk <jason@zemos.net>
32 lines
1.1 KiB
Rust
32 lines
1.1 KiB
Rust
use ruma::{CanonicalJsonObject, OwnedEventId, RoomVersionId};
|
|
use serde_json::value::RawValue as RawJsonValue;
|
|
|
|
use crate::{Result, err, matrix::room_version};
|
|
|
|
/// 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 room_version_rules = room_version::rules(room_version_id)?;
|
|
let reference_hash = ruma::signatures::reference_hash(value, &room_version_rules)?;
|
|
|
|
OwnedEventId::from_parts('$', &reference_hash, None).map_err(Into::into)
|
|
}
|