mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 21:58:53 +01:00
Localize emoji patterns
This commit is contained in:
@@ -81,6 +81,7 @@ dependencies {
|
|||||||
implementation "com.f2prateek.rx.preferences2:rx-preferences:$rx_preferences_version"
|
implementation "com.f2prateek.rx.preferences2:rx-preferences:$rx_preferences_version"
|
||||||
implementation "com.jakewharton.timber:timber:$timber_version"
|
implementation "com.jakewharton.timber:timber:$timber_version"
|
||||||
implementation "com.squareup.moshi:moshi:$moshi_version"
|
implementation "com.squareup.moshi:moshi:$moshi_version"
|
||||||
|
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
|
||||||
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
|
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
|
||||||
implementation 'io.michaelrocks:libphonenumber-android:8.13.47'
|
implementation 'io.michaelrocks:libphonenumber-android:8.13.47'
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"emoji_reaction_google_messages_added": "^\u200A\u200B(.+?)\u200B to \u201C\u200A(.+?)\u200A\u201D\u200A$",
|
||||||
|
"emoji_reaction_google_messages_removed": "^\u200ARemoved \u200C(.+?)\u200C from \u201C\u200A(.+?)\u200A\u201D\u200A$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_generic_added": "^Reacted (.+?) to \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_generic_removed": "^Removed (.+?) from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_heart_added": "^Loved \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_heart_removed": "^Removed a heart from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_like_added": "^Liked \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_like_removed": "^Removed a like from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_dislike_added": "^Disliked \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_dislike_removed": "^Removed a dislike from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_laugh_added": "^Laughed at \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_laugh_removed": "^Removed a laugh from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_exclamation_added": "^Emphasized \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_exclamation_removed": "^Removed an exclamation from \u201C(.+?)\u201D$",
|
||||||
|
|
||||||
|
"emoji_reaction_ios_question_mark_added": "^Questioned \u201C(.+?)\u201D$",
|
||||||
|
"emoji_reaction_ios_question_mark_removed": "^Removed a question mark from \u201C(.+?)\u201D$"
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package dev.octoshrimpy.quik.repository
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = false)
|
||||||
|
data class EmojiPatternStrings(
|
||||||
|
@Json(name = "emoji_reaction_google_messages_added") val googleAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_google_messages_removed") val googleRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_generic_added") val iosGenericAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_generic_removed") val iosGenericRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_heart_added") val iosHeartAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_heart_removed") val iosHeartRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_like_added") val iosLikeAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_like_removed") val iosLikeRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_dislike_added") val iosDislikeAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_dislike_removed") val iosDislikeRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_laugh_added") val iosLaughAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_laugh_removed") val iosLaughRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_exclamation_added") val iosExclamationAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_exclamation_removed") val iosExclamationRemoved: String,
|
||||||
|
|
||||||
|
@Json(name = "emoji_reaction_ios_question_mark_added") val iosQuestionMarkAdded: String,
|
||||||
|
@Json(name = "emoji_reaction_ios_question_mark_removed") val iosQuestionMarkRemoved: String,
|
||||||
|
)
|
||||||
@@ -18,19 +18,98 @@
|
|||||||
*/
|
*/
|
||||||
package dev.octoshrimpy.quik.repository
|
package dev.octoshrimpy.quik.repository
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.squareup.moshi.Moshi
|
||||||
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
|
||||||
import dev.octoshrimpy.quik.repository.EmojiReactionUtils.reactionPatterns
|
|
||||||
import dev.octoshrimpy.quik.repository.EmojiReactionUtils.removalPatterns
|
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class EmojiReactionRepositoryImpl @Inject constructor(
|
class EmojiReactionRepositoryImpl @Inject constructor(
|
||||||
private val keyManager: KeyManager
|
private val context: Context,
|
||||||
|
private val keyManager: KeyManager,
|
||||||
|
private val moshi: Moshi,
|
||||||
) : EmojiReactionRepository {
|
) : EmojiReactionRepository {
|
||||||
|
|
||||||
|
// We use an ordered map to make sure we test tapback regexes before generic ones
|
||||||
|
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf()
|
||||||
|
private val removalPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf()
|
||||||
|
|
||||||
|
init {
|
||||||
|
val assetEntries = loadEmojiPatternEntriesFromAssets()
|
||||||
|
assetEntries.forEach { (localeTag, strings) ->
|
||||||
|
try {
|
||||||
|
addPatternsForLocaleStrings(localeTag, strings, reactionPatterns, removalPatterns)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(e, "Failed to load asset patterns for locale: $localeTag")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Timber.i("Loaded emoji reaction patterns for locales: ${assetEntries.map { it.first }}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addPatternsForLocaleStrings(
|
||||||
|
localeTag: String,
|
||||||
|
strings: EmojiPatternStrings,
|
||||||
|
reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?>,
|
||||||
|
removalPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?>
|
||||||
|
) {
|
||||||
|
// Google Messages
|
||||||
|
reactionPatterns[Regex(strings.googleAdded)] = { match -> ParsedEmojiReaction(match.groupValues[1], match.groupValues[2]) }
|
||||||
|
removalPatterns[Regex(strings.googleRemoved)] = { match -> ParsedEmojiReaction(match.groupValues[1], match.groupValues[2], isRemoval = true) }
|
||||||
|
|
||||||
|
// iOS tapbacks (order matters: specific before generic)
|
||||||
|
val tapbacks = listOf(
|
||||||
|
Triple("❤️", strings.iosHeartAdded, strings.iosHeartRemoved),
|
||||||
|
Triple("👍", strings.iosLikeAdded, strings.iosLikeRemoved),
|
||||||
|
Triple("👎", strings.iosDislikeAdded, strings.iosDislikeRemoved),
|
||||||
|
Triple("😂", strings.iosLaughAdded, strings.iosLaughRemoved),
|
||||||
|
Triple("‼️", strings.iosExclamationAdded, strings.iosExclamationRemoved),
|
||||||
|
Triple("❓", strings.iosQuestionMarkAdded, strings.iosQuestionMarkRemoved)
|
||||||
|
)
|
||||||
|
tapbacks.forEach { (emoji, added, removed) ->
|
||||||
|
reactionPatterns[Regex(added)] = { match -> ParsedEmojiReaction(emoji, match.groupValues[1]) }
|
||||||
|
|
||||||
|
removalPatterns[Regex(removed)] = { match -> ParsedEmojiReaction(emoji, match.groupValues[1], isRemoval = true) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic iOS emoji patterns
|
||||||
|
reactionPatterns[Regex(strings.iosGenericAdded)] = { match ->
|
||||||
|
if (match.groupValues.getOrNull(1) == "with a sticker") null // TODO: localize "with a sticker"
|
||||||
|
else ParsedEmojiReaction(match.groupValues[1], match.groupValues[2])
|
||||||
|
}
|
||||||
|
removalPatterns[Regex(strings.iosGenericRemoved)] = { match ->
|
||||||
|
// TODO: sticker?
|
||||||
|
ParsedEmojiReaction(match.groupValues[1], match.groupValues[2], isRemoval = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.d("Loaded emoji regex patterns for $localeTag from assets")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadEmojiPatternEntriesFromAssets(): List<Pair<String, EmojiPatternStrings>> {
|
||||||
|
val dir = "emojis"
|
||||||
|
val files = context.assets.list(dir) ?: emptyArray()
|
||||||
|
return files.filter { it.endsWith(".json", ignoreCase = true) }
|
||||||
|
.mapNotNull { filename ->
|
||||||
|
val localeTag = filename.removeSuffix(".json")
|
||||||
|
try {
|
||||||
|
val json = context.assets.open("$dir/$filename").bufferedReader().use { it.readText() }
|
||||||
|
val data = parseEmojiPatternsJson(json)
|
||||||
|
localeTag to data
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(e, "Failed parsing emoji patterns asset: $filename")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseEmojiPatternsJson(json: String): EmojiPatternStrings {
|
||||||
|
val adapter = moshi.adapter(EmojiPatternStrings::class.java)
|
||||||
|
return requireNotNull(adapter.fromJson(json)) { "Invalid emoji patterns JSON" }
|
||||||
|
}
|
||||||
|
|
||||||
override fun parseEmojiReaction(body: String): ParsedEmojiReaction? {
|
override fun parseEmojiReaction(body: String): ParsedEmojiReaction? {
|
||||||
val removal = parseRemoval(body)
|
val removal = parseRemoval(body)
|
||||||
if (removal != null) return removal
|
if (removal != null) return removal
|
||||||
@@ -68,7 +147,11 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
* 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
|
||||||
*/
|
*/
|
||||||
override fun findTargetMessage(threadId: Long, originalMessageText: String, realm: Realm): Message? {
|
override fun findTargetMessage(
|
||||||
|
threadId: Long,
|
||||||
|
originalMessageText: String,
|
||||||
|
realm: Realm
|
||||||
|
): Message? {
|
||||||
val startTime = System.currentTimeMillis()
|
val startTime = System.currentTimeMillis()
|
||||||
val messages = realm.where(Message::class.java)
|
val messages = realm.where(Message::class.java)
|
||||||
.equalTo("threadId", threadId)
|
.equalTo("threadId", threadId)
|
||||||
@@ -93,7 +176,6 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
reactionMessage: Message,
|
reactionMessage: Message,
|
||||||
reaction: ParsedEmojiReaction,
|
reaction: ParsedEmojiReaction,
|
||||||
targetMessage: Message?,
|
targetMessage: Message?,
|
||||||
realm: Realm,
|
|
||||||
) {
|
) {
|
||||||
if (targetMessage == null) {
|
if (targetMessage == null) {
|
||||||
Timber.w("Cannot remove emoji reaction '${reaction.emoji}': no target message found")
|
Timber.w("Cannot remove emoji reaction '${reaction.emoji}': no target message found")
|
||||||
@@ -122,7 +204,7 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
realm: Realm,
|
realm: Realm,
|
||||||
) {
|
) {
|
||||||
if (parsedReaction.isRemoval) {
|
if (parsedReaction.isRemoval) {
|
||||||
removeEmojiReaction(reactionMessage, parsedReaction, targetMessage, realm)
|
removeEmojiReaction(reactionMessage, parsedReaction, targetMessage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,50 +275,3 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -95,6 +95,13 @@ android {
|
|||||||
signingConfigs.release.keyAlias = System.getenv("key_alias")
|
signingConfigs.release.keyAlias = System.getenv("key_alias")
|
||||||
signingConfigs.release.keyPassword = System.getenv("key_password")
|
signingConfigs.release.keyPassword = System.getenv("key_password")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bundle {
|
||||||
|
language {
|
||||||
|
// avoid splitting out languages because we want to handle an emoji reaction in any language
|
||||||
|
enableSplit = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
androidExtensions {
|
androidExtensions {
|
||||||
|
|||||||
Reference in New Issue
Block a user