Files
continuwuity/src/core/matrix/event/id.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
1.0 KiB
Rust
Raw Normal View History

use ruma::{CanonicalJsonObject, OwnedEventId, room_version_rules::RoomVersionRules};
2024-10-25 01:16:01 +00:00
use serde_json::value::RawValue as RawJsonValue;
use crate::{Err, Result, err};
2024-10-25 01:16:01 +00:00
/// 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_rules: &RoomVersionRules,
2024-10-25 01:16:01 +00:00
) -> 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_rules)?;
2024-10-25 01:16:01 +00:00
Ok((event_id, value))
}
/// Generates a correct eventId for the incoming pdu.
pub fn gen_event_id(
value: &CanonicalJsonObject,
room_version_rules: &RoomVersionRules,
) -> Result<OwnedEventId> {
let reference_hash = ruma::signatures::reference_hash(value, room_version_rules)?;
2024-10-25 01:16:01 +00:00
let event_id: OwnedEventId = format!("${reference_hash}").try_into()?;
Ok(event_id)
}