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(
|
||||
|
||||
@@ -21,12 +21,26 @@ package dev.octoshrimpy.quik.repository
|
||||
import dev.octoshrimpy.quik.model.Message
|
||||
import io.realm.Realm
|
||||
|
||||
data class ParsedEmojiReaction(val emoji: String, val originalMessage: String, val isRemoval: Boolean = false)
|
||||
data class ParsedEmojiReaction(
|
||||
val emoji: String,
|
||||
val originalMessage: String,
|
||||
val isRemoval: Boolean = false,
|
||||
val quikSenderAddress: String? = null,
|
||||
val quikTimestamp: Long? = null,
|
||||
)
|
||||
|
||||
interface EmojiReactionRepository {
|
||||
fun parseEmojiReaction(body: String): ParsedEmojiReaction?
|
||||
|
||||
fun findTargetMessage(threadId: Long, originalMessageText: String, realm: Realm): Message?
|
||||
fun buildReactionBody(emoji: String, targetMessage: Message): String
|
||||
|
||||
fun findTargetMessage(
|
||||
threadId: Long,
|
||||
originalMessageText: String,
|
||||
realm: Realm,
|
||||
quikSenderAddress: String? = null,
|
||||
quikTimestamp: Long? = null,
|
||||
): Message?
|
||||
|
||||
fun saveEmojiReaction(
|
||||
reactionMessage: Message,
|
||||
|
||||
@@ -42,8 +42,14 @@ import android.view.DragEvent.ACTION_DRAG_EXITED
|
||||
import android.view.DragEvent.ACTION_DROP
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.PopupWindow
|
||||
import android.widget.SeekBar
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.constraintlayout.widget.ConstraintSet
|
||||
@@ -141,6 +147,8 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
override val clearCurrentMessageIntent: Subject<Boolean> = PublishSubject.create()
|
||||
override val messageLinkAskIntent: Subject<Uri> by lazy { messageAdapter.messageLinkClicks }
|
||||
override val reactionClickIntent: Subject<Long> by lazy { messageAdapter.reactionClicks }
|
||||
override val reactionPickerIntent: Observable<Long> by lazy { messageAdapter.reactionPickerIntent }
|
||||
override val sendReactionIntent: Subject<Pair<Long, String>> = PublishSubject.create()
|
||||
override val speechRecogniserIntent by lazy { binding.speechToTextIcon.clicks() }
|
||||
override val shadeIntent by lazy { binding.shadeBackground.clicks() }
|
||||
override val recordAudioStartStopRecording: Subject<Boolean> = PublishSubject.create()
|
||||
@@ -716,6 +724,64 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
.show()
|
||||
}
|
||||
|
||||
override fun showReactionPicker(messageId: Long) {
|
||||
val defaultEmojis = listOf("❤️", "👍", "👎", "😂", "😮", "😢")
|
||||
val pad = 12.dpToPx(this)
|
||||
|
||||
val row = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
setPadding(pad, pad, pad, pad)
|
||||
}
|
||||
|
||||
val popup = PopupWindow(
|
||||
row,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
true
|
||||
).apply {
|
||||
elevation = 8.dpToPx(this@ComposeActivity).toFloat()
|
||||
setBackgroundDrawable(ContextCompat.getDrawable(this@ComposeActivity, R.drawable.rounded_rectangle_4dp))
|
||||
}
|
||||
|
||||
fun emojiView(label: String, onClick: () -> Unit) = TextView(this).apply {
|
||||
text = label
|
||||
textSize = 24f
|
||||
gravity = Gravity.CENTER
|
||||
setPadding(pad, pad / 2, pad, pad / 2)
|
||||
setOnClickListener {
|
||||
onClick()
|
||||
popup.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
defaultEmojis.forEach { emoji ->
|
||||
row.addView(emojiView(emoji) { sendReactionIntent.onNext(Pair(messageId, emoji)) })
|
||||
}
|
||||
|
||||
// "+" opens a simple input so the user can type or paste any emoji
|
||||
row.addView(emojiView("+") {
|
||||
val input = EditText(this).apply { hint = getString(R.string.compose_reaction_custom_hint) }
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle(R.string.compose_reaction_custom_title)
|
||||
.setView(input)
|
||||
.setPositiveButton(R.string.button_send) { _, _ ->
|
||||
val emoji = input.text.toString().trim()
|
||||
if (emoji.isNotEmpty()) sendReactionIntent.onNext(Pair(messageId, emoji))
|
||||
}
|
||||
.setNegativeButton(R.string.button_cancel, null)
|
||||
.show()
|
||||
})
|
||||
|
||||
// Anchor near the long-pressed message if its view is on screen, else the message list
|
||||
val position = messageAdapter.data?.second?.indexOfLast { it.id == messageId } ?: -1
|
||||
val anchor = position
|
||||
.takeIf { it != -1 }
|
||||
?.let { binding.messageList.layoutManager?.findViewByPosition(it) }
|
||||
?: binding.messageList
|
||||
|
||||
popup.showAtLocation(anchor, Gravity.CENTER, 0, 0)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.compose, menu)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
|
||||
@@ -78,6 +78,8 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val clearCurrentMessageIntent: Subject<Boolean>
|
||||
val messageLinkAskIntent: Observable<Uri>
|
||||
val reactionClickIntent: Subject<Long>
|
||||
val reactionPickerIntent: Observable<Long> // message ID to react to
|
||||
val sendReactionIntent: Subject<Pair<Long, String>> // (messageId, emoji)
|
||||
val speechRecogniserIntent: Observable<*>
|
||||
val shadeIntent: Observable<Unit>
|
||||
val recordAudioStartStopRecording: Subject<Boolean>
|
||||
@@ -113,6 +115,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
fun showDeleteDialog( messages: List<Long>)
|
||||
fun showClearCurrentMessageDialog()
|
||||
fun showReactionsDialog(reactions: List<String>)
|
||||
fun showReactionPicker(messageId: Long)
|
||||
fun startSpeechRecognition()
|
||||
fun focusMessage()
|
||||
}
|
||||
@@ -71,6 +71,7 @@ import dev.octoshrimpy.quik.model.Recipient
|
||||
import dev.octoshrimpy.quik.model.getText
|
||||
import dev.octoshrimpy.quik.repository.ContactRepository
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.EmojiReactionRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
|
||||
import dev.octoshrimpy.quik.util.ActiveSubscriptionObservable
|
||||
@@ -115,6 +116,7 @@ class ComposeViewModel @Inject constructor(
|
||||
private val actionDelayedMessage: ActionDelayedMessage,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val deleteMessages: DeleteMessages,
|
||||
private val reactions: EmojiReactionRepository,
|
||||
private val markRead: MarkRead,
|
||||
private val messageDetailsFormatter: MessageDetailsFormatter,
|
||||
private val messageRepo: MessageRepository,
|
||||
@@ -776,6 +778,42 @@ class ComposeViewModel @Inject constructor(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { reactions -> view.showReactionsDialog(reactions) }
|
||||
|
||||
// Long-press on a message opens the emoji reaction picker
|
||||
view.reactionPickerIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { messageId -> view.showReactionPicker(messageId) }
|
||||
|
||||
// Send a reaction: build a quik-encoded body and send it as a normal message. The sent
|
||||
// message comes back to this device and is parsed as a reaction (isEmojiReaction = true),
|
||||
// so it is hidden from the conversation automatically.
|
||||
view.sendReactionIntent
|
||||
.observeOn(Schedulers.io())
|
||||
.withLatestFrom(state, conversation) { pair, state, conversation ->
|
||||
Triple(pair, state, conversation)
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { (pair, state, conversation) ->
|
||||
if (!permissionManager.isDefaultSms()) {
|
||||
view.requestDefaultSms()
|
||||
return@subscribe
|
||||
}
|
||||
if (!permissionManager.hasSendSms()) {
|
||||
view.requestSmsPermission()
|
||||
return@subscribe
|
||||
}
|
||||
|
||||
val (messageId, emoji) = pair
|
||||
val targetMessage = messageRepo.getMessage(messageId) ?: return@subscribe
|
||||
val addresses = conversation.recipients.map { it.address }
|
||||
if (addresses.isEmpty()) return@subscribe
|
||||
|
||||
val subId = state.subscription?.subscriptionId ?: -1
|
||||
val sendAsGroup = (addresses.size > 1) && state.sendAsGroup
|
||||
val body = reactions.buildReactionBody(emoji, targetMessage)
|
||||
|
||||
messageRepo.sendNewMessages(subId, addresses, body, emptyList(), sendAsGroup)
|
||||
}
|
||||
|
||||
// Set the current conversation
|
||||
Observables
|
||||
.combineLatest(
|
||||
|
||||
@@ -111,6 +111,7 @@ class MessagesAdapter @Inject constructor(
|
||||
val resendClicks: Subject<Long> = PublishSubject.create()
|
||||
val partContextMenuRegistrar: Subject<View> = PublishSubject.create()
|
||||
val reactionClicks: Subject<Long> = PublishSubject.create()
|
||||
val reactionPickerIntent: Subject<Long> = PublishSubject.create()
|
||||
|
||||
var data: Pair<Conversation, RealmResults<Message>>? = null
|
||||
set(value) {
|
||||
@@ -182,8 +183,14 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
getItem(adapterPosition)?.let {
|
||||
toggleSelection(it.id)
|
||||
view.isActivated = isSelected(it.id)
|
||||
// If a selection is already active, keep extending it (multi-select).
|
||||
// Otherwise, long-press opens the emoji reaction picker for this message.
|
||||
// Selection mode remains reachable via the toolbar (select all) once active.
|
||||
if (toggleSelection(it.id, false)) {
|
||||
view.isActivated = isSelected(it.id)
|
||||
} else {
|
||||
reactionPickerIntent.onNext(it.id)
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -155,6 +155,9 @@
|
||||
<string name="compose_menu_clear">Clear</string>
|
||||
<string name="compose_details_title">Message details</string>
|
||||
<string name="compose_reactions_title">Reactions</string>
|
||||
<string name="compose_reaction_custom_title">Custom reaction</string>
|
||||
<string name="compose_reaction_custom_hint">Type or paste an emoji</string>
|
||||
<string name="button_send">Send</string>
|
||||
<string name="compose_details_type">Type: %s</string>
|
||||
<string name="compose_details_from">From: %s</string>
|
||||
<string name="compose_details_to">To: %s</string>
|
||||
|
||||
Reference in New Issue
Block a user