#![allow(clippy::exhaustive_structs)]
use ruma_macros::Event;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::RawValue as RawJsonValue;
use super::{
room::redaction::SyncRoomRedactionEvent, EphemeralRoomEventContent, EventContent,
GlobalAccountDataEventContent, MessageLikeEventContent, MessageLikeEventType,
MessageLikeUnsigned, Redact, RedactContent, RedactedEventContent,
RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned,
RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType,
StateUnsigned, ToDeviceEventContent,
};
use crate::{
serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId,
OwnedUserId, RoomId, RoomVersionId, UserId,
};
#[derive(Clone, Debug, Event)]
pub struct GlobalAccountDataEvent<C: GlobalAccountDataEventContent> {
pub content: C,
}
#[derive(Clone, Debug, Event)]
pub struct RoomAccountDataEvent<C: RoomAccountDataEventContent> {
pub content: C,
}
#[derive(Clone, Debug, Event)]
pub struct EphemeralRoomEvent<C: EphemeralRoomEventContent> {
pub content: C,
pub room_id: OwnedRoomId,
}
#[derive(Clone, Debug, Event)]
pub struct SyncEphemeralRoomEvent<C: EphemeralRoomEventContent> {
pub content: C,
}
#[derive(Clone, Debug, Event)]
pub struct OriginalMessageLikeEvent<C: MessageLikeEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub room_id: OwnedRoomId,
pub unsigned: MessageLikeUnsigned,
}
#[derive(Clone, Debug, Event)]
pub struct OriginalSyncMessageLikeEvent<C: MessageLikeEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub unsigned: MessageLikeUnsigned,
}
#[derive(Clone, Debug, Event)]
pub struct RedactedMessageLikeEvent<C: RedactedMessageLikeEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub room_id: OwnedRoomId,
pub unsigned: RedactedUnsigned,
}
#[derive(Clone, Debug, Event)]
pub struct RedactedSyncMessageLikeEvent<C: RedactedMessageLikeEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub unsigned: RedactedUnsigned,
}
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum MessageLikeEvent<C: MessageLikeEventContent + RedactContent>
where
C::Redacted: MessageLikeEventContent + RedactedEventContent,
{
Original(OriginalMessageLikeEvent<C>),
Redacted(RedactedMessageLikeEvent<C::Redacted>),
}
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum SyncMessageLikeEvent<C: MessageLikeEventContent + RedactContent>
where
C::Redacted: MessageLikeEventContent + RedactedEventContent,
{
Original(OriginalSyncMessageLikeEvent<C>),
Redacted(RedactedSyncMessageLikeEvent<C::Redacted>),
}
#[derive(Clone, Debug, Event)]
pub struct OriginalStateEvent<C: StateEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub room_id: OwnedRoomId,
pub state_key: C::StateKey,
pub unsigned: StateUnsigned<C>,
}
#[derive(Clone, Debug, Event)]
pub struct OriginalSyncStateEvent<C: StateEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub state_key: C::StateKey,
pub unsigned: StateUnsigned<C>,
}
#[derive(Clone, Debug, Event)]
pub struct StrippedStateEvent<C: StateEventContent> {
pub content: C,
pub sender: OwnedUserId,
pub state_key: C::StateKey,
}
#[derive(Clone, Debug, Event)]
pub struct InitialStateEvent<C: StateEventContent> {
pub content: C,
pub state_key: C::StateKey,
}
#[derive(Clone, Debug, Event)]
pub struct RedactedStateEvent<C: RedactedStateEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub room_id: OwnedRoomId,
pub state_key: C::StateKey,
pub unsigned: RedactedUnsigned,
}
#[derive(Clone, Debug, Event)]
pub struct RedactedSyncStateEvent<C: RedactedStateEventContent> {
pub content: C,
pub event_id: OwnedEventId,
pub sender: OwnedUserId,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub state_key: C::StateKey,
pub unsigned: RedactedUnsigned,
}
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum StateEvent<C: StateEventContent + RedactContent>
where
C::Redacted: StateEventContent + RedactedEventContent,
{
Original(OriginalStateEvent<C>),
Redacted(RedactedStateEvent<C::Redacted>),
}
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum SyncStateEvent<C: StateEventContent + RedactContent>
where
C::Redacted: StateEventContent + RedactedEventContent,
{
Original(OriginalSyncStateEvent<C>),
Redacted(RedactedSyncStateEvent<C::Redacted>),
}
#[derive(Clone, Debug, Event)]
pub struct ToDeviceEvent<C: ToDeviceEventContent> {
pub content: C,
pub sender: OwnedUserId,
}
#[derive(Clone, Debug, Event)]
pub struct DecryptedOlmV1Event<C: MessageLikeEventContent> {
pub content: C,
pub sender: OwnedUserId,
pub recipient: OwnedUserId,
pub recipient_keys: OlmV1Keys,
pub keys: OlmV1Keys,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OlmV1Keys {
pub ed25519: String,
}
#[derive(Clone, Debug, Event)]
pub struct DecryptedMegolmV1Event<C: MessageLikeEventContent> {
pub content: C,
pub room_id: OwnedRoomId,
}
macro_rules! impl_possibly_redacted_event {
(
$ty:ident ( $content_trait:ident, $event_type:ident )
$( where C::Redacted: $trait:ident<StateKey = C::StateKey>, )?
{ $($extra:tt)* }
) => {
impl<C> $ty<C>
where
C: $content_trait + RedactContent,
C::Redacted: $content_trait + RedactedEventContent,
$( C::Redacted: $trait<StateKey = C::StateKey>, )?
{
pub fn event_type(&self) -> $event_type {
match self {
Self::Original(ev) => ev.content.event_type(),
Self::Redacted(ev) => ev.content.event_type(),
}
}
pub fn event_id(&self) -> &EventId {
match self {
Self::Original(ev) => &ev.event_id,
Self::Redacted(ev) => &ev.event_id,
}
}
pub fn sender(&self) -> &UserId {
match self {
Self::Original(ev) => &ev.sender,
Self::Redacted(ev) => &ev.sender,
}
}
pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
match self {
Self::Original(ev) => ev.origin_server_ts,
Self::Redacted(ev) => ev.origin_server_ts,
}
}
$($extra)*
}
impl<C> Redact for $ty<C>
where
C: $content_trait + RedactContent,
C::Redacted: $content_trait + RedactedEventContent,
$( C::Redacted: $trait<StateKey = C::StateKey>, )?
{
type Redacted = Self;
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self {
match self {
Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)),
Self::Redacted(ev) => Self::Redacted(ev),
}
}
}
impl<'de, C> Deserialize<'de> for $ty<C>
where
C: $content_trait + RedactContent,
C::Redacted: $content_trait + RedactedEventContent,
$( C::Redacted: $trait<StateKey = C::StateKey>, )?
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let json = Box::<RawJsonValue>::deserialize(deserializer)?;
let RedactionDeHelper { unsigned } = from_raw_json_value(&json)?;
if unsigned.and_then(|u| u.redacted_because).is_some() {
Ok(Self::Redacted(from_raw_json_value(&json)?))
} else {
Ok(Self::Original(from_raw_json_value(&json)?))
}
}
}
}
}
impl_possibly_redacted_event!(MessageLikeEvent(MessageLikeEventContent, MessageLikeEventType) {
pub fn room_id(&self) -> &RoomId {
match self {
Self::Original(ev) => &ev.room_id,
Self::Redacted(ev) => &ev.room_id,
}
}
pub fn as_original(&self) -> Option<&OriginalMessageLikeEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
}
});
impl_possibly_redacted_event!(SyncMessageLikeEvent(MessageLikeEventContent, MessageLikeEventType) {
pub fn as_original(&self) -> Option<&OriginalSyncMessageLikeEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
}
pub fn into_full_event(self, room_id: OwnedRoomId) -> MessageLikeEvent<C> {
match self {
Self::Original(ev) => MessageLikeEvent::Original(ev.into_full_event(room_id)),
Self::Redacted(ev) => MessageLikeEvent::Redacted(ev.into_full_event(room_id)),
}
}
});
impl_possibly_redacted_event!(
StateEvent(StateEventContent, StateEventType)
where
C::Redacted: StateEventContent<StateKey = C::StateKey>,
{
pub fn room_id(&self) -> &RoomId {
match self {
Self::Original(ev) => &ev.room_id,
Self::Redacted(ev) => &ev.room_id,
}
}
pub fn state_key(&self) -> &C::StateKey {
match self {
Self::Original(ev) => &ev.state_key,
Self::Redacted(ev) => &ev.state_key,
}
}
pub fn as_original(&self) -> Option<&OriginalStateEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
}
}
);
impl_possibly_redacted_event!(
SyncStateEvent(StateEventContent, StateEventType)
where
C::Redacted: StateEventContent<StateKey = C::StateKey>,
{
pub fn state_key(&self) -> &C::StateKey {
match self {
Self::Original(ev) => &ev.state_key,
Self::Redacted(ev) => &ev.state_key,
}
}
pub fn as_original(&self) -> Option<&OriginalSyncStateEvent<C>> {
match self {
Self::Original(v) => Some(v),
_ => None,
}
}
pub fn into_full_event(self, room_id: OwnedRoomId) -> StateEvent<C> {
match self {
Self::Original(ev) => StateEvent::Original(ev.into_full_event(room_id)),
Self::Redacted(ev) => StateEvent::Redacted(ev.into_full_event(room_id)),
}
}
}
);
macro_rules! impl_sync_from_full {
($ty:ident, $full:ident, $content_trait:ident) => {
impl<C> From<$full<C>> for $ty<C>
where
C: $content_trait + RedactContent,
C::Redacted: $content_trait + RedactedEventContent,
{
fn from(full: $full<C>) -> Self {
match full {
$full::Original(ev) => Self::Original(ev.into()),
$full::Redacted(ev) => Self::Redacted(ev.into()),
}
}
}
};
}
impl_sync_from_full!(SyncMessageLikeEvent, MessageLikeEvent, MessageLikeEventContent);
impl_sync_from_full!(SyncStateEvent, StateEvent, StateEventContent);