From baa6dc591e540545433d8b2c7a7edba419e30a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= <76261501+zecakeh@users.noreply.github.com> Date: Tue, 24 May 2022 14:30:27 +0200 Subject: [PATCH] events: Change BundledAnnotation to a struct --- crates/ruma-common/CHANGELOG.md | 2 + crates/ruma-common/src/events/relation.rs | 55 ++++++++++++++--------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 0d3d4606..082e7c57 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -9,6 +9,8 @@ Breaking changes: Improvements: * All push rules are now considered to not apply to events sent by the user themselves +* Change `events::relation::BundledAnnotation` to a struct instead of an enum + * Remove `BundledReaction` # 0.9.2 diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index 163a2d76..811d7fc8 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -6,45 +6,56 @@ use js_int::UInt; use serde::{Deserialize, Serialize}; use super::AnySyncMessageLikeEvent; -use crate::{serde::Raw, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId}; +use crate::{ + serde::{Raw, StringEnum}, + MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, PrivOwnedStr, +}; -/// Summary of all reactions with the given key to an event. -#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)] +/// Summary of all annotations to an event with the given key and type. +#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] #[cfg(feature = "unstable-msc2677")] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct BundledReaction { - /// The key used for the reaction. +pub struct BundledAnnotation { + /// The type of the annotation. + #[serde(rename = "type")] + pub annotation_type: AnnotationType, + + /// The key used for the annotation. pub key: String, - /// Time of the bundled reaction being compiled on the server. + /// Time of the bundled annotation being compiled on the server. #[serde(skip_serializing_if = "Option::is_none")] pub origin_server_ts: Option, - /// Number of reactions. + /// Number of annotations. pub count: UInt, } #[cfg(feature = "unstable-msc2677")] -impl BundledReaction { - /// Creates a new `BundledReaction`. - pub fn new( - key: String, - origin_server_ts: Option, - count: UInt, - ) -> Self { - Self { key, origin_server_ts, count } +impl BundledAnnotation { + /// Creates a new `BundledAnnotation` with the given type, key and count. + pub fn new(annotation_type: AnnotationType, key: String, count: UInt) -> Self { + Self { annotation_type, key, count, origin_server_ts: None } + } + + /// Creates a new `BundledAnnotation` for a reaction with the given key and count. + pub fn reaction(key: String, count: UInt) -> Self { + Self::new(AnnotationType::Reaction, key, count) } } -/// Type of bundled annotation. -#[derive(Clone, Debug, Deserialize, Serialize)] +/// Type of annotation. +#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[cfg(feature = "unstable-msc2677")] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(tag = "type")] -pub enum BundledAnnotation { - /// An emoji reaction and its count. - #[serde(rename = "m.reaction")] - Reaction(BundledReaction), +pub enum AnnotationType { + /// A reaction. + #[ruma_enum(rename = "m.reaction")] + Reaction, + + #[doc(hidden)] + _Custom(PrivOwnedStr), } /// The first chunk of annotations with a token for loading more.