Files
tuwunel/src/core/matrix/event.rs

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

115 lines
2.4 KiB
Rust
Raw Normal View History

mod content;
mod format;
mod redact;
mod type_ext;
use ruma::{
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, RoomVersionId, UserId,
events::TimelineEventType,
};
use serde::Deserialize;
use serde_json::{Value as JsonValue, value::RawValue as RawJsonValue};
pub use self::type_ext::TypeExt;
use super::state_key::StateKey;
use crate::Result;
2025-02-05 12:22:22 +00:00
/// Abstraction of a PDU so users can have their own PDU types.
pub trait Event {
/// Serialize into a Ruma JSON format, consuming.
#[inline]
fn into_format<T>(self) -> T
where
T: From<format::Owned<Self>>,
Self: Sized,
{
format::Owned(self).into()
}
2025-02-05 12:22:22 +00:00
/// Serialize into a Ruma JSON format
#[inline]
fn to_format<'a, T>(&'a self) -> T
where
T: From<format::Ref<'a, Self>>,
Self: Sized + 'a,
{
format::Ref(self).into()
}
2025-02-05 12:22:22 +00:00
#[inline]
fn get_content_as_value(&self) -> JsonValue
where
Self: Sized,
{
content::as_value(self)
}
2025-02-05 12:22:22 +00:00
#[inline]
fn get_content<T>(&self) -> Result<T>
where
for<'de> T: Deserialize<'de>,
Self: Sized,
{
content::get::<T, _>(self)
}
2025-02-05 12:22:22 +00:00
#[inline]
fn redacts_id(&self, room_version: &RoomVersionId) -> Option<OwnedEventId>
where
Self: Sized,
{
redact::redacts_id(self, room_version)
}
2025-02-05 12:22:22 +00:00
#[inline]
fn is_redacted(&self) -> bool
where
Self: Sized,
{
redact::is_redacted(self)
}
2025-02-05 12:22:22 +00:00
fn is_owned(&self) -> bool;
2025-02-05 12:22:22 +00:00
//
// Canonical properties
//
2025-02-05 12:22:22 +00:00
/// All the authenticating events for this event.
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Send + '_;
2025-02-05 12:22:22 +00:00
/// The event's content.
fn content(&self) -> &RawJsonValue;
2025-02-05 12:22:22 +00:00
/// The `EventId` of this event.
fn event_id(&self) -> &EventId;
2025-02-05 12:22:22 +00:00
/// The time of creation on the originating server.
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
2025-02-05 12:22:22 +00:00
/// The events before this event.
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Send + '_;
2025-02-05 12:22:22 +00:00
/// If this event is a redaction event this is the event it redacts.
fn redacts(&self) -> Option<&EventId>;
2025-02-05 12:22:22 +00:00
/// The `RoomId` of this event.
fn room_id(&self) -> &RoomId;
2025-02-05 12:22:22 +00:00
/// The `UserId` of this event.
fn sender(&self) -> &UserId;
2025-02-05 12:22:22 +00:00
/// The state key for this event.
fn state_key(&self) -> Option<&str>;
2025-02-05 12:22:22 +00:00
/// The event type.
fn kind(&self) -> &TimelineEventType;
2025-02-05 12:22:22 +00:00
/// Metadata container; peer-trusted only.
fn unsigned(&self) -> Option<&RawJsonValue>;
2025-02-05 12:22:22 +00:00
//#[deprecated]
#[inline]
fn event_type(&self) -> &TimelineEventType { self.kind() }
2025-02-05 12:22:22 +00:00
}