Skip to content

Commit 220ccc8

Browse files
authored
1 parent fdf099a commit 220ccc8

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

core/service/src/androidHostTest/kotlin/org/meshtastic/core/service/MeshNotificationManagerImplConversationTest.kt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.meshtastic.core.service
1919
import android.app.Notification
2020
import android.app.NotificationManager
2121
import android.content.Context
22+
import androidx.core.app.NotificationCompat
2223
import androidx.test.core.app.ApplicationProvider
2324
import androidx.test.ext.junit.runners.AndroidJUnit4
2425
import dev.mokkery.MockMode
@@ -37,6 +38,7 @@ import org.meshtastic.core.model.ConnectionState
3738
import org.meshtastic.core.model.Message
3839
import org.meshtastic.core.model.MyNodeInfo
3940
import org.meshtastic.core.model.Node
41+
import org.meshtastic.core.model.Reaction
4042
import org.meshtastic.core.repository.NodeRepository
4143
import org.meshtastic.core.repository.PacketRepository
4244
import org.meshtastic.core.repository.RadioConfigRepository
@@ -102,7 +104,22 @@ class MeshNotificationManagerImplConversationTest {
102104
everySuspend { packetRepository.getMessagesFrom(any(), any(), any(), any()) } returns flowOf(messages.toList())
103105
}
104106

105-
private fun message(text: String, read: Boolean, receivedTime: Long): Message = Message(
107+
private fun reaction(emoji: String, timestamp: Long): Reaction = Reaction(
108+
replyId = 0,
109+
user = sender.user,
110+
emoji = emoji,
111+
timestamp = timestamp,
112+
snr = 0f,
113+
rssi = 0,
114+
hopsAway = 0,
115+
)
116+
117+
private fun message(
118+
text: String,
119+
read: Boolean,
120+
receivedTime: Long,
121+
emojis: List<Reaction> = emptyList(),
122+
): Message = Message(
106123
uuid = receivedTime,
107124
receivedTime = receivedTime,
108125
node = sender,
@@ -113,7 +130,7 @@ class MeshNotificationManagerImplConversationTest {
113130
status = null,
114131
routingError = 0,
115132
packetId = receivedTime.toInt(),
116-
emojis = emptyList(),
133+
emojis = emojis,
117134
snr = 0f,
118135
rssi = 0,
119136
hopsAway = 0,
@@ -142,7 +159,7 @@ class MeshNotificationManagerImplConversationTest {
142159
fun `read messages become historic context and unread messages stay alerting`() = runTest {
143160
mockHistory(
144161
message("new unread", read = false, receivedTime = 3_000),
145-
message("older read 2", read = true, receivedTime = 2_000),
162+
message("older read 2", read = true, receivedTime = 2_000, emojis = listOf(reaction("👍", 2_100))),
146163
message("older read 1", read = true, receivedTime = 1_000),
147164
)
148165

@@ -158,7 +175,7 @@ class MeshNotificationManagerImplConversationTest {
158175
val alerting = posted.extras.getParcelableArray(Notification.EXTRA_MESSAGES)
159176
val historic = posted.extras.getParcelableArray(Notification.EXTRA_HISTORIC_MESSAGES)
160177
assertEquals(1, alerting?.size, "only the unread message should be presented as new content")
161-
assertEquals(2, historic?.size, "read context should be carried as historic messages")
178+
assertEquals(3, historic?.size, "read context and its reactions should be carried as historic messages")
162179
assertEquals("new unread", posted.extras.getCharSequence(Notification.EXTRA_TEXT)?.toString())
163180
}
164181

@@ -170,6 +187,11 @@ class MeshNotificationManagerImplConversationTest {
170187

171188
val summary = activeByTag("message_summary").single().notification
172189
assertEquals(Notification.GROUP_ALERT_CHILDREN, summary.groupAlertBehavior)
190+
val summaryMessage =
191+
NotificationCompat.MessagingStyle.Message
192+
.getMessagesFromBundleArray(summary.extras.getParcelableArray(Notification.EXTRA_MESSAGES))
193+
.single()
194+
assertEquals("Hawk Ridge", summaryMessage.person?.name)
173195

174196
manager.cancelMessageNotification("0^all")
175197

core/service/src/androidMain/kotlin/org/meshtastic/core/service/MeshNotificationManagerImpl.kt

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,16 @@ class MeshNotificationManagerImpl(
523523
.setConversationTitle(getString(Res.string.meshtastic_app_name))
524524

525525
activeNotifications.forEach { sbn ->
526-
val senderTitle = sbn.notification.extras.getCharSequence(Notification.EXTRA_TITLE)
527-
val messageText = sbn.notification.extras.getCharSequence(Notification.EXTRA_TEXT)
528-
val postTime = sbn.postTime
529-
530-
if (senderTitle != null && messageText != null) {
531-
// For the summary, we're creating a generic Person for the sender from the active notification's title.
532-
// We don't have the original Person object or its colors/ID, so we're just using the name.
533-
val senderPerson = Person.Builder().setName(senderTitle).build()
526+
val latestMessage =
527+
NotificationCompat.MessagingStyle.Message
528+
.getMessagesFromBundleArray(sbn.notification.extras.getParcelableArray(Notification.EXTRA_MESSAGES))
529+
.lastOrNull()
530+
val senderName = latestMessage?.person?.name
531+
val messageText = latestMessage?.text
532+
val postTime = latestMessage?.timestamp
533+
534+
if (senderName != null && messageText != null && postTime != null) {
535+
val senderPerson = Person.Builder().setName(senderName).build()
534536
messagingStyle.addMessage(messageText, postTime, senderPerson)
535537
}
536538
}
@@ -705,7 +707,8 @@ class MeshNotificationManagerImpl(
705707
"↩️ \"${original.node.user.short_name}: ${original.text.take(SNIPPET_LENGTH)}...\": ${msg.text}"
706708
} ?: msg.text
707709

708-
if (msg.read && (anyUnread || index != lastIndex)) {
710+
val isHistoricContext = msg.read && (anyUnread || index != lastIndex)
711+
if (isHistoricContext) {
709712
style.addHistoricMessage(NotificationCompat.MessagingStyle.Message(text, msg.receivedTime, person))
710713
} else {
711714
style.addMessage(text, msg.receivedTime, person)
@@ -727,11 +730,17 @@ class MeshNotificationManagerImpl(
727730
),
728731
)
729732
.build()
730-
style.addMessage(
731-
"${reaction.emoji} to \"${msg.text.take(SNIPPET_LENGTH)}...\"",
732-
reaction.timestamp,
733-
reactor,
734-
)
733+
val reactionMessage =
734+
NotificationCompat.MessagingStyle.Message(
735+
"${reaction.emoji} to \"${msg.text.take(SNIPPET_LENGTH)}...\"",
736+
reaction.timestamp,
737+
reactor,
738+
)
739+
if (isHistoricContext) {
740+
style.addHistoricMessage(reactionMessage)
741+
} else {
742+
style.addMessage(reactionMessage)
743+
}
735744
}
736745
}
737746
val lastMessage = history.last()

0 commit comments

Comments
 (0)