1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-29 19:28:31 +00:00
continuwuity/src/core/pdu/mod.rs

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

94 lines
2.5 KiB
Rust
Raw Normal View History

mod builder;
mod content;
mod count;
mod event;
mod event_id;
mod filter;
mod id;
mod raw_id;
mod redact;
mod relation;
mod strip;
mod tests;
mod unsigned;
use std::{cmp::Ordering, sync::Arc};
2020-06-05 18:19:26 +02:00
use ruma::{
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};
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,
event::Event,
event_id::*,
id::*,
raw_id::*,
};
use crate::Result;
/// Persistent Data Unit (Event)
2020-12-22 12:45:35 -05:00
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct PduEvent {
pub event_id: Arc<EventId>,
pub room_id: OwnedRoomId,
pub sender: OwnedUserId,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<String>,
pub origin_server_ts: UInt,
2020-04-04 11:53:37 +02:00
#[serde(rename = "type")]
pub kind: TimelineEventType,
pub content: Box<RawJsonValue>,
2020-04-04 11:53:37 +02:00
#[serde(skip_serializing_if = "Option::is_none")]
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")]
pub redacts: Option<Arc<EventId>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unsigned: Option<Box<RawJsonValue>>,
pub hashes: EventHash,
#[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
}
/// Content hashes of a PDU.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EventHash {
/// The SHA-256 hash.
pub sha256: String,
}
impl PduEvent {
pub fn from_id_val(event_id: &EventId, mut json: CanonicalJsonObject) -> Result<Self> {
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)
}
}
/// Prevent derived equality which wouldn't limit itself to event_id
impl Eq for PduEvent {}
/// Equality determined by the Pdu's ID, not the memory representations.
impl PartialEq for PduEvent {
fn eq(&self, other: &Self) -> bool { self.event_id == other.event_id }
}
/// Ordering determined by the Pdu's ID, not the memory representations.
impl PartialOrd for PduEvent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}
/// Ordering determined by the Pdu's ID, not the memory representations.
impl Ord for PduEvent {
fn cmp(&self, other: &Self) -> Ordering { self.event_id.cmp(&other.event_id) }
}