forked from upstream-mirrors/quik
feat(reactions): send emoji reactions via quik-encoded SMS/MMS
Long-press a message to open an emoji reaction picker. Reactions are sent as a plain SMS/MMS body using a quik-specific zero-width encoding that carries the original sender's address and the message's timestamp, so a receiving quik instance can attach the reaction to the exact message via address + date lookup (fast path), falling back to the existing text matching for non-quik senders. The quik pattern is registered first so it wins over the generic Google Messages pattern; iOS/Google parsing is unchanged. New ParsedEmojiReaction fields are in-memory only, so no Realm migration is needed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -36,6 +36,16 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
) : EmojiReactionRepository {
|
||||
// We use an ordered map to make sure we can test tapback regexes before generic ones
|
||||
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
||||
Regex( // quik-to-quik (must be first so it wins over the generic Google Messages pattern)
|
||||
"(?s)^\u200aQUIK\u200b([^\u200b]*)\u200b([^\u200b]*)\u200b([^\u200b]*)\u200a(.*)\u200aQUIK\u200a\\Z"
|
||||
) to { match ->
|
||||
ParsedEmojiReaction(
|
||||
emoji = match.groupValues[1],
|
||||
originalMessage = match.groupValues[4],
|
||||
quikSenderAddress = match.groupValues[2],
|
||||
quikTimestamp = match.groupValues[3].toLongOrNull(),
|
||||
)
|
||||
},
|
||||
Regex( // Google Messages
|
||||
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
||||
) to { match ->
|
||||
@@ -147,6 +157,13 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
return null
|
||||
}
|
||||
|
||||
override fun buildReactionBody(emoji: String, targetMessage: Message): String {
|
||||
val senderAddress = targetMessage.address
|
||||
val timestampMs = targetMessage.date
|
||||
val originalText = targetMessage.getText(false)
|
||||
return "\u200aQUIK\u200b$emoji\u200b$senderAddress\u200b$timestampMs\u200a$originalText\u200aQUIK\u200a"
|
||||
}
|
||||
|
||||
private fun parseRemoval(body: String): ParsedEmojiReaction? {
|
||||
for ((pattern, parser) in removalPatterns) {
|
||||
val match = pattern.find(body) ?: continue
|
||||
@@ -180,8 +197,39 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
override fun findTargetMessage(
|
||||
threadId: Long,
|
||||
originalMessageText: String,
|
||||
realm: Realm
|
||||
realm: Realm,
|
||||
quikSenderAddress: String?,
|
||||
quikTimestamp: Long?,
|
||||
): Message? {
|
||||
// Fast path: quik reactions carry the original sender's address and the original
|
||||
// message's timestamp, so we can look up the exact target without text matching
|
||||
if (quikSenderAddress != null && quikTimestamp != null) {
|
||||
// 1. exact timestamp match
|
||||
realm.where(Message::class.java)
|
||||
.equalTo("threadId", threadId)
|
||||
.equalTo("address", quikSenderAddress)
|
||||
.equalTo("date", quikTimestamp)
|
||||
.findFirst()
|
||||
?.let {
|
||||
Timber.d("Found quik reaction target by exact timestamp: message ID ${it.id}")
|
||||
return it
|
||||
}
|
||||
|
||||
// 2. widen to ±2000ms in case the stored date drifted slightly
|
||||
realm.where(Message::class.java)
|
||||
.equalTo("threadId", threadId)
|
||||
.equalTo("address", quikSenderAddress)
|
||||
.between("date", quikTimestamp - 2000, quikTimestamp + 2000)
|
||||
.sort("date", Sort.DESCENDING)
|
||||
.findFirst()
|
||||
?.let {
|
||||
Timber.d("Found quik reaction target by timestamp window: message ID ${it.id}")
|
||||
return it
|
||||
}
|
||||
|
||||
Timber.w("No quik reaction target by timestamp, falling back to text matching")
|
||||
}
|
||||
|
||||
val startTime = System.currentTimeMillis()
|
||||
val messages = realm.where(Message::class.java)
|
||||
.equalTo("threadId", threadId)
|
||||
@@ -300,7 +348,9 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
val targetMessage = findTargetMessage(
|
||||
message.threadId,
|
||||
parsedReaction.originalMessage,
|
||||
realm
|
||||
realm,
|
||||
parsedReaction.quikSenderAddress,
|
||||
parsedReaction.quikTimestamp,
|
||||
)
|
||||
saveEmojiReaction(
|
||||
message,
|
||||
|
||||
@@ -751,7 +751,9 @@ open class MessageRepositoryImpl @Inject constructor(
|
||||
val targetMessage = reactions.findTargetMessage(
|
||||
savedMessage.threadId,
|
||||
parsedReaction.originalMessage,
|
||||
realm
|
||||
realm,
|
||||
parsedReaction.quikSenderAddress,
|
||||
parsedReaction.quikTimestamp,
|
||||
)
|
||||
realm.executeTransaction {
|
||||
reactions.saveEmojiReaction(
|
||||
|
||||
@@ -335,7 +335,9 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
val targetMessage = reactions.findTargetMessage(
|
||||
threadId,
|
||||
parsedReaction.originalMessage,
|
||||
realm
|
||||
realm,
|
||||
parsedReaction.quikSenderAddress,
|
||||
parsedReaction.quikTimestamp,
|
||||
)
|
||||
realm.executeTransaction {
|
||||
reactions.saveEmojiReaction(
|
||||
|
||||
Reference in New Issue
Block a user