events: Use a BTreeSet for Mentions' user_ids field

Avoids to serialize duplicate IDs
This commit is contained in:
Kévin Commaille 2023-08-21 17:06:19 +02:00 committed by Kévin Commaille
parent 8bdfd809e4
commit 3fe6ba7f52

View File

@ -102,6 +102,8 @@
//! )); //! ));
//! ``` //! ```
use std::collections::BTreeSet;
use serde::{de::IgnoredAny, Deserialize, Serialize, Serializer}; use serde::{de::IgnoredAny, Deserialize, Serialize, Serializer};
use crate::{EventEncryptionAlgorithm, OwnedUserId, RoomVersionId}; use crate::{EventEncryptionAlgorithm, OwnedUserId, RoomVersionId};
@ -231,9 +233,9 @@ pub fn serialize_custom_event_error<T, S: Serializer>(_: &T, _: S) -> Result<S::
pub struct Mentions { pub struct Mentions {
/// The list of mentioned users. /// The list of mentioned users.
/// ///
/// Defaults to an empty `Vec`. /// Defaults to an empty `BTreeSet`.
#[serde(default, skip_serializing_if = "Vec::is_empty")] #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub user_ids: Vec<OwnedUserId>, pub user_ids: BTreeSet<OwnedUserId>,
/// Whether the whole room is mentioned. /// Whether the whole room is mentioned.
/// ///
@ -249,8 +251,8 @@ impl Mentions {
} }
/// Create a `Mentions` for the given user IDs. /// Create a `Mentions` for the given user IDs.
pub fn with_user_ids(user_ids: Vec<OwnedUserId>) -> Self { pub fn with_user_ids(user_ids: impl IntoIterator<Item = OwnedUserId>) -> Self {
Self { user_ids, ..Default::default() } Self { user_ids: BTreeSet::from_iter(user_ids), ..Default::default() }
} }
/// Create a `Mentions` for a room mention. /// Create a `Mentions` for a room mention.