perf(reactions): bound reaction target search and bulk reparse

Fixes the ANR in #840, reported as "parsing emoji reactions" hanging after a
first sync. Three costs compounded, all inside a single Realm transaction:

- The bulk reparse cleared isEmojiReaction by walking every message in the
  database and writing the field whether or not it was set. Query the flagged
  messages instead, which on a typical history is a handful rather than tens of
  thousands.

- findTargetMessage loaded every message in the thread and ran a regex over each
  one, and it runs once per reaction. That product is quadratic across an
  imported history. Bound it: a reaction can only target a message that already
  existed, so filter by date, and cap the scan at the 200 most recent
  candidates. A reaction refers to something the sender can still see, so a
  target further back is not plausible.

- An untruncated quote, the common case, now matches natively in Realm on the
  body column instead of materialising each candidate's text through getText(),
  which walks MMS parts. MMS still falls through to the scan, since its text
  lives in parts rather than body.

The date filter allows a minute of slack, because clock differences between two
devices can timestamp a reaction fractionally before the message it targets.

Also fixes the progress bar, which counted only messages that turned out to be
reactions while max counted every message examined. Progress could therefore
never reach max, so the bar never completed and the final callback never fired.

reactionDate is optional on the interface so existing callers keep compiling;
all three in-tree callers now pass it.
This commit is contained in:
octoshrimpy
2026-08-01 14:49:00 -04:00
parent 7e44142b4e
commit 1394e6e5a2
4 changed files with 90 additions and 28 deletions
@@ -26,7 +26,16 @@ data class ParsedEmojiReaction(val emoji: String, val originalMessage: String, v
interface EmojiReactionRepository {
fun parseEmojiReaction(body: String): ParsedEmojiReaction?
fun findTargetMessage(threadId: Long, originalMessageText: String, realm: Realm): Message?
/**
* [reactionDate] bounds the search to messages that already existed when the reaction was
* sent. Omitting it searches the whole thread, which is slow on a long history.
*/
fun findTargetMessage(
threadId: Long,
originalMessageText: String,
realm: Realm,
reactionDate: Long? = null,
): Message?
fun saveEmojiReaction(
reactionMessage: Message,