events: Clean up reaction event

This commit is contained in:
Jonas Platte 2021-06-17 16:20:57 +02:00
parent b51b0c54f9
commit e3084b1f91
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67

View File

@ -1,15 +1,10 @@
//! Types for the *m.reaction* event.
use std::convert::TryFrom;
use ruma_events_macros::EventContent;
use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize};
use crate::{
room::relationships::{Annotation, RelatesToJsonRepr, RelationJsonRepr},
MessageEvent,
};
use crate::MessageEvent;
/// A reaction to another event.
pub type ReactionEvent = MessageEvent<ReactionEventContent>;
@ -21,28 +16,28 @@ pub type ReactionEvent = MessageEvent<ReactionEventContent>;
pub struct ReactionEventContent {
/// Information about the related event.
#[serde(rename = "m.relates_to")]
pub relation: Relation,
pub relates_to: Relation,
}
impl ReactionEventContent {
/// Creates a new `ReactionEventContent` from the given relation.
///
/// You can also construct a `ReactionEventContent` from a relation using `From` / `Into`.
pub fn new(relation: Relation) -> Self {
Self { relation }
pub fn new(relates_to: Relation) -> Self {
Self { relates_to }
}
}
impl From<Relation> for ReactionEventContent {
fn from(relation: Relation) -> Self {
Self::new(relation)
fn from(relates_to: Relation) -> Self {
Self::new(relates_to)
}
}
/// The relation that contains info which event the reaction is applying to.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(try_from = "RelatesToJsonRepr", into = "RelatesToJsonRepr")]
#[serde(tag = "rel_type", rename = "m.annotation")]
pub struct Relation {
/// The event that is being reacted to.
pub event_id: EventId,
@ -57,24 +52,3 @@ impl Relation {
Self { event_id, emoji }
}
}
impl From<Relation> for RelatesToJsonRepr {
fn from(relation: Relation) -> Self {
RelatesToJsonRepr::Relation(RelationJsonRepr::Annotation(Annotation {
event_id: relation.event_id,
key: relation.emoji,
}))
}
}
impl TryFrom<RelatesToJsonRepr> for Relation {
type Error = &'static str;
fn try_from(value: RelatesToJsonRepr) -> Result<Self, Self::Error> {
if let RelatesToJsonRepr::Relation(RelationJsonRepr::Annotation(a)) = value {
Ok(Relation { event_id: a.event_id, emoji: a.key })
} else {
Err("Expected a relation with a rel_type of `annotation`")
}
}
}