Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .skills/compose-ui/strings-index.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.paging.PagingData
import androidx.paging.map
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -139,6 +140,7 @@ class PacketRepositoryImpl(private val dbManager: DatabaseProvider, private val
rssi = packet.rssi,
hopsAway = packet.hopsAway,
filtered = filtered,
messageText = packet.text.orEmpty(),
)
insertRoomPacket(packetToSave)
}
Expand Down Expand Up @@ -278,6 +280,7 @@ class PacketRepositoryImpl(private val dbManager: DatabaseProvider, private val
rssi = packet.rssi,
hopsAway = packet.hopsAway,
filtered = filtered,
messageText = packet.text.orEmpty(),
)
insertRoomPacket(packetToSave)
}
Expand Down Expand Up @@ -510,6 +513,54 @@ class PacketRepositoryImpl(private val dbManager: DatabaseProvider, private val
sfpp_hash = sfppHash,
)

override fun searchMessages(query: String, contactKey: String?, getNode: (String?) -> Node): Flow<List<Message>> {
val sanitized = sanitizeFtsQuery(query)
if (sanitized.isBlank()) return flowOf(emptyList())
return dbManager.currentDb.flatMapLatest { db ->
kotlinx.coroutines.flow.flow {
val dao = db.packetDao()
val packets =
if (contactKey != null) {
dao.searchMessagesInConversation(sanitized, contactKey)
} else {
dao.searchMessages(sanitized)
}
emit(
packets.map { packet ->
val node = getNode(packet.data.from)
val isFromLocal =
node.user.id == DataPacket.ID_LOCAL ||
(packet.myNodeNum != 0 && node.num == packet.myNodeNum)
Message(
uuid = packet.uuid,
receivedTime = packet.received_time,
node = node,
text = packet.data.text.orEmpty(),
fromLocal = isFromLocal,
time = org.meshtastic.core.model.util.getShortDateTime(packet.data.time),
snr = packet.snr,
rssi = packet.rssi,
hopsAway = packet.hopsAway,
read = packet.read,
status = packet.data.status,
routingError = packet.routingError,
packetId = packet.packetId,
emojis = emptyList(),
replyId = packet.data.replyId,
)
},
)
}
}
}

/**
* Sanitizes a user query for FTS5 by wrapping each token in double quotes. This escapes FTS5 special characters (*,
* -, NEAR, etc.) while still allowing multi-word searches as implicit AND queries.
*/
private fun sanitizeFtsQuery(query: String): String =
query.split("\\s+".toRegex()).filter { it.isNotBlank() }.joinToString(" ") { "\"${it.replace("\"", "")}\"" }

companion object {
private const val CONTACTS_PAGE_SIZE = 30
private const val MESSAGES_PAGE_SIZE = 50
Expand Down
Loading
Loading