deltachat/
reaction.rs

1//! # Reactions.
2//!
3//! Reactions are short messages representing an emoji sent in reply to
4//! messages. Unlike normal messages which are added to the end of the chat,
5//! reactions are supposed to be displayed near the original messages.
6//!
7//! RFC 9078 specifies how reactions are transmitted in MIME messages.
8//!
9//! Reaction update semantics is not well-defined in RFC 9078, so
10//! Delta Chat uses the same semantics as in
11//! [XEP-0444](https://xmpp.org/extensions/xep-0444.html) section
12//! "3.2 Updating reactions to a message". Received reactions override
13//! all previously received reactions from the same user and it is
14//! possible to remove the reaction by sending an empty string as a reaction,
15//! even though RFC 9078 requires at least one emoji to be sent.
16
17use std::cmp::Ordering;
18use std::collections::BTreeMap;
19use std::fmt;
20
21use anyhow::Result;
22use serde::{Deserialize, Serialize};
23
24use crate::chat::{Chat, ChatId, send_msg};
25use crate::chatlist_events;
26use crate::contact::ContactId;
27use crate::context::Context;
28use crate::events::EventType;
29use crate::message::{Message, MsgId, rfc724_mid_exists};
30use crate::param::Param;
31
32/// A single reaction.
33#[derive(Debug, Default, Clone, Deserialize, Eq, PartialEq, Serialize)]
34pub struct Reaction {
35    /// Canonical representation of reaction as a string of space-separated emojis.
36    reaction: String,
37}
38
39impl Reaction {
40    /// Convert a `&str` into a `Reaction`.
41    /// Everything after the first whitespace is ignored.
42    ///
43    /// Any short enough string is accepted as a reaction to avoid the
44    /// complexity of validating emoji sequences as required by RFC
45    /// 9078. On the sender side UI is responsible to provide only
46    /// valid emoji sequences via reaction picker. On the receiver
47    /// side, abuse of the possibility to use arbitrary strings as
48    /// reactions is not different from other kinds of spam attacks
49    /// such as sending large numbers of large messages, and should be
50    /// dealt with the same way, e.g. by blocking the user.
51    pub fn new(reaction: &str) -> Self {
52        let reaction: &str = reaction
53            .split_ascii_whitespace()
54            .next()
55            .filter(|&emoji| emoji.len() < 30)
56            .unwrap_or("");
57        Self {
58            reaction: reaction.to_string(),
59        }
60    }
61
62    /// Returns true if reaction contains no emoji.
63    pub fn is_empty(&self) -> bool {
64        self.reaction.is_empty()
65    }
66
67    /// Returns a string representing the emoji.
68    pub fn as_str(&self) -> &str {
69        &self.reaction
70    }
71}
72
73/// Structure representing all reactions to a particular message.
74#[derive(Debug)]
75pub struct Reactions {
76    /// Map from a contact to its reaction to message.
77    reactions: BTreeMap<ContactId, Reaction>,
78}
79
80impl Reactions {
81    /// Returns vector of contacts that reacted to the message.
82    pub fn contacts(&self) -> Vec<ContactId> {
83        self.reactions.keys().copied().collect()
84    }
85
86    /// Returns reaction of a given contact to message.
87    ///
88    /// If contact did not react to message or removed the reaction,
89    /// this method returns an empty reaction.
90    pub fn get(&self, contact_id: ContactId) -> Reaction {
91        self.reactions.get(&contact_id).cloned().unwrap_or_default()
92    }
93
94    /// Returns true if the message has no reactions.
95    pub fn is_empty(&self) -> bool {
96        self.reactions.is_empty()
97    }
98
99    /// Returns a map from emojis to their frequencies.
100    #[expect(clippy::arithmetic_side_effects)]
101    pub fn emoji_frequencies(&self) -> BTreeMap<String, usize> {
102        let mut emoji_frequencies: BTreeMap<String, usize> = BTreeMap::new();
103        for reaction in self.reactions.values() {
104            emoji_frequencies
105                .entry(reaction.as_str().to_string())
106                .and_modify(|x| *x += 1)
107                .or_insert(1);
108        }
109        emoji_frequencies
110    }
111
112    /// Returns a vector of emojis
113    /// sorted in descending order of frequencies.
114    ///
115    /// This function can be used to display the reactions in
116    /// the message bubble in the UIs.
117    pub fn emoji_sorted_by_frequency(&self) -> Vec<(String, usize)> {
118        let mut emoji_frequencies: Vec<(String, usize)> =
119            self.emoji_frequencies().into_iter().collect();
120        emoji_frequencies.sort_by(|(a, a_count), (b, b_count)| {
121            match a_count.cmp(b_count).reverse() {
122                Ordering::Equal => a.cmp(b),
123                other => other,
124            }
125        });
126        emoji_frequencies
127    }
128
129    /// Returns an iterator of the contacts that reacted and their corresponding reactions.
130    pub fn iter(&self) -> impl Iterator<Item = (&ContactId, &Reaction)> {
131        self.reactions.iter()
132    }
133}
134
135impl fmt::Display for Reactions {
136    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137        let emoji_frequencies = self.emoji_sorted_by_frequency();
138        let mut first = true;
139        for (emoji, frequency) in emoji_frequencies {
140            if !first {
141                write!(f, " ")?;
142            }
143            first = false;
144            write!(f, "{emoji}{frequency}")?;
145        }
146        Ok(())
147    }
148}
149
150async fn set_msg_id_reaction(
151    context: &Context,
152    msg_id: MsgId,
153    chat_id: ChatId,
154    contact_id: ContactId,
155    timestamp: i64,
156    reaction: &Reaction,
157) -> Result<()> {
158    if reaction.is_empty() {
159        // Simply remove the record instead of setting it to empty string.
160        context
161            .sql
162            .execute(
163                "DELETE FROM reactions
164                 WHERE msg_id = ?1
165                 AND contact_id = ?2",
166                (msg_id, contact_id),
167            )
168            .await?;
169    } else {
170        context
171            .sql
172            .execute(
173                "INSERT INTO reactions (msg_id, contact_id, reaction)
174                 VALUES (?1, ?2, ?3)
175                 ON CONFLICT(msg_id, contact_id)
176                 DO UPDATE SET reaction=excluded.reaction",
177                (msg_id, contact_id, reaction.as_str()),
178            )
179            .await?;
180        let mut chat = Chat::load_from_db(context, chat_id).await?;
181        if chat
182            .param
183            .update_timestamp(Param::LastReactionTimestamp, timestamp)?
184        {
185            chat.param
186                .set_i64(Param::LastReactionMsgId, i64::from(msg_id.to_u32()));
187            chat.param
188                .set_i64(Param::LastReactionContactId, i64::from(contact_id.to_u32()));
189            chat.update_param(context).await?;
190        }
191    }
192
193    context.emit_event(EventType::ReactionsChanged {
194        chat_id,
195        msg_id,
196        contact_id,
197    });
198    chatlist_events::emit_chatlist_item_changed(context, chat_id);
199    Ok(())
200}
201
202/// Sends a reaction to message `msg_id`, overriding previously sent reactions.
203///
204/// `reaction` is a string consisting of a single emoji. Use
205/// empty string to retract a reaction.
206pub async fn send_reaction(context: &Context, msg_id: MsgId, reaction: &str) -> Result<MsgId> {
207    let msg = Message::load_from_db(context, msg_id).await?;
208    let chat_id = msg.chat_id;
209
210    let reaction = Reaction::new(reaction);
211    let mut reaction_msg = Message::new_text(reaction.as_str().to_string());
212    reaction_msg.set_reaction();
213    reaction_msg.in_reply_to = Some(msg.rfc724_mid);
214    reaction_msg.hidden = true;
215
216    // Send message first.
217    let reaction_msg_id = send_msg(context, chat_id, &mut reaction_msg).await?;
218
219    // Only set reaction if we successfully sent the message.
220    set_msg_id_reaction(
221        context,
222        msg_id,
223        msg.chat_id,
224        ContactId::SELF,
225        reaction_msg.timestamp_sort,
226        &reaction,
227    )
228    .await?;
229    Ok(reaction_msg_id)
230}
231
232/// Updates reaction of `contact_id` on the message with `in_reply_to`
233/// Message-ID. If no such message is found in the database, reaction
234/// is ignored.
235///
236/// `reaction` is string representing the emoji. It can be empty
237/// if contact wants to remove the reaction.
238pub(crate) async fn set_msg_reaction(
239    context: &Context,
240    in_reply_to: &str,
241    chat_id: ChatId,
242    contact_id: ContactId,
243    timestamp: i64,
244    reaction: Reaction,
245    is_incoming_fresh: bool,
246) -> Result<()> {
247    if let Some(msg_id) = rfc724_mid_exists(context, in_reply_to).await? {
248        set_msg_id_reaction(context, msg_id, chat_id, contact_id, timestamp, &reaction).await?;
249
250        if is_incoming_fresh
251            && !reaction.is_empty()
252            && msg_id.get_state(context).await?.is_outgoing()
253        {
254            context.emit_event(EventType::IncomingReaction {
255                chat_id,
256                contact_id,
257                msg_id,
258                reaction,
259            });
260        }
261    } else {
262        info!(
263            context,
264            "Can't assign reaction to unknown message with Message-ID {}", in_reply_to
265        );
266    }
267    Ok(())
268}
269
270/// Returns a structure containing all reactions to the message.
271pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
272    let mut reactions: BTreeMap<ContactId, Reaction> = context
273        .sql
274        .query_map_collect(
275            "SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
276            (msg_id,),
277            |row| {
278                let contact_id: ContactId = row.get(0)?;
279                let reaction: String = row.get(1)?;
280                Ok((contact_id, Reaction::new(reaction.as_str())))
281            },
282        )
283        .await?;
284    reactions.retain(|_contact, reaction| !reaction.is_empty());
285    Ok(Reactions { reactions })
286}
287
288impl Chat {
289    /// Check if there is a reaction newer than the given timestamp.
290    ///
291    /// If so, reaction details are returned and can be used to create a summary string.
292    pub async fn get_last_reaction_if_newer_than(
293        &self,
294        context: &Context,
295        timestamp: i64,
296    ) -> Result<Option<(Message, ContactId, String)>> {
297        if self
298            .param
299            .get_i64(Param::LastReactionTimestamp)
300            .is_none_or(|reaction_timestamp| reaction_timestamp <= timestamp)
301        {
302            return Ok(None);
303        };
304        let reaction_msg_id = MsgId::new(
305            self.param
306                .get_int(Param::LastReactionMsgId)
307                .unwrap_or_default() as u32,
308        );
309        let Some(reaction_msg) = Message::load_from_db_optional(context, reaction_msg_id).await?
310        else {
311            // The message reacted to may be deleted.
312            // These are no errors as `Param::LastReaction*` are just weak pointers.
313            // Instead, just return `Ok(None)` and let the caller create another summary.
314            return Ok(None);
315        };
316        let reaction_contact_id = ContactId::new(
317            self.param
318                .get_int(Param::LastReactionContactId)
319                .unwrap_or_default() as u32,
320        );
321        if let Some(reaction) = context
322            .sql
323            .query_get_value(
324                "SELECT reaction FROM reactions WHERE msg_id=? AND contact_id=?",
325                (reaction_msg.id, reaction_contact_id),
326            )
327            .await?
328        {
329            Ok(Some((reaction_msg, reaction_contact_id, reaction)))
330        } else {
331            Ok(None)
332        }
333    }
334}
335
336#[cfg(test)]
337mod tests {
338    use deltachat_contact_tools::ContactAddress;
339
340    use super::*;
341    use crate::chat::{forward_msgs, get_chat_msgs, marknoticed_chat, send_text_msg};
342    use crate::chatlist::Chatlist;
343    use crate::config::Config;
344    use crate::contact::{Contact, Origin};
345    use crate::key::{load_self_public_key, load_self_secret_key};
346    use crate::message::{MessageState, Viewtype, delete_msgs, markseen_msgs};
347    use crate::pgp::{SeipdVersion, pk_encrypt};
348    use crate::receive_imf::receive_imf;
349    use crate::sql::housekeeping;
350    use crate::test_utils::E2EE_INFO_MSGS;
351    use crate::test_utils::TestContext;
352    use crate::test_utils::TestContextManager;
353    use crate::tools::SystemTime;
354    use std::time::Duration;
355
356    #[test]
357    fn test_parse_reaction() {
358        // Check that basic set of emojis from RFC 9078 is supported.
359        assert_eq!(Reaction::new("๐Ÿ‘").as_str(), "๐Ÿ‘");
360        assert_eq!(Reaction::new("๐Ÿ‘Ž").as_str(), "๐Ÿ‘Ž");
361        assert_eq!(Reaction::new("๐Ÿ˜€").as_str(), "๐Ÿ˜€");
362        assert_eq!(Reaction::new("โ˜น").as_str(), "โ˜น");
363        assert_eq!(Reaction::new("๐Ÿ˜ข").as_str(), "๐Ÿ˜ข");
364
365        // Empty string can be used to remove all reactions.
366        assert!(Reaction::new("").is_empty());
367
368        // Short strings can be used as emojis, could be used to add
369        // support for custom emojis via emoji shortcodes.
370        assert_eq!(Reaction::new(":deltacat:").as_str(), ":deltacat:");
371
372        // Check that long strings are not valid emojis.
373        assert!(
374            Reaction::new(":foobarbazquuxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:").is_empty()
375        );
376
377        // Multiple reactions separated by spaces or tabs are not supported.
378        assert_eq!(Reaction::new("๐Ÿ‘ โค").as_str(), "๐Ÿ‘");
379        assert_eq!(Reaction::new("๐Ÿ‘\tโค").as_str(), "๐Ÿ‘");
380
381        assert_eq!(Reaction::new("๐Ÿ‘\t:foo: โค").as_str(), "๐Ÿ‘");
382        assert_eq!(Reaction::new("๐Ÿ‘\t:foo: โค").as_str(), "๐Ÿ‘");
383
384        assert_eq!(Reaction::new("๐Ÿ‘ ๐Ÿ‘").as_str(), "๐Ÿ‘");
385    }
386
387    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
388    async fn test_receive_reaction() -> Result<()> {
389        let alice = TestContext::new_alice().await;
390
391        // Alice receives BCC-self copy of a message sent to Bob.
392        receive_imf(
393            &alice,
394            "To: bob@example.net\n\
395From: alice@example.org\n\
396Date: Today, 29 February 2021 00:00:00 -800\n\
397Message-ID: 12345@example.org\n\
398Subject: Meeting\n\
399\n\
400Can we chat at 1pm pacific, today?"
401                .as_bytes(),
402            false,
403        )
404        .await?;
405        let msg = alice.get_last_msg().await;
406        assert_eq!(msg.state, MessageState::OutDelivered);
407        let reactions = get_msg_reactions(&alice, msg.id).await?;
408        let contacts = reactions.contacts();
409        assert_eq!(contacts.len(), 0);
410
411        let bob_id = Contact::add_or_lookup(
412            &alice,
413            "",
414            &ContactAddress::new("bob@example.net")?,
415            Origin::ManuallyCreated,
416        )
417        .await?
418        .0;
419        let bob_reaction = reactions.get(bob_id);
420        assert!(bob_reaction.is_empty()); // Bob has not reacted to message yet.
421
422        // Alice receives reaction to her message from Bob.
423        receive_imf(
424            &alice,
425            "To: alice@example.org\n\
426From: bob@example.net\n\
427Date: Today, 29 February 2021 00:00:10 -800\n\
428Message-ID: 56789@example.net\n\
429In-Reply-To: 12345@example.org\n\
430Subject: Meeting\n\
431Mime-Version: 1.0 (1.0)\n\
432Content-Type: text/plain; charset=utf-8\n\
433Content-Disposition: reaction\n\
434\n\
435\u{1F44D}"
436                .as_bytes(),
437            false,
438        )
439        .await?;
440
441        let reactions = get_msg_reactions(&alice, msg.id).await?;
442        assert_eq!(reactions.to_string(), "๐Ÿ‘1");
443
444        let contacts = reactions.contacts();
445        assert_eq!(contacts.len(), 1);
446
447        assert_eq!(contacts.first(), Some(&bob_id));
448        let bob_reaction = reactions.get(bob_id);
449        assert_eq!(bob_reaction.is_empty(), false);
450        assert_eq!(bob_reaction.as_str(), "๐Ÿ‘");
451        assert_eq!(bob_reaction.as_str(), "๐Ÿ‘");
452
453        // Alice receives reaction to her message from Bob with a footer.
454        receive_imf(
455            &alice,
456            "To: alice@example.org\n\
457From: bob@example.net\n\
458Date: Today, 29 February 2021 00:00:10 -800\n\
459Message-ID: 56790@example.net\n\
460In-Reply-To: 12345@example.org\n\
461Subject: Meeting\n\
462Mime-Version: 1.0 (1.0)\n\
463Content-Type: text/plain; charset=utf-8\n\
464Content-Disposition: reaction\n\
465\n\
466๐Ÿ˜€\n\
467\n\
468--\n\
469_______________________________________________\n\
470Here's my footer -- bob@example.net"
471                .as_bytes(),
472            false,
473        )
474        .await?;
475
476        let reactions = get_msg_reactions(&alice, msg.id).await?;
477        assert_eq!(reactions.to_string(), "๐Ÿ˜€1");
478
479        // Alice receives a message with reaction to her message from Bob.
480        let msg_bob = receive_imf(
481            &alice,
482            "To: alice@example.org\n\
483From: bob@example.net\n\
484Date: Today, 29 February 2021 00:00:10 -800\n\
485Message-ID: 56791@example.net\n\
486In-Reply-To: 12345@example.org\n\
487Mime-Version: 1.0\n\
488Content-Type: multipart/mixed; boundary=\"YiEDa0DAkWCtVeE4\"\n\
489Content-Disposition: inline\n\
490\n\
491--YiEDa0DAkWCtVeE4\n\
492Content-Type: text/plain; charset=utf-8\n\
493Content-Disposition: inline\n\
494\n\
495Reply + reaction\n\
496\n\
497--YiEDa0DAkWCtVeE4\n\
498Content-Type: text/plain; charset=utf-8\n\
499Content-Disposition: reaction\n\
500\n\
501\u{1F44D}\n\
502\n\
503--YiEDa0DAkWCtVeE4--"
504                .as_bytes(),
505            false,
506        )
507        .await?
508        .unwrap();
509        let msg_bob = Message::load_from_db(&alice, msg_bob.msg_ids[0]).await?;
510        assert_eq!(msg_bob.from_id, bob_id);
511        assert_eq!(msg_bob.chat_id, msg.chat_id);
512        assert_eq!(msg_bob.viewtype, Viewtype::Text);
513        assert_eq!(msg_bob.state, MessageState::InFresh);
514        assert_eq!(msg_bob.hidden, false);
515        assert_eq!(msg_bob.text, "Reply + reaction");
516        let reactions = get_msg_reactions(&alice, msg.id).await?;
517        assert_eq!(reactions.to_string(), "๐Ÿ‘1");
518
519        Ok(())
520    }
521
522    async fn expect_reactions_changed_event(
523        t: &TestContext,
524        expected_chat_id: ChatId,
525        expected_msg_id: MsgId,
526        expected_contact_id: ContactId,
527    ) -> Result<()> {
528        let event = t
529            .evtracker
530            .get_matching(|evt| {
531                matches!(
532                    evt,
533                    EventType::ReactionsChanged { .. } | EventType::IncomingMsg { .. }
534                )
535            })
536            .await;
537        match event {
538            EventType::ReactionsChanged {
539                chat_id,
540                msg_id,
541                contact_id,
542            } => {
543                assert_eq!(chat_id, expected_chat_id);
544                assert_eq!(msg_id, expected_msg_id);
545                assert_eq!(contact_id, expected_contact_id);
546            }
547            _ => panic!("Unexpected event {event:?}."),
548        }
549        Ok(())
550    }
551
552    async fn expect_incoming_reactions_event(
553        t: &TestContext,
554        expected_chat_id: ChatId,
555        expected_msg_id: MsgId,
556        expected_contact_id: ContactId,
557        expected_reaction: &str,
558    ) -> Result<()> {
559        let event = t
560            .evtracker
561            // Check for absence of `IncomingMsg` events -- it appeared that it's quite easy to make
562            // bugs when `IncomingMsg` is issued for reactions.
563            .get_matching(|evt| {
564                matches!(
565                    evt,
566                    EventType::IncomingReaction { .. } | EventType::IncomingMsg { .. }
567                )
568            })
569            .await;
570        match event {
571            EventType::IncomingReaction {
572                chat_id,
573                msg_id,
574                contact_id,
575                reaction,
576            } => {
577                assert_eq!(chat_id, expected_chat_id);
578                assert_eq!(msg_id, expected_msg_id);
579                assert_eq!(contact_id, expected_contact_id);
580                assert_eq!(reaction, Reaction::new(expected_reaction));
581            }
582            _ => panic!("Unexpected event {event:?}."),
583        }
584        Ok(())
585    }
586
587    /// Checks that no unwanted events remain after expecting "wanted" reaction events.
588    async fn expect_no_unwanted_events(t: &TestContext) {
589        let ev = t
590            .evtracker
591            .get_matching_opt(t, |evt| {
592                matches!(
593                    evt,
594                    EventType::IncomingReaction { .. }
595                        | EventType::IncomingMsg { .. }
596                        | EventType::MsgsChanged { .. }
597                )
598            })
599            .await;
600        if let Some(ev) = ev {
601            panic!("Unwanted event {ev:?}.")
602        }
603    }
604
605    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
606    async fn test_send_reaction() -> Result<()> {
607        let alice = TestContext::new_alice().await;
608        let bob = TestContext::new_bob().await;
609
610        // Test that the status does not get mixed up into reactions.
611        alice
612            .set_config(
613                Config::Selfstatus,
614                Some("Buy Delta Chat today and make this banner go away!"),
615            )
616            .await?;
617        bob.set_config(Config::Selfstatus, Some("Sent from my Delta Chat Pro. ๐Ÿ‘"))
618            .await?;
619
620        let chat_alice = alice.create_chat(&bob).await;
621        let alice_msg = alice.send_text(chat_alice.id, "Hi!").await;
622        let bob_msg = bob.recv_msg(&alice_msg).await;
623        assert_eq!(
624            get_chat_msgs(&alice, chat_alice.id).await?.len(),
625            E2EE_INFO_MSGS + 1
626        );
627        assert_eq!(
628            get_chat_msgs(&bob, bob_msg.chat_id).await?.len(),
629            E2EE_INFO_MSGS + 1
630        );
631
632        let alice_msg2 = alice.send_text(chat_alice.id, "Hi again!").await;
633        bob.recv_msg(&alice_msg2).await;
634        assert_eq!(
635            get_chat_msgs(&alice, chat_alice.id).await?.len(),
636            E2EE_INFO_MSGS + 2
637        );
638        assert_eq!(
639            get_chat_msgs(&bob, bob_msg.chat_id).await?.len(),
640            E2EE_INFO_MSGS + 2
641        );
642
643        bob_msg.chat_id.accept(&bob).await?;
644
645        bob.evtracker.clear_events();
646        send_reaction(&bob, bob_msg.id, "๐Ÿ‘").await.unwrap();
647        expect_reactions_changed_event(&bob, bob_msg.chat_id, bob_msg.id, ContactId::SELF).await?;
648        expect_no_unwanted_events(&bob).await;
649        assert_eq!(
650            get_chat_msgs(&bob, bob_msg.chat_id).await?.len(),
651            E2EE_INFO_MSGS + 2
652        );
653
654        let bob_reaction_msg = bob.pop_sent_msg().await;
655        let alice_reaction_msg = alice.recv_msg_hidden(&bob_reaction_msg).await;
656        assert_eq!(alice_reaction_msg.state, MessageState::InFresh);
657        assert_eq!(
658            get_chat_msgs(&alice, chat_alice.id).await?.len(),
659            E2EE_INFO_MSGS + 2
660        );
661
662        let reactions = get_msg_reactions(&alice, alice_msg.sender_msg_id).await?;
663        assert_eq!(reactions.to_string(), "๐Ÿ‘1");
664        let contacts = reactions.contacts();
665        assert_eq!(contacts.len(), 1);
666        let bob_id = contacts.first().unwrap();
667        let bob_reaction = reactions.get(*bob_id);
668        assert_eq!(bob_reaction.is_empty(), false);
669        assert_eq!(bob_reaction.as_str(), "๐Ÿ‘");
670        assert_eq!(bob_reaction.as_str(), "๐Ÿ‘");
671        expect_reactions_changed_event(&alice, chat_alice.id, alice_msg.sender_msg_id, *bob_id)
672            .await?;
673        expect_incoming_reactions_event(
674            &alice,
675            chat_alice.id,
676            alice_msg.sender_msg_id,
677            *bob_id,
678            "๐Ÿ‘",
679        )
680        .await?;
681        expect_no_unwanted_events(&alice).await;
682
683        marknoticed_chat(&alice, chat_alice.id).await?;
684        assert_eq!(
685            alice_reaction_msg.id.get_state(&alice).await?,
686            MessageState::InSeen
687        );
688        // Reactions don't request MDNs, but an MDN to self is sent.
689        assert_eq!(
690            alice
691                .sql
692                .count("SELECT COUNT(*) FROM smtp_mdns", ())
693                .await?,
694            1
695        );
696        assert_eq!(
697            alice
698                .sql
699                .count(
700                    "SELECT COUNT(*) FROM smtp_mdns WHERE from_id=?",
701                    (ContactId::SELF,)
702                )
703                .await?,
704            1
705        );
706
707        // Alice reacts to own message.
708        // Trying to set multiple reactions at once is not allowed.
709        send_reaction(&alice, alice_msg.sender_msg_id, "๐Ÿ‘ ๐Ÿ˜€")
710            .await
711            .unwrap();
712        let reactions = get_msg_reactions(&alice, alice_msg.sender_msg_id).await?;
713        assert_eq!(reactions.to_string(), "๐Ÿ‘2");
714
715        assert_eq!(
716            reactions.emoji_sorted_by_frequency(),
717            vec![("๐Ÿ‘".to_string(), 2)]
718        );
719
720        Ok(())
721    }
722
723    async fn assert_summary(t: &TestContext, expected: &str) {
724        let chatlist = Chatlist::try_load(t, 0, None, None).await.unwrap();
725        let summary = chatlist.get_summary(t, 0, None).await.unwrap();
726        assert_eq!(summary.text, expected);
727    }
728
729    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
730    async fn test_reaction_summary() -> Result<()> {
731        let mut tcm = TestContextManager::new();
732        let alice = tcm.alice().await;
733        let bob = tcm.bob().await;
734        alice.set_config(Config::Displayname, Some("ALICE")).await?;
735        bob.set_config(Config::Displayname, Some("BOB")).await?;
736        let alice_bob_id = alice.add_or_lookup_contact_id(&bob).await;
737
738        // Alice sends message to Bob
739        let alice_chat = alice.create_chat(&bob).await;
740        let alice_msg1 = alice.send_text(alice_chat.id, "Party?").await;
741        let bob_msg1 = bob.recv_msg(&alice_msg1).await;
742
743        // Bob reacts to Alice's message, this is shown in the summaries
744        SystemTime::shift(Duration::from_secs(10));
745        bob_msg1.chat_id.accept(&bob).await?;
746        send_reaction(&bob, bob_msg1.id, "๐Ÿ‘").await?;
747        let bob_send_reaction = bob.pop_sent_msg().await;
748        alice.recv_msg_hidden(&bob_send_reaction).await;
749        expect_incoming_reactions_event(
750            &alice,
751            alice_chat.id,
752            alice_msg1.sender_msg_id,
753            alice_bob_id,
754            "๐Ÿ‘",
755        )
756        .await?;
757        expect_no_unwanted_events(&alice).await;
758
759        let chatlist = Chatlist::try_load(&bob, 0, None, None).await?;
760        let summary = chatlist.get_summary(&bob, 0, None).await?;
761        assert_eq!(summary.text, "You reacted ๐Ÿ‘ to \"Party?\"");
762        assert_eq!(summary.timestamp, bob_msg1.get_timestamp()); // time refers to message, not to reaction
763        assert_eq!(summary.state, MessageState::InFresh); // state refers to message, not to reaction
764        assert!(summary.prefix.is_none());
765        assert!(summary.thumbnail_path.is_none());
766        assert_summary(&alice, "BOB reacted ๐Ÿ‘ to \"Party?\"").await;
767
768        // Alice reacts to own message as well
769        SystemTime::shift(Duration::from_secs(10));
770        send_reaction(&alice, alice_msg1.sender_msg_id, "๐Ÿฟ").await?;
771        let alice_send_reaction = alice.pop_sent_msg().await;
772        bob.evtracker.clear_events();
773        bob.recv_msg_opt(&alice_send_reaction).await;
774        expect_no_unwanted_events(&bob).await;
775
776        assert_summary(&alice, "You reacted ๐Ÿฟ to \"Party?\"").await;
777        assert_summary(&bob, "ALICE reacted ๐Ÿฟ to \"Party?\"").await;
778
779        // Alice sends a newer message, this overwrites reaction summaries
780        SystemTime::shift(Duration::from_secs(10));
781        let alice_msg2 = alice.send_text(alice_chat.id, "kewl").await;
782        bob.recv_msg(&alice_msg2).await;
783
784        assert_summary(&alice, "kewl").await;
785        assert_summary(&bob, "kewl").await;
786
787        // Reactions to older messages still overwrite newer messages
788        SystemTime::shift(Duration::from_secs(10));
789        send_reaction(&alice, alice_msg1.sender_msg_id, "๐Ÿค˜").await?;
790        let alice_send_reaction = alice.pop_sent_msg().await;
791        bob.recv_msg_opt(&alice_send_reaction).await;
792
793        assert_summary(&alice, "You reacted ๐Ÿค˜ to \"Party?\"").await;
794        assert_summary(&bob, "ALICE reacted ๐Ÿค˜ to \"Party?\"").await;
795
796        // Retracted reactions remove all summary reactions
797        SystemTime::shift(Duration::from_secs(10));
798        send_reaction(&alice, alice_msg1.sender_msg_id, "").await?;
799        let alice_remove_reaction = alice.pop_sent_msg().await;
800        bob.recv_msg_opt(&alice_remove_reaction).await;
801
802        assert_summary(&alice, "kewl").await;
803        assert_summary(&bob, "kewl").await;
804
805        // Alice adds another reaction and then deletes the message reacted to; this will also delete reaction summary
806        SystemTime::shift(Duration::from_secs(10));
807        send_reaction(&alice, alice_msg1.sender_msg_id, "๐Ÿงน").await?;
808        assert_summary(&alice, "You reacted ๐Ÿงน to \"Party?\"").await;
809
810        delete_msgs(&alice, &[alice_msg1.sender_msg_id]).await?; // this will leave a tombstone
811        assert_summary(&alice, "kewl").await;
812        housekeeping(&alice).await?; // this will delete the tombstone
813        assert_summary(&alice, "kewl").await;
814
815        Ok(())
816    }
817
818    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
819    async fn test_reaction_forwarded_summary() -> Result<()> {
820        let alice = TestContext::new_alice().await;
821
822        // Alice adds a message to "Saved Messages"
823        let self_chat = alice.get_self_chat().await;
824        let msg_id = send_text_msg(&alice, self_chat.id, "foo".to_string()).await?;
825        assert_summary(&alice, "foo").await;
826
827        // Alice reacts to that message
828        SystemTime::shift(Duration::from_secs(10));
829        send_reaction(&alice, msg_id, "๐Ÿซ").await?;
830        assert_summary(&alice, "You reacted ๐Ÿซ to \"foo\"").await;
831        let reactions = get_msg_reactions(&alice, msg_id).await?;
832        assert_eq!(reactions.reactions.len(), 1);
833
834        // Alice forwards that message to Bob: Reactions are not forwarded, the message is prefixed by "Forwarded".
835        let bob_id = Contact::create(&alice, "", "bob@example.net").await?;
836        let bob_chat_id = ChatId::create_for_contact(&alice, bob_id).await?;
837        forward_msgs(&alice, &[msg_id], bob_chat_id).await?;
838        assert_summary(&alice, "Forwarded: foo").await; // forwarded messages are prefixed
839        let chatlist = Chatlist::try_load(&alice, 0, None, None).await.unwrap();
840        let forwarded_msg_id = chatlist.get_msg_id(0)?.unwrap();
841        let reactions = get_msg_reactions(&alice, forwarded_msg_id).await?;
842        assert!(reactions.reactions.is_empty()); // reactions are not forwarded
843
844        // Alice reacts to forwarded message:
845        // For reaction summary neither original message author nor "Forwarded" prefix is shown
846        SystemTime::shift(Duration::from_secs(10));
847        send_reaction(&alice, forwarded_msg_id, "๐Ÿณ").await?;
848        assert_summary(&alice, "You reacted ๐Ÿณ to \"foo\"").await;
849        let reactions = get_msg_reactions(&alice, msg_id).await?;
850        assert_eq!(reactions.reactions.len(), 1);
851
852        Ok(())
853    }
854
855    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
856    async fn test_reaction_self_chat_multidevice_summary() -> Result<()> {
857        let alice0 = TestContext::new_alice().await;
858        let alice1 = TestContext::new_alice().await;
859        let chat = alice0.get_self_chat().await;
860
861        let msg_id = send_text_msg(&alice0, chat.id, "mom's birthday!".to_string()).await?;
862        alice1.recv_msg(&alice0.pop_sent_msg().await).await;
863
864        SystemTime::shift(Duration::from_secs(10));
865        send_reaction(&alice0, msg_id, "๐Ÿ‘†").await?;
866        let sync = alice0.pop_sent_msg().await;
867        receive_imf(&alice1, sync.payload().as_bytes(), false).await?;
868
869        assert_summary(&alice0, "You reacted ๐Ÿ‘† to \"mom's birthday!\"").await;
870        assert_summary(&alice1, "You reacted ๐Ÿ‘† to \"mom's birthday!\"").await;
871
872        Ok(())
873    }
874
875    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
876    async fn test_send_reaction_multidevice() -> Result<()> {
877        let mut tcm = TestContextManager::new();
878        let alice0 = tcm.alice().await;
879        let alice1 = tcm.alice().await;
880        let bob = tcm.bob().await;
881        let chat_id = alice0.create_chat(&bob).await.id;
882
883        let alice0_msg_id = send_text_msg(&alice0, chat_id, "foo".to_string()).await?;
884        let alice1_msg = alice1.recv_msg(&alice0.pop_sent_msg().await).await;
885
886        send_reaction(&alice0, alice0_msg_id, "๐Ÿ‘€").await?;
887        alice1.recv_msg_hidden(&alice0.pop_sent_msg().await).await;
888
889        expect_reactions_changed_event(&alice0, chat_id, alice0_msg_id, ContactId::SELF).await?;
890        expect_reactions_changed_event(&alice1, alice1_msg.chat_id, alice1_msg.id, ContactId::SELF)
891            .await?;
892        for a in [&alice0, &alice1] {
893            expect_no_unwanted_events(a).await;
894        }
895        Ok(())
896    }
897
898    /// Tests that if reaction requests a read receipt,
899    /// no read receipt is sent when the chat is marked as noticed.
900    ///
901    /// Reactions create hidden messages in the chat,
902    /// and when marking the chat as noticed marks
903    /// such messages as seen, read receipts should never be sent
904    /// to avoid the sender of reaction from learning
905    /// that receiver opened the chat.
906    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
907    async fn test_reaction_request_mdn() -> Result<()> {
908        let mut tcm = TestContextManager::new();
909        let alice = &tcm.alice().await;
910        let bob = &tcm.bob().await;
911
912        let alice_chat_id = alice.create_chat_id(bob).await;
913        let alice_sent_msg = alice.send_text(alice_chat_id, "Hello!").await;
914
915        let bob_msg = bob.recv_msg(&alice_sent_msg).await;
916        bob_msg.chat_id.accept(bob).await?;
917        assert_eq!(bob_msg.state, MessageState::InFresh);
918        let bob_chat_id = bob_msg.chat_id;
919        bob_chat_id.accept(bob).await?;
920
921        markseen_msgs(bob, vec![bob_msg.id]).await?;
922        assert_eq!(
923            bob.sql
924                .count(
925                    "SELECT COUNT(*) FROM smtp_mdns WHERE from_id!=?",
926                    (ContactId::SELF,)
927                )
928                .await?,
929            1
930        );
931        bob.sql.execute("DELETE FROM smtp_mdns", ()).await?;
932
933        // Construct reaction with an MDN request.
934        // Note the `Chat-Disposition-Notification-To` header.
935        let known_id = bob_msg.rfc724_mid;
936        let new_id = "e2b6e69e-4124-4e2a-b79f-e4f1be667165@localhost";
937
938        let plain_text = format!(
939            "Content-Type: text/plain; charset=\"utf-8\"; protected-headers=\"v1\"; \r
940        hp=\"cipher\"\r
941Content-Disposition: reaction\r
942From: \"Alice\" <alice@example.org>\r
943To: \"Bob\" <bob@example.net>\r
944Subject: Message from Alice\r
945Date: Sat, 14 Mar 2026 01:02:03 +0000\r
946In-Reply-To: <{known_id}>\r
947References: <{known_id}>\r
948Chat-Version: 1.0\r
949Chat-Disposition-Notification-To: alice@example.org\r
950Message-ID: <{new_id}>\r
951HP-Outer: From: <alice@example.org>\r
952HP-Outer: To: \"hidden-recipients\": ;\r
953HP-Outer: Subject: [...]\r
954HP-Outer: Date: Sat, 14 Mar 2026 01:02:03 +0000\r
955HP-Outer: Message-ID: <{new_id}>\r
956HP-Outer: In-Reply-To: <{known_id}>\r
957HP-Outer: References: <{known_id}>\r
958HP-Outer: Chat-Version: 1.0\r
959Content-Transfer-Encoding: base64\r
960\r
9618J+RgA==\r
962"
963        );
964
965        let alice_public_key = load_self_public_key(alice).await?;
966        let bob_public_key = load_self_public_key(bob).await?;
967        let alice_secret_key = load_self_secret_key(alice).await?;
968        let public_keys_for_encryption = vec![alice_public_key, bob_public_key];
969        let compress = true;
970        let encrypted_payload = pk_encrypt(
971            plain_text.as_bytes().to_vec(),
972            public_keys_for_encryption,
973            alice_secret_key,
974            compress,
975            SeipdVersion::V2,
976        )
977        .await?;
978
979        let boundary = "boundary123";
980        let rcvd_mail = format!(
981            "From: <alice@example.org>\r
982To: \"hidden-recipients\": ;\r
983Subject: [...]\r
984Date: Sat, 14 Mar 2026 01:02:03 +0000\r
985Message-ID: <{new_id}>\r
986In-Reply-To: <{known_id}>\r
987References: <{known_id}>\r
988Content-Type: multipart/encrypted; protocol=\"application/pgp-encrypted\";\r
989        boundary=\"{boundary}\"\r
990MIME-Version: 1.0\r
991\r
992--{boundary}\r
993Content-Type: application/pgp-encrypted; charset=\"utf-8\"\r
994Content-Description: PGP/MIME version identification\r
995Content-Transfer-Encoding: 7bit\r
996\r
997Version: 1\r
998\r
999--{boundary}\r
1000Content-Type: application/octet-stream; name=\"encrypted.asc\";\r
1001        charset=\"utf-8\"\r
1002Content-Description: OpenPGP encrypted message\r
1003Content-Disposition: inline; filename=\"encrypted.asc\";\r
1004Content-Transfer-Encoding: 7bit\r
1005\r
1006{encrypted_payload}
1007--{boundary}--\r
1008"
1009        );
1010
1011        let received = receive_imf(bob, rcvd_mail.as_bytes(), false)
1012            .await?
1013            .unwrap();
1014        let bob_hidden_msg = Message::load_from_db(bob, *received.msg_ids.last().unwrap())
1015            .await
1016            .unwrap();
1017        assert!(bob_hidden_msg.hidden);
1018        assert_eq!(bob_hidden_msg.chat_id, bob_chat_id);
1019
1020        // Bob does not see new message and cannot mark it as seen directly,
1021        // but can mark the chat as noticed when opening it.
1022        marknoticed_chat(bob, bob_chat_id).await?;
1023
1024        assert_eq!(
1025            bob.sql
1026                .count(
1027                    "SELECT COUNT(*) FROM smtp_mdns WHERE from_id!=?",
1028                    (ContactId::SELF,)
1029                )
1030                .await?,
1031            0,
1032            "Bob should not send MDN to Alice"
1033        );
1034
1035        // MDN request was ignored, but reaction was not.
1036        let reactions = get_msg_reactions(bob, bob_msg.id).await?;
1037        assert_eq!(reactions.reactions.len(), 1);
1038        assert_eq!(
1039            reactions.emoji_sorted_by_frequency(),
1040            vec![("๐Ÿ‘€".to_string(), 1)]
1041        );
1042
1043        Ok(())
1044    }
1045}