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

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

88 lines
2.5 KiB
Rust
Raw Normal View History

2024-07-03 21:05:24 +00:00
mod builder;
2024-10-25 01:16:01 +00:00
mod content;
2024-07-03 21:05:24 +00:00
mod count;
2024-10-27 20:53:22 +00:00
mod event;
2024-11-04 18:20:32 +00:00
mod event_id;
2024-10-25 02:56:24 +00:00
mod filter;
2024-10-25 01:16:01 +00:00
mod redact;
mod strip;
mod unsigned;
2024-07-03 21:05:24 +00:00
2024-10-25 01:16:01 +00:00
use std::{cmp::Ordering, sync::Arc};
2024-03-05 19:48:54 -05:00
2020-06-05 18:19:26 +02:00
use ruma::{
2024-10-25 01:16:01 +00:00
events::TimelineEventType, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedRoomId, OwnedUserId, UInt,
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::{
builder::{Builder, Builder as PduBuilder},
count::PduCount,
2024-10-27 20:53:22 +00:00
event::Event,
2024-11-04 18:20:32 +00:00
event_id::*,
};
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-05-09 15:59:08 -07:00
pub struct PduEvent {
pub event_id: Arc<EventId>,
pub room_id: OwnedRoomId,
pub sender: OwnedUserId,
2024-04-09 02:09:25 -07:00
#[serde(skip_serializing_if = "Option::is_none")]
2024-05-09 15:59:08 -07:00
pub origin: Option<String>,
pub origin_server_ts: UInt,
2020-04-04 11:53:37 +02:00
#[serde(rename = "type")]
2024-05-09 15:59:08 -07:00
pub kind: TimelineEventType,
pub content: Box<RawJsonValue>,
2020-04-04 11:53:37 +02:00
#[serde(skip_serializing_if = "Option::is_none")]
2024-05-09 15:59:08 -07:00
pub state_key: Option<String>,
pub prev_events: Vec<Arc<EventId>>,
pub depth: UInt,
pub auth_events: Vec<Arc<EventId>>,
2020-04-04 11:53:37 +02:00
#[serde(skip_serializing_if = "Option::is_none")]
2024-05-09 15:59:08 -07:00
pub redacts: Option<Arc<EventId>>,
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>>,
pub hashes: EventHash,
2021-10-13 10:16:45 +02:00
#[serde(default, skip_serializing_if = "Option::is_none")]
// BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
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-10-25 01:16:01 +00:00
impl PduEvent {
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)
}
}
2024-10-25 01:16:01 +00:00
/// Prevent derived equality which wouldn't limit itself to event_id
2021-01-18 19:08:59 -05:00
impl Eq for PduEvent {}
2024-10-25 01:16:01 +00:00
/// Equality determined by the Pdu's ID, not the memory representations.
2021-01-18 19:08:59 -05:00
impl PartialEq for PduEvent {
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.
2021-01-18 19:08:59 -05:00
impl PartialOrd for PduEvent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}
2024-10-25 01:16:01 +00:00
/// Ordering determined by the Pdu's ID, not the memory representations.
2021-01-18 19:08:59 -05:00
impl Ord for PduEvent {
fn cmp(&self, other: &Self) -> Ordering { self.event_id.cmp(&other.event_id) }
}