events: Add event relation conversions

This commit is contained in:
Jonas Platte 2021-06-20 19:04:33 +02:00
parent 84829e23dc
commit 61671f4440
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 33 additions and 2 deletions

View File

@ -13,6 +13,8 @@ Breaking changes:
* Rename `relation` field in some events to `relates_to`
* All events that support relations now have their own `Relation` types (the `room::relationships`
module has been removed)
* The `room::encryption` relation type can represent any kind of relation and has `From`
implementations so any other relation can be converted to it
Improvements:

View File

@ -10,8 +10,11 @@ use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize};
#[cfg(feature = "unstable-pre-spec")]
use crate::room::message::Replacement;
use crate::{room::message::InReplyTo, MessageEvent};
use crate::{key::verification, reaction, room::message::Replacement};
use crate::{
room::message::{self, InReplyTo},
MessageEvent,
};
mod relation_serde;
@ -218,6 +221,32 @@ impl From<MegolmV1AesSha2ContentInit> for MegolmV1AesSha2Content {
}
}
impl From<message::Relation> for Relation {
fn from(rel: message::Relation) -> Self {
match rel {
message::Relation::Reply { in_reply_to } => Self::Reply { in_reply_to },
#[cfg(feature = "unstable-pre-spec")]
message::Relation::Replacement(re) => Self::Replacement(re),
}
}
}
#[cfg(feature = "unstable-pre-spec")]
impl From<reaction::Relation> for Relation {
fn from(rel: reaction::Relation) -> Self {
let reaction::Relation { event_id, emoji } = rel;
Self::Annotation(Annotation { event_id, key: emoji })
}
}
#[cfg(feature = "unstable-pre-spec")]
impl From<verification::Relation> for Relation {
fn from(rel: verification::Relation) -> Self {
let verification::Relation { event_id } = rel;
Self::Reference(Reference { event_id })
}
}
#[cfg(test)]
mod tests {
use matches::assert_matches;