Files
continuwuity/src/core/matrix/pdu.rs
T

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

207 lines
4.9 KiB
Rust
Raw Normal View History

2024-07-03 21:05:24 +00:00
mod builder;
mod count;
2024-11-02 06:12:54 +00:00
mod id;
mod raw_id;
2024-10-25 01:16:01 +00:00
mod redact;
2025-01-08 20:43:02 +00:00
#[cfg(test)]
2024-11-02 06:12:54 +00:00
mod tests;
2024-10-25 01:16:01 +00:00
mod unsigned;
2024-07-03 21:05:24 +00:00
use std::cmp::Ordering;
2024-03-05 19:48:54 -05:00
2020-06-05 18:19:26 +02:00
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId,
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, UInt, UserId, events::TimelineEventType,
2020-09-15 16:13:54 +02:00
};
2020-04-04 11:53:37 +02:00
use serde::{Deserialize, Serialize};
2024-10-25 01:16:01 +00:00
use serde_json::value::RawValue as RawJsonValue;
2020-04-04 11:53:37 +02:00
pub use self::{
Count as PduCount, Id as PduId, Pdu as PduEvent, RawId as RawPduId,
builder::{Builder, Builder as PduBuilder},
2024-11-07 03:30:47 +00:00
count::Count,
2025-04-26 08:24:47 +00:00
id::{ShortId, *},
2024-11-02 06:12:54 +00:00
raw_id::*,
};
2025-04-26 08:24:47 +00:00
use super::{Event, StateKey};
2024-10-25 01:16:01 +00:00
use crate::Result;
2021-10-13 10:16:45 +02:00
2024-10-25 01:16:01 +00:00
/// Persistent Data Unit (Event)
2020-12-22 12:45:35 -05:00
#[derive(Clone, Deserialize, Serialize, Debug)]
2024-11-07 03:30:47 +00:00
pub struct Pdu {
pub event_id: OwnedEventId,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub room_id: OwnedRoomId,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub sender: OwnedUserId,
2025-04-26 08:24:47 +00:00
2024-04-09 02:09:25 -07:00
#[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<OwnedServerName>,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub origin_server_ts: UInt,
2025-04-26 08:24:47 +00:00
2020-04-04 11:53:37 +02:00
#[serde(rename = "type")]
2024-05-09 15:59:08 -07:00
pub kind: TimelineEventType,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub content: Box<RawJsonValue>,
2025-04-26 08:24:47 +00:00
2020-04-04 11:53:37 +02:00
#[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<StateKey>,
2025-04-26 08:24:47 +00:00
pub prev_events: Vec<OwnedEventId>,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub depth: UInt,
2025-04-26 08:24:47 +00:00
pub auth_events: Vec<OwnedEventId>,
2025-04-26 08:24:47 +00:00
2020-04-04 11:53:37 +02:00
#[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<OwnedEventId>,
2025-04-26 08:24:47 +00:00
2021-10-13 10:16:45 +02:00
#[serde(default, skip_serializing_if = "Option::is_none")]
2024-05-09 15:59:08 -07:00
pub unsigned: Option<Box<RawJsonValue>>,
2025-04-26 08:24:47 +00:00
2024-05-09 15:59:08 -07:00
pub hashes: EventHash,
2025-04-26 08:24:47 +00:00
// BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
2025-04-26 08:24:47 +00:00
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<Box<RawJsonValue>>,
2020-04-04 11:53:37 +02:00
}
2024-10-25 01:16:01 +00:00
/// Content hashes of a PDU.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EventHash {
/// The SHA-256 hash.
pub sha256: String,
}
2024-03-05 19:48:54 -05:00
2024-11-07 03:30:47 +00:00
impl Pdu {
pub fn from_id_val(event_id: &EventId, mut json: CanonicalJsonObject) -> Result<Self> {
2024-10-25 01:16:01 +00:00
let event_id = CanonicalJsonValue::String(event_id.into());
json.insert("event_id".into(), event_id);
serde_json::to_value(json)
.and_then(serde_json::from_value)
.map_err(Into::into)
}
}
impl Event for Pdu {
2025-04-26 08:24:47 +00:00
#[inline]
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_ {
2025-04-26 08:24:47 +00:00
self.auth_events.iter().map(AsRef::as_ref)
}
#[inline]
fn content(&self) -> &RawJsonValue { &self.content }
#[inline]
fn event_id(&self) -> &EventId { &self.event_id }
2025-04-26 08:24:47 +00:00
#[inline]
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
MilliSecondsSinceUnixEpoch(self.origin_server_ts)
}
#[inline]
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_ {
2025-04-26 08:24:47 +00:00
self.prev_events.iter().map(AsRef::as_ref)
}
#[inline]
fn redacts(&self) -> Option<&EventId> { self.redacts.as_deref() }
#[inline]
fn room_id(&self) -> &RoomId { &self.room_id }
2025-04-26 08:24:47 +00:00
#[inline]
fn sender(&self) -> &UserId { &self.sender }
2025-04-26 08:24:47 +00:00
#[inline]
fn state_key(&self) -> Option<&str> { self.state_key.as_deref() }
#[inline]
fn kind(&self) -> &TimelineEventType { &self.kind }
#[inline]
fn unsigned(&self) -> Option<&RawJsonValue> { self.unsigned.as_deref() }
#[inline]
fn as_mut_pdu(&mut self) -> &mut Pdu { self }
#[inline]
fn as_pdu(&self) -> &Pdu { self }
#[inline]
fn into_pdu(self) -> Pdu { self }
2025-04-26 08:24:47 +00:00
#[inline]
fn is_owned(&self) -> bool { true }
}
2025-04-26 08:24:47 +00:00
impl Event for &Pdu {
#[inline]
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_ {
2025-04-26 08:24:47 +00:00
self.auth_events.iter().map(AsRef::as_ref)
}
#[inline]
fn content(&self) -> &RawJsonValue { &self.content }
2025-04-26 08:24:47 +00:00
#[inline]
fn event_id(&self) -> &EventId { &self.event_id }
#[inline]
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
MilliSecondsSinceUnixEpoch(self.origin_server_ts)
}
2025-04-26 08:24:47 +00:00
#[inline]
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_ {
self.prev_events.iter().map(AsRef::as_ref)
}
2025-04-26 08:24:47 +00:00
#[inline]
fn redacts(&self) -> Option<&EventId> { self.redacts.as_deref() }
2025-04-26 08:24:47 +00:00
#[inline]
fn room_id(&self) -> &RoomId { &self.room_id }
#[inline]
fn sender(&self) -> &UserId { &self.sender }
#[inline]
fn state_key(&self) -> Option<&str> { self.state_key.as_deref() }
#[inline]
fn kind(&self) -> &TimelineEventType { &self.kind }
#[inline]
fn unsigned(&self) -> Option<&RawJsonValue> { self.unsigned.as_deref() }
#[inline]
fn as_pdu(&self) -> &Pdu { self }
#[inline]
fn into_pdu(self) -> Pdu { self.clone() }
2025-04-26 08:24:47 +00:00
#[inline]
fn is_owned(&self) -> bool { false }
}
2024-10-25 01:16:01 +00:00
/// Prevent derived equality which wouldn't limit itself to event_id
2024-11-07 03:30:47 +00:00
impl Eq for Pdu {}
2024-10-25 01:16:01 +00:00
/// Equality determined by the Pdu's ID, not the memory representations.
2024-11-07 03:30:47 +00:00
impl PartialEq for Pdu {
2021-01-18 19:08:59 -05:00
fn eq(&self, other: &Self) -> bool { self.event_id == other.event_id }
}
2024-10-25 01:16:01 +00:00
/// Ordering determined by the Pdu's ID, not the memory representations.
impl Ord for Pdu {
fn cmp(&self, other: &Self) -> Ordering { self.event_id.cmp(&other.event_id) }
2021-01-18 19:08:59 -05:00
}
2024-10-25 01:16:01 +00:00
/// Ordering determined by the Pdu's ID, not the memory representations.
impl PartialOrd for Pdu {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}