mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 13:48:52 +01:00
Merge pull request #642 from Inhishonor/emoji-ellipses
Support matching message reactions that are truncated
This commit is contained in:
@@ -20,7 +20,6 @@ package dev.octoshrimpy.quik.repository
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import dev.octoshrimpy.quik.extensions.insertOrUpdate
|
|
||||||
import dev.octoshrimpy.quik.manager.KeyManager
|
import dev.octoshrimpy.quik.manager.KeyManager
|
||||||
import dev.octoshrimpy.quik.model.EmojiReaction
|
import dev.octoshrimpy.quik.model.EmojiReaction
|
||||||
import dev.octoshrimpy.quik.model.Message
|
import dev.octoshrimpy.quik.model.Message
|
||||||
@@ -39,12 +38,20 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
||||||
Regex( // Google Messages
|
Regex( // Google Messages
|
||||||
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
||||||
) to { match -> ParsedEmojiReaction(match.groupValues[1], match.groupValues[2]) }
|
) to { match ->
|
||||||
|
ParsedEmojiReaction(
|
||||||
|
match.groupValues[1], match.groupValues[2]
|
||||||
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
private val removalPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
private val removalPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
||||||
Regex( // Google Messages
|
Regex( // Google Messages
|
||||||
"(?s)^\u200a[^\u200c\u200a]*\u200c([^\u200c]*)\u200c[^\u200c\u200a]*\u200a(.*)\u200a[^\u200c\u200a]*\u200a\\Z"
|
"(?s)^\u200a[^\u200c\u200a]*\u200c([^\u200c]*)\u200c[^\u200c\u200a]*\u200a(.*)\u200a[^\u200c\u200a]*\u200a\\Z"
|
||||||
) to { match -> ParsedEmojiReaction(match.groupValues[1], match.groupValues[2], isRemoval = true) }
|
) to { match ->
|
||||||
|
ParsedEmojiReaction(
|
||||||
|
match.groupValues[1], match.groupValues[2], isRemoval = true
|
||||||
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -107,7 +114,10 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
.mapNotNull { filename ->
|
.mapNotNull { filename ->
|
||||||
val localeTag = filename.removeSuffix(".json")
|
val localeTag = filename.removeSuffix(".json")
|
||||||
try {
|
try {
|
||||||
val json = context.assets.open("$dir/$filename").bufferedReader().use { it.readText() }
|
val json = context.assets.open("$dir/$filename")
|
||||||
|
.bufferedReader().use {
|
||||||
|
it.readText()
|
||||||
|
}
|
||||||
val data = parseEmojiPatternsJson(json)
|
val data = parseEmojiPatternsJson(json)
|
||||||
localeTag to data
|
localeTag to data
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -127,11 +137,8 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
if (removal != null) return removal
|
if (removal != null) return removal
|
||||||
|
|
||||||
for ((pattern, parser) in reactionPatterns) {
|
for ((pattern, parser) in reactionPatterns) {
|
||||||
val match = pattern.find(body)
|
val match = pattern.find(body) ?: continue
|
||||||
if (match == null) continue;
|
val result = parser(match) ?: continue
|
||||||
|
|
||||||
val result = parser(match)
|
|
||||||
if (result == null) continue
|
|
||||||
|
|
||||||
Timber.d("Reaction found with ${result.emoji}")
|
Timber.d("Reaction found with ${result.emoji}")
|
||||||
return result
|
return result
|
||||||
@@ -142,11 +149,8 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
private fun parseRemoval(body: String): ParsedEmojiReaction? {
|
private fun parseRemoval(body: String): ParsedEmojiReaction? {
|
||||||
for ((pattern, parser) in removalPatterns) {
|
for ((pattern, parser) in removalPatterns) {
|
||||||
val match = pattern.find(body)
|
val match = pattern.find(body) ?: continue
|
||||||
if (match == null) continue;
|
val result = parser(match) ?: continue
|
||||||
|
|
||||||
val result = parser(match)
|
|
||||||
if (result == null) continue
|
|
||||||
|
|
||||||
Timber.d("Removal found with ${result.emoji}")
|
Timber.d("Removal found with ${result.emoji}")
|
||||||
return result
|
return result
|
||||||
@@ -155,6 +159,20 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun parseTruncatedMessages(originalMessageText: String): Regex {
|
||||||
|
val reactionText = originalMessageText.trim()
|
||||||
|
|
||||||
|
val delimiter = "\u2026"
|
||||||
|
val index = reactionText.lastIndexOf(delimiter)
|
||||||
|
val regexPattern = if (index == -1) {
|
||||||
|
Regex.escape(reactionText)
|
||||||
|
} else {
|
||||||
|
val before = reactionText.take(index)
|
||||||
|
Regex.escape(before) + ".*"
|
||||||
|
}
|
||||||
|
return Regex("^$regexPattern$", RegexOption.DOT_MATCHES_ALL)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for messages in the same thread with matching text content
|
* Search for messages in the same thread with matching text content
|
||||||
* We'll search recent messages first
|
* We'll search recent messages first
|
||||||
@@ -172,8 +190,9 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
val endTime = System.currentTimeMillis()
|
val endTime = System.currentTimeMillis()
|
||||||
Timber.d("Found ${messages.size} messages as potential emoji targets in ${endTime - startTime}ms")
|
Timber.d("Found ${messages.size} messages as potential emoji targets in ${endTime - startTime}ms")
|
||||||
|
|
||||||
|
val originalMessageRegex = parseTruncatedMessages(originalMessageText)
|
||||||
val match = messages.find { message ->
|
val match = messages.find { message ->
|
||||||
message.getText(false).trim() == originalMessageText.trim()
|
originalMessageRegex.matches(message.getText(false).trim())
|
||||||
}
|
}
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
Timber.d("Found match for reaction target: message ID ${match.id}")
|
Timber.d("Found match for reaction target: message ID ${match.id}")
|
||||||
|
|||||||
Reference in New Issue
Block a user