move emoji reaction code to repo and impl

This commit is contained in:
Ben Iofel
2025-09-02 18:45:52 -04:00
parent b4bc87a479
commit c1dcf8805d
5 changed files with 97 additions and 69 deletions
@@ -1,194 +0,0 @@
/*
* Copyright (C) 2025
*
* This file is part of QUIK.
*
* QUIK is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QUIK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QUIK. If not, see <http://www.gnu.org/licenses/>.
*/
package dev.octoshrimpy.quik.util
import dev.octoshrimpy.quik.manager.KeyManager
import dev.octoshrimpy.quik.model.EmojiReaction
import dev.octoshrimpy.quik.model.Message
import io.realm.Realm
import io.realm.Sort
import timber.log.Timber
data class ParsedEmojiReaction(val emoji: String, val originalMessage: String, val isRemoval: Boolean = false)
object EmojiReactionUtils {
private fun tapback(emoji: String, isRemoval: Boolean = false): (MatchResult) -> ParsedEmojiReaction {
return { match ->
ParsedEmojiReaction(emoji, match.groupValues[1], isRemoval)
}
}
val reactionPatterns: Map<Regex, (MatchResult) -> ParsedEmojiReaction?> = mapOf(
// Google Messages - https://github.com/octoshrimpy/quik/issues/152#issuecomment-2330183516
Regex("^\u200A\u200B(.+?)\u200B to \u201C\u200A(.+?)\u200A\u201D\u200A$") to { match ->
ParsedEmojiReaction(match.groupValues[1], match.groupValues[2])
},
// iOS
Regex("^Reacted (.+?) to \u201C(.+?)\u201D$") to { match ->
if (match.groupValues[1] == "with a sticker")
null
else
ParsedEmojiReaction(match.groupValues[1], match.groupValues[2])
},
// iOS tapbacks
Regex("^Loved \u201C(.+?)\u201D$") to tapback("❤️"),
Regex("^Liked \u201C(.+?)\u201D$") to tapback("👍"),
Regex("^Disliked \u201C(.+?)\u201D$") to tapback("👎"),
Regex("^Laughed at \u201C(.+?)\u201D$") to tapback("😂"),
Regex("^Emphasized \u201C(.+?)\u201D$") to tapback("‼️"),
Regex("^Questioned \u201C(.+?)\u201D$") to tapback("❓"),
)
val removalPatterns: Map<Regex, (MatchResult) -> ParsedEmojiReaction?> = mapOf(
// Google Messages
Regex("^\u200ARemoved \u200C(.+?)\u200C from \u201C\u200A(.+?)\u200A\u201D\u200A$") to { match ->
ParsedEmojiReaction(match.groupValues[1], match.groupValues[2], isRemoval = true)
},
// iOS tapbacks
Regex("^Removed a heart from \u201C(.+?)\u201D$") to tapback("❤️", true),
Regex("^Removed a like from \u201C(.+?)\u201D$") to tapback("👍", true),
Regex("^Removed a dislike from \u201C(.+?)\u201D$") to tapback("👎", true),
Regex("^Removed a laugh from \u201C(.+?)\u201D$") to tapback("😂", true),
Regex("^Removed an exclamation from \u201C(.+?)\u201D$") to tapback("‼️", true),
Regex("^Removed a question mark from \u201C(.+?)\u201D$") to tapback("❓", true),
// iOS emoji - keep this below tapbacks as this regex would otherwise also match the patterns above
Regex("^Removed (.+?) from \u201C(.+?)\u201D$") to { match ->
ParsedEmojiReaction(match.groupValues[1], match.groupValues[2], isRemoval = true)
},
)
fun parseEmojiReaction(body: String): ParsedEmojiReaction? {
val removal = parseRemoval(body)
if (removal != null) return removal
for ((pattern, parser) in reactionPatterns) {
val match = pattern.find(body)
if (match == null) continue;
val result = parser(match)
if (result == null) continue
Timber.d("Reaction found with ${result.emoji}")
return result
}
return null
}
private fun parseRemoval(body: String): ParsedEmojiReaction? {
for ((pattern, parser) in removalPatterns) {
val match = pattern.find(body)
if (match == null) continue;
val result = parser(match)
if (result == null) continue
Timber.d("Removal found with ${result.emoji}")
return result
}
return null
}
/**
* Search for messages in the same thread with matching text content
* We'll search recent messages first (within reasonable time window)
*/
fun findTargetMessage(threadId: Long, originalMessageText: String, realm: Realm): Message? {
val messages = realm.where(Message::class.java)
.equalTo("threadId", threadId)
.sort("date", Sort.DESCENDING)
.limit(50) // TODO: should we keep this limit?
.findAll()
val match = messages.find { message ->
message.getText(false).trim() == originalMessageText.trim()
}
if (match != null) {
Timber.d("Found match for reaction target: message ID ${match.id}")
return match
}
Timber.w("No target message found for reaction text: '$originalMessageText'")
return null
}
private fun removeEmojiReaction(
reactionMessage: Message,
reaction: ParsedEmojiReaction,
targetMessage: Message?,
realm: Realm,
) {
if (targetMessage == null) {
Timber.w("Cannot remove emoji reaction '${reaction.emoji}': no target message found")
reactionMessage.isEmojiReaction = true
realm.insertOrUpdate(reactionMessage)
return
}
val existingReaction = targetMessage.emojiReactions.find { candidate ->
candidate.senderAddress == reactionMessage.address && candidate.emoji == reaction.emoji
}
if (existingReaction != null) {
existingReaction.deleteFromRealm()
Timber.d("Removed emoji reaction: ${reaction.emoji} to message ${targetMessage.id}")
} else {
Timber.w("No existing emoji reaction found to remove: ${reaction.emoji} to message ${targetMessage.id}")
}
reactionMessage.isEmojiReaction = true
realm.insertOrUpdate(reactionMessage)
}
fun saveEmojiReaction(
reactionMessage: Message,
parsedReaction: ParsedEmojiReaction,
targetMessage: Message?,
keyManager: KeyManager,
realm: Realm,
) {
if (parsedReaction.isRemoval) {
removeEmojiReaction(reactionMessage, parsedReaction, targetMessage, realm)
return
}
val reaction = EmojiReaction().apply {
id = keyManager.newId()
reactionMessageId = reactionMessage.id
senderAddress = reactionMessage.address
emoji = parsedReaction.emoji
originalMessageText = parsedReaction.originalMessage
threadId = reactionMessage.threadId
}
realm.insertOrUpdate(reaction)
reactionMessage.isEmojiReaction = true
realm.insertOrUpdate(reactionMessage)
if (targetMessage != null) {
targetMessage.emojiReactions.add(reaction)
Timber.i("Saved emoji reaction: ${reaction.emoji} to message ${targetMessage.id}")
} else {
Timber.w("Saved emoji reaction without target message: ${reaction.emoji}")
}
}
}