deltachat/
reaction.rs

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