fix(reactions): correct thread safety, self-receipt, anchoring, and attribution

Address review findings on the send-reaction feature:

- Run permission checks on the main thread and only switch to IO for the
  actual send, instead of touching the UI from a background thread.
- Parse outgoing reactions on the sender's own device. Sent messages don't
  arrive back as incoming, so they would otherwise never be attached or
  hidden until a full reparse.
- Anchor the reaction picker above the long-pressed message instead of
  centering it on the screen, falling back to the list when off-screen.
- Always send reactions as group MMS in a group thread.
- Attribute our own reactions to our own number (via isMe + getMyPhoneNumber)
  rather than the recipient's address, and show "Me" for them in the
  reactions dialog.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
octoshrimpy
2026-06-14 11:10:02 -04:00
parent d0d3501a83
commit 88b57ee9d0
5 changed files with 70 additions and 22 deletions
@@ -19,6 +19,7 @@
package dev.octoshrimpy.quik.repository package dev.octoshrimpy.quik.repository
import android.content.Context import android.content.Context
import com.klinker.android.send_message.Utils
import com.squareup.moshi.Moshi 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
@@ -251,6 +252,17 @@ class EmojiReactionRepositoryImpl @Inject constructor(
return null return null
} }
/**
* The address to attribute a reaction to. For incoming reactions this is the message sender;
* for our own outgoing reactions [Message.address] is the recipient, so use our own number
* instead so the reaction shows as coming from us.
*/
private fun reactionSenderAddress(reactionMessage: Message): String =
if (reactionMessage.isMe())
Utils.getMyPhoneNumber(context).takeUnless { it.isNullOrEmpty() } ?: reactionMessage.address
else
reactionMessage.address
private fun removeEmojiReaction( private fun removeEmojiReaction(
reactionMessage: Message, reactionMessage: Message,
reaction: ParsedEmojiReaction, reaction: ParsedEmojiReaction,
@@ -262,8 +274,9 @@ class EmojiReactionRepositoryImpl @Inject constructor(
return return
} }
val senderAddress = reactionSenderAddress(reactionMessage)
val existingReaction = targetMessage.emojiReactions.find { candidate -> val existingReaction = targetMessage.emojiReactions.find { candidate ->
candidate.senderAddress == reactionMessage.address && candidate.emoji == reaction.emoji candidate.senderAddress == senderAddress && candidate.emoji == reaction.emoji
} }
if (existingReaction != null) { if (existingReaction != null) {
@@ -291,7 +304,7 @@ class EmojiReactionRepositoryImpl @Inject constructor(
val reaction = EmojiReaction().apply { val reaction = EmojiReaction().apply {
id = keyManager.newId() id = keyManager.newId()
reactionMessageId = reactionMessage.id reactionMessageId = reactionMessage.id
senderAddress = reactionMessage.address senderAddress = reactionSenderAddress(reactionMessage)
emoji = parsedReaction.emoji emoji = parsedReaction.emoji
originalMessageText = parsedReaction.originalMessage originalMessageText = parsedReaction.originalMessage
threadId = reactionMessage.threadId threadId = reactionMessage.threadId
@@ -614,6 +614,29 @@ open class MessageRepositoryImpl @Inject constructor(
Timber.v("created message id ${message.id} from uri $messageUri") Timber.v("created message id ${message.id} from uri $messageUri")
// Parse outgoing reactions on the sender's own device. Sent messages don't arrive back as
// incoming, so without this they'd never be parsed (only on a full reparse). This attaches
// the reaction to its target locally and marks the reaction message as hidden.
reactions.parseEmojiReaction(body)?.let { parsedReaction ->
Realm.getDefaultInstance().use { realm ->
realm.where(Message::class.java)
.equalTo("id", message.id)
.findFirst()
?.let { savedMessage ->
val targetMessage = reactions.findTargetMessage(
savedMessage.threadId,
parsedReaction.originalMessage,
realm,
parsedReaction.quikSenderAddress,
parsedReaction.quikTimestamp,
)
realm.executeTransaction {
reactions.saveEmojiReaction(savedMessage, parsedReaction, targetMessage, realm)
}
}
}
}
if (delayMs > 0) { // if delaying if (delayMs > 0) { // if delaying
val sendTime = (now() + delayMs) val sendTime = (now() + delayMs)
@@ -772,14 +772,18 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
.show() .show()
}) })
// Anchor near the long-pressed message if its view is on screen, else the message list // Anchor above the long-pressed message if its view is on screen; else center on the list
val position = messageAdapter.data?.second?.indexOfLast { it.id == messageId } ?: -1 val position = messageAdapter.data?.second?.indexOfLast { it.id == messageId } ?: -1
val anchor = position val messageView = position
.takeIf { it != -1 } .takeIf { it != -1 }
?.let { binding.messageList.layoutManager?.findViewByPosition(it) } ?.let { binding.messageList.layoutManager?.findViewByPosition(it) }
?: binding.messageList
popup.showAtLocation(anchor, Gravity.CENTER, 0, 0) if (messageView != null) {
row.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
popup.showAsDropDown(messageView, 0, -(messageView.height + row.measuredHeight))
} else {
popup.showAtLocation(binding.messageList, Gravity.CENTER, 0, 0)
}
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean { override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -766,12 +766,17 @@ class ComposeViewModel @Inject constructor(
.mapNotNull { messageId -> messageRepo.getMessage(messageId) } .mapNotNull { messageId -> messageRepo.getMessage(messageId) }
.withLatestFrom(conversation) { message, conv -> .withLatestFrom(conversation) { message, conv ->
message.emojiReactions.map { reaction -> message.emojiReactions.map { reaction ->
val contactName = conv.recipients val contactName = when {
.firstOrNull { recipient -> // our own reactions are stored against our own number, so show "Me"
phoneNumberUtils.compare(recipient.address, reaction.senderAddress) messageRepo.getMessage(reaction.reactionMessageId)?.isMe() == true ->
} context.getString(R.string.compose_reaction_me)
?.getDisplayName() else -> conv.recipients
?: reaction.senderAddress .firstOrNull { recipient ->
phoneNumberUtils.compare(recipient.address, reaction.senderAddress)
}
?.getDisplayName()
?: reaction.senderAddress
}
"${reaction.emoji} $contactName" "${reaction.emoji} $contactName"
} }
} }
@@ -787,28 +792,30 @@ class ComposeViewModel @Inject constructor(
// message comes back to this device and is parsed as a reaction (isEmojiReaction = true), // message comes back to this device and is parsed as a reaction (isEmojiReaction = true),
// so it is hidden from the conversation automatically. // so it is hidden from the conversation automatically.
view.sendReactionIntent view.sendReactionIntent
// permission checks touch the UI, so keep them on the main thread
.observeOn(AndroidSchedulers.mainThread())
.filter {
when {
!permissionManager.isDefaultSms() -> { view.requestDefaultSms(); false }
!permissionManager.hasSendSms() -> { view.requestSmsPermission(); false }
else -> true
}
}
// sending and realm access happen off the main thread
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.withLatestFrom(state, conversation) { pair, state, conversation -> .withLatestFrom(state, conversation) { pair, state, conversation ->
Triple(pair, state, conversation) Triple(pair, state, conversation)
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { (pair, state, conversation) -> .subscribe { (pair, state, conversation) ->
if (!permissionManager.isDefaultSms()) {
view.requestDefaultSms()
return@subscribe
}
if (!permissionManager.hasSendSms()) {
view.requestSmsPermission()
return@subscribe
}
val (messageId, emoji) = pair val (messageId, emoji) = pair
val targetMessage = messageRepo.getMessage(messageId) ?: return@subscribe val targetMessage = messageRepo.getMessage(messageId) ?: return@subscribe
val addresses = conversation.recipients.map { it.address } val addresses = conversation.recipients.map { it.address }
if (addresses.isEmpty()) return@subscribe if (addresses.isEmpty()) return@subscribe
val subId = state.subscription?.subscriptionId ?: -1 val subId = state.subscription?.subscriptionId ?: -1
val sendAsGroup = (addresses.size > 1) && state.sendAsGroup // always send a group reaction in a group thread so it reaches everyone
val sendAsGroup = addresses.size > 1
val body = reactions.buildReactionBody(emoji, targetMessage) val body = reactions.buildReactionBody(emoji, targetMessage)
messageRepo.sendNewMessages(subId, addresses, body, emptyList(), sendAsGroup) messageRepo.sendNewMessages(subId, addresses, body, emptyList(), sendAsGroup)
@@ -155,6 +155,7 @@
<string name="compose_menu_clear">Clear</string> <string name="compose_menu_clear">Clear</string>
<string name="compose_details_title">Message details</string> <string name="compose_details_title">Message details</string>
<string name="compose_reactions_title">Reactions</string> <string name="compose_reactions_title">Reactions</string>
<string name="compose_reaction_me">Me</string>
<string name="compose_reaction_custom_title">Custom reaction</string> <string name="compose_reaction_custom_title">Custom reaction</string>
<string name="compose_reaction_custom_hint">Type or paste an emoji</string> <string name="compose_reaction_custom_hint">Type or paste an emoji</string>
<string name="button_send">Send</string> <string name="button_send">Send</string>