events: Clean up reaction event
This commit is contained in:
parent
b51b0c54f9
commit
e3084b1f91
@ -1,15 +1,10 @@
|
|||||||
//! Types for the *m.reaction* event.
|
//! Types for the *m.reaction* event.
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
use ruma_events_macros::EventContent;
|
use ruma_events_macros::EventContent;
|
||||||
use ruma_identifiers::EventId;
|
use ruma_identifiers::EventId;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::MessageEvent;
|
||||||
room::relationships::{Annotation, RelatesToJsonRepr, RelationJsonRepr},
|
|
||||||
MessageEvent,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// A reaction to another event.
|
/// A reaction to another event.
|
||||||
pub type ReactionEvent = MessageEvent<ReactionEventContent>;
|
pub type ReactionEvent = MessageEvent<ReactionEventContent>;
|
||||||
@ -21,28 +16,28 @@ pub type ReactionEvent = MessageEvent<ReactionEventContent>;
|
|||||||
pub struct ReactionEventContent {
|
pub struct ReactionEventContent {
|
||||||
/// Information about the related event.
|
/// Information about the related event.
|
||||||
#[serde(rename = "m.relates_to")]
|
#[serde(rename = "m.relates_to")]
|
||||||
pub relation: Relation,
|
pub relates_to: Relation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReactionEventContent {
|
impl ReactionEventContent {
|
||||||
/// Creates a new `ReactionEventContent` from the given relation.
|
/// Creates a new `ReactionEventContent` from the given relation.
|
||||||
///
|
///
|
||||||
/// You can also construct a `ReactionEventContent` from a relation using `From` / `Into`.
|
/// You can also construct a `ReactionEventContent` from a relation using `From` / `Into`.
|
||||||
pub fn new(relation: Relation) -> Self {
|
pub fn new(relates_to: Relation) -> Self {
|
||||||
Self { relation }
|
Self { relates_to }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Relation> for ReactionEventContent {
|
impl From<Relation> for ReactionEventContent {
|
||||||
fn from(relation: Relation) -> Self {
|
fn from(relates_to: Relation) -> Self {
|
||||||
Self::new(relation)
|
Self::new(relates_to)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The relation that contains info which event the reaction is applying to.
|
/// The relation that contains info which event the reaction is applying to.
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
#[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 {
|
pub struct Relation {
|
||||||
/// The event that is being reacted to.
|
/// The event that is being reacted to.
|
||||||
pub event_id: EventId,
|
pub event_id: EventId,
|
||||||
@ -57,24 +52,3 @@ impl Relation {
|
|||||||
Self { event_id, emoji }
|
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`")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user