Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class AndroidReticulumTransport(context: Context) : ReticulumTransport {
)
}

override suspend fun announce() {
if (!_nodeReady.value) {
throw IllegalStateException("Start the Reticulum node before announcing.")
}
sendCommand(ReticulumNodeService.ACTION_LXMF_ANNOUNCE)
}

fun dispose() {
runCatching { appContext.unregisterReceiver(receiver) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ReticulumNodeService : Service() {
}
ACTION_RRC_CONNECT, ACTION_RRC_DISCONNECT, ACTION_RRC_JOIN, ACTION_RRC_PART, ACTION_RRC_SEND ->
handleRrcCommand(intent)
ACTION_LXMF_SEND, ACTION_LXMF_SET_NAME -> handleLxmfCommand(intent)
ACTION_LXMF_SEND, ACTION_LXMF_SET_NAME, ACTION_LXMF_ANNOUNCE -> handleLxmfCommand(intent)
ACTION_RRC_STATUS -> publishRrcCurrentState()
else -> stopSelf(startId)
}
Expand Down Expand Up @@ -286,13 +286,19 @@ class ReticulumNodeService : Service() {

private fun handleLxmfCommand(intent: Intent) {
val module = lxmfModule
val isAnnounce = intent.action == ACTION_LXMF_ANNOUNCE
if (module == null) {
publishLxmfError("Start the Reticulum node before sending LXMF messages.")
publishLxmfError(
if (isAnnounce) "Start the Reticulum node before announcing."
else "Start the Reticulum node before sending LXMF messages.",
)
return
}
rrcWorker.execute {
try {
if (intent.action == ACTION_LXMF_SET_NAME) {
if (isAnnounce) {
module.callAttr("announce")
} else if (intent.action == ACTION_LXMF_SET_NAME) {
module.callAttr("set_display_name", intent.getStringExtra(EXTRA_DISPLAY_NAME).orEmpty())
} else {
module.callAttr(
Expand All @@ -303,8 +309,13 @@ class ReticulumNodeService : Service() {
}
drainLxmfEvents()
} catch (error: Exception) {
Log.w(TAG, "LXMF send failed", error)
publishLxmfError(error.message ?: "Could not send the LXMF message.")
if (isAnnounce) {
Log.w(TAG, "LXMF announce failed", error)
publishLxmfError(error.message ?: "Could not announce the LXMF address.")
} else {
Log.w(TAG, "LXMF send failed", error)
publishLxmfError(error.message ?: "Could not send the LXMF message.")
}
}
}
}
Expand Down Expand Up @@ -476,6 +487,7 @@ class ReticulumNodeService : Service() {
const val ACTION_RRC_STATUS = "com.sidpatchy.Starling.action.RRC_STATUS"
const val ACTION_LXMF_SEND = "com.sidpatchy.Starling.action.LXMF_SEND"
const val ACTION_LXMF_SET_NAME = "com.sidpatchy.Starling.action.LXMF_SET_NAME"
const val ACTION_LXMF_ANNOUNCE = "com.sidpatchy.Starling.action.LXMF_ANNOUNCE"
const val EXTRA_MODE = "mode"
const val EXTRA_TCP_PEERS = "tcp_peers"
const val EXTRA_TCP_STATUS = "tcp_status"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ internal class BridgeReticulumTransport(
sendCommand("LXMF_SET_NAME\t${encodeBridgeBase64(displayName)}")
}

override suspend fun announce() {
if (nodeState.value !is ReticulumNodeState.Running) {
throw IllegalStateException("Start the Reticulum node before announcing.")
}
sendCommand("LXMF_ANNOUNCE")
}

suspend fun close() {
runCatching { disconnect() }
scope.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlin.time.Clock

data class ChatRoom(
val id: String,
Expand Down Expand Up @@ -140,6 +141,8 @@ interface ChatRepository {
val contactMemberIds: StateFlow<Set<String>>
val blockedMemberIds: StateFlow<Set<String>>
val contactRequests: StateFlow<List<ChatContactRequest>>
/** People heard through LXMF announces; sample people while Local preview is on. */
val nearbyPeers: StateFlow<List<NearbyPeer>>
fun messages(roomId: String): Flow<List<ChatMessage>>
fun directMessages(memberId: String): Flow<List<ChatMessage>>
fun members(roomId: String): Flow<List<ChatMember>>
Expand All @@ -154,6 +157,11 @@ interface ChatRepository {
fun setPreviewMode(enabled: Boolean)
fun addRoom(networkId: String, name: String, topic: String): ChatRoom
fun addContact(member: ChatMember): ChatMember?
/**
* Save someone heard nearby as a contact without opening or creating a conversation.
* [name] is the name the user chose when the announce carried none.
*/
fun addContactFromAnnounce(peer: NearbyPeer, name: String? = null): ChatMember?
fun toggleContact(member: ChatMember)
fun acceptContactRequest(requestId: String)
fun ignoreContactRequest(requestId: String)
Expand Down Expand Up @@ -221,6 +229,25 @@ class LocalPreviewChatRepository : ChatRepository {
private val _contactRequests = MutableStateFlow(allContactRequests.value)
override val contactRequests: StateFlow<List<ChatContactRequest>> = _contactRequests.asStateFlow()

private val allNearbyPeers = Clock.System.now().toEpochMilliseconds().let { now ->
listOf(
NearbyPeer(
address = "5a1c0e94b7d2436f8e0a9c3b1d7f2e68", name = "wren",
bleHeardMillis = now - 60_000L, bleHops = 1, isPreview = true,
),
NearbyPeer(
address = "7f3a9c21d4e85b06a1c97e3f02b8d5c4",
bleHeardMillis = now - 12 * 60_000L, bleHops = 1, isPreview = true,
),
NearbyPeer(
address = "c28e6f10a9b34d7e85f1063c4a7d9b2e", name = "heron",
networkHeardMillis = now - 2 * 60 * 60_000L, networkHops = 3, networkKind = "tcp", isPreview = true,
),
)
}
private val _nearbyPeers = MutableStateFlow(allNearbyPeers)
override val nearbyPeers: StateFlow<List<NearbyPeer>> = _nearbyPeers.asStateFlow()

private val allMessagesByRoom = MutableStateFlow(
mapOf(
"general" to listOf(
Expand Down Expand Up @@ -392,6 +419,13 @@ class LocalPreviewChatRepository : ChatRepository {
return contact
}

override fun addContactFromAnnounce(peer: NearbyPeer, name: String?): ChatMember? {
if (!_previewMode.value || !peer.isPreview) return null
val displayName = sanitizeNearbyName(name ?: peer.name)
if (displayName.isBlank()) return null
return addContact(ChatMember(nearbyMemberId(peer), displayName))
}

override fun toggleContact(member: ChatMember) {
if (member.isSelf) return
val address = member.address?.let(::normalizeLxmfAddress)
Expand Down Expand Up @@ -498,6 +532,7 @@ class LocalPreviewChatRepository : ChatRepository {

private fun refreshVisibleRequests() {
_contactRequests.value = allContactRequests.value.filter { it.isPreview == _previewMode.value }
_nearbyPeers.value = allNearbyPeers.filter { it.isPreview == _previewMode.value }
}

internal fun persistedNetworks(): List<ChatNetwork> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ data class PersistedChatState(
val globalNickname: String = "Starling",
val uiSettings: StarlingUiSettings = StarlingUiSettings(),
val roomMemberIds: Map<String, Set<String>> = emptyMap(),
/** People heard over network interfaces; Bluetooth readings are not stored. */
val nearbyPeers: List<NearbyPeer> = emptyList(),
)

/** Small versioned text codec to avoid platform-specific serialization APIs. */
Expand Down Expand Up @@ -101,6 +103,12 @@ object ChatStateCodec {
request.isPreview.bit(), request.timestampMillis?.toString().orEmpty(),
)
}
state.nearbyPeers.filter { it.networkHeardMillis > 0 && !it.isPreview }.forEach { peer ->
appendRecord(
"Y", peer.address, peer.name, peer.networkHeardMillis.toString(),
peer.networkHops.toString(), peer.networkKind,
)
}
state.roomMessages.forEach { (roomId, messages) ->
messages.forEach { appendMessage("R", roomId, it) }
}
Expand All @@ -121,6 +129,7 @@ object ChatStateCodec {
val contacts = linkedSetOf<String>()
val blocked = linkedSetOf<String>()
val requests = linkedMapOf<String, ChatContactRequest>()
val nearbyPeers = linkedMapOf<String, NearbyPeer>()
val rooms = linkedMapOf<String, MutableList<ChatMessage>>()
val direct = linkedMapOf<String, MutableList<ChatMessage>>()
var nodeMode: ReticulumRuntimeMode? = null
Expand Down Expand Up @@ -213,6 +222,19 @@ object ChatStateCodec {
)
requests[request.id] = request
}
"Y" -> if (decoded.size >= 5) {
val address = normalizeLxmfAddress(decoded[0])
val heardMillis = decoded[2].toLongOrNull() ?: 0L
if (address != null && heardMillis > 0) {
nearbyPeers[address] = NearbyPeer(
address = address,
name = sanitizeNearbyName(decoded[1]),
networkHeardMillis = heardMillis,
networkHops = decoded[3].toIntOrNull() ?: 0,
networkKind = decoded[4],
)
}
}
"R", "D" -> if (decoded.size >= 8) {
val isSystem = decoded.getOrNull(9) == "1"
val hasEventTimestamps = header in EVENT_TIMESTAMP_HEADERS
Expand Down Expand Up @@ -294,6 +316,7 @@ object ChatStateCodec {
globalNickname = globalNickname,
uiSettings = uiSettings,
roomMemberIds = roomMemberIds.mapValues { it.value.toSet() },
nearbyPeers = nearbyPeers.values.toList(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ internal fun IdentityPage(
onExportIdentity: () -> Unit,
onDeleteIdentity: () -> Unit,
onDisconnectNode: () -> Unit,
canAnnounce: Boolean = false,
onAnnounce: () -> Unit = {},
globalNickname: String,
networks: List<ChatNetwork>,
onGlobalNicknameChange: (String) -> Unit,
Expand Down Expand Up @@ -161,8 +163,12 @@ internal fun IdentityPage(
if (localDeliveryAddress.isNullOrBlank()) {
Text("Not available right now.", color = Muted, fontSize = 12.sp)
} else {
SelectionContainer {
Text("lxmf://$localDeliveryAddress", color = Accent, fontSize = 13.sp, fontFamily = FontFamily.Monospace)
Row(verticalAlignment = Alignment.CenterVertically) {
SelectionContainer(Modifier.weight(1f)) {
Text("lxmf://$localDeliveryAddress", color = Accent, fontSize = 13.sp, fontFamily = FontFamily.Monospace)
}
Spacer(Modifier.width(8.dp))
AnnounceButton(enabled = canAnnounce, onAnnounce = { onDismissKeyboard(); onAnnounce() })
}
}
}
Expand Down
Loading
Loading