1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//! # Reactions.
//!
//! Reactions are short messages consisting of emojis sent in reply to
//! messages. Unlike normal messages which are added to the end of the chat,
//! reactions are supposed to be displayed near the original messages.
//!
//! RFC 9078 specifies how reactions are transmitted in MIME messages.
//!
//! Reaction update semantics is not well-defined in RFC 9078, so
//! Delta Chat uses the same semantics as in
//! [XEP-0444](https://xmpp.org/extensions/xep-0444.html) section
//! "3.2 Updating reactions to a message". Received reactions override
//! all previously received reactions from the same user and it is
//! possible to remove all reactions by sending an empty string as a reaction,
//! even though RFC 9078 requires at least one emoji to be sent.

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt;

use anyhow::Result;

use crate::chat::{send_msg, Chat, ChatId};
use crate::chatlist_events;
use crate::contact::ContactId;
use crate::context::Context;
use crate::events::EventType;
use crate::message::{rfc724_mid_exists, Message, MsgId, Viewtype};
use crate::param::Param;

/// A single reaction consisting of multiple emoji sequences.
///
/// It is guaranteed to have all emojis sorted and deduplicated inside.
#[derive(Debug, Default, Clone)]
pub struct Reaction {
    /// Canonical representation of reaction as a string of space-separated emojis.
    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 {
    /// Parses a string containing a reaction.
    ///
    /// Reaction string is separated by spaces or tabs (`WSP` in ABNF),
    /// but this function accepts any ASCII whitespace, so even a CRLF at
    /// the end of string is acceptable.
    ///
    /// Any short enough string is accepted as a reaction to avoid the
    /// complexity of validating emoji sequences as required by RFC
    /// 9078. On the sender side UI is responsible to provide only
    /// valid emoji sequences via reaction picker. On the receiver
    /// side, abuse of the possibility to use arbitrary strings as
    /// 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 {
        let mut emojis: Vec<&str> = reaction
            .split_ascii_whitespace()
            .filter(|&emoji| emoji.len() < 30)
            .collect();
        emojis.sort_unstable();
        emojis.dedup();
        let reaction = emojis.join(" ");
        Self { reaction }
    }
}

impl Reaction {
    /// Returns true if reaction contains no emojis.
    pub fn is_empty(&self) -> bool {
        self.reaction.is_empty()
    }

    /// Returns a vector of emojis composing a reaction.
    pub fn emojis(&self) -> Vec<&str> {
        self.reaction.split(' ').collect()
    }

    /// Returns space-separated string of emojis
    pub fn as_str(&self) -> &str {
        &self.reaction
    }

    /// Appends emojis from another reaction to this reaction.
    pub fn add(&self, other: Self) -> Self {
        let mut emojis: Vec<&str> = self.emojis();
        emojis.append(&mut other.emojis());
        emojis.sort_unstable();
        emojis.dedup();
        let reaction = emojis.join(" ");
        Self { reaction }
    }
}

/// Structure representing all reactions to a particular message.
#[derive(Debug)]
pub struct Reactions {
    /// Map from a contact to its reaction to message.
    reactions: BTreeMap<ContactId, Reaction>,
}

impl Reactions {
    /// Returns vector of contacts that reacted to the message.
    pub fn contacts(&self) -> Vec<ContactId> {
        self.reactions.keys().copied().collect()
    }

    /// Returns reaction of a given contact to message.
    ///
    /// If contact did not react to message or removed the reaction,
    /// this method returns an empty reaction.
    pub fn get(&self, contact_id: ContactId) -> Reaction {
        self.reactions.get(&contact_id).cloned().unwrap_or_default()
    }

    /// Returns true if the message has no reactions.
    pub fn is_empty(&self) -> bool {
        self.reactions.is_empty()
    }

    /// Returns a map from emojis to their frequencies.
    pub fn emoji_frequencies(&self) -> BTreeMap<String, usize> {
        let mut emoji_frequencies: BTreeMap<String, usize> = BTreeMap::new();
        for reaction in self.reactions.values() {
            for emoji in reaction.emojis() {
                emoji_frequencies
                    .entry(emoji.to_string())
                    .and_modify(|x| *x += 1)
                    .or_insert(1);
            }
        }
        emoji_frequencies
    }

    /// Returns a vector of emojis
    /// sorted in descending order of frequencies.
    ///
    /// This function can be used to display the reactions in
    /// the message bubble in the UIs.
    pub fn emoji_sorted_by_frequency(&self) -> Vec<(String, usize)> {
        let mut emoji_frequencies: Vec<(String, usize)> =
            self.emoji_frequencies().into_iter().collect();
        emoji_frequencies.sort_by(|(a, a_count), (b, b_count)| {
            match a_count.cmp(b_count).reverse() {
                Ordering::Equal => a.cmp(b),
                other => other,
            }
        });
        emoji_frequencies
    }
}

impl fmt::Display for Reactions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let emoji_frequencies = self.emoji_sorted_by_frequency();
        let mut first = true;
        for (emoji, frequency) in emoji_frequencies {
            if !first {
                write!(f, " ")?;
            }
            first = false;
            write!(f, "{emoji}{frequency}")?;
        }
        Ok(())
    }
}

async fn set_msg_id_reaction(
    context: &Context,
    msg_id: MsgId,
    chat_id: ChatId,
    contact_id: ContactId,
    timestamp: i64,
    reaction: Reaction,
) -> Result<()> {
    if reaction.is_empty() {
        // Simply remove the record instead of setting it to empty string.
        context
            .sql
            .execute(
                "DELETE FROM reactions
                 WHERE msg_id = ?1
                 AND contact_id = ?2",
                (msg_id, contact_id),
            )
            .await?;
    } else {
        context
            .sql
            .execute(
                "INSERT INTO reactions (msg_id, contact_id, reaction)
                 VALUES (?1, ?2, ?3)
                 ON CONFLICT(msg_id, contact_id)
                 DO UPDATE SET reaction=excluded.reaction",
                (msg_id, contact_id, reaction.as_str()),
            )
            .await?;
        let mut chat = Chat::load_from_db(context, chat_id).await?;
        if chat
            .param
            .update_timestamp(Param::LastReactionTimestamp, timestamp)?
        {
            chat.param
                .set_i64(Param::LastReactionMsgId, i64::from(msg_id.to_u32()));
            chat.param
                .set_i64(Param::LastReactionContactId, i64::from(contact_id.to_u32()));
            chat.update_param(context).await?;
        }
    }

    context.emit_event(EventType::ReactionsChanged {
        chat_id,
        msg_id,
        contact_id,
    });
    chatlist_events::emit_chatlist_item_changed(context, chat_id);
    Ok(())
}

/// Sends a reaction to message `msg_id`, overriding previously sent reactions.
///
/// `reaction` is a string consisting of space-separated emoji. Use
/// empty string to retract a reaction.
pub async fn send_reaction(context: &Context, msg_id: MsgId, reaction: &str) -> Result<MsgId> {
    let msg = Message::load_from_db(context, msg_id).await?;
    let chat_id = msg.chat_id;

    let reaction: Reaction = reaction.into();
    let mut reaction_msg = Message::new(Viewtype::Text);
    reaction_msg.text = reaction.as_str().to_string();
    reaction_msg.set_reaction();
    reaction_msg.in_reply_to = Some(msg.rfc724_mid);
    reaction_msg.hidden = true;

    // Send message first.
    let reaction_msg_id = send_msg(context, chat_id, &mut reaction_msg).await?;

    // Only set reaction if we successfully sent the message.
    set_msg_id_reaction(
        context,
        msg_id,
        msg.chat_id,
        ContactId::SELF,
        reaction_msg.timestamp_sort,
        reaction,
    )
    .await?;
    Ok(reaction_msg_id)
}

/// Adds given reaction to message `msg_id` and sends an update.
///
/// This can be used to implement advanced clients that allow reacting
/// with multiple emojis. For a simple messenger UI, you probably want
/// to use [`send_reaction()`] instead so reacting with a new emoji
/// removes previous emoji at the same time.
pub async fn add_reaction(context: &Context, msg_id: MsgId, reaction: &str) -> Result<MsgId> {
    let self_reaction = get_self_reaction(context, msg_id).await?;
    let reaction = self_reaction.add(Reaction::from(reaction));
    send_reaction(context, msg_id, reaction.as_str()).await
}

/// Updates reaction of `contact_id` on the message with `in_reply_to`
/// Message-ID. If no such message is found in the database, reaction
/// is ignored.
///
/// `reaction` is a space-separated string of emojis. It can be empty
/// if contact wants to remove all reactions.
pub(crate) async fn set_msg_reaction(
    context: &Context,
    in_reply_to: &str,
    chat_id: ChatId,
    contact_id: ContactId,
    timestamp: i64,
    reaction: Reaction,
) -> Result<()> {
    if let Some((msg_id, _)) = rfc724_mid_exists(context, in_reply_to).await? {
        set_msg_id_reaction(context, msg_id, chat_id, contact_id, timestamp, reaction).await
    } else {
        info!(
            context,
            "Can't assign reaction to unknown message with Message-ID {}", in_reply_to
        );
        Ok(())
    }
}

/// Get our own reaction for a given message.
async fn get_self_reaction(context: &Context, msg_id: MsgId) -> Result<Reaction> {
    let reaction_str: Option<String> = context
        .sql
        .query_get_value(
            "SELECT reaction
             FROM reactions
             WHERE msg_id=? AND contact_id=?",
            (msg_id, ContactId::SELF),
        )
        .await?;
    Ok(reaction_str
        .as_deref()
        .map(Reaction::from)
        .unwrap_or_default())
}

/// Returns a structure containing all reactions to the message.
pub async fn get_msg_reactions(context: &Context, msg_id: MsgId) -> Result<Reactions> {
    let reactions = context
        .sql
        .query_map(
            "SELECT contact_id, reaction FROM reactions WHERE msg_id=?",
            (msg_id,),
            |row| {
                let contact_id: ContactId = row.get(0)?;
                let reaction: String = row.get(1)?;
                Ok((contact_id, reaction))
            },
            |rows| {
                let mut reactions = Vec::new();
                for row in rows {
                    let (contact_id, reaction) = row?;
                    reactions.push((contact_id, Reaction::from(reaction.as_str())));
                }
                Ok(reactions)
            },
        )
        .await?
        .into_iter()
        .collect();
    Ok(Reactions { reactions })
}

impl Chat {
    /// Check if there is a reaction newer than the given timestamp.
    ///
    /// If so, reaction details are returned and can be used to create a summary string.
    pub async fn get_last_reaction_if_newer_than(
        &self,
        context: &Context,
        timestamp: i64,
    ) -> Result<Option<(Message, ContactId, String)>> {
        if self
            .param
            .get_i64(Param::LastReactionTimestamp)
            .filter(|&reaction_timestamp| reaction_timestamp > timestamp)
            .is_none()
        {
            return Ok(None);
        };
        let reaction_msg_id = MsgId::new(
            self.param
                .get_int(Param::LastReactionMsgId)
                .unwrap_or_default() as u32,
        );
        let Some(reaction_msg) = Message::load_from_db_optional(context, reaction_msg_id).await?
        else {
            // The message reacted to may be deleted.
            // These are no errors as `Param::LastReaction*` are just weak pointers.
            // Instead, just return `Ok(None)` and let the caller create another summary.
            return Ok(None);
        };
        let reaction_contact_id = ContactId::new(
            self.param
                .get_int(Param::LastReactionContactId)
                .unwrap_or_default() as u32,
        );
        if let Some(reaction) = context
            .sql
            .query_get_value(
                "SELECT reaction FROM reactions WHERE msg_id=? AND contact_id=?",
                (reaction_msg.id, reaction_contact_id),
            )
            .await?
        {
            Ok(Some((reaction_msg, reaction_contact_id, reaction)))
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
mod tests {
    use deltachat_contact_tools::ContactAddress;

    use super::*;
    use crate::chat::{forward_msgs, get_chat_msgs, send_text_msg};
    use crate::chatlist::Chatlist;
    use crate::config::Config;
    use crate::contact::{Contact, Origin};
    use crate::download::DownloadState;
    use crate::message::{delete_msgs, MessageState};
    use crate::receive_imf::{receive_imf, receive_imf_from_inbox};
    use crate::sql::housekeeping;
    use crate::test_utils::TestContext;
    use crate::test_utils::TestContextManager;
    use crate::tools::SystemTime;
    use std::time::Duration;

    #[test]
    fn test_parse_reaction() {
        // Check that basic set of emojis from RFC 9078 is supported.
        assert_eq!(Reaction::from("👍").emojis(), vec!["👍"]);
        assert_eq!(Reaction::from("👎").emojis(), vec!["👎"]);
        assert_eq!(Reaction::from("😀").emojis(), vec!["😀"]);
        assert_eq!(Reaction::from("☹").emojis(), vec!["☹"]);
        assert_eq!(Reaction::from("😢").emojis(), vec!["😢"]);

        // Empty string can be used to remove all reactions.
        assert!(Reaction::from("").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:").emojis(), vec![":deltacat:"]);

        // Check that long strings are not valid emojis.
        assert!(
            Reaction::from(":foobarbazquuxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:").is_empty()
        );

        // Multiple reactions separated by spaces or tabs are supported.
        assert_eq!(Reaction::from("👍 ❤").emojis(), vec!["❤", "👍"]);
        assert_eq!(Reaction::from("👍\t❤").emojis(), vec!["❤", "👍"]);

        // Invalid emojis are removed, but valid emojis are retained.
        assert_eq!(
            Reaction::from("👍\t:foo: ❤").emojis(),
            vec![":foo:", "❤", "👍"]
        );
        assert_eq!(Reaction::from("👍\t:foo: ❤").as_str(), ":foo: ❤ 👍");

        // Duplicates are removed.
        assert_eq!(Reaction::from("👍 👍").emojis(), vec!["👍"]);
    }

    #[test]
    fn test_add_reaction() {
        let reaction1 = Reaction::from("👍 😀");
        let reaction2 = Reaction::from("❤");
        let reaction_sum = reaction1.add(reaction2);

        assert_eq!(reaction_sum.emojis(), vec!["❤", "👍", "😀"]);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_receive_reaction() -> Result<()> {
        let alice = TestContext::new_alice().await;

        // Alice receives BCC-self copy of a message sent to Bob.
        receive_imf(
            &alice,
            "To: bob@example.net\n\
From: alice@example.org\n\
Date: Today, 29 February 2021 00:00:00 -800\n\
Message-ID: 12345@example.org\n\
Subject: Meeting\n\
\n\
Can we chat at 1pm pacific, today?"
                .as_bytes(),
            false,
        )
        .await?;
        let msg = alice.get_last_msg().await;
        assert_eq!(msg.state, MessageState::OutDelivered);
        let reactions = get_msg_reactions(&alice, msg.id).await?;
        let contacts = reactions.contacts();
        assert_eq!(contacts.len(), 0);

        let bob_id = Contact::add_or_lookup(
            &alice,
            "",
            &ContactAddress::new("bob@example.net")?,
            Origin::ManuallyCreated,
        )
        .await?
        .0;
        let bob_reaction = reactions.get(bob_id);
        assert!(bob_reaction.is_empty()); // Bob has not reacted to message yet.

        // Alice receives reaction to her message from Bob.
        receive_imf(
            &alice,
            "To: alice@example.org\n\
From: bob@example.net\n\
Date: Today, 29 February 2021 00:00:10 -800\n\
Message-ID: 56789@example.net\n\
In-Reply-To: 12345@example.org\n\
Subject: Meeting\n\
Mime-Version: 1.0 (1.0)\n\
Content-Type: text/plain; charset=utf-8\n\
Content-Disposition: reaction\n\
\n\
\u{1F44D}"
                .as_bytes(),
            false,
        )
        .await?;

        let reactions = get_msg_reactions(&alice, msg.id).await?;
        assert_eq!(reactions.to_string(), "👍1");

        let contacts = reactions.contacts();
        assert_eq!(contacts.len(), 1);

        assert_eq!(contacts.first(), Some(&bob_id));
        let bob_reaction = reactions.get(bob_id);
        assert_eq!(bob_reaction.is_empty(), false);
        assert_eq!(bob_reaction.emojis(), vec!["👍"]);
        assert_eq!(bob_reaction.as_str(), "👍");

        // Alice receives reaction to her message from Bob with a footer.
        receive_imf(
            &alice,
            "To: alice@example.org\n\
From: bob@example.net\n\
Date: Today, 29 February 2021 00:00:10 -800\n\
Message-ID: 56790@example.net\n\
In-Reply-To: 12345@example.org\n\
Subject: Meeting\n\
Mime-Version: 1.0 (1.0)\n\
Content-Type: text/plain; charset=utf-8\n\
Content-Disposition: reaction\n\
\n\
😀\n\
\n\
--\n\
_______________________________________________\n\
Here's my footer -- bob@example.net"
                .as_bytes(),
            false,
        )
        .await?;

        let reactions = get_msg_reactions(&alice, msg.id).await?;
        assert_eq!(reactions.to_string(), "😀1");

        Ok(())
    }

    async fn expect_reactions_changed_event(
        t: &TestContext,
        expected_chat_id: ChatId,
        expected_msg_id: MsgId,
        expected_contact_id: ContactId,
    ) -> Result<()> {
        let event = t
            .evtracker
            .get_matching(|evt| matches!(evt, EventType::ReactionsChanged { .. }))
            .await;
        match event {
            EventType::ReactionsChanged {
                chat_id,
                msg_id,
                contact_id,
            } => {
                assert_eq!(chat_id, expected_chat_id);
                assert_eq!(msg_id, expected_msg_id);
                assert_eq!(contact_id, expected_contact_id);
            }
            _ => unreachable!(),
        }
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_send_reaction() -> Result<()> {
        let alice = TestContext::new_alice().await;
        let bob = TestContext::new_bob().await;

        // Test that the status does not get mixed up into reactions.
        alice
            .set_config(
                Config::Selfstatus,
                Some("Buy Delta Chat today and make this banner go away!"),
            )
            .await?;
        bob.set_config(Config::Selfstatus, Some("Sent from my Delta Chat Pro. 👍"))
            .await?;

        let chat_alice = alice.create_chat(&bob).await;
        let alice_msg = alice.send_text(chat_alice.id, "Hi!").await;
        let bob_msg = bob.recv_msg(&alice_msg).await;
        assert_eq!(get_chat_msgs(&alice, chat_alice.id).await?.len(), 1);
        assert_eq!(get_chat_msgs(&bob, bob_msg.chat_id).await?.len(), 1);

        let alice_msg2 = alice.send_text(chat_alice.id, "Hi again!").await;
        bob.recv_msg(&alice_msg2).await;
        assert_eq!(get_chat_msgs(&alice, chat_alice.id).await?.len(), 2);
        assert_eq!(get_chat_msgs(&bob, bob_msg.chat_id).await?.len(), 2);

        bob_msg.chat_id.accept(&bob).await?;

        send_reaction(&bob, bob_msg.id, "👍").await.unwrap();
        expect_reactions_changed_event(&bob, bob_msg.chat_id, bob_msg.id, ContactId::SELF).await?;
        assert_eq!(get_chat_msgs(&bob, bob_msg.chat_id).await?.len(), 2);

        let bob_reaction_msg = bob.pop_sent_msg().await;
        alice.recv_msg_trash(&bob_reaction_msg).await;
        assert_eq!(get_chat_msgs(&alice, chat_alice.id).await?.len(), 2);

        let reactions = get_msg_reactions(&alice, alice_msg.sender_msg_id).await?;
        assert_eq!(reactions.to_string(), "👍1");
        let contacts = reactions.contacts();
        assert_eq!(contacts.len(), 1);
        let bob_id = contacts.first().unwrap();
        let bob_reaction = reactions.get(*bob_id);
        assert_eq!(bob_reaction.is_empty(), false);
        assert_eq!(bob_reaction.emojis(), vec!["👍"]);
        assert_eq!(bob_reaction.as_str(), "👍");
        expect_reactions_changed_event(&alice, chat_alice.id, alice_msg.sender_msg_id, *bob_id)
            .await?;

        // Alice reacts to own message.
        send_reaction(&alice, alice_msg.sender_msg_id, "👍 😀")
            .await
            .unwrap();
        let reactions = get_msg_reactions(&alice, alice_msg.sender_msg_id).await?;
        assert_eq!(reactions.to_string(), "👍2 😀1");

        assert_eq!(
            reactions.emoji_sorted_by_frequency(),
            vec![("👍".to_string(), 2), ("😀".to_string(), 1)]
        );

        Ok(())
    }

    async fn assert_summary(t: &TestContext, expected: &str) {
        let chatlist = Chatlist::try_load(t, 0, None, None).await.unwrap();
        let summary = chatlist.get_summary(t, 0, None).await.unwrap();
        assert_eq!(summary.text, expected);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_reaction_summary() -> Result<()> {
        let alice = TestContext::new_alice().await;
        let bob = TestContext::new_bob().await;
        alice.set_config(Config::Displayname, Some("ALICE")).await?;
        bob.set_config(Config::Displayname, Some("BOB")).await?;

        // Alice sends message to Bob
        let alice_chat = alice.create_chat(&bob).await;
        let alice_msg1 = alice.send_text(alice_chat.id, "Party?").await;
        let bob_msg1 = bob.recv_msg(&alice_msg1).await;

        // Bob reacts to Alice's message, this is shown in the summaries
        SystemTime::shift(Duration::from_secs(10));
        bob_msg1.chat_id.accept(&bob).await?;
        send_reaction(&bob, bob_msg1.id, "👍").await?;
        let bob_send_reaction = bob.pop_sent_msg().await;
        alice.recv_msg_trash(&bob_send_reaction).await;

        let chatlist = Chatlist::try_load(&bob, 0, None, None).await?;
        let summary = chatlist.get_summary(&bob, 0, None).await?;
        assert_eq!(summary.text, "You reacted 👍 to \"Party?\"");
        assert_eq!(summary.timestamp, bob_msg1.get_timestamp()); // time refers to message, not to reaction
        assert_eq!(summary.state, MessageState::InFresh); // state refers to message, not to reaction
        assert!(summary.prefix.is_none());
        assert!(summary.thumbnail_path.is_none());
        assert_summary(&alice, "BOB reacted 👍 to \"Party?\"").await;

        // Alice reacts to own message as well
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, alice_msg1.sender_msg_id, "🍿").await?;
        let alice_send_reaction = alice.pop_sent_msg().await;
        bob.recv_msg_opt(&alice_send_reaction).await;

        assert_summary(&alice, "You reacted 🍿 to \"Party?\"").await;
        assert_summary(&bob, "ALICE reacted 🍿 to \"Party?\"").await;

        // Alice sends a newer message, this overwrites reaction summaries
        SystemTime::shift(Duration::from_secs(10));
        let alice_msg2 = alice.send_text(alice_chat.id, "kewl").await;
        bob.recv_msg(&alice_msg2).await;

        assert_summary(&alice, "kewl").await;
        assert_summary(&bob, "kewl").await;

        // Reactions to older messages still overwrite newer messages
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, alice_msg1.sender_msg_id, "🤘").await?;
        let alice_send_reaction = alice.pop_sent_msg().await;
        bob.recv_msg_opt(&alice_send_reaction).await;

        assert_summary(&alice, "You reacted 🤘 to \"Party?\"").await;
        assert_summary(&bob, "ALICE reacted 🤘 to \"Party?\"").await;

        // Retracted reactions remove all summary reactions
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, alice_msg1.sender_msg_id, "").await?;
        let alice_remove_reaction = alice.pop_sent_msg().await;
        bob.recv_msg_opt(&alice_remove_reaction).await;

        assert_summary(&alice, "kewl").await;
        assert_summary(&bob, "kewl").await;

        // Alice adds another reaction and then deletes the message reacted to; this will also delete reaction summary
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, alice_msg1.sender_msg_id, "🧹").await?;
        assert_summary(&alice, "You reacted 🧹 to \"Party?\"").await;

        delete_msgs(&alice, &[alice_msg1.sender_msg_id]).await?; // this will leave a tombstone
        assert_summary(&alice, "kewl").await;
        housekeeping(&alice).await?; // this will delete the tombstone
        assert_summary(&alice, "kewl").await;

        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_reaction_forwarded_summary() -> Result<()> {
        let alice = TestContext::new_alice().await;

        // Alice adds a message to "Saved Messages"
        let self_chat = alice.get_self_chat().await;
        let msg_id = send_text_msg(&alice, self_chat.id, "foo".to_string()).await?;
        assert_summary(&alice, "foo").await;

        // Alice reacts to that message
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, msg_id, "🐫").await?;
        assert_summary(&alice, "You reacted 🐫 to \"foo\"").await;
        let reactions = get_msg_reactions(&alice, msg_id).await?;
        assert_eq!(reactions.reactions.len(), 1);

        // Alice forwards that message to Bob: Reactions are not forwarded, the message is prefixed by "Forwarded".
        let bob_id = Contact::create(&alice, "", "bob@example.net").await?;
        let bob_chat_id = ChatId::create_for_contact(&alice, bob_id).await?;
        forward_msgs(&alice, &[msg_id], bob_chat_id).await?;
        assert_summary(&alice, "Forwarded: foo").await; // forwarded messages are prefixed
        let chatlist = Chatlist::try_load(&alice, 0, None, None).await.unwrap();
        let forwarded_msg_id = chatlist.get_msg_id(0)?.unwrap();
        let reactions = get_msg_reactions(&alice, forwarded_msg_id).await?;
        assert!(reactions.reactions.is_empty()); // reactions are not forwarded

        // Alice reacts to forwarded message:
        // For reaction summary neither original message author nor "Forwarded" prefix is shown
        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice, forwarded_msg_id, "🐳").await?;
        assert_summary(&alice, "You reacted 🐳 to \"foo\"").await;
        let reactions = get_msg_reactions(&alice, msg_id).await?;
        assert_eq!(reactions.reactions.len(), 1);

        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_reaction_self_chat_multidevice_summary() -> Result<()> {
        let alice0 = TestContext::new_alice().await;
        let alice1 = TestContext::new_alice().await;
        let chat = alice0.get_self_chat().await;

        let msg_id = send_text_msg(&alice0, chat.id, "mom's birthday!".to_string()).await?;
        alice1.recv_msg(&alice0.pop_sent_msg().await).await;

        SystemTime::shift(Duration::from_secs(10));
        send_reaction(&alice0, msg_id, "👆").await?;
        let sync = alice0.pop_sent_msg().await;
        receive_imf(&alice1, sync.payload().as_bytes(), false).await?;

        assert_summary(&alice0, "You reacted 👆 to \"mom's birthday!\"").await;
        assert_summary(&alice1, "You reacted 👆 to \"mom's birthday!\"").await;

        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_partial_download_and_reaction() -> Result<()> {
        let alice = TestContext::new_alice().await;
        let bob = TestContext::new_bob().await;

        alice
            .create_chat_with_contact("Bob", "bob@example.net")
            .await;

        let msg_header = "From: Bob <bob@example.net>\n\
                    To: Alice <alice@example.org>\n\
                    Chat-Version: 1.0\n\
                    Subject: subject\n\
                    Message-ID: <first@example.org>\n\
                    Date: Sun, 14 Nov 2021 00:10:00 +0000\
                    Content-Type: text/plain";
        let msg_full = format!("{msg_header}\n\n100k text...");

        // Alice downloads message from Bob partially.
        let alice_received_message = receive_imf_from_inbox(
            &alice,
            "first@example.org",
            msg_header.as_bytes(),
            false,
            Some(100000),
            false,
        )
        .await?
        .unwrap();
        let alice_msg_id = *alice_received_message.msg_ids.first().unwrap();

        // Bob downloads own message on the other device.
        let bob_received_message = receive_imf(&bob, msg_full.as_bytes(), false)
            .await?
            .unwrap();
        let bob_msg_id = *bob_received_message.msg_ids.first().unwrap();

        // Bob reacts to own message.
        send_reaction(&bob, bob_msg_id, "👍").await.unwrap();
        let bob_reaction_msg = bob.pop_sent_msg().await;

        // Alice receives a reaction.
        alice.recv_msg_trash(&bob_reaction_msg).await;

        let reactions = get_msg_reactions(&alice, alice_msg_id).await?;
        assert_eq!(reactions.to_string(), "👍1");
        let msg = Message::load_from_db(&alice, alice_msg_id).await?;
        assert_eq!(msg.download_state(), DownloadState::Available);

        // Alice downloads full message.
        receive_imf_from_inbox(
            &alice,
            "first@example.org",
            msg_full.as_bytes(),
            false,
            None,
            false,
        )
        .await?;

        // Check that reaction is still on the message after full download.
        let msg = Message::load_from_db(&alice, alice_msg_id).await?;
        assert_eq!(msg.download_state(), DownloadState::Done);
        let reactions = get_msg_reactions(&alice, alice_msg_id).await?;
        assert_eq!(reactions.to_string(), "👍1");

        Ok(())
    }

    /// Regression test for reaction resetting self-status.
    ///
    /// Reactions do not contain the status,
    /// but should not result in self-status being reset on other devices.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_reaction_status_multidevice() -> Result<()> {
        let mut tcm = TestContextManager::new();
        let alice1 = tcm.alice().await;
        let alice2 = tcm.alice().await;

        alice1
            .set_config(Config::Selfstatus, Some("New status"))
            .await?;

        let alice2_msg = tcm.send_recv(&alice1, &alice2, "Hi!").await;
        assert_eq!(
            alice2.get_config(Config::Selfstatus).await?.as_deref(),
            Some("New status")
        );

        // Alice reacts to own message from second device,
        // first device receives rection.
        {
            send_reaction(&alice2, alice2_msg.id, "👍").await?;
            let msg = alice2.pop_sent_msg().await;
            alice1.recv_msg_trash(&msg).await;
        }

        // Check that the status is still the same.
        assert_eq!(
            alice1.get_config(Config::Selfstatus).await?.as_deref(),
            Some("New status")
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_send_reaction_multidevice() -> Result<()> {
        let alice0 = TestContext::new_alice().await;
        let alice1 = TestContext::new_alice().await;
        let bob_id = Contact::create(&alice0, "", "bob@example.net").await?;
        let chat_id = ChatId::create_for_contact(&alice0, bob_id).await?;

        let alice0_msg_id = send_text_msg(&alice0, chat_id, "foo".to_string()).await?;
        let alice1_msg = alice1.recv_msg(&alice0.pop_sent_msg().await).await;

        send_reaction(&alice0, alice0_msg_id, "👀").await?;
        alice1.recv_msg_trash(&alice0.pop_sent_msg().await).await;

        expect_reactions_changed_event(&alice0, chat_id, alice0_msg_id, ContactId::SELF).await?;
        expect_reactions_changed_event(&alice1, alice1_msg.chat_id, alice1_msg.id, ContactId::SELF)
            .await?;

        Ok(())
    }
}