Also remove the From<&str> implementation for Reaction

This commit is contained in:
Hocuri
2026-04-26 09:48:43 +02:00
parent 47b94f3f56
commit 23d734c65d
2 changed files with 20 additions and 24 deletions

View File

@@ -36,10 +36,7 @@ pub struct Reaction {
reaction: String,
}
// We implement From<&str> instead of std::str::FromStr, because
// FromStr requires error type and reaction parsing never returns an
// error.
impl From<&str> for Reaction {
impl Reaction {
/// Convert a `&str` into a `Reaction`.
/// Everything after the first whitespace is ignored.
///
@@ -51,7 +48,7 @@ impl From<&str> for Reaction {
/// reactions is not different from other kinds of spam attacks
/// such as sending large numbers of large messages, and should be
/// dealt with the same way, e.g. by blocking the user.
fn from(reaction: &str) -> Self {
pub fn from_str(reaction: &str) -> Self {
let reaction: &str = reaction
.split_ascii_whitespace()
.next()
@@ -61,9 +58,7 @@ impl From<&str> for Reaction {
reaction: reaction.to_string(),
}
}
}
impl Reaction {
/// Returns true if reaction contains no emoji.
pub fn is_empty(&self) -> bool {
self.reaction.is_empty()
@@ -212,7 +207,7 @@ pub async fn send_reaction(context: &Context, msg_id: MsgId, reaction: &str) ->
let msg = Message::load_from_db(context, msg_id).await?;
let chat_id = msg.chat_id;
let reaction: Reaction = reaction.into();
let reaction = Reaction::from_str(reaction);
let mut reaction_msg = Message::new_text(reaction.as_str().to_string());
reaction_msg.set_reaction();
reaction_msg.in_reply_to = Some(msg.rfc724_mid);
@@ -282,7 +277,7 @@ pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<React
|row| {
let contact_id: ContactId = row.get(0)?;
let reaction: String = row.get(1)?;
Ok((contact_id, Reaction::from(reaction.as_str())))
Ok((contact_id, Reaction::from_str(reaction.as_str())))
},
)
.await?;
@@ -361,32 +356,33 @@ mod tests {
#[test]
fn test_parse_reaction() {
// Check that basic set of emojis from RFC 9078 is supported.
assert_eq!(Reaction::from("👍").as_str(), "👍");
assert_eq!(Reaction::from("👎").as_str(), "👎");
assert_eq!(Reaction::from("😀").as_str(), "😀");
assert_eq!(Reaction::from("").as_str(), "");
assert_eq!(Reaction::from("😢").as_str(), "😢");
assert_eq!(Reaction::from_str("👍").as_str(), "👍");
assert_eq!(Reaction::from_str("👎").as_str(), "👎");
assert_eq!(Reaction::from_str("😀").as_str(), "😀");
assert_eq!(Reaction::from_str("").as_str(), "");
assert_eq!(Reaction::from_str("😢").as_str(), "😢");
// Empty string can be used to remove all reactions.
assert!(Reaction::from("").is_empty());
assert!(Reaction::from_str("").is_empty());
// Short strings can be used as emojis, could be used to add
// support for custom emojis via emoji shortcodes.
assert_eq!(Reaction::from(":deltacat:").as_str(), ":deltacat:");
assert_eq!(Reaction::from_str(":deltacat:").as_str(), ":deltacat:");
// Check that long strings are not valid emojis.
assert!(
Reaction::from(":foobarbazquuxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:").is_empty()
Reaction::from_str(":foobarbazquuxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:")
.is_empty()
);
// Multiple reactions separated by spaces or tabs are not supported.
assert_eq!(Reaction::from("👍 ❤").as_str(), "👍");
assert_eq!(Reaction::from("👍\t").as_str(), "👍");
assert_eq!(Reaction::from_str("👍 ❤").as_str(), "👍");
assert_eq!(Reaction::from_str("👍\t").as_str(), "👍");
assert_eq!(Reaction::from("👍\t:foo: ❤").as_str(), "👍");
assert_eq!(Reaction::from("👍\t:foo: ❤").as_str(), "👍");
assert_eq!(Reaction::from_str("👍\t:foo: ❤").as_str(), "👍");
assert_eq!(Reaction::from_str("👍\t:foo: ❤").as_str(), "👍");
assert_eq!(Reaction::from("👍 👍").as_str(), "👍");
assert_eq!(Reaction::from_str("👍 👍").as_str(), "👍");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -582,7 +578,7 @@ Content-Disposition: reaction\n\
assert_eq!(chat_id, expected_chat_id);
assert_eq!(msg_id, expected_msg_id);
assert_eq!(contact_id, expected_contact_id);
assert_eq!(reaction, Reaction::from(expected_reaction));
assert_eq!(reaction, Reaction::from_str(expected_reaction));
}
_ => panic!("Unexpected event {event:?}."),
}

View File

@@ -2151,7 +2151,7 @@ async fn add_parts(
chat_id,
from_id,
sort_timestamp,
Reaction::from(reaction_str.as_str()),
Reaction::from_str(reaction_str.as_str()),
is_incoming_fresh,
)
.await?;