forked from upstream-mirrors/quik
refactor(reactions): Code cleanup for emojireaction repository
Co-authored-by: Inhishonor <inhishonor@protonmail.com>
This commit is contained in:
@@ -44,22 +44,23 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
/** Give a bit of slack for messages potentially being in the wrong order */
|
/** Give a bit of slack for messages potentially being in the wrong order */
|
||||||
private const val MESSAGE_DATE_TOLERANCE_MS = 60_000L
|
private const val MESSAGE_DATE_TOLERANCE_MS = 60_000L
|
||||||
|
private const val MESSAGE_TRUNCATION_DELIMITER = "\u2026"
|
||||||
|
private const val GOOGLE_MESSAGE_REACTION_REGEX =
|
||||||
|
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
||||||
|
private const val GOOGLE_MESSAGE_REACTION_REMOVAL_REGEX =
|
||||||
|
"(?s)^\u200a[^\u200c\u200a]*\u200c([^\u200c]*)\u200c[^\u200c\u200a]*\u200a(.*)\u200a[^\u200c\u200a]*\u200a\\Z"
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use an ordered map to make sure we can test tapback regexes before generic ones
|
// We use an ordered map to make sure we can test tapback regexes before generic ones
|
||||||
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
||||||
Regex( // Google Messages
|
Regex(GOOGLE_MESSAGE_REACTION_REGEX) to { match ->
|
||||||
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
|
||||||
) to { match ->
|
|
||||||
ParsedEmojiReaction(
|
ParsedEmojiReaction(
|
||||||
match.groupValues[1], match.groupValues[2]
|
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_MESSAGE_REACTION_REMOVAL_REGEX) to { match ->
|
||||||
"(?s)^\u200a[^\u200c\u200a]*\u200c([^\u200c]*)\u200c[^\u200c\u200a]*\u200a(.*)\u200a[^\u200c\u200a]*\u200a\\Z"
|
|
||||||
) to { match ->
|
|
||||||
ParsedEmojiReaction(
|
ParsedEmojiReaction(
|
||||||
match.groupValues[1], match.groupValues[2], isRemoval = true
|
match.groupValues[1], match.groupValues[2], isRemoval = true
|
||||||
)
|
)
|
||||||
@@ -106,7 +107,8 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
// Generic iOS emoji patterns
|
// Generic iOS emoji patterns
|
||||||
strings.iosGenericAdded?.let { pattern ->
|
strings.iosGenericAdded?.let { pattern ->
|
||||||
reactionPatterns[Regex(pattern)] = { match ->
|
reactionPatterns[Regex(pattern)] = { match ->
|
||||||
if (match.groupValues.getOrNull(1) == "with a sticker") null // TODO: localize "with a sticker"
|
// TODO: localize "with a sticker"
|
||||||
|
if (match.groupValues.getOrNull(1) == "with a sticker") null
|
||||||
else ParsedEmojiReaction(match.groupValues[1], match.groupValues[2])
|
else ParsedEmojiReaction(match.groupValues[1], match.groupValues[2])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,7 +176,7 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
private fun parseTruncatedMessages(originalMessageText: String): Regex {
|
private fun parseTruncatedMessages(originalMessageText: String): Regex {
|
||||||
val reactionText = originalMessageText.trim()
|
val reactionText = originalMessageText.trim()
|
||||||
|
|
||||||
val delimiter = "\u2026"
|
val delimiter = MESSAGE_TRUNCATION_DELIMITER
|
||||||
val index = reactionText.lastIndexOf(delimiter)
|
val index = reactionText.lastIndexOf(delimiter)
|
||||||
val regexPattern = if (index == -1) {
|
val regexPattern = if (index == -1) {
|
||||||
Regex.escape(reactionText)
|
Regex.escape(reactionText)
|
||||||
@@ -204,7 +206,7 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
.apply { latestDate?.let { lessThanOrEqualTo("date", it) } }
|
.apply { latestDate?.let { lessThanOrEqualTo("date", it) } }
|
||||||
|
|
||||||
// Match the text directly with the messages that do not have the delimiter
|
// Match the text directly with the messages that do not have the delimiter
|
||||||
if (!originalMessageText.contains("\u2026")) {
|
if (!originalMessageText.contains(MESSAGE_TRUNCATION_DELIMITER)) {
|
||||||
candidateQuery()
|
candidateQuery()
|
||||||
.equalTo("body", originalMessageText)
|
.equalTo("body", originalMessageText)
|
||||||
.sort("date", Sort.DESCENDING)
|
.sort("date", Sort.DESCENDING)
|
||||||
@@ -324,7 +326,8 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
.isNotEmpty("parts.text")
|
.isNotEmpty("parts.text")
|
||||||
.endGroup()
|
.endGroup()
|
||||||
.endGroup()
|
.endGroup()
|
||||||
.sort("date", Sort.ASCENDING) // parse oldest to newest to handle reactions & removals properly
|
// parse oldest to newest to handle reactions & removals properly
|
||||||
|
.sort("date", Sort.ASCENDING)
|
||||||
.findAll()
|
.findAll()
|
||||||
|
|
||||||
val max = allMessages?.count() ?: 0
|
val max = allMessages?.count() ?: 0
|
||||||
|
|||||||
Reference in New Issue
Block a user